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 |
5765
|
29 #include <sstream> |
5099
|
30 #include <string> |
|
31 |
|
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 { |
5765
|
81 std::ostringstream buf; |
5275
|
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 |
5765
|
92 std::string tmp = buf.str (); |
5679
|
93 bool match = (tmp.compare (0, strlen (keyword), keyword) == 0); |
5275
|
94 |
|
95 if (match) |
|
96 { |
|
97 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
98 ; // Skip whitespace and the colon. |
|
99 |
|
100 is.putback (c); |
|
101 if (c != '\n') |
|
102 is >> value; |
|
103 if (is) |
|
104 status = true; |
|
105 while (is.get (c) && c != '\n') |
|
106 ; // Skip to beginning of next line; |
|
107 break; |
|
108 } |
|
109 else if (next_only) |
|
110 break; |
|
111 } |
|
112 } |
|
113 return status; |
|
114 } |
|
115 |
|
116 // Match one of the elements in KEYWORDS on stream IS, placing the |
|
117 // matched keyword in KW and the associated value in VALUE, |
|
118 // returning TRUE if successful and FALSE otherwise. |
|
119 // |
|
120 // Input should look something like: |
|
121 // |
|
122 // [%#][ \t]*keyword[ \t]*int-value.*\n |
|
123 |
|
124 template <class T> |
|
125 bool |
|
126 extract_keyword (std::istream& is, const string_vector& keywords, |
|
127 std::string& kw, T& value, const bool next_only = false) |
|
128 { |
|
129 bool status = false; |
|
130 kw = ""; |
|
131 value = 0; |
|
132 |
|
133 char c; |
|
134 while (is.get (c)) |
|
135 { |
|
136 if (c == '%' || c == '#') |
|
137 { |
5765
|
138 std::ostringstream buf; |
5275
|
139 |
|
140 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
141 ; // Skip whitespace and comment characters. |
|
142 |
|
143 if (isalpha (c)) |
|
144 buf << c; |
|
145 |
|
146 while (is.get (c) && isalpha (c)) |
|
147 buf << c; |
|
148 |
5765
|
149 std::string tmp = buf.str (); |
5275
|
150 |
|
151 for (int i = 0; i < keywords.length (); i++) |
|
152 { |
|
153 int match = (tmp == keywords[i]); |
|
154 |
|
155 if (match) |
|
156 { |
|
157 kw = keywords[i]; |
|
158 |
|
159 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
160 ; // Skip whitespace and the colon. |
|
161 |
|
162 is.putback (c); |
|
163 if (c != '\n') |
|
164 is >> value; |
|
165 if (is) |
|
166 status = true; |
|
167 while (is.get (c) && c != '\n') |
|
168 ; // Skip to beginning of next line; |
|
169 return status; |
|
170 } |
|
171 } |
|
172 |
|
173 if (next_only) |
|
174 break; |
|
175 } |
|
176 } |
|
177 return status; |
|
178 } |
|
179 |
4634
|
180 #endif |
|
181 |
|
182 /* |
|
183 ;;; Local Variables: *** |
|
184 ;;; mode: C++ *** |
|
185 ;;; End: *** |
|
186 */ |
|
187 |