From: Ben Pfaff Date: Mon, 24 Dec 2012 22:18:01 +0000 (-0800) Subject: u8-line: Add new u8_line_set_length() function. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b745220f07d9ea7d4196318a79835563bbcbad6f;p=pspp u8-line: Add new u8_line_set_length() function. 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. --- diff --git a/src/libpspp/u8-line.c b/src/libpspp/u8-line.c index 33442ba287..34e650952a 100644 --- a/src/libpspp/u8-line.c +++ b/src/libpspp/u8-line.c @@ -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; + } + } +} diff --git a/src/libpspp/u8-line.h b/src/libpspp/u8-line.h index af7bd6e8aa..2674e2e275 100644 --- a/src/libpspp/u8-line.h +++ b/src/libpspp/u8-line.h @@ -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 */ diff --git a/src/output/ascii.c b/src/output/ascii.c index b076faf25a..756235c5cb 100644 --- a/src/output/ascii.c +++ b/src/output/ascii.c @@ -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); +} /* ascii_close_page () and support routines. */ diff --git a/src/output/ascii.h b/src/output/ascii.h index 5d324d8464..bf68cafec7 100644 --- a/src/output/ascii.h +++ b/src/output/ascii.h @@ -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 */ diff --git a/tests/output/ascii.at b/tests/output/ascii.at index 07b440efb5..a3f66b933b 100644 --- a/tests/output/ascii.at +++ b/tests/output/ascii.at @@ -520,6 +520,47 @@ _à_e_î_o̧_ũ_y ]) 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 diff --git a/tests/output/render-test.c b/tests/output/render-test.c index 0376e5b49b..5f4c1da00b 100644 --- a/tests/output/render-test.c +++ b/tests/output/render-test.c @@ -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); } }