X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fdata-io%2Fdata-reader.c;h=c4e7183056147e3df88f63de25efd58bf23abff5;hb=51d8c9b54d65bd0aa3944b8fb9d4460875048e14;hp=c2da55046219aebedf77e90e78da2faa5a45794f;hpb=b74d09af5e07f954c18e7cdb8aca3af47fa10208;p=pspp-builds.git diff --git a/src/language/data-io/data-reader.c b/src/language/data-io/data-reader.c index c2da5504..c4e71830 100644 --- a/src/language/data-io/data-reader.c +++ b/src/language/data-io/data-reader.c @@ -1,21 +1,18 @@ -/* PSPP - computes sample statistics. +/* PSPP - a program for statistical analysis. Copyright (C) 1997-2004, 2006 Free Software Foundation, Inc. - Written by Ben Pfaff . - 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 the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + 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 + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ #include @@ -23,31 +20,34 @@ #include #include +#include #include #include +#include #include #include #include #include #include #include -#include -#include +#include +#include #include #include #include "minmax.h" -#include "size_max.h" +#include "xalloc.h" #include "gettext.h" #define _(msgid) gettext (msgid) +#define N_(msgid) (msgid) /* Flags for DFM readers. */ enum dfm_reader_flags { DFM_ADVANCE = 002, /* Read next line on dfm_get_record() call? */ - DFM_SAW_BEGIN_DATA = 004, /* For inline_file only, whether we've + DFM_SAW_BEGIN_DATA = 004, /* For inline_file only, whether we've already read a BEGIN DATA line. */ DFM_TABS_EXPANDED = 010, /* Tabs have been expanded. */ }; @@ -56,6 +56,7 @@ enum dfm_reader_flags struct dfm_reader { struct file_handle *fh; /* File handle. */ + struct fh_lock *lock; /* Mutual exclusion lock for file. */ struct msg_locator where; /* Current location in data file. */ struct string line; /* Current line. */ struct string scratch; /* Extra line buffer. */ @@ -63,45 +64,40 @@ struct dfm_reader FILE *file; /* Associated file. */ size_t pos; /* Offset in line of current character. */ unsigned eof_cnt; /* # of attempts to advance past EOF. */ + struct lexer *lexer; /* The lexer reading the file */ }; /* Closes reader R opened by dfm_open_reader(). */ void dfm_close_reader (struct dfm_reader *r) { - int still_open; - bool is_inline; - char *file_name; - if (r == NULL) return; - is_inline = r->fh == fh_inline_file (); - file_name = is_inline ? NULL : xstrdup (fh_get_file_name (r->fh)); - still_open = fh_close (r->fh, "data file", "rs"); - if (still_open) + if (fh_unlock (r->lock)) { - free (file_name); - return; + /* File is still locked by another client. */ + return; } - if (!is_inline) - fn_close (file_name, r->file); + /* This was the last client, so close the underlying file. */ + if (fh_get_referent (r->fh) != FH_REF_INLINE) + fn_close (fh_get_file_name (r->fh), r->file); else { /* Skip any remaining data on the inline file. */ - if (r->flags & DFM_SAW_BEGIN_DATA) + if (r->flags & DFM_SAW_BEGIN_DATA) { dfm_reread_record (r, 0); while (!dfm_eof (r)) - dfm_forward_record (r); + dfm_forward_record (r); } } + fh_unref (r->fh); ds_destroy (&r->line); ds_destroy (&r->scratch); free (r); - free (file_name); } /* Opens the file designated by file handle FH for reading as a @@ -110,45 +106,53 @@ dfm_close_reader (struct dfm_reader *r) file between BEGIN FILE and END FILE. Returns a reader if successful, or a null pointer otherwise. */ struct dfm_reader * -dfm_open_reader (struct file_handle *fh) +dfm_open_reader (struct file_handle *fh, struct lexer *lexer) { struct dfm_reader *r; - void **rp; + struct fh_lock *lock; - rp = fh_open (fh, FH_REF_FILE | FH_REF_INLINE, "data file", "rs"); - if (rp == NULL) + /* TRANSLATORS: this fragment will be interpolated into + messages in fh_lock() that identify types of files. */ + lock = fh_lock (fh, FH_REF_FILE | FH_REF_INLINE, N_("data file"), + FH_ACC_READ, false); + if (lock == NULL) return NULL; - if (*rp != NULL) - return *rp; - + + r = fh_lock_get_aux (lock); + if (r != NULL) + return r; + r = xmalloc (sizeof *r); - r->fh = fh; + r->fh = fh_ref (fh); + r->lock = lock; + r->lexer = lexer; ds_init_empty (&r->line); ds_init_empty (&r->scratch); r->flags = DFM_ADVANCE; r->eof_cnt = 0; - if (fh != fh_inline_file ()) + if (fh_get_referent (fh) != FH_REF_INLINE) { r->where.file_name = fh_get_file_name (fh); - r->where.line_number = 0; + r->where.line_number = 0; r->file = fn_open (fh_get_file_name (fh), "rb"); if (r->file == NULL) { msg (ME, _("Could not open \"%s\" for reading as a data file: %s."), fh_get_file_name (r->fh), strerror (errno)); - fh_close (fh,"data file", "rs"); + fh_unlock (r->lock); + fh_unref (fh); free (r); return NULL; } } - *rp = r; + fh_lock_set_aux (lock, r); return r; } /* Returns true if an I/O error occurred on READER, false otherwise. */ bool -dfm_reader_error (const struct dfm_reader *r) +dfm_reader_error (const struct dfm_reader *r) { return fh_get_referent (r->fh) == FH_REF_FILE && ferror (r->file); } @@ -162,14 +166,14 @@ read_inline_record (struct dfm_reader *r) { r->flags |= DFM_SAW_BEGIN_DATA; - while (token == '.') - lex_get (); - if (!lex_force_match_id ("BEGIN") || !lex_force_match_id ("DATA")) + while (lex_token (r->lexer) == '.') + lex_get (r->lexer); + if (!lex_force_match_id (r->lexer, "BEGIN") || !lex_force_match_id (r->lexer, "DATA")) return false; - getl_set_prompt_style (GETL_PROMPT_DATA); + prompt_set_style (PROMPT_DATA); } - - if (!getl_read_line (NULL)) + + if (!lex_get_line_raw (r->lexer)) { msg (SE, _("Unexpected end-of-file while reading data in BEGIN " "DATA. This probably indicates " @@ -179,14 +183,15 @@ read_inline_record (struct dfm_reader *r) return false; } - if (ds_length (&getl_buf) >= 8 - && !strncasecmp (ds_cstr (&getl_buf), "end data", 8)) + if (ds_length (lex_entire_line_ds (r->lexer) ) >= 8 + && !strncasecmp (lex_entire_line (r->lexer), "end data", 8)) { - lex_set_prog (ds_end (&getl_buf)); + lex_discard_line (r->lexer); return false; } - ds_assign_string (&r->line, &getl_buf); + ds_assign_string (&r->line, lex_entire_line_ds (r->lexer) ); + return true; } @@ -199,13 +204,14 @@ read_file_record (struct dfm_reader *r) ds_clear (&r->line); if (fh_get_mode (r->fh) == FH_MODE_TEXT) { - if (!ds_read_line (&r->line, r->file)) + if (!ds_read_line (&r->line, r->file)) { if (ferror (r->file)) msg (ME, _("Error reading file %s: %s."), fh_get_name (r->fh), strerror (errno)); return false; } + ds_chomp (&r->line, '\n'); } else if (fh_get_mode (r->fh) == FH_MODE_BINARY) { @@ -224,7 +230,7 @@ read_file_record (struct dfm_reader *r) } } else - abort (); + NOT_REACHED (); r->where.line_number++; @@ -252,16 +258,16 @@ read_record (struct dfm_reader *r) an error message is issued, and the caller should more forcibly abort to avoid an infinite loop. */ unsigned -dfm_eof (struct dfm_reader *r) +dfm_eof (struct dfm_reader *r) { if (r->flags & DFM_ADVANCE) { r->flags &= ~DFM_ADVANCE; - if (r->eof_cnt == 0 && read_record (r)) + if (r->eof_cnt == 0 && read_record (r) ) { r->pos = 0; - return 0; + return 0; } r->eof_cnt++; @@ -295,7 +301,7 @@ dfm_get_record (struct dfm_reader *r) reading from the file is necessary or at end of file, so call dfm_eof() first.*/ void -dfm_expand_tabs (struct dfm_reader *r) +dfm_expand_tabs (struct dfm_reader *r) { size_t ofs, new_pos, tab_width; @@ -320,21 +326,21 @@ dfm_expand_tabs (struct dfm_reader *r) for (ofs = 0; ofs < ds_length (&r->line); ofs++) { unsigned char c; - + if (ofs == r->pos) new_pos = ds_length (&r->scratch); c = ds_data (&r->line)[ofs]; if (c != '\t') ds_put_char (&r->scratch, c); - else + else { do ds_put_char (&r->scratch, ' '); while (ds_length (&r->scratch) % tab_width != 0); } } - if (new_pos == SIZE_MAX) + if (new_pos == SIZE_MAX) { /* Maintain the same relationship between position and line length that we had before. DATA LIST uses a @@ -389,7 +395,7 @@ dfm_column_start (const struct dfm_reader *r) of the line. At or before end-of-line, this is 0; one column after end-of-line, this is 1; and so on. */ size_t -dfm_columns_past_end (const struct dfm_reader *r) +dfm_columns_past_end (const struct dfm_reader *r) { return r->pos < ds_length (&r->line) ? 0 : ds_length (&r->line) - r->pos; } @@ -397,7 +403,7 @@ dfm_columns_past_end (const struct dfm_reader *r) /* Returns the 1-based column within the current line that P designates. */ size_t -dfm_get_column (const struct dfm_reader *r, const char *p) +dfm_get_column (const struct dfm_reader *r, const char *p) { return ds_pointer_to_position (&r->line, p) + 1; } @@ -422,12 +428,12 @@ dfm_pop (struct dfm_reader *r) /* Perform BEGIN DATA...END DATA as a procedure in itself. */ int -cmd_begin_data (void) +cmd_begin_data (struct lexer *lexer, struct dataset *ds) { struct dfm_reader *r; bool ok; - if (!fh_is_open (fh_inline_file ())) + if (!fh_is_locked (fh_inline_file (), FH_ACC_READ)) { msg (SE, _("This command is not valid here since the current " "input program does not access the inline file.")); @@ -435,13 +441,13 @@ cmd_begin_data (void) } /* Open inline file. */ - r = dfm_open_reader (fh_inline_file ()); + r = dfm_open_reader (fh_inline_file (), lexer); r->flags |= DFM_SAW_BEGIN_DATA; /* Input procedure reads from inline file. */ - getl_set_prompt_style (GETL_PROMPT_DATA); - ok = procedure (NULL, NULL); - + prompt_set_style (PROMPT_DATA); + casereader_destroy (proc_open (ds)); + ok = proc_commit (ds); dfm_close_reader (r); return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;