From octave-sources-request at bevo dot che dot wisc dot edu Sat Jan 30 01:06:13 1999 Subject: Announce: octave-2.1.12-load-save.cc.patch From: Joao Cardoso To: octave-sources at bevo dot che dot wisc dot edu Date: Sat, 30 Jan 1999 06:05:58 +0000 This is a multi-part message in MIME format. --------------AEEBA38DB763291CCDCAA6CA Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hi, I enclose a (6Kbytes) patch against octave-2.1.12 that enables load/save of lists and structures. Comments and bug reports are welcome. There is no guarantee that this will be the final implementation by John Eaton. The current implementation is due to . I enclose an example of use. The error: `error: invalid conversion from string to real matrix' is due to a known bug in octave-2.1.12 related with saving of string arrays (John, don't you want to post a patch? :-)). Enjoy. Joao octave:1> c.a=1;c.b=rand(2);c.c="pois";c.d=["pois"; "sim"]; octave:2> b=list(1,2,3); octave:3> a=list(1,rand(2),"pois",["pois"; "sim"],b,c); octave:4> save foo a error: invalid conversion from string to real matrix error: invalid conversion from string to real matrix error: invalid conversion from string to real matrix error: invalid conversion from string to real matrix octave:4> implicit_str_to_num_ok =1 implicit_str_to_num_ok = 1 octave:5> save foo a octave:6> clear a b c octave:7> a error: `a' undefined near line 7 column 1 octave:7> load po octave:8> a } octave:19> a=list(1,rand(2),"pois",["pois"; "sim"],b,c) octave:20> save po a octave:21> clear a octave:22> load po octave:19> a=list(1,rand(2),"pois",["pois"; "sim"],b,c) octave:20> save po a octave:21> clear a octave:22> load po octave:23> a,b,c a = ( [1] = 1 [2] = 0.620967 0.492812 0.830178 0.053845 [3] = 112 111 105 115 [4] = 112 111 105 115 115 105 109 32 [5] = ( [1] = 1 [2] = 2 [3] = 3 ) [6] = { a = 1 b = 0.43203 0.22569 0.30463 0.40677 c = 112 111 105 115 d = 112 111 105 115 115 105 109 32 } ) b = ( [1] = 1 [2] = 2 [3] = 3 ) c = { a = 1 b = 0.43203 0.22569 0.30463 0.40677 c = 112 111 105 115 d = 112 111 105 115 115 105 109 32 } -- Joao Cardoso, INESC | e-mail: R. Jose Falcao 110 | tel: + 351 2 2094345 4050 Porto, Portugal | fax: + 351 2 2008487 --------------AEEBA38DB763291CCDCAA6CA Content-Type: text/plain; charset=us-ascii; name="octave-2.1.12-load-save.cc.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="octave-2.1.12-load-save.cc.patch" *** load-save.cc.org Sat Jan 30 02:28:42 1999 --- load-save.cc Sat Jan 30 02:29:03 1999 *************** *** 59,64 **** --- 59,66 ---- #include "variables.h" #include "version.h" + #include "ov-struct.h" + // The number of decimal digits to use when writing ascii data. static bool Vcrash_dumps_octave_core; *************** *** 622,627 **** --- 624,631 ---- if (! error_state) tc = octave_value (chm, true); } + else if ( elements == 0 ) + tc = octave_value ("",true); else error ("load: failed to extract number of string elements"); } *************** *** 652,657 **** --- 656,707 ---- else error ("load: failed to load range constant"); } + else if (strncmp (ptr, "list", 4) == 0) + { + int ne = 0; + + if (extract_keyword (is, "elements", ne) && ne-- > 0) + { + octave_value_list tmp; + //avoid those useless copies when resizing the list + //another way to do this is to reverse the save order of the + //list elements... easy... but looks better in the file this way + tmp(ne)=octave_value(true); + for( int elem=0;elem<=ne;elem++ ) + if(!read_ascii_data(is,filename,global,tmp(elem), count)) + { + error ("load: list element `%s' missing", tag); + break; + } + tc=tmp; + } + else + panic_impossible (); + } + else if (strncmp (ptr, "struct", 6) == 0) + { + int ne = 0; + + if (extract_keyword (is, "elements", ne) && ne > 0) + { + Octave_map om; + char* s; + + for( int elem=0;elem=0;elem-- ) + if(!read_binary_data(is,swap,fmt,filename,global,tmp(elem),ldoc)) + { + error ("load: list element `%s' missing", name); + break; + } + tc=tmp; + } + break; + case 9: + { + FOUR_BYTE_INT ne; + if (! is.read (&ne, 4)) + goto data_read_error; + if (swap) + swap_4_bytes (X_CAST (char *, &ne)); + Octave_map om; + char *s; + char *ldoc=""; + for( int elem=0;elem=0;elem-- ) + { + octave_value ov = ovl(elem); + save_binary_data ( os, ov, string("_"), string(""), + mark_as_global, save_as_floats); + } + } + else if (tc.is_map ()) + { + tmp = 9; + os.write (&tmp, 1); + Octave_map om = tc.map_value(); + FOUR_BYTE_INT elements = om.length(); + os.write (&elements, 4); + for( Pix elem=om.first();elem!=0;om.next(elem) ) + { + octave_value ov = om.contents(elem); + string s = om.key(elem); + save_binary_data ( os, ov, s, string(""), + mark_as_global, save_as_floats); + } + } else gripe_wrong_type_arg ("save", tc, false); *************** *** 2316,2321 **** --- 2439,2474 ---- << tmp.base () << " " << tmp.limit () << " " << tmp.inc () << "\n"; + } + else if (tc.is_list ()) + { + octave_value_list ovl = tc.list_value(); + int elements = ovl.length(); + ascii_save_type (os, "list", mark_as_global); + os << "# elements: " << elements << "\n"; + for( int elem=0;elem