[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] src/data/any-reader.c (any_reader_may_open): Return tri-state va
From: |
John Darrington |
Subject: |
[PATCH] src/data/any-reader.c (any_reader_may_open): Return tri-state value. |
Date: |
Sun, 15 Dec 2013 17:22:16 +0100 |
Previously, any_reader_may_open returned bool. This change makes it
return a tri-state variable. YES, NO or ERROR. This is useful to
distinguish between a file which is not "readable" (ie not in a format
which PSPP knows how to read) and a file which cannot be opened due
to lack of permission or other lower level errors.
---
src/data/any-reader.c | 30 +++++++++++++-----------------
src/data/any-reader.h | 11 ++++++++++-
src/ui/gui/psppire-window.c | 7 ++++---
src/ui/gui/psppire.c | 8 +++++---
4 files changed, 32 insertions(+), 24 deletions(-)
diff --git a/src/data/any-reader.c b/src/data/any-reader.c
index 1b488f2..eaec63d 100644
--- a/src/data/any-reader.c
+++ b/src/data/any-reader.c
@@ -38,14 +38,6 @@
#include "gettext.h"
#define _(msgid) gettext (msgid)
-/* Result of type detection. */
-enum detect_result
- {
- YES, /* It is this type. */
- NO, /* It is not this type. */
- IO_ERROR /* File couldn't be opened. */
- };
-
/* Tries to detect whether FILE is a given type of file, by opening the file
and passing it to DETECT, and returns a detect_result. */
static enum detect_result
@@ -59,23 +51,27 @@ try_detect (const char *file_name, bool (*detect) (FILE *))
{
msg (ME, _("An error occurred while opening `%s': %s."),
file_name, strerror (errno));
- return IO_ERROR;
+ return ANY_ERROR;
}
is_type = detect (file);
fn_close (file_name, file);
- return is_type ? YES : NO;
+ return is_type ? ANY_YES : ANY_NO;
}
/* Returns true if any_reader_open() would be able to open FILE as a data
file, false otherwise. */
-bool
+enum detect_result
any_reader_may_open (const char *file)
{
- return (try_detect (file, sfm_detect) == YES
- || try_detect (file, pfm_detect) == YES);
+ enum detect_result res = try_detect (file, sfm_detect);
+
+ if (res == ANY_NO)
+ res = try_detect (file, pfm_detect);
+
+ return res;
}
/* Returns a casereader for HANDLE. On success, returns the new
@@ -97,15 +93,15 @@ any_reader_open (struct file_handle *handle, const char
*encoding,
enum detect_result result;
result = try_detect (fh_get_file_name (handle), sfm_detect);
- if (result == IO_ERROR)
+ if (result == ANY_ERROR)
return NULL;
- else if (result == YES)
+ else if (result == ANY_YES)
return sfm_open_reader (handle, encoding, dict, NULL);
result = try_detect (fh_get_file_name (handle), pfm_detect);
- if (result == IO_ERROR)
+ if (result == ANY_ERROR)
return NULL;
- else if (result == YES)
+ else if (result == ANY_YES)
return pfm_open_reader (handle, dict, NULL);
msg (SE, _("`%s' is not a system or portable file."),
diff --git a/src/data/any-reader.h b/src/data/any-reader.h
index fb36e99..063a7e6 100644
--- a/src/data/any-reader.h
+++ b/src/data/any-reader.h
@@ -19,9 +19,18 @@
#include <stdbool.h>
+/* Result of type detection. */
+enum detect_result
+ {
+ ANY_YES, /* It is this type. */
+ ANY_NO, /* It is not this type. */
+ ANY_ERROR /* File couldn't be opened. */
+ };
+
+
struct file_handle;
struct dictionary;
-bool any_reader_may_open (const char *file_name);
+enum detect_result any_reader_may_open (const char *file_name);
struct casereader *any_reader_open (struct file_handle *, const char *encoding,
struct dictionary **);
diff --git a/src/ui/gui/psppire-window.c b/src/ui/gui/psppire-window.c
index 65842bd..8421113 100644
--- a/src/ui/gui/psppire-window.c
+++ b/src/ui/gui/psppire-window.c
@@ -703,7 +703,7 @@ on_selection_changed (GtkFileChooser *chooser, GtkWidget
*encoding_selector)
return;
}
- gtk_widget_set_sensitive (encoding_selector, ! any_reader_may_open
(sysname));
+ gtk_widget_set_sensitive (encoding_selector, ANY_NO == any_reader_may_open
(sysname));
}
GtkWidget *
@@ -805,9 +805,10 @@ psppire_window_open (PsppireWindow *de)
gchar *encoding = psppire_encoding_selector_get_encoding (
gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
- if (any_reader_may_open (sysname))
+ enum detect_result res = any_reader_may_open (sysname);
+ if (ANY_YES == res)
open_data_window (de, name, NULL);
- else
+ else if (ANY_NO == res)
open_syntax_window (name, encoding);
g_free (encoding);
diff --git a/src/ui/gui/psppire.c b/src/ui/gui/psppire.c
index 7013fcf..38845ed 100644
--- a/src/ui/gui/psppire.c
+++ b/src/ui/gui/psppire.c
@@ -105,11 +105,13 @@ initialize (const char *data_file)
{
gchar *filename = local_to_filename_encoding (data_file);
+ enum detect_result res = any_reader_may_open (filename);
+
/* Check to see if the file is a .sav or a .por file. If not
assume that it is a syntax file */
- if ( any_reader_may_open (filename))
- open_data_window (NULL, filename, NULL);
- else
+ if (res == ANY_YES)
+ open_data_window (NULL, filename, NULL);
+ else if (res == ANY_NO)
{
create_data_window ();
open_syntax_window (filename, NULL);
--
1.7.10.4
- [PATCH] src/data/any-reader.c (any_reader_may_open): Return tri-state value.,
John Darrington <=