comparison src/ls-mat5.cc @ 8907:5a956c026b6c

preserve field order when saving structs
author John W. Eaton <jwe@octave.org>
date Tue, 03 Mar 2009 18:52:07 -0500
parents 25bc2d31e1bf
children eb63fbe60fab
comparison
equal deleted inserted replaced
8906:ab87d08d9a1b 8907:5a956c026b6c
2185 // an Octave structure */ 2185 // an Octave structure */
2186 // recursively write each element of the structure 2186 // recursively write each element of the structure
2187 { 2187 {
2188 char buf[64]; 2188 char buf[64];
2189 int32_t maxfieldnamelength = max_namelen + 1; 2189 int32_t maxfieldnamelength = max_namelen + 1;
2190 int fieldcnt = 0; 2190
2191 2191 octave_idx_type nf = m.nfields ();
2192 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) 2192
2193 fieldcnt++; 2193 int fieldcnt = nf;
2194 2194
2195 write_mat5_tag (os, miINT32, 4); 2195 write_mat5_tag (os, miINT32, 4);
2196 os.write (reinterpret_cast<char *> (&maxfieldnamelength), 4); 2196 os.write (reinterpret_cast<char *> (&maxfieldnamelength), 4);
2197 write_mat5_tag (os, miINT8, fieldcnt*maxfieldnamelength); 2197 write_mat5_tag (os, miINT8, fieldcnt*maxfieldnamelength);
2198 2198
2199 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) 2199 // Iterating over the list of keys will preserve the order of
2200 { 2200 // the fields.
2201 string_vector keys = m.keys ();
2202
2203 for (octave_idx_type i = 0; i < nf; i++)
2204 {
2205 std::string key = keys(i);
2206
2201 // write the name of each element 2207 // write the name of each element
2202 std::string tstr = m.key (i);
2203 memset (buf, 0, max_namelen + 1); 2208 memset (buf, 0, max_namelen + 1);
2204 strncpy (buf, tstr.c_str (), max_namelen); // only 31 or 63 char names permitted 2209 // only 31 or 63 char names permitted
2210 strncpy (buf, key.c_str (), max_namelen);
2205 os.write (buf, max_namelen + 1); 2211 os.write (buf, max_namelen + 1);
2206 } 2212 }
2207 2213
2208 int len = m.numel (); 2214 int len = m.numel ();
2209 2215
2210 for (int j = 0; j < len; j++) 2216 for (int j = 0; j < len; j++)
2211 { 2217 {
2212 // write the data of each element 2218 // write the data of each element
2213 2219
2214 for (Octave_map::const_iterator i = m.begin (); i != m.end (); i++) 2220 // Iterating over the list of keys will preserve the order
2221 // of the fields.
2222 for (octave_idx_type i = 0; i < nf; i++)
2215 { 2223 {
2216 Cell elts = m.contents (i); 2224 Cell elts = m.contents (keys(i));
2217 2225
2218 bool retval2 = save_mat5_binary_element (os, elts(j), "", 2226 bool retval2 = save_mat5_binary_element (os, elts(j), "",
2219 mark_as_global, 2227 mark_as_global,
2220 false, 2228 false,
2221 save_as_floats); 2229 save_as_floats);