4634
|
1 /* |
|
2 |
|
3 Copyright (C) 2003 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4634
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_ls_oct_ascii_h) |
|
25 #define octave_ls_oct_ascii_h 1 |
|
26 |
4687
|
27 #include <cfloat> |
|
28 |
5099
|
29 #include <string> |
|
30 |
5275
|
31 #include "lo-sstream.h" |
5099
|
32 #include "str-vec.h" |
|
33 |
4687
|
34 // Flag for cell elements |
|
35 #define CELL_ELT_TAG "<cell-element>" |
|
36 |
|
37 // Used when converting Inf to something that gnuplot can read. |
|
38 |
|
39 #ifndef OCT_RBV |
|
40 #define OCT_RBV DBL_MAX / 100.0 |
|
41 #endif |
|
42 |
4634
|
43 extern std::string |
4687
|
44 extract_keyword (std::istream& is, const char *keyword, |
|
45 const bool next_only = false); |
|
46 |
4634
|
47 extern std::string |
|
48 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
5754
|
49 octave_value& tc, octave_idx_type count); |
4634
|
50 |
|
51 extern bool |
|
52 save_ascii_data (std::ostream& os, const octave_value& val_arg, |
|
53 const std::string& name, bool& infnan_warned, |
|
54 bool strip_nan_and_inf, bool mark_as_global, |
|
55 int precision); |
|
56 |
|
57 extern bool |
|
58 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
|
59 const std::string& name); |
|
60 |
5275
|
61 // Match KEYWORD on stream IS, placing the associated value in VALUE, |
|
62 // returning TRUE if successful and FALSE otherwise. |
|
63 // |
|
64 // Input should look something like: |
|
65 // |
|
66 // [%#][ \t]*keyword[ \t]*int-value.*\n |
|
67 |
|
68 template <class T> |
|
69 bool |
|
70 extract_keyword (std::istream& is, const char *keyword, T& value, |
|
71 const bool next_only = false) |
|
72 { |
|
73 bool status = false; |
|
74 value = 0; |
|
75 |
|
76 char c; |
|
77 while (is.get (c)) |
|
78 { |
|
79 if (c == '%' || c == '#') |
|
80 { |
|
81 OSSTREAM buf; |
|
82 |
|
83 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
84 ; // Skip whitespace and comment characters. |
|
85 |
|
86 if (isalpha (c)) |
|
87 buf << c; |
|
88 |
|
89 while (is.get (c) && isalpha (c)) |
|
90 buf << c; |
|
91 |
|
92 buf << OSSTREAM_ENDS; |
5679
|
93 std::string tmp = OSSTREAM_STR (buf); |
|
94 bool match = (tmp.compare (0, strlen (keyword), keyword) == 0); |
5275
|
95 OSSTREAM_FREEZE (buf); |
|
96 |
|
97 if (match) |
|
98 { |
|
99 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
100 ; // Skip whitespace and the colon. |
|
101 |
|
102 is.putback (c); |
|
103 if (c != '\n') |
|
104 is >> value; |
|
105 if (is) |
|
106 status = true; |
|
107 while (is.get (c) && c != '\n') |
|
108 ; // Skip to beginning of next line; |
|
109 break; |
|
110 } |
|
111 else if (next_only) |
|
112 break; |
|
113 } |
|
114 } |
|
115 return status; |
|
116 } |
|
117 |
|
118 // Match one of the elements in KEYWORDS on stream IS, placing the |
|
119 // matched keyword in KW and the associated value in VALUE, |
|
120 // returning TRUE if successful and FALSE otherwise. |
|
121 // |
|
122 // Input should look something like: |
|
123 // |
|
124 // [%#][ \t]*keyword[ \t]*int-value.*\n |
|
125 |
|
126 template <class T> |
|
127 bool |
|
128 extract_keyword (std::istream& is, const string_vector& keywords, |
|
129 std::string& kw, T& value, const bool next_only = false) |
|
130 { |
|
131 bool status = false; |
|
132 kw = ""; |
|
133 value = 0; |
|
134 |
|
135 char c; |
|
136 while (is.get (c)) |
|
137 { |
|
138 if (c == '%' || c == '#') |
|
139 { |
|
140 OSSTREAM buf; |
|
141 |
|
142 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
143 ; // Skip whitespace and comment characters. |
|
144 |
|
145 if (isalpha (c)) |
|
146 buf << c; |
|
147 |
|
148 while (is.get (c) && isalpha (c)) |
|
149 buf << c; |
|
150 |
|
151 buf << OSSTREAM_ENDS; |
|
152 std::string tmp = OSSTREAM_STR (buf); |
|
153 OSSTREAM_FREEZE (buf); |
|
154 |
|
155 for (int i = 0; i < keywords.length (); i++) |
|
156 { |
|
157 int match = (tmp == keywords[i]); |
|
158 |
|
159 if (match) |
|
160 { |
|
161 kw = keywords[i]; |
|
162 |
|
163 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
164 ; // Skip whitespace and the colon. |
|
165 |
|
166 is.putback (c); |
|
167 if (c != '\n') |
|
168 is >> value; |
|
169 if (is) |
|
170 status = true; |
|
171 while (is.get (c) && c != '\n') |
|
172 ; // Skip to beginning of next line; |
|
173 return status; |
|
174 } |
|
175 } |
|
176 |
|
177 if (next_only) |
|
178 break; |
|
179 } |
|
180 } |
|
181 return status; |
|
182 } |
|
183 |
4634
|
184 #endif |
|
185 |
|
186 /* |
|
187 ;;; Local Variables: *** |
|
188 ;;; mode: C++ *** |
|
189 ;;; End: *** |
|
190 */ |
|
191 |