Mercurial > hg > octave-terminal
changeset 1427:a2305feef016
[project @ 1995-09-19 03:51:16 by jwe]
author | jwe |
---|---|
date | Tue, 19 Sep 1995 03:51:16 +0000 |
parents | 29f274b42cb1 |
children | fb6f8b634333 |
files | src/load-save.cc |
diffstat | 1 files changed, 114 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/src/load-save.cc +++ b/src/load-save.cc @@ -1214,14 +1214,19 @@ // | complex matrix // | string // | range +// | string array // // <info> : <matrix info> // | <string info> +// | <string array info> // // <matrix info> : # rows: <integer> -// | # columns: <integer> +// : # columns: <integer> // -// <string info> : # len: <integer> +// <string info> : # length: <integer> +// +// <string array info> : # elements: <integer> +// : # length: <integer> (once before each string) // // Formatted ASCII data follows the header. // @@ -1234,6 +1239,22 @@ // 2 4 // 1 3 // +// Example: +// +// # name: foo +// # type: string array +// # elements: 5 +// # length: 4 +// this +// # length: 2 +// is +// # length: 1 +// a +// # length: 6 +// string +// # length: 5 +// array +// // XXX FIXME XXX -- this format is fairly rigid, and doesn't allow for // arbitrary comments, etc. Someone should fix that. @@ -1342,6 +1363,37 @@ else error ("load: failed to extract number of rows and columns"); } + else if (strncmp (ptr, "string array", 12) == 0) + { + int elements; + if (extract_keyword (is, "elements", elements) && elements > 0) + { + Octave_str_obj s (elements); + for (int i = 0; i < elements; i++) + { + int len; + if (extract_keyword (is, "length", len) && len > 0) + { + char *tmp = new char [len]; + if (! is.read (tmp, len)) + { + error ("load: failed to load string constant"); + break; + } + else + s.elem (i).assign (tmp, len); + delete [] tmp; + } + else + error ("load: failed to extract string length for element %d", i+1); + } + + if (! error_state) + tc = s; + } + else + error ("load: failed to extract number of string elements"); + } else if (strncmp (ptr, "string", 6) == 0) { int len; @@ -1447,6 +1499,13 @@ // limit real 8 // increment real 8 // +// string array +// elements int 4 +// +// for each element: +// length int 4 +// data string length +// // FILENAME is used for error messages. static char * @@ -1569,9 +1628,7 @@ case 5: { - int nr = tc.rows (); - int nc = tc.columns (); - FOUR_BYTE_INT len = nr * nc; + FOUR_BYTE_INT len; if (! is.read (&len, 4)) goto data_read_error; if (swap) @@ -1609,6 +1666,34 @@ } break; + case 7: + { + FOUR_BYTE_INT elements; + if (! is.read (&elements, 4)) + goto data_read_error; + if (swap) + swap_4_bytes ((char *) &elements); + Octave_str_obj s (elements); + for (int i = 0; i < elements; i++) + { + FOUR_BYTE_INT len; + if (! is.read (&len, 4)) + goto data_read_error; + if (swap) + swap_4_bytes ((char *) &len); + char *tmp = new char [len]; + if (! is.read (tmp, len)) + { + delete [] tmp; + goto data_read_error; + } + s.elem (i).assign (tmp, len); + delete [] tmp; + } + tc = s; + } + break; + default: data_read_error: error ("load: trouble reading binary file `%s'", filename); @@ -1866,9 +1951,7 @@ else tc = re; - // XXX FIXME XXX -- this needs to change once strings really work. - - if (type == 1 && nr == 1) + if (type == 1) tc = tc.convert_to_str (); return name; @@ -2301,7 +2384,7 @@ // Save the data from TC along with the corresponding NAME, help // string DOC, and global flag MARK_AS_GLOBAL on stream OS in the -// binary format described above for load_binary_data. +// binary format described above for read_binary_data. static int save_binary_data (ostream& os, const tree_constant& tc, char *name, @@ -2408,14 +2491,18 @@ } else if (tc.is_string ()) { - tmp = 5; + tmp = 7; os.write (&tmp, 1); - int nr = tc.rows (); - int nc = tc.columns (); - FOUR_BYTE_INT len = nr * nc; - os.write (&len, 4); - const char *s = tc.string_value (); - os.write (s, len); + FOUR_BYTE_INT nr = tc.rows (); + os.write (&nr, 4); + Octave_str_obj s = tc.all_strings (); + for (int i = 0; i < nr; i++) + { + FOUR_BYTE_INT len = s.elem (i).length (); + os.write (&len, 4); + const char *tmp = s.elem (i).data (); + os.write (tmp, len); + } } else if (tc.is_range ()) { @@ -2711,10 +2798,17 @@ } else if (tc.is_string ()) { - ascii_save_type (os, "string", mark_as_global); - const char *tmp = tc.string_value (); - os << "# length: " << strlen (tmp) << "\n" - << tmp << "\n"; + ascii_save_type (os, "string array", mark_as_global); + Octave_str_obj tmp = tc.all_strings (); + int elements = tmp.length (); + os << "# elements: " << elements << "\n"; + for (int i = 0; i < elements; i++) + { + int len = tmp.elem (i).length (); + os << "# length: " << len << "\n"; + os.write (tmp.elem (i).data (), len); + os << "\n"; + } } else if (tc.is_range ()) {