u8-line: Add new u8_line_set_length() function.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 24 Dec 2012 22:18:01 +0000 (14:18 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 25 Dec 2012 01:13:39 +0000 (17:13 -0800)
The other functions in the u8-line library come directly from the ASCII
output driver.  This function is new, so I broke it into this separate
commit to emphasize that.

src/libpspp/u8-line.c
src/libpspp/u8-line.h
src/output/ascii.c
src/output/ascii.h
tests/output/ascii.at
tests/output/render-test.c

index 33442ba287644fe09f7e2458bc113147cebecd75..34e650952a2c201da5e7b3080e25b60989aecda4 100644 (file)
@@ -193,3 +193,29 @@ u8_line_put (struct u8_line *line, int x0, int x1, const char *s, int n)
 {
   memcpy (u8_line_reserve (line, x0, x1, n), s, n);
 }
+
+/* Changes the width of LINE to X column widths.  If X is longer than LINE's
+   previous width, LINE is extended by appending spaces.  If X is shorter than
+   LINE's previous width, LINE is shortened by removing trailing characters. */
+void
+u8_line_set_length (struct u8_line *line, int x)
+{
+  if (x > line->width)
+    {
+      ds_put_byte_multiple (&line->s, ' ', x - line->width);
+      line->width = x;
+    }
+  else if (x < line->width)
+    {
+      struct u8_pos pos;
+
+      u8_line_find_pos (line, x, &pos);
+      ds_truncate (&line->s, pos.ofs0);
+      line->width = pos.x0;
+      if (x > line->width)
+        {
+          ds_put_byte_multiple (&line->s, '?', x - line->width);
+          line->width = x;
+        }
+    }
+}
index af7bd6e8aaa1ff04fc4e9f461a380db20e3ae523..2674e2e275b92ed1a5ae902c64d39e948353b624 100644 (file)
@@ -37,5 +37,6 @@ void u8_line_destroy (struct u8_line *);
 void u8_line_clear (struct u8_line *);
 char *u8_line_reserve (struct u8_line *, int x0, int x1, int n);
 void u8_line_put (struct u8_line *, int x0, int x1, const char *s, int n);
+void u8_line_set_length (struct u8_line *, int x);
 
 #endif /* libpspp/u8-line.h */
index b076faf25a93ff50bb27c267b66a5d458468c409..756235c5cbf6cae7183e19d4a287ff6566840c7a 100644 (file)
@@ -943,6 +943,19 @@ ascii_test_write (struct output_driver *driver,
 
   a->y = 1;
 }
+
+void
+ascii_test_set_length (struct output_driver *driver, int y, int length)
+{
+  struct ascii_driver *a = ascii_driver_cast (driver);
+
+  if (a->file == NULL && !ascii_open_page (a))
+    return;
+
+  if (y < 0 || y >= a->length)
+    return;
+  u8_line_set_length (&a->lines[y], length);
+}
 \f
 /* ascii_close_page () and support routines. */
 
index 5d324d84646a130e1339939a4f95415ab40428c4..bf68cafec7a82bd4842ff2b151b9d71bb45a68de 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 2011, 2012 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
@@ -21,5 +21,6 @@ struct output_driver;
 
 void ascii_test_write (struct output_driver *,
                        const char *s, int x, int y, unsigned int options);
+void ascii_test_set_length (struct output_driver *, int y, int length);
 
 #endif /* ascii.h */
index 07b440efb591d797194f732666b952f2a23185d1..a3f66b933be8bb5afdaa570d7799b3282db5dfc6 100644 (file)
@@ -520,6 +520,47 @@ _\bà_\be_\bî_\bo̧_\bũ_\by
 ])
 AT_CLEANUP
 
+AT_SETUP([ASCII driver u8_line_set_length])
+AT_KEYWORDS([render rendering])
+AT_DATA([input], [dnl
+0 0 0 àéî
+0 1 0 àéî
+0 2 0 àéî
+0 3 0 àéî
+0 4 0 àéî
+set-length 0 4
+set-length 1 3
+set-length 2 2
+set-length 3 1
+set-length 4 0
+
+0 6 0 あい
+0 7 0 あい
+0 8 0 あい
+0 9 0 あい
+0 10 0 あい
+0 11 0 あい
+set-length 6 5
+set-length 7 4
+set-length 8 3
+set-length 9 2
+set-length 10 1
+set-length 11 0
+])
+AT_CHECK([render-test --draw-mode input], [0], [dnl
+àéî
+àéî
+àé
+à
+
+あい
+あい
+あ?
+あ
+?
+])
+AT_CLEANUP
+
 AT_SETUP([ASCII driver Unicode box characters])
 AT_KEYWORDS([render rendering])
 AT_DATA([input], [3 3
index 0376e5b49b08ed60ada321424f241ab54fdaeb7a..5f4c1da00bc5313d98558e9bfa702981f1929e3b 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
+   Copyright (C) 2009, 2010, 2011, 2012 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
@@ -361,6 +361,7 @@ draw (FILE *stream)
   while (fgets (buffer, sizeof buffer, stream))
     {
       char text[sizeof buffer];
+      int length;
       int emph;
       int x, y;
 
@@ -368,9 +369,11 @@ draw (FILE *stream)
       if (strchr ("#\r\n", buffer[0]))
         continue;
 
-      if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) != 4)
+      if (sscanf (buffer, "%d %d %d %[^\n]", &x, &y, &emph, text) == 4)
+        ascii_test_write (ascii_driver, text, x, y, emph ? TAB_EMPH : 0);
+      else if (sscanf (buffer, "set-length %d %d", &y, &length) == 2)
+        ascii_test_set_length (ascii_driver, y, length);
+      else
         error (1, 0, "line %d has invalid format", line);
-
-      ascii_test_write (ascii_driver, text, x, y, emph ? TAB_EMPH : 0);
     }
 }