comparison liboctave/lo-utils.cc @ 9469:c6edba80dfae

sanity checks for loading sparse matrices
author John W. Eaton <jwe@octave.org>
date Wed, 29 Jul 2009 12:15:27 -0400
parents eb63fbe60fab
children ca93f583573d
comparison
equal deleted inserted replaced
9468:5af462716bff 9469:c6edba80dfae
288 } 288 }
289 289
290 return d; 290 return d;
291 } 291 }
292 292
293 template <>
293 double 294 double
294 octave_read_double (std::istream& is) 295 octave_read_value (std::istream& is)
295 { 296 {
296 double d = 0.0; 297 double d = 0.0;
297 298
298 char c1 = ' '; 299 char c1 = ' ';
299 300
343 } 344 }
344 345
345 return d; 346 return d;
346 } 347 }
347 348
349 template <>
348 Complex 350 Complex
349 octave_read_complex (std::istream& is) 351 octave_read_value (std::istream& is)
350 { 352 {
351 double re = 0.0, im = 0.0; 353 double re = 0.0, im = 0.0;
352 354
353 Complex cx = 0.0; 355 Complex cx = 0.0;
354 356
357 while (isspace (ch)) 359 while (isspace (ch))
358 ch = is.get (); 360 ch = is.get ();
359 361
360 if (ch == '(') 362 if (ch == '(')
361 { 363 {
362 re = octave_read_double (is); 364 re = octave_read_value<double> (is);
363 ch = is.get (); 365 ch = is.get ();
364 366
365 if (ch == ',') 367 if (ch == ',')
366 { 368 {
367 im = octave_read_double (is); 369 im = octave_read_value<double> (is);
368 ch = is.get (); 370 ch = is.get ();
369 371
370 if (ch == ')') 372 if (ch == ')')
371 cx = Complex (re, im); 373 cx = Complex (re, im);
372 else 374 else
378 is.setstate (std::ios::failbit); 380 is.setstate (std::ios::failbit);
379 } 381 }
380 else 382 else
381 { 383 {
382 is.putback (ch); 384 is.putback (ch);
383 cx = octave_read_double (is); 385 cx = octave_read_value<double> (is);
384 } 386 }
385 387
386 return cx; 388 return cx;
387 389
388 } 390 }
389
390 void
391 octave_write_double (std::ostream& os, double d)
392 {
393 if (lo_ieee_is_NA (d))
394 os << "NA";
395 else if (lo_ieee_isnan (d))
396 os << "NaN";
397 else if (lo_ieee_isinf (d))
398 os << (d < 0 ? "-Inf" : "Inf");
399 else
400 os << d;
401 }
402
403 void
404 octave_write_complex (std::ostream& os, const Complex& c)
405 {
406 os << "(";
407 octave_write_double (os, real (c));
408 os << ",";
409 octave_write_double (os, imag (c));
410 os << ")";
411 }
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433 391
434 static inline float 392 static inline float
435 read_float_inf_nan_na (std::istream& is, char c, char sign = '+') 393 read_float_inf_nan_na (std::istream& is, char c, char sign = '+')
436 { 394 {
437 float d = 0.0; 395 float d = 0.0;
478 } 436 }
479 437
480 return d; 438 return d;
481 } 439 }
482 440
441 template <>
483 float 442 float
484 octave_read_float (std::istream& is) 443 octave_read_value (std::istream& is)
485 { 444 {
486 float d = 0.0; 445 float d = 0.0;
487 446
488 char c1 = ' '; 447 char c1 = ' ';
489 448
533 } 492 }
534 493
535 return d; 494 return d;
536 } 495 }
537 496
497 template <>
538 FloatComplex 498 FloatComplex
539 octave_read_float_complex (std::istream& is) 499 octave_read_value (std::istream& is)
540 { 500 {
541 float re = 0.0, im = 0.0; 501 float re = 0.0, im = 0.0;
542 502
543 FloatComplex cx = 0.0; 503 FloatComplex cx = 0.0;
544 504
547 while (isspace (ch)) 507 while (isspace (ch))
548 ch = is.get (); 508 ch = is.get ();
549 509
550 if (ch == '(') 510 if (ch == '(')
551 { 511 {
552 re = octave_read_float (is); 512 re = octave_read_value<float> (is);
553 ch = is.get (); 513 ch = is.get ();
554 514
555 if (ch == ',') 515 if (ch == ',')
556 { 516 {
557 im = octave_read_float (is); 517 im = octave_read_value<float> (is);
558 ch = is.get (); 518 ch = is.get ();
559 519
560 if (ch == ')') 520 if (ch == ')')
561 cx = FloatComplex (re, im); 521 cx = FloatComplex (re, im);
562 else 522 else
568 is.setstate (std::ios::failbit); 528 is.setstate (std::ios::failbit);
569 } 529 }
570 else 530 else
571 { 531 {
572 is.putback (ch); 532 is.putback (ch);
573 cx = octave_read_float (is); 533 cx = octave_read_value<float> (is);
574 } 534 }
575 535
576 return cx; 536 return cx;
577 537
578 } 538 }
579 539
580 void 540 void
581 octave_write_float (std::ostream& os, float d) 541 octave_write_double (std::ostream& os, double d)
582 { 542 {
583 if (lo_ieee_is_NA (d)) 543 if (lo_ieee_is_NA (d))
584 os << "NA"; 544 os << "NA";
585 else if (lo_ieee_isnan (d)) 545 else if (lo_ieee_isnan (d))
586 os << "NaN"; 546 os << "NaN";
589 else 549 else
590 os << d; 550 os << d;
591 } 551 }
592 552
593 void 553 void
554 octave_write_complex (std::ostream& os, const Complex& c)
555 {
556 os << "(";
557 octave_write_double (os, real (c));
558 os << ",";
559 octave_write_double (os, imag (c));
560 os << ")";
561 }
562
563 void
564 octave_write_float (std::ostream& os, float d)
565 {
566 if (lo_ieee_is_NA (d))
567 os << "NA";
568 else if (lo_ieee_isnan (d))
569 os << "NaN";
570 else if (lo_ieee_isinf (d))
571 os << (d < 0 ? "-Inf" : "Inf");
572 else
573 os << d;
574 }
575
576 void
594 octave_write_float_complex (std::ostream& os, const FloatComplex& c) 577 octave_write_float_complex (std::ostream& os, const FloatComplex& c)
595 { 578 {
596 os << "("; 579 os << "(";
597 octave_write_float (os, real (c)); 580 octave_write_float (os, real (c));
598 os << ","; 581 os << ",";