Mercurial > hg > octave-nkf
annotate src/ls-oct-ascii.h @ 8087:7d19f4f70c61
ls-oct-ascii.{h,cc} (extract_keyword): accept \r as line ending character
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 08 Sep 2008 12:45:53 -0400 |
parents | a1dbe9d80eee |
children | ebf6f6a0f9a7 278afaecddd4 |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2003, 2004, 2005, 2006, 2007 John W. Eaton |
4634 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
4634 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
4634 | 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 | |
5765 | 28 #include <sstream> |
5099 | 29 #include <string> |
30 | |
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 | |
6109 | 42 extern OCTINTERP_API std::string |
4687 | 43 extract_keyword (std::istream& is, const char *keyword, |
44 const bool next_only = false); | |
45 | |
6109 | 46 extern OCTINTERP_API std::string |
4634 | 47 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
5754 | 48 octave_value& tc, octave_idx_type count); |
4634 | 49 |
6109 | 50 extern OCTINTERP_API bool |
4634 | 51 save_ascii_data (std::ostream& os, const octave_value& val_arg, |
6974 | 52 const std::string& name, bool mark_as_global, int precision); |
4634 | 53 |
6109 | 54 extern OCTINTERP_API bool |
4634 | 55 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
56 const std::string& name); | |
57 | |
6109 | 58 extern OCTINTERP_API bool |
5958 | 59 save_three_d (std::ostream& os, const octave_value& t, |
60 bool parametric = false); | |
61 | |
5275 | 62 // Match KEYWORD on stream IS, placing the associated value in VALUE, |
63 // returning TRUE if successful and FALSE otherwise. | |
64 // | |
65 // Input should look something like: | |
66 // | |
67 // [%#][ \t]*keyword[ \t]*int-value.*\n | |
68 | |
69 template <class T> | |
70 bool | |
71 extract_keyword (std::istream& is, const char *keyword, T& value, | |
72 const bool next_only = false) | |
73 { | |
74 bool status = false; | |
75 value = 0; | |
76 | |
77 char c; | |
78 while (is.get (c)) | |
79 { | |
80 if (c == '%' || c == '#') | |
81 { | |
5765 | 82 std::ostringstream buf; |
5275 | 83 |
84 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) | |
85 ; // Skip whitespace and comment characters. | |
86 | |
87 if (isalpha (c)) | |
88 buf << c; | |
89 | |
90 while (is.get (c) && isalpha (c)) | |
91 buf << c; | |
92 | |
5765 | 93 std::string tmp = buf.str (); |
5679 | 94 bool match = (tmp.compare (0, strlen (keyword), keyword) == 0); |
5275 | 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); | |
8087
7d19f4f70c61
ls-oct-ascii.{h,cc} (extract_keyword): accept \r as line ending character
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
102 if (c != '\n' && c != '\r') |
5275 | 103 is >> value; |
104 if (is) | |
105 status = true; | |
8087
7d19f4f70c61
ls-oct-ascii.{h,cc} (extract_keyword): accept \r as line ending character
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
106 while (is.get (c) && c != '\n' && c != '\r') |
5275 | 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 { | |
5765 | 139 std::ostringstream buf; |
5275 | 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 | |
5765 | 150 std::string tmp = buf.str (); |
5275 | 151 |
152 for (int i = 0; i < keywords.length (); i++) | |
153 { | |
154 int match = (tmp == keywords[i]); | |
155 | |
156 if (match) | |
157 { | |
158 kw = keywords[i]; | |
159 | |
160 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) | |
161 ; // Skip whitespace and the colon. | |
162 | |
163 is.putback (c); | |
8087
7d19f4f70c61
ls-oct-ascii.{h,cc} (extract_keyword): accept \r as line ending character
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
164 if (c != '\n' && c != '\r') |
5275 | 165 is >> value; |
166 if (is) | |
167 status = true; | |
8087
7d19f4f70c61
ls-oct-ascii.{h,cc} (extract_keyword): accept \r as line ending character
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
168 while (is.get (c) && c != '\n' && c != '\r') |
5275 | 169 ; // Skip to beginning of next line; |
170 return status; | |
171 } | |
172 } | |
173 | |
174 if (next_only) | |
175 break; | |
176 } | |
177 } | |
178 return status; | |
179 } | |
180 | |
4634 | 181 #endif |
182 | |
183 /* | |
184 ;;; Local Variables: *** | |
185 ;;; mode: C++ *** | |
186 ;;; End: *** | |
187 */ | |
188 |