pspp-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pspp-cvs] pspp src/data/ChangeLog src/data/dictionary.c s...


From: Ben Pfaff
Subject: [Pspp-cvs] pspp src/data/ChangeLog src/data/dictionary.c s...
Date: Sun, 06 May 2007 22:05:22 +0000

CVSROOT:        /cvsroot/pspp
Module name:    pspp
Changes by:     Ben Pfaff <blp> 07/05/06 22:05:22

Modified files:
        src/data       : ChangeLog dictionary.c dictionary.h 
                         sys-file-reader.c 
        src/language   : command.c 
        src/language/data-io: ChangeLog get.c 
        src/language/dictionary: sys-file-info.c 
        src/language/expressions: evaluate.c 
        src/language/lexer: ChangeLog lexer.c lexer.h 
        src/language/stats: aggregate.c 
        src/language/tests: moments-test.c 
        src/language/utilities: ChangeLog title.c 
        tests          : ChangeLog 
        tests/command  : file-label.sh 

Log message:
        Abstract the documents within a dictionary a little better.  Thanks to
        John Darrington for suggestion, initial version, and review.  Patch
        #5917.
        
        * command/file-label.sh: Update to match new DOCUMENT behavior.
        
        * title.c (add_document_line): Removed.
        (add_document_trailer): New function.
        (cmd_document): Rewrite to yield SPSS-like output and to use the
        updated document API.
        (cmd_add_documents): Ditto.
        
        * lexer.c (lex_entire_line): Add const to parameter.
        (lex_entire_line_ds): Ditto.
        (lex_rest_of_line): Drop end_dot parameter.  Update all callers to
        use lex_end_dot instead.
        (lex_end_dot): New function.
        
        * get.c (mtf_merge_dictionary): Simplify creating merged document.
        
        * dictionary.c (struct dictionary): Change `documents' member from
        char * to struct string.
        (dict_clear): Destroy struct string.
        (dict_get_documents): Convert struct string to char *.
        (dict_set_documents): Set struct string.  Pad to 80-character
        multiple.
        (dict_clear_documents): New function.
        (dict_add_document_line): New function.
        (dict_get_document_line_cnt): New function.
        (dict_get_document_line): New function.
        
        * dictionary.h (macro DOC_LINE_LENGTH): New macro.
        
        * sys-file-reader.c (read_documents): Use new document functions.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/pspp/src/data/ChangeLog?cvsroot=pspp&r1=1.115&r2=1.116
http://cvs.savannah.gnu.org/viewcvs/pspp/src/data/dictionary.c?cvsroot=pspp&r1=1.35&r2=1.36
http://cvs.savannah.gnu.org/viewcvs/pspp/src/data/dictionary.h?cvsroot=pspp&r1=1.13&r2=1.14
http://cvs.savannah.gnu.org/viewcvs/pspp/src/data/sys-file-reader.c?cvsroot=pspp&r1=1.35&r2=1.36
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/command.c?cvsroot=pspp&r1=1.23&r2=1.24
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/data-io/ChangeLog?cvsroot=pspp&r1=1.37&r2=1.38
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/data-io/get.c?cvsroot=pspp&r1=1.30&r2=1.31
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/dictionary/sys-file-info.c?cvsroot=pspp&r1=1.23&r2=1.24
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/expressions/evaluate.c?cvsroot=pspp&r1=1.18&r2=1.19
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/lexer/ChangeLog?cvsroot=pspp&r1=1.25&r2=1.26
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/lexer/lexer.c?cvsroot=pspp&r1=1.20&r2=1.21
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/lexer/lexer.h?cvsroot=pspp&r1=1.12&r2=1.13
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/stats/aggregate.c?cvsroot=pspp&r1=1.31&r2=1.32
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/tests/moments-test.c?cvsroot=pspp&r1=1.9&r2=1.10
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/utilities/ChangeLog?cvsroot=pspp&r1=1.8&r2=1.9
http://cvs.savannah.gnu.org/viewcvs/pspp/src/language/utilities/title.c?cvsroot=pspp&r1=1.10&r2=1.11
http://cvs.savannah.gnu.org/viewcvs/pspp/tests/ChangeLog?cvsroot=pspp&r1=1.86&r2=1.87
http://cvs.savannah.gnu.org/viewcvs/pspp/tests/command/file-label.sh?cvsroot=pspp&r1=1.16&r2=1.17

Patches:
Index: src/data/ChangeLog
===================================================================
RCS file: /cvsroot/pspp/pspp/src/data/ChangeLog,v
retrieving revision 1.115
retrieving revision 1.116
diff -u -b -r1.115 -r1.116
--- src/data/ChangeLog  26 Apr 2007 09:57:51 -0000      1.115
+++ src/data/ChangeLog  6 May 2007 22:05:21 -0000       1.116
@@ -1,3 +1,24 @@
+2007-05-06  Ben Pfaff  <address@hidden>
+
+       Abstract the documents within a dictionary a little better.
+       Thanks to John Darrington for suggestion, initial version, and
+       review.  Patch #5917.
+
+       * dictionary.c (struct dictionary): Change `documents' member from
+       char * to struct string.
+       (dict_clear): Destroy struct string.
+       (dict_get_documents): Convert struct string to char *.
+       (dict_set_documents): Set struct string.  Pad to 80-character
+       multiple.
+       (dict_clear_documents): New function.
+       (dict_add_document_line): New function.
+       (dict_get_document_line_cnt): New function.
+       (dict_get_document_line): New function.
+
+       * dictionary.h (macro DOC_LINE_LENGTH): New macro.
+
+       * sys-file-reader.c (read_documents): Use new document functions.
+
 2007-04-19 John Darrington <address@hidden>
 
        * sys-file-reader.c: When reading a system file which has no 

Index: src/data/dictionary.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/data/dictionary.c,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -b -r1.35 -r1.36
--- src/data/dictionary.c       23 Apr 2007 01:32:29 -0000      1.35
+++ src/data/dictionary.c       6 May 2007 22:05:21 -0000       1.36
@@ -57,7 +57,7 @@
     struct variable *filter;    /* FILTER variable. */
     size_t case_limit;          /* Current case limit (N command). */
     char *label;               /* File label. */
-    char *documents;           /* Documents, as a string. */
+    struct string documents;    /* Documents, as a string. */
     struct vector **vector;     /* Vectors of variables. */
     size_t vector_cnt;          /* Number of vectors. */
     const struct dict_callbacks *callbacks; /* Callbacks on dictionary
@@ -177,8 +177,7 @@
   d->case_limit = 0;
   free (d->label);
   d->label = NULL;
-  free (d->documents);
-  d->documents = NULL;
+  ds_destroy (&d->documents);
   dict_clear_vectors (d);
 }
 
@@ -1096,27 +1095,73 @@
 }
 
 /* Returns the documents for D, or a null pointer if D has no
-   documents (see cmd_document()).. */
+   documents.  If the return value is nonnull, then the string
+   will be an exact multiple of DOC_LINE_LENGTH bytes in length,
+   with each segment corresponding to one line. */
 const char *
 dict_get_documents (const struct dictionary *d)
 {
-  assert (d != NULL);
-
-  return d->documents;
+  return ds_is_empty (&d->documents) ? NULL : ds_cstr (&d->documents);
 }
 
 /* Sets the documents for D to DOCUMENTS, or removes D's
-   documents if DOCUMENT is a null pointer. */
+   documents if DOCUMENT is a null pointer.  If DOCUMENTS is
+   nonnull, then it should be an exact multiple of
+   DOC_LINE_LENGTH bytes in length, with each segment
+   corresponding to one line. */
 void
 dict_set_documents (struct dictionary *d, const char *documents)
 {
-  assert (d != NULL);
+  size_t remainder;
 
-  free (d->documents);
-  if (documents == NULL)
-    d->documents = NULL;
-  else
-    d->documents = xstrdup (documents);
+  ds_assign_cstr (&d->documents, documents != NULL ? documents : "");
+
+  /* In case the caller didn't get it quite right, pad out the
+     final line with spaces. */
+  remainder = ds_length (&d->documents) % DOC_LINE_LENGTH;
+  if (remainder != 0)
+    ds_put_char_multiple (&d->documents, ' ', DOC_LINE_LENGTH - remainder);
+}
+
+/* Drops the documents from dictionary D. */
+void
+dict_clear_documents (struct dictionary *d) 
+{
+  ds_clear (&d->documents);
+}
+
+/* Appends LINE to the documents in D.  LINE will be truncated or
+   padded on the right with spaces to make it exactly
+   DOC_LINE_LENGTH bytes long. */
+void
+dict_add_document_line (struct dictionary *d, const char *line)
+{
+  if (strlen (line) > DOC_LINE_LENGTH) 
+    {
+      /* Note to translators: "bytes" is correct, not characters */
+      msg (SW, _("Truncating document line to %d bytes."), DOC_LINE_LENGTH); 
+    }
+  buf_copy_str_rpad (ds_put_uninit (&d->documents, DOC_LINE_LENGTH),
+                     DOC_LINE_LENGTH, line);
+}
+
+/* Returns the number of document lines in dictionary D. */
+size_t
+dict_get_document_line_cnt (const struct dictionary *d) 
+{
+  return ds_length (&d->documents) / DOC_LINE_LENGTH;
+}
+
+/* Copies document line number IDX from dictionary D into
+   LINE, trimming off any trailing white space. */
+void
+dict_get_document_line (const struct dictionary *d,
+                        size_t idx, struct string *line) 
+{
+  assert (idx < dict_get_document_line_cnt (d));
+  ds_assign_substring (line, ds_substr (&d->documents, idx * DOC_LINE_LENGTH,
+                                        DOC_LINE_LENGTH));
+  ds_rtrim (line, ss_cstr (CC_SPACES));
 }
 
 /* Creates in D a vector named NAME that contains the CNT

Index: src/data/dictionary.h
===================================================================
RCS file: /cvsroot/pspp/pspp/src/data/dictionary.h,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -r1.13 -r1.14
--- src/data/dictionary.h       2 Apr 2007 08:55:51 -0000       1.13
+++ src/data/dictionary.h       6 May 2007 22:05:21 -0000       1.14
@@ -26,6 +26,7 @@
 
 struct variable;
 struct dictionary;
+struct string;
 
 struct dict_callbacks
  {
@@ -123,8 +124,16 @@
 const char *dict_get_label (const struct dictionary *);
 void dict_set_label (struct dictionary *, const char *);
 
+/* Fixed length of lines in dictionary documents. */
+#define DOC_LINE_LENGTH 80      
+
 const char *dict_get_documents (const struct dictionary *);
 void dict_set_documents (struct dictionary *, const char *);
+void dict_clear_documents (struct dictionary *);
+void dict_add_document_line (struct dictionary *, const char *);
+size_t dict_get_document_line_cnt (const struct dictionary *);
+void dict_get_document_line (const struct dictionary *,
+                             size_t, struct string *);
 
 bool dict_create_vector (struct dictionary *,
                          const char *name,

Index: src/data/sys-file-reader.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/data/sys-file-reader.c,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -b -r1.35 -r1.36
--- src/data/sys-file-reader.c  22 Apr 2007 00:48:50 -0000      1.35
+++ src/data/sys-file-reader.c  6 May 2007 22:05:21 -0000       1.36
@@ -671,9 +671,12 @@
     sys_error (r, _("Number of document lines (%d) "
                     "must be greater than 0."), line_cnt);
 
-  documents = pool_nmalloc (r->pool, line_cnt + 1, 80);
-  read_string (r, documents, 80 * line_cnt + 1);
+  documents = pool_nmalloc (r->pool, line_cnt + 1, DOC_LINE_LENGTH);
+  read_string (r, documents, DOC_LINE_LENGTH * line_cnt + 1);
+  if (strlen (documents) == DOC_LINE_LENGTH * line_cnt)
   dict_set_documents (dict, documents);
+  else
+    sys_error (r, _("Document line contains null byte."));
   pool_free (r->pool, documents);
 }
 

Index: src/language/command.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/command.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -b -r1.23 -r1.24
--- src/language/command.c      12 Feb 2007 02:10:54 -0000      1.23
+++ src/language/command.c      6 May 2007 22:05:21 -0000       1.24
@@ -830,7 +830,7 @@
     }
   else 
     {
-      bool ok = run_command (lex_rest_of_line (lexer, NULL));
+      bool ok = run_command (lex_rest_of_line (lexer));
       lex_discard_line (lexer);
       return ok ? CMD_SUCCESS : CMD_FAILURE;
     }

Index: src/language/data-io/ChangeLog
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/data-io/ChangeLog,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -b -r1.37 -r1.38
--- src/language/data-io/ChangeLog      2 Feb 2007 00:57:32 -0000       1.37
+++ src/language/data-io/ChangeLog      6 May 2007 22:05:22 -0000       1.38
@@ -1,3 +1,14 @@
+2007-05-06  Ben Pfaff  <address@hidden>
+
+       Abstract the documents within a dictionary a little better.
+       Thanks to John Darrington for suggestion, initial version, and
+       review.  Patch #5917.
+
+       * get.c (mtf_merge_dictionary): Simplify creating merged document.
+
+       * sys-file-info.c (display_documents): Use new
+       dict_get_document_line_cnt and dict_get_document_line functions.
+
 Thu Feb  1 16:56:02 2007  Ben Pfaff  <address@hidden>
 
        * file-handle.q (fh_parse): Update to new fh_create_file

Index: src/language/data-io/get.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/data-io/get.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -b -r1.30 -r1.31
--- src/language/data-io/get.c  2 Apr 2007 08:55:51 -0000       1.30
+++ src/language/data-io/get.c  6 May 2007 22:05:22 -0000       1.31
@@ -1455,13 +1455,7 @@
         dict_set_documents (m, d_docs);
       else
         {
-          char *new_docs;
-          size_t new_len;
-
-          new_len = strlen (m_docs) + strlen (d_docs);
-          new_docs = xmalloc (new_len + 1);
-          strcpy (new_docs, m_docs);
-          strcat (new_docs, d_docs);
+          char *new_docs = xasprintf ("%s%s", m_docs, d_docs);
           dict_set_documents (m, new_docs);
           free (new_docs);
         }

Index: src/language/dictionary/sys-file-info.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/dictionary/sys-file-info.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -b -r1.23 -r1.24
--- src/language/dictionary/sys-file-info.c     2 Apr 2007 08:55:52 -0000       
1.23
+++ src/language/dictionary/sys-file-info.c     6 May 2007 22:05:22 -0000       
1.24
@@ -316,25 +316,18 @@
                                  "contain any documents."));
   else
     {
-      size_t n_lines = strlen (documents) / 80;
-      char buf[81];
+      struct string line = DS_EMPTY_INITIALIZER;
       size_t i;
 
       tab_output_text (TAB_LEFT | TAT_TITLE,
                       _("Documents in the active file:"));
       som_blank_line ();
-      buf[80] = 0;
-      for (i = 0; i < n_lines; i++)
+      for (i = 0; i < dict_get_document_line_cnt (dict); i++) 
        {
-         int len = 79;
-
-         memcpy (buf, &documents[i * 80], 80);
-         while ((isspace ((unsigned char) buf[len]) || buf[len] == 0)
-                && len > 0)
-           len--;
-         buf[len + 1] = 0;
-         tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, buf);
+          dict_get_document_line (dict, i, &line);
+          tab_output_text (TAB_LEFT | TAB_FIX | TAT_NOWRAP, ds_cstr (&line));
        }
+      ds_destroy (&line);
     }
 }
 

Index: src/language/expressions/evaluate.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/expressions/evaluate.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -b -r1.18 -r1.19
--- src/language/expressions/evaluate.c 16 Jan 2007 15:30:28 -0000      1.18
+++ src/language/expressions/evaluate.c 6 May 2007 22:05:22 -0000       1.19
@@ -199,7 +199,7 @@
 
   if ( ds != NULL ) 
     fprintf(stderr, "; ");
-  fprintf (stderr, "%s => ", lex_rest_of_line (lexer, NULL));
+  fprintf (stderr, "%s => ", lex_rest_of_line (lexer));
   lex_get (lexer);
 
   expr = expr_parse_any (lexer, ds, optimize);

Index: src/language/lexer/ChangeLog
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/lexer/ChangeLog,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -b -r1.25 -r1.26
--- src/language/lexer/ChangeLog        3 May 2007 10:15:26 -0000       1.25
+++ src/language/lexer/ChangeLog        6 May 2007 22:05:22 -0000       1.26
@@ -1,3 +1,15 @@
+2007-05-06  Ben Pfaff  <address@hidden>
+
+       Abstract the documents within a dictionary a little better.
+       Thanks to John Darrington for suggestion, initial version, and
+       review.  Patch #5917.
+
+       * lexer.c (lex_entire_line): Add const to parameter.
+       (lex_entire_line_ds): Ditto.
+       (lex_rest_of_line): Drop end_dot parameter.  Update all callers to
+       use lex_end_dot instead.
+       (lex_end_dot): New function.
+
 2007-05-03  John Darrington <address@hidden>
        
        * lexer.c lexer.h: Added lex_is_string function.

Index: src/language/lexer/lexer.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/lexer/lexer.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -b -r1.20 -r1.21
--- src/language/lexer/lexer.c  3 May 2007 10:15:26 -0000       1.20
+++ src/language/lexer/lexer.c  6 May 2007 22:05:22 -0000       1.21
@@ -719,29 +719,33 @@
 
 /* Returns the entire contents of the current line. */
 const char *
-lex_entire_line (struct lexer *lexer)
+lex_entire_line (const struct lexer *lexer)
 {
   return ds_cstr (&lexer->line_buffer);
 }
 
 const struct string *
-lex_entire_line_ds (struct lexer *lexer)
+lex_entire_line_ds (const struct lexer *lexer)
 {
   return &lexer->line_buffer;
 }
 
 /* As lex_entire_line(), but only returns the part of the current line
-   that hasn't already been tokenized.
-   If END_DOT is non-null, stores nonzero into *END_DOT if the line
-   ends with a terminal dot, or zero if it doesn't. */
+   that hasn't already been tokenized. */
 const char *
-lex_rest_of_line (struct lexer *lexer, int *end_dot)
+lex_rest_of_line (const struct lexer *lexer)
 {
-  if (end_dot)
-    *end_dot = lexer->dot;
   return lexer->prog;
 }
 
+/* Returns true if the current line ends in a terminal dot,
+   false otherwise. */
+bool
+lex_end_dot (const struct lexer *lexer) 
+{
+  return lexer->dot;
+}
+
 /* Causes the rest of the current input line to be ignored for
    tokenization purposes. */
 void

Index: src/language/lexer/lexer.h
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/lexer/lexer.h,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- src/language/lexer/lexer.h  3 May 2007 10:15:26 -0000       1.12
+++ src/language/lexer/lexer.h  6 May 2007 22:05:22 -0000       1.13
@@ -69,9 +69,10 @@
 void lex_put_back_id (struct lexer *, const char *tokid);
 
 /* Weird line processing functions. */
-const char *lex_entire_line (struct lexer *);
-const struct string *lex_entire_line_ds (struct lexer *);
-const char *lex_rest_of_line (struct lexer *, int *end_dot);
+const char *lex_entire_line (const struct lexer *);
+const struct string *lex_entire_line_ds (const struct lexer *);
+const char *lex_rest_of_line (const struct lexer *);
+bool lex_end_dot (const struct lexer *);
 void lex_preprocess_line (struct string *, enum getl_syntax,
                           bool *line_starts_command,
                           bool *line_ends_command);

Index: src/language/stats/aggregate.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/stats/aggregate.c,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -b -r1.31 -r1.32
--- src/language/stats/aggregate.c      2 Apr 2007 08:55:52 -0000       1.31
+++ src/language/stats/aggregate.c      6 May 2007 22:05:22 -0000       1.32
@@ -254,7 +254,7 @@
 
   /* Delete documents. */
   if (!copy_documents)
-    dict_set_documents (agr.dict, NULL);
+    dict_clear_documents (agr.dict);
 
   /* Cancel SPLIT FILE. */
   dict_set_split_vars (agr.dict, NULL, 0);

Index: src/language/tests/moments-test.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/tests/moments-test.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- src/language/tests/moments-test.c   9 Feb 2007 05:19:09 -0000       1.9
+++ src/language/tests/moments-test.c   6 May 2007 22:05:22 -0000       1.10
@@ -86,7 +86,7 @@
       lex_force_match (lexer, '/');
       goto done;
     }
-  fprintf (stderr, "%s => ", lex_rest_of_line (lexer, NULL));
+  fprintf (stderr, "%s => ", lex_rest_of_line (lexer));
   lex_get (lexer);
 
   if (two_pass) 

Index: src/language/utilities/ChangeLog
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/utilities/ChangeLog,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- src/language/utilities/ChangeLog    3 May 2007 10:15:26 -0000       1.8
+++ src/language/utilities/ChangeLog    6 May 2007 22:05:22 -0000       1.9
@@ -1,3 +1,15 @@
+2007-05-06  Ben Pfaff  <address@hidden>
+
+       Abstract the documents within a dictionary a little better.
+       Thanks to John Darrington for suggestion, initial version, and
+       review.  Patch #5917.
+
+       * title.c (add_document_line): Removed.
+       (add_document_trailer): New function.
+       (cmd_document): Rewrite to yield SPSS-like output and to use the
+       updated document API.
+       (cmd_add_documents): Ditto.
+
 2007-05-03 John Darrington <address@hidden>
 
        * title.c: Implemented ADD DOCUMENT command.

Index: src/language/utilities/title.c
===================================================================
RCS file: /cvsroot/pspp/pspp/src/language/utilities/title.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- src/language/utilities/title.c      3 May 2007 10:15:26 -0000       1.10
+++ src/language/utilities/title.c      6 May 2007 22:05:22 -0000       1.11
@@ -76,7 +76,7 @@
 
       if (*title)
        free (*title);
-      *title = xstrdup (lex_rest_of_line (lexer, NULL));
+      *title = xstrdup (lex_rest_of_line (lexer));
       lex_discard_line (lexer);
       for (cp = *title; *cp; cp++)
        *cp = toupper ((unsigned char) (*cp));
@@ -90,7 +90,7 @@
 {
   const char *label;
 
-  label = lex_rest_of_line (lexer, NULL);
+  label = lex_rest_of_line (lexer);
   lex_discard_line (lexer);
   while (isspace ((unsigned char) *label))
     label++;
@@ -100,27 +100,14 @@
   return CMD_SUCCESS;
 }
 
-/* Add LINE as a line of document information to dictionary
-   indented by INDENT spaces. */
+/* Add entry date line to DICT's documents. */
 static void
-add_document_line (struct dictionary *dict, const char *line, int indent)
+add_document_trailer (struct dictionary *dict) 
 {
-  const char *old_documents;
-  size_t old_len;
-  char *new_documents;
-
-  old_documents = dict_get_documents (dict);
-  old_len = old_documents != NULL ? strlen (old_documents) : 0;
-  new_documents = xmalloc (old_len + 81);
-
-  memcpy (new_documents, old_documents, old_len);
-  memset (new_documents + old_len, ' ', indent);
-  buf_copy_str_rpad (new_documents + old_len + indent, 80 - indent, line);
-  new_documents[old_len + 80] = '\0';
+  char buf[64];
 
-  dict_set_documents (dict, new_documents);
-
-  free (new_documents);
+  sprintf (buf, _("   (Entered %s)"), get_start_date ());
+  dict_add_document_line (dict, buf);
 }
 
 /* Performs the DOCUMENT command. */
@@ -128,40 +115,23 @@
 cmd_document (struct lexer *lexer, struct dataset *ds)
 {
   struct dictionary *dict = dataset_dict (ds);
-  /* Add a few header lines for reference. */
-  {
-    char buf[256];
-
-    if (dict && dict_get_documents (dict))
-      add_document_line (dict, "", 0);
+  struct string line = DS_EMPTY_INITIALIZER;
+  bool end_dot;
 
-    sprintf (buf, _("Document entered %s by %s:"), get_start_date (), version);
-    add_document_line (dict, buf, 1);
-  }
-
-  for (;;)
+  do
     {
-      int had_dot;
-      const char *orig_line;
-      char *copy_line;
+      end_dot = lex_end_dot (lexer);
+      ds_assign_string (&line, lex_entire_line_ds (lexer));
+      if (end_dot)
+        ds_put_char (&line, '.');
+      dict_add_document_line (dict, ds_cstr (&line));
 
-      orig_line = lex_rest_of_line (lexer, &had_dot);
       lex_discard_line (lexer);
-      while (isspace ((unsigned char) *orig_line))
-       orig_line++;
-
-      copy_line = xmalloc (strlen (orig_line) + 2);
-      strcpy (copy_line, orig_line);
-      if (had_dot)
-        strcat (copy_line, ".");
-
-      add_document_line (dict, copy_line, 3);
-      free (copy_line);
-
       lex_get_line (lexer);
-      if (had_dot)
-       break;
     }
+  while (!end_dot);
+
+  add_document_trailer (dict);
 
   return CMD_SUCCESS;
 }
@@ -170,7 +140,7 @@
 int
 cmd_drop_documents (struct lexer *lexer, struct dataset *ds)
 {
-  dict_set_documents (dataset_dict (ds), NULL);
+  dict_clear_documents (dataset_dict (ds));
 
   return lex_end_of_command (lexer);
 }
@@ -180,49 +150,18 @@
 int
 cmd_add_documents (struct lexer *lexer, struct dataset *ds)
 {
-  int i;
-  int n_lines = 0;
-  char buf[256];
-  struct string *lines = NULL;
-
-  sprintf (buf, _("(Entered %s)"), get_start_date ());
+  struct dictionary *dict = dataset_dict (ds);
 
   if ( ! lex_force_string (lexer) )
     return CMD_FAILURE;
 
   while ( lex_is_string (lexer))
     {
-      const struct string *s = lex_tokstr (lexer);
-      if ( ds_length (s) > 80)
-       {
-         /* Note to translators: "bytes" is correct, not characters */
-         msg (SE, _("Document lines may not be more than 80 bytes long."));
-         goto failure;
-
-       }
-      lines = xrealloc (lines, (n_lines + 1) * sizeof (*lines));
-      ds_init_string (&lines[n_lines++], s);
-
+      dict_add_document_line (dict, ds_cstr (lex_tokstr (lexer)));
       lex_get (lexer);
     }
 
-  for ( i = 0 ; i < n_lines ; ++i)
-    {
-      add_document_line (dataset_dict (ds), ds_cstr (&lines[i]), 0);
-      ds_destroy (&lines[i]);
-    }
-
-  free (lines);
-
-  add_document_line (dataset_dict (ds), buf, 3);
+  add_document_trailer (dict);
 
   return lex_end_of_command (lexer) ;
-
- failure:
-  for ( i = 0 ; i < n_lines ; ++i)
-    ds_destroy (&lines[i]);
-
-  free (lines);
-
-  return CMD_FAILURE;
 }

Index: tests/ChangeLog
===================================================================
RCS file: /cvsroot/pspp/pspp/tests/ChangeLog,v
retrieving revision 1.86
retrieving revision 1.87
diff -u -b -r1.86 -r1.87
--- tests/ChangeLog     20 Apr 2007 11:04:55 -0000      1.86
+++ tests/ChangeLog     6 May 2007 22:05:22 -0000       1.87
@@ -1,3 +1,11 @@
+2007-05-06  Ben Pfaff  <address@hidden>
+
+       Abstract the documents within a dictionary a little better.
+       Thanks to John Darrington for suggestion, initial version, and
+       review.  Patch #5917.
+
+       * command/file-label.sh: Update to match new DOCUMENT behavior.
+
 2007-04-19 John Darrington <address@hidden>
        
         * tests/command/no_case_size.sh tests/command/sysfiles-old.sh :

Index: tests/command/file-label.sh
===================================================================
RCS file: /cvsroot/pspp/pspp/tests/command/file-label.sh,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- tests/command/file-label.sh 3 May 2007 10:15:27 -0000       1.16
+++ tests/command/file-label.sh 6 May 2007 22:05:22 -0000       1.17
@@ -74,8 +74,8 @@
 /* Add a file label and a few documents.
 file label This is a test file label.
 document First line of a document
-This is the second very long line of a document in an attempt to overflow the 
input buffer with a really long line
-Note that the last line should end with a period: .
+Second line of a document
+The last line should end with a period: .
 
 
 /* Display the documents.
@@ -135,33 +135,33 @@
 |Y       |     1|  2-  2|F1.0  |
 +--------+------+-------+------+
 Documents in the active file:
-   First line of a document
-   This is the second very long line of a document in an attempt to overflow 
the
-   Note that the last line should end with a period: .
+document First line of a document
+Second line of a document
+The last line should end with a period: .
 File label:
 This is a test file label
 Documents in the active file:
-   First line of a document
-   This is the second very long line of a document in an attempt to overflow 
the
-   Note that the last line should end with a period: .
+document First line of a document
+Second line of a document
+The last line should end with a period: .
 Line one
 Line two
 File label:
 This is a test file label
 Documents in the active file:
-   First line of a document
-   This is the second very long line of a document in an attempt to overflow 
the
-   Note that the last line should end with a period: .
+document First line of a document
+Second line of a document
+The last line should end with a period: .
 Line one
 Line two
-   There should be another document now.
+document There should be another document now.
 Documents in the active file:
-   First line of a document
-   This is the second very long line of a document in an attempt to overflow 
the
-   Note that the last line should end with a period: .
+document First line of a document
+Second line of a document
+The last line should end with a period: .
 Line one
 Line two
-   There should be another document now.
+document There should be another document now.
 File label:
 This is a test file label
 EOF




reply via email to

[Prev in Thread] Current Thread [Next in Thread]