annotate liboctave/util/lo-utils.cc @ 31608:23664317f0d3

maint: merge stable to default
author Rik <rik@octave.org>
date Thu, 01 Dec 2022 20:05:44 -0800
parents dfa5d9c3ae72 aac27ad79be6
children 5f11de0e7440
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 30394
diff changeset
3 // Copyright (C) 1996-2022 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
1967
4cb6aaa0f2c7 [project @ 1996-02-17 02:54:24 by jwe]
jwe
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21723
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21202
diff changeset
27 # include "config.h"
1967
4cb6aaa0f2c7 [project @ 1996-02-17 02:54:24 by jwe]
jwe
parents:
diff changeset
28 #endif
4cb6aaa0f2c7 [project @ 1996-02-17 02:54:24 by jwe]
jwe
parents:
diff changeset
29
10463
bbe99b2a5ba7 undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents: 10447
diff changeset
30 #include <cstdlib>
7048
845ca0affec0 [project @ 2007-10-22 16:55:41 by jwe]
jwe
parents: 7017
diff changeset
31 #include <cstring>
1967
4cb6aaa0f2c7 [project @ 1996-02-17 02:54:24 by jwe]
jwe
parents:
diff changeset
32
23443
3f1bf237908b maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents: 23220
diff changeset
33 #include <complex>
25438
cb1606f78f6b prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
34 #include <istream>
6490
0ad7655cf2bc [project @ 2007-04-05 02:44:34 by jwe]
jwe
parents: 6202
diff changeset
35 #include <limits>
25438
cb1606f78f6b prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
36 #include <ostream>
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2847
diff changeset
37 #include <string>
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2847
diff changeset
38
10068
ca93f583573d handle interrupts octave_fgets
Jaroslav Hajek <highegg@gmail.com>
parents: 9469
diff changeset
39 #include "quit.h"
ca93f583573d handle interrupts octave_fgets
Jaroslav Hajek <highegg@gmail.com>
parents: 9469
diff changeset
40
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
41 #include "intprops-wrappers.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2847
diff changeset
42 #include "lo-error.h"
4130
7d9bda865012 [project @ 2002-10-28 21:05:30 by jwe]
jwe
parents: 3970
diff changeset
43 #include "lo-ieee.h"
1967
4cb6aaa0f2c7 [project @ 1996-02-17 02:54:24 by jwe]
jwe
parents:
diff changeset
44 #include "lo-mappers.h"
4cb6aaa0f2c7 [project @ 1996-02-17 02:54:24 by jwe]
jwe
parents:
diff changeset
45 #include "lo-utils.h"
29230
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
46 #include "oct-inttypes.h"
1967
4cb6aaa0f2c7 [project @ 1996-02-17 02:54:24 by jwe]
jwe
parents:
diff changeset
47
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30564
diff changeset
48 OCTAVE_BEGIN_NAMESPACE(octave)
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30564
diff changeset
49
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
50 bool is_int_or_inf_or_nan (double x)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
51 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
52 return math::isnan (x) || math::x_nint (x) == x;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
53 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
54
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
55 bool too_large_for_float (double x)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
56 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
57 return (math::isfinite (x)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
58 && fabs (x) > std::numeric_limits<float>::max ());
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
59 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
60
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
61 bool too_large_for_float (const Complex& x)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
62 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
63 return (too_large_for_float (x.real ())
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
64 || too_large_for_float (x.imag ()));
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
65 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
66
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
67 bool is_int_or_inf_or_nan (float x)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
68 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
69 return math::isnan (x) || math::x_nint (x) == x;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
70 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
71
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
72 // Save a string.
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
73
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
74 char * strsave (const char *s)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
75 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
76 if (! s)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
77 return nullptr;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
78
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
79 int len = strlen (s);
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
80 char *tmp = new char [len+1];
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
81 tmp = strcpy (tmp, s);
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
82 return tmp;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
83 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
84
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
85 std::string fgets (FILE *f)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
86 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
87 bool eof;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
88 return fgets (f, eof);
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
89 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
90
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
91 std::string fgets (FILE *f, bool& eof)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
92 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
93 eof = false;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
94
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
95 std::string retval;
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
96
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
97 int grow_size = 1024;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
98 int max_size = grow_size;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
99
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
100 char *buf = static_cast<char *> (std::malloc (max_size));
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
101 if (! buf)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
102 (*current_liboctave_error_handler) ("octave_fgets: unable to malloc %d bytes", max_size);
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
103
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
104 char *bufptr = buf;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
105 int len = 0;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
106
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
107 do
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
108 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
109 if (std::fgets (bufptr, grow_size, f))
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
110 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
111 len = strlen (bufptr);
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
112
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
113 if (len == grow_size - 1)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
114 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
115 int tmp = bufptr - buf + grow_size - 1;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
116 grow_size *= 2;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
117 max_size += grow_size;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
118 auto tmpbuf = static_cast<char *> (std::realloc (buf, max_size));
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
119 if (! tmpbuf)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
120 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
121 free (buf);
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
122 (*current_liboctave_error_handler) ("octave_fgets: unable to realloc %d bytes", max_size);
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
123 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
124 buf = tmpbuf;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
125 bufptr = buf + tmp;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
126
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
127 if (*(bufptr-1) == '\n')
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
128 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
129 *bufptr = '\0';
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
130 retval = buf;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
131 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
132 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
133 else if (bufptr[len-1] != '\n')
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
134 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
135 bufptr[len++] = '\n';
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
136 bufptr[len] = '\0';
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
137 retval = buf;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
138 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
139 else
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
140 retval = buf;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
141 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
142 else
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
143 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
144 if (len == 0)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
145 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
146 eof = true;
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
147
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
148 free (buf);
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
149
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
150 buf = nullptr;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
151 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
152
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
153 break;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
154 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
155 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
156 while (retval.empty ());
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
157
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
158 free (buf);
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
159
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
160 octave_quit ();
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
161
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
162 return retval;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
163 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
164
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
165 std::string fgetl (FILE *f)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
166 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
167 bool eof;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
168 return fgetl (f, eof);
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
169 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
170
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
171 std::string fgetl (FILE *f, bool& eof)
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
172 {
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
173 std::string retval = fgets (f, eof);
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
174
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
175 if (! retval.empty () && retval.back () == '\n')
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
176 retval.pop_back ();
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
177
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
178 return retval;
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
179 }
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
180
29230
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
181 template <typename T>
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
182 T
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
183 read_value (std::istream& is)
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
184 {
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
185 T retval;
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
186 is >> retval;
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
187 return retval;
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
188 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
189
29244
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
190 template OCTAVE_API bool read_value<bool> (std::istream& is);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
191 template OCTAVE_API octave_int8 read_value<octave_int8> (std::istream& is);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
192 template OCTAVE_API octave_int16 read_value<octave_int16> (std::istream& is);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
193 template OCTAVE_API octave_int32 read_value<octave_int32> (std::istream& is);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
194 template OCTAVE_API octave_int64 read_value<octave_int64> (std::istream& is);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
195 template OCTAVE_API octave_uint8 read_value<octave_uint8> (std::istream& is);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
196 template OCTAVE_API octave_uint16 read_value<octave_uint16> (std::istream& is);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
197 template OCTAVE_API octave_uint32 read_value<octave_uint32> (std::istream& is);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
198 template OCTAVE_API octave_uint64 read_value<octave_uint64> (std::istream& is);
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
199
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
200 // Note that the caller is responsible for repositioning the stream on
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
201 // failure.
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
202
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
203 template <typename T>
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
204 T
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
205 read_inf_nan_na (std::istream& is, char c0)
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
206 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
207 T val = 0.0;
14806
980e2d5c83f7 avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
208
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
209 switch (c0)
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
210 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
211 case 'i': case 'I':
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
212 {
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
213 char c1 = is.get ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
214 if (c1 == 'n' || c1 == 'N')
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
215 {
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
216 char c2 = is.get ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
217 if (c2 == 'f' || c2 == 'F')
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
218 {
31542
2f08a53e0a23 Fix scanf handling of exceptional values (NA, NaN, Inf) at EOF (bug #63383)
Rik <rik@octave.org>
parents: 31504
diff changeset
219 val = std::numeric_limits<T>::infinity ();
2f08a53e0a23 Fix scanf handling of exceptional values (NA, NaN, Inf) at EOF (bug #63383)
Rik <rik@octave.org>
parents: 31504
diff changeset
220 is.peek (); // Potentially set EOF bit
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
221 }
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
222 else
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
223 is.setstate (std::ios::failbit);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
224 }
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
225 else
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
226 is.setstate (std::ios::failbit);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
227 }
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
228 break;
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
229
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
230 case 'n': case 'N':
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
231 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
232 char c1 = is.get ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
233 if (c1 == 'a' || c1 == 'A')
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
234 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
235 char c2 = is.get ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
236 if (c2 == 'n' || c2 == 'N')
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
237 {
31542
2f08a53e0a23 Fix scanf handling of exceptional values (NA, NaN, Inf) at EOF (bug #63383)
Rik <rik@octave.org>
parents: 31504
diff changeset
238 val = std::numeric_limits<T>::quiet_NaN ();
2f08a53e0a23 Fix scanf handling of exceptional values (NA, NaN, Inf) at EOF (bug #63383)
Rik <rik@octave.org>
parents: 31504
diff changeset
239 is.peek (); // Potentially set EOF bit
2f08a53e0a23 Fix scanf handling of exceptional values (NA, NaN, Inf) at EOF (bug #63383)
Rik <rik@octave.org>
parents: 31504
diff changeset
240 }
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
241 else
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
242 {
28857
43ad651cf5a0 eliminate unnecessary uses of octave:: namespace qualifier
John W. Eaton <jwe@octave.org>
parents: 28849
diff changeset
243 val = numeric_limits<T>::NA ();
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
244 if (c2 != std::istream::traits_type::eof ())
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
245 is.putback (c2);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
246 else
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
247 is.clear (is.rdstate () & ~std::ios::failbit);
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
248 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
249 }
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
250 else
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
251 is.setstate (std::ios::failbit);
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
252 }
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
253 break;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
254
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
255 default:
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
256 (*current_liboctave_error_handler)
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
257 ("read_inf_nan_na: invalid character '%c'", c0);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
258 }
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
259
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
260 return val;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
261 }
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
262
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
263 // Read a double value. Discard any sign on NaN and NA.
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
264
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
265 template <typename T>
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
266 double
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
267 read_fp_value (std::istream& is)
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
268 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
269 T val = 0.0;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
270
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
271 // FIXME: resetting stream position is likely to fail unless we are
31504
d006285b7509 Fix scanf handling of bare '-' character for floating point formats (bug #63383)
Rik <rik@octave.org>
parents: 31500
diff changeset
272 // reading from a file.
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
273 std::streampos pos = is.tellg ();
4130
7d9bda865012 [project @ 2002-10-28 21:05:30 by jwe]
jwe
parents: 3970
diff changeset
274
31504
d006285b7509 Fix scanf handling of bare '-' character for floating point formats (bug #63383)
Rik <rik@octave.org>
parents: 31500
diff changeset
275 is >> std::ws; // skip through whitespace and advance stream pointer
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
276
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
277 bool neg = false;
31504
d006285b7509 Fix scanf handling of bare '-' character for floating point formats (bug #63383)
Rik <rik@octave.org>
parents: 31500
diff changeset
278 char c1 = is.get ();
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
279 switch (c1)
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
280 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
281 case '-':
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
282 neg = true;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
283 OCTAVE_FALLTHROUGH;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
284
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
285 case '+':
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
286 {
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
287 char c2 = 0;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
288 c2 = is.get ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
289 if (c2 == 'i' || c2 == 'I' || c2 == 'n' || c2 == 'N')
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
290 val = read_inf_nan_na<T> (is, c2);
31504
d006285b7509 Fix scanf handling of bare '-' character for floating point formats (bug #63383)
Rik <rik@octave.org>
parents: 31500
diff changeset
291 else if (isspace (c2))
d006285b7509 Fix scanf handling of bare '-' character for floating point formats (bug #63383)
Rik <rik@octave.org>
parents: 31500
diff changeset
292 is.setstate (std::ios::failbit);
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
293 else
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
294 {
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
295 is.putback (c2);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
296 is >> val;
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
297 }
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
298
31542
2f08a53e0a23 Fix scanf handling of exceptional values (NA, NaN, Inf) at EOF (bug #63383)
Rik <rik@octave.org>
parents: 31504
diff changeset
299 if (neg && ! math::isnan (val) && ! is.fail ())
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
300 val = -val;
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
301 }
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
302 break;
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
303
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
304 case 'i': case 'I':
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
305 case 'n': case 'N':
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
306 val = read_inf_nan_na<T> (is, c1);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
307 break;
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
308
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
309 default:
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
310 is.putback (c1);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
311 is >> val;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
312 break;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
313 }
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
314
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
315 std::ios::iostate status = is.rdstate ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
316 if (status & std::ios::failbit)
4130
7d9bda865012 [project @ 2002-10-28 21:05:30 by jwe]
jwe
parents: 3970
diff changeset
317 {
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
318 // Convert MAX_VAL returned by C++ streams for very large numbers to Inf
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
319 if (val == std::numeric_limits<T>::max ())
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
320 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
321 if (neg)
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
322 val = -std::numeric_limits<T>::infinity ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
323 else
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
324 val = std::numeric_limits<T>::infinity ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
325 is.clear (status & ~std::ios::failbit);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
326 }
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
327 else
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
328 {
31500
6bd338605fd3 Overhaul scanf functionality for correctness and Matlab compatibility (bug #63383, bug #62723)
Rik <rik@octave.org>
parents: 30564
diff changeset
329 // True error.
6bd338605fd3 Overhaul scanf functionality for correctness and Matlab compatibility (bug #63383, bug #62723)
Rik <rik@octave.org>
parents: 30564
diff changeset
330 // Reset stream to original position, clear eof bit, pass status on.
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
331 is.clear ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
332 is.seekg (pos);
31500
6bd338605fd3 Overhaul scanf functionality for correctness and Matlab compatibility (bug #63383, bug #62723)
Rik <rik@octave.org>
parents: 30564
diff changeset
333 is.setstate (status & ~std::ios_base::eofbit);
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
334 }
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
335 }
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
336
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
337 return val;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
338 }
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
339
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
340 template <typename T>
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
341 std::complex<T>
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
342 read_cx_fp_value (std::istream& is)
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
343 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
344 T re = 0.0;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
345 T im = 0.0;
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
346
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
347 std::complex<T> cx = 0.0;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
348
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
349 char ch = ' ';
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
350
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
351 while (isspace (ch))
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
352 ch = is.get ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
353
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
354 if (ch == '(')
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
355 {
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
356 re = read_value<T> (is);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
357 ch = is.get ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
358
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
359 if (ch == ',')
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
360 {
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
361 im = read_value<T> (is);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
362 ch = is.get ();
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
363
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
364 if (ch == ')')
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
365 cx = std::complex<T> (re, im);
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
366 else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
367 is.setstate (std::ios::failbit);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
368 }
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
369 else if (ch == ')')
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
370 cx = re;
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
371 else
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
372 is.setstate (std::ios::failbit);
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
373 }
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
374 else
6194
0d2ff0dfb159 [project @ 2006-12-01 05:25:38 by jwe]
jwe
parents: 5775
diff changeset
375 {
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
376 is.putback (ch);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
377 cx = read_value<T> (is);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
378 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
379
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
380 return cx;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
381 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
382
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
383 // FIXME: Could we use traits and enable_if to avoid duplication in the
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
384 // following specializations?
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
385
29230
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
386 template <> OCTAVE_API double read_value (std::istream& is)
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
387 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
388 return read_fp_value<double> (is);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
389 }
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
390
29230
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
391 template <> OCTAVE_API Complex read_value (std::istream& is)
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
392 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
393 return read_cx_fp_value<double> (is);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
394 }
29937
da7210e30f3e move some utility functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29445
diff changeset
395
29230
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
396 template <> OCTAVE_API float read_value (std::istream& is)
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
397 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
398 return read_fp_value<float> (is);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
399 }
29230
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
400
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
401 template <> OCTAVE_API FloatComplex read_value (std::istream& is)
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
402 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
403 return read_cx_fp_value<float> (is);
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
404 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
405
29230
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
406 template <typename T>
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
407 void
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
408 write_value (std::ostream& os, const T& value)
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
409 {
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
410 os << value;
6b3faa844395 Set API tags in files in liboctave/util (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28857
diff changeset
411 }
31607
aac27ad79be6 maint: Re-indent code after switch to using namespace macros.
Rik <rik@octave.org>
parents: 31605
diff changeset
412
29244
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
413 template OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
414 write_value<bool> (std::ostream& os, const bool& value);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
415 template OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
416 write_value<octave_int8> (std::ostream& os, const octave_int8& value);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
417 template OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
418 write_value<octave_int16> (std::ostream& os, const octave_int16& value);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
419 template OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
420 write_value<octave_int32> (std::ostream& os, const octave_int32& value);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
421 template OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
422 write_value<octave_int64> (std::ostream& os, const octave_int64& value);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
423 template OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
424 write_value<octave_uint8> (std::ostream& os, const octave_uint8& value);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
425 template OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
426 write_value<octave_uint16> (std::ostream& os, const octave_uint16& value);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
427 template OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
428 write_value<octave_uint32> (std::ostream& os, const octave_uint32& value);
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
429 template OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
430 write_value<octave_uint64> (std::ostream& os, const octave_uint64& value);
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
431
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
432 // Note: precision is supposed to be managed outside of this function by
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
433 // setting stream parameters.
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
434
29244
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
435 template <> OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
436 write_value (std::ostream& os, const double& value)
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
437 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
438 if (lo_ieee_is_NA (value))
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
439 os << "NA";
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
440 else if (lo_ieee_isnan (value))
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
441 os << "NaN";
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
442 else if (lo_ieee_isinf (value))
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
443 os << (value < 0 ? "-Inf" : "Inf");
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
444 else
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
445 os << value;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
446 }
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
447
29244
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
448 template <> OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
449 write_value (std::ostream& os, const Complex& value)
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
450 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
451 os << '(';
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
452 write_value<double> (os, real (value));
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
453 os << ',';
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
454 write_value<double> (os, imag (value));
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
455 os << ')';
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
456 }
6194
0d2ff0dfb159 [project @ 2006-12-01 05:25:38 by jwe]
jwe
parents: 5775
diff changeset
457
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
458 // Note: precision is supposed to be managed outside of this function by
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
459 // setting stream parameters.
6194
0d2ff0dfb159 [project @ 2006-12-01 05:25:38 by jwe]
jwe
parents: 5775
diff changeset
460
29244
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
461 template <> OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
462 write_value (std::ostream& os, const float& value)
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
463 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
464 if (lo_ieee_is_NA (value))
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
465 os << "NA";
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
466 else if (lo_ieee_isnan (value))
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
467 os << "NaN";
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
468 else if (lo_ieee_isinf (value))
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
469 os << (value < 0 ? "-Inf" : "Inf");
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
470 else
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
471 os << value;
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
472 }
4130
7d9bda865012 [project @ 2002-10-28 21:05:30 by jwe]
jwe
parents: 3970
diff changeset
473
29244
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
474 template <> OCTAVE_API void
d1473e75d7d6 lo-utils: Export template instantiations (patch #8919).
Markus Mützel <markus.muetzel@gmx.de>
parents: 29230
diff changeset
475 write_value (std::ostream& os, const FloatComplex& value)
28628
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
476 {
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
477 os << '(';
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
478 write_value<float> (os, real (value));
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
479 os << ',';
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
480 write_value<float> (os, imag (value));
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
481 os << ')';
83172e1c77f2 refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
482 }
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
483
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30564
diff changeset
484 OCTAVE_BEGIN_NAMESPACE(math)
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30564
diff changeset
485
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
486 bool int_multiply_overflow (int a, int b, int *r)
14806
980e2d5c83f7 avoid calling putback more than once between reads
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
487 {
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
488 return octave_i_multiply_overflow_wrapper (a, b, r);
4130
7d9bda865012 [project @ 2002-10-28 21:05:30 by jwe]
jwe
parents: 3970
diff changeset
489 }
7d9bda865012 [project @ 2002-10-28 21:05:30 by jwe]
jwe
parents: 3970
diff changeset
490
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
491 bool int_multiply_overflow (long int a, long int b, long int *r)
4130
7d9bda865012 [project @ 2002-10-28 21:05:30 by jwe]
jwe
parents: 3970
diff changeset
492 {
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
493 return octave_li_multiply_overflow_wrapper (a, b, r);
4130
7d9bda865012 [project @ 2002-10-28 21:05:30 by jwe]
jwe
parents: 3970
diff changeset
494 }
7d9bda865012 [project @ 2002-10-28 21:05:30 by jwe]
jwe
parents: 3970
diff changeset
495
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
496 #if defined (OCTAVE_HAVE_LONG_LONG_INT)
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
497 bool int_multiply_overflow (long long int a, long long int b,
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
498 long long int *r)
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
499 {
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
500 return octave_lli_multiply_overflow_wrapper (a, b, r);
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
501 }
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
502 #endif
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7048
diff changeset
503
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
504 bool int_multiply_overflow (unsigned int a, unsigned int b,
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
505 unsigned int *r)
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
506 {
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
507 return octave_ui_multiply_overflow_wrapper (a, b, r);
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
508 }
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7048
diff changeset
509
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
510 bool int_multiply_overflow (unsigned long int a, unsigned long int b,
30394
f3f3e3793fb5 maint: style check C++ files in liboctave/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 29937
diff changeset
511 unsigned long int *r)
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
512 {
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
513 return octave_uli_multiply_overflow_wrapper (a, b, r);
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
514 }
9469
c6edba80dfae sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents: 8920
diff changeset
515
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
516 #if defined (OCTAVE_HAVE_UNSIGNED_LONG_LONG_INT)
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
517 bool int_multiply_overflow (unsigned long long int a,
30394
f3f3e3793fb5 maint: style check C++ files in liboctave/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 29937
diff changeset
518 unsigned long long int b,
f3f3e3793fb5 maint: style check C++ files in liboctave/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 29937
diff changeset
519 unsigned long long int *r)
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
520 {
29445
b5f3ae7610cf Use gnulib macros that account for the output type in integer overflow checks.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29359
diff changeset
521 return octave_ulli_multiply_overflow_wrapper (a, b, r);
28849
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
522 }
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
523 #endif
8b0675c2cfe0 Add functions to check for integer overflow on multiplication (bug #59094).
Markus Mützel <markus.muetzel@gmx.de>
parents: 28643
diff changeset
524
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30564
diff changeset
525 OCTAVE_END_NAMESPACE(math)
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30564
diff changeset
526 OCTAVE_END_NAMESPACE(octave)