comparison src/ls-oct-ascii.cc @ 5275:23b37da9fd5b

[project @ 2005-04-08 16:07:35 by jwe]
author jwe
date Fri, 08 Apr 2005 16:07:37 +0000
parents e2ed74b9bfa0
children 4c8a2e4e0717
comparison
equal deleted inserted replaced
5274:eae7b40388e9 5275:23b37da9fd5b
72 // Functions for reading ascii data. 72 // Functions for reading ascii data.
73 73
74 static Matrix 74 static Matrix
75 strip_infnan (const Matrix& m) 75 strip_infnan (const Matrix& m)
76 { 76 {
77 int nr = m.rows (); 77 octave_idx_type nr = m.rows ();
78 int nc = m.columns (); 78 octave_idx_type nc = m.columns ();
79 79
80 Matrix retval (nr, nc); 80 Matrix retval (nr, nc);
81 81
82 int k = 0; 82 octave_idx_type k = 0;
83 for (int i = 0; i < nr; i++) 83 for (octave_idx_type i = 0; i < nr; i++)
84 { 84 {
85 for (int j = 0; j < nc; j++) 85 for (octave_idx_type j = 0; j < nc; j++)
86 { 86 {
87 double d = m (i, j); 87 double d = m (i, j);
88 if (xisnan (d)) 88 if (xisnan (d))
89 goto next_row; 89 goto next_row;
90 else 90 else
176 } 176 }
177 177
178 return retval; 178 return retval;
179 } 179 }
180 180
181 // Match KEYWORD on stream IS, placing the associated value in VALUE,
182 // returning TRUE if successful and FALSE otherwise.
183 //
184 // Input should look something like:
185 //
186 // [%#][ \t]*keyword[ \t]*int-value.*\n
187
188 bool
189 extract_keyword (std::istream& is, const char *keyword, int& value,
190 const bool next_only)
191 {
192 bool status = false;
193 value = 0;
194
195 char c;
196 while (is.get (c))
197 {
198 if (c == '%' || c == '#')
199 {
200 OSSTREAM buf;
201
202 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
203 ; // Skip whitespace and comment characters.
204
205 if (isalpha (c))
206 buf << c;
207
208 while (is.get (c) && isalpha (c))
209 buf << c;
210
211 buf << OSSTREAM_ENDS;
212 const char *tmp = OSSTREAM_C_STR (buf);
213 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0);
214 OSSTREAM_FREEZE (buf);
215
216 if (match)
217 {
218 while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
219 ; // Skip whitespace and the colon.
220
221 is.putback (c);
222 if (c != '\n')
223 is >> value;
224 if (is)
225 status = true;
226 while (is.get (c) && c != '\n')
227 ; // Skip to beginning of next line;
228 break;
229 }
230 else if (next_only)
231 break;
232 }
233 }
234 return status;
235 }
236
237 // Match one of the elements in KEYWORDS on stream IS, placing the
238 // matched keyword in KW and the associated value in VALUE,
239 // returning TRUE if successful and FALSE otherwise.
240 //
241 // Input should look something like:
242 //
243 // [%#][ \t]*keyword[ \t]*int-value.*\n
244
245 bool
246 extract_keyword (std::istream& is, const string_vector& keywords,
247 std::string& kw, int& value, const bool next_only)
248 {
249 bool status = false;
250 kw = "";
251 value = 0;
252
253 char c;
254 while (is.get (c))
255 {
256 if (c == '%' || c == '#')
257 {
258 OSSTREAM buf;
259
260 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
261 ; // Skip whitespace and comment characters.
262
263 if (isalpha (c))
264 buf << c;
265
266 while (is.get (c) && isalpha (c))
267 buf << c;
268
269 buf << OSSTREAM_ENDS;
270 std::string tmp = OSSTREAM_STR (buf);
271 OSSTREAM_FREEZE (buf);
272
273 for (int i = 0; i < keywords.length (); i++)
274 {
275 int match = (tmp == keywords[i]);
276
277 if (match)
278 {
279 kw = keywords[i];
280
281 while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
282 ; // Skip whitespace and the colon.
283
284 is.putback (c);
285 if (c != '\n')
286 is >> value;
287 if (is)
288 status = true;
289 while (is.get (c) && c != '\n')
290 ; // Skip to beginning of next line;
291 return status;
292 }
293 }
294
295 if (next_only)
296 break;
297 }
298 }
299 return status;
300 }
301
302 // Extract one value (scalar, matrix, string, etc.) from stream IS and 181 // Extract one value (scalar, matrix, string, etc.) from stream IS and
303 // place it in TC, returning the name of the variable. If the value 182 // place it in TC, returning the name of the variable. If the value
304 // is tagged as global in the file, return TRUE in GLOBAL. 183 // is tagged as global in the file, return TRUE in GLOBAL.
305 // 184 //
306 // Each type supplies its own function to load the data, and so this 185 // Each type supplies its own function to load the data, and so this
521 bool 400 bool
522 save_three_d (std::ostream& os, const octave_value& tc, bool parametric) 401 save_three_d (std::ostream& os, const octave_value& tc, bool parametric)
523 { 402 {
524 bool fail = false; 403 bool fail = false;
525 404
526 int nr = tc.rows (); 405 octave_idx_type nr = tc.rows ();
527 int nc = tc.columns (); 406 octave_idx_type nc = tc.columns ();
528 407
529 if (tc.is_real_matrix ()) 408 if (tc.is_real_matrix ())
530 { 409 {
531 os << "# 3D data...\n" 410 os << "# 3D data...\n"
532 << "# type: matrix\n" 411 << "# type: matrix\n"
533 << "# total rows: " << nr << "\n" 412 << "# total rows: " << nr << "\n"
534 << "# total columns: " << nc << "\n"; 413 << "# total columns: " << nc << "\n";
535 414
536 if (parametric) 415 if (parametric)
537 { 416 {
538 int extras = nc % 3; 417 octave_idx_type extras = nc % 3;
539 if (extras) 418 if (extras)
540 warning ("ignoring last %d columns", extras); 419 warning ("ignoring last %d columns", extras);
541 420
542 Matrix tmp = tc.matrix_value (); 421 Matrix tmp = tc.matrix_value ();
543 tmp = strip_infnan (tmp); 422 tmp = strip_infnan (tmp);
544 nr = tmp.rows (); 423 nr = tmp.rows ();
545 424
546 for (int i = 0; i < nc-extras; i += 3) 425 for (octave_idx_type i = 0; i < nc-extras; i += 3)
547 { 426 {
548 os << tmp.extract (0, i, nr-1, i+2); 427 os << tmp.extract (0, i, nr-1, i+2);
549 if (i+3 < nc-extras) 428 if (i+3 < nc-extras)
550 os << "\n"; 429 os << "\n";
551 } 430 }
554 { 433 {
555 Matrix tmp = tc.matrix_value (); 434 Matrix tmp = tc.matrix_value ();
556 tmp = strip_infnan (tmp); 435 tmp = strip_infnan (tmp);
557 nr = tmp.rows (); 436 nr = tmp.rows ();
558 437
559 for (int i = 0; i < nc; i++) 438 for (octave_idx_type i = 0; i < nc; i++)
560 { 439 {
561 os << tmp.extract (0, i, nr-1, i); 440 os << tmp.extract (0, i, nr-1, i);
562 if (i+1 < nc) 441 if (i+1 < nc)
563 os << "\n"; 442 os << "\n";
564 } 443 }