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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_ls_oct_ascii_h) |
|
24 #define octave_ls_oct_ascii_h 1 |
|
25 |
4687
|
26 #include <cfloat> |
|
27 |
5099
|
28 #include <string> |
|
29 |
5275
|
30 #include "lo-sstream.h" |
5099
|
31 #include "str-vec.h" |
|
32 |
4687
|
33 // Flag for cell elements |
|
34 #define CELL_ELT_TAG "<cell-element>" |
|
35 |
|
36 // Used when converting Inf to something that gnuplot can read. |
|
37 |
|
38 #ifndef OCT_RBV |
|
39 #define OCT_RBV DBL_MAX / 100.0 |
|
40 #endif |
|
41 |
4634
|
42 extern std::string |
4687
|
43 extract_keyword (std::istream& is, const char *keyword, |
|
44 const bool next_only = false); |
|
45 |
4634
|
46 extern std::string |
|
47 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
|
48 octave_value& tc, int count); |
|
49 |
|
50 extern bool |
|
51 save_ascii_data (std::ostream& os, const octave_value& val_arg, |
|
52 const std::string& name, bool& infnan_warned, |
|
53 bool strip_nan_and_inf, bool mark_as_global, |
|
54 int precision); |
|
55 |
|
56 extern bool |
|
57 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
|
58 const std::string& name); |
|
59 |
5275
|
60 // Match KEYWORD on stream IS, placing the associated value in VALUE, |
|
61 // returning TRUE if successful and FALSE otherwise. |
|
62 // |
|
63 // Input should look something like: |
|
64 // |
|
65 // [%#][ \t]*keyword[ \t]*int-value.*\n |
|
66 |
|
67 template <class T> |
|
68 bool |
|
69 extract_keyword (std::istream& is, const char *keyword, T& value, |
|
70 const bool next_only = false) |
|
71 { |
|
72 bool status = false; |
|
73 value = 0; |
|
74 |
|
75 char c; |
|
76 while (is.get (c)) |
|
77 { |
|
78 if (c == '%' || c == '#') |
|
79 { |
|
80 OSSTREAM buf; |
|
81 |
|
82 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
83 ; // Skip whitespace and comment characters. |
|
84 |
|
85 if (isalpha (c)) |
|
86 buf << c; |
|
87 |
|
88 while (is.get (c) && isalpha (c)) |
|
89 buf << c; |
|
90 |
|
91 buf << OSSTREAM_ENDS; |
|
92 const char *tmp = OSSTREAM_C_STR (buf); |
|
93 int match = (strncmp (tmp, keyword, strlen (keyword)) == 0); |
|
94 OSSTREAM_FREEZE (buf); |
|
95 |
|
96 if (match) |
|
97 { |
|
98 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
99 ; // Skip whitespace and the colon. |
|
100 |
|
101 is.putback (c); |
|
102 if (c != '\n') |
|
103 is >> value; |
|
104 if (is) |
|
105 status = true; |
|
106 while (is.get (c) && c != '\n') |
|
107 ; // Skip to beginning of next line; |
|
108 break; |
|
109 } |
|
110 else if (next_only) |
|
111 break; |
|
112 } |
|
113 } |
|
114 return status; |
|
115 } |
|
116 |
|
117 // Match one of the elements in KEYWORDS on stream IS, placing the |
|
118 // matched keyword in KW and the associated value in VALUE, |
|
119 // returning TRUE if successful and FALSE otherwise. |
|
120 // |
|
121 // Input should look something like: |
|
122 // |
|
123 // [%#][ \t]*keyword[ \t]*int-value.*\n |
|
124 |
|
125 template <class T> |
|
126 bool |
|
127 extract_keyword (std::istream& is, const string_vector& keywords, |
|
128 std::string& kw, T& value, const bool next_only = false) |
|
129 { |
|
130 bool status = false; |
|
131 kw = ""; |
|
132 value = 0; |
|
133 |
|
134 char c; |
|
135 while (is.get (c)) |
|
136 { |
|
137 if (c == '%' || c == '#') |
|
138 { |
|
139 OSSTREAM buf; |
|
140 |
|
141 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
|
142 ; // Skip whitespace and comment characters. |
|
143 |
|
144 if (isalpha (c)) |
|
145 buf << c; |
|
146 |
|
147 while (is.get (c) && isalpha (c)) |
|
148 buf << c; |
|
149 |
|
150 buf << OSSTREAM_ENDS; |
|
151 std::string tmp = OSSTREAM_STR (buf); |
|
152 OSSTREAM_FREEZE (buf); |
|
153 |
|
154 for (int i = 0; i < keywords.length (); i++) |
|
155 { |
|
156 int match = (tmp == keywords[i]); |
|
157 |
|
158 if (match) |
|
159 { |
|
160 kw = keywords[i]; |
|
161 |
|
162 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
|
163 ; // Skip whitespace and the colon. |
|
164 |
|
165 is.putback (c); |
|
166 if (c != '\n') |
|
167 is >> value; |
|
168 if (is) |
|
169 status = true; |
|
170 while (is.get (c) && c != '\n') |
|
171 ; // Skip to beginning of next line; |
|
172 return status; |
|
173 } |
|
174 } |
|
175 |
|
176 if (next_only) |
|
177 break; |
|
178 } |
|
179 } |
|
180 return status; |
|
181 } |
|
182 |
4634
|
183 #endif |
|
184 |
|
185 /* |
|
186 ;;; Local Variables: *** |
|
187 ;;; mode: C++ *** |
|
188 ;;; End: *** |
|
189 */ |
|
190 |