X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fany-reader.c;h=ad3fcb5b7a91a99c6acd22798c459037bc0b22c5;hb=df2f1ae30c2835a39f4bc6b0c14f10a2090d9401;hp=50feb6892e936f4fbf9ea54d14f90707c11acf67;hpb=fe8dc2171009e90d2335f159d05f7e6660e24780;p=pspp diff --git a/src/data/any-reader.c b/src/data/any-reader.c index 50feb6892e..ad3fcb5b7a 100644 --- a/src/data/any-reader.c +++ b/src/data/any-reader.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2006, 2010, 2011 Free Software Foundation, Inc. + Copyright (C) 2006, 2010, 2011, 2012, 2014 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -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,30 +51,40 @@ 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 casereader and stores the file's dictionary into *DICT. On - failure, returns a null pointer. */ + failure, returns a null pointer. + + Ordinarily the reader attempts to automatically detect the character + encoding based on the file's contents. This isn't always possible, + especially for files written by old versions of SPSS or PSPP, so specifying + a nonnull ENCODING overrides the choice of character encoding. */ struct casereader * -any_reader_open (struct file_handle *handle, struct dictionary **dict) +any_reader_open (struct file_handle *handle, const char *encoding, + struct dictionary **dict) { switch (fh_get_referent (handle)) { @@ -91,15 +93,23 @@ any_reader_open (struct file_handle *handle, struct dictionary **dict) 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) - return sfm_open_reader (handle, dict, NULL); + else if (result == ANY_YES) + { + struct sfm_reader *r; + + r = sfm_open (handle); + if (r == NULL) + return NULL; + + return sfm_decode (r, 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."),