Mercurial > hg > octave-nkf
annotate src/ls-oct-ascii.h @ 10825:cace99cb01ab
rewrite logm (M. Caliari, R.T. Guy)
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 28 Jul 2010 09:22:41 +0200 |
parents | f3b65e1ae355 |
children | fd0a3ac60b0e |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 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 | |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
33 #include "ls-ascii-helper.h" |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
34 |
4687 | 35 // Flag for cell elements |
36 #define CELL_ELT_TAG "<cell-element>" | |
37 | |
38 // Used when converting Inf to something that gnuplot can read. | |
39 | |
40 #ifndef OCT_RBV | |
41 #define OCT_RBV DBL_MAX / 100.0 | |
42 #endif | |
43 | |
6109 | 44 extern OCTINTERP_API std::string |
4687 | 45 extract_keyword (std::istream& is, const char *keyword, |
10313 | 46 const bool next_only = false); |
4687 | 47 |
6109 | 48 extern OCTINTERP_API std::string |
4634 | 49 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
10313 | 50 octave_value& tc, octave_idx_type count); |
4634 | 51 |
6109 | 52 extern OCTINTERP_API bool |
4634 | 53 save_ascii_data (std::ostream& os, const octave_value& val_arg, |
10313 | 54 const std::string& name, bool mark_as_global, int precision); |
4634 | 55 |
6109 | 56 extern OCTINTERP_API bool |
4634 | 57 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t, |
10313 | 58 const std::string& name); |
4634 | 59 |
6109 | 60 extern OCTINTERP_API bool |
5958 | 61 save_three_d (std::ostream& os, const octave_value& t, |
10313 | 62 bool parametric = false); |
5958 | 63 |
5275 | 64 // Match KEYWORD on stream IS, placing the associated value in VALUE, |
65 // returning TRUE if successful and FALSE otherwise. | |
66 // | |
67 // Input should look something like: | |
68 // | |
69 // [%#][ \t]*keyword[ \t]*int-value.*\n | |
70 | |
71 template <class T> | |
72 bool | |
73 extract_keyword (std::istream& is, const char *keyword, T& value, | |
10313 | 74 const bool next_only = false) |
5275 | 75 { |
76 bool status = false; | |
8212
ebf6f6a0f9a7
Allow saving/loading of classes. Add saveobj and loadobj methods
David Bateman <dbateman@free.fr>
parents:
8087
diff
changeset
|
77 value = T(); |
5275 | 78 |
79 char c; | |
80 while (is.get (c)) | |
81 { | |
82 if (c == '%' || c == '#') | |
10313 | 83 { |
84 std::ostringstream buf; | |
5275 | 85 |
10313 | 86 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
87 ; // Skip whitespace and comment characters. | |
5275 | 88 |
10313 | 89 if (isalpha (c)) |
90 buf << c; | |
5275 | 91 |
10313 | 92 while (is.get (c) && isalpha (c)) |
93 buf << c; | |
5275 | 94 |
10313 | 95 std::string tmp = buf.str (); |
96 bool match = (tmp.compare (0, strlen (keyword), keyword) == 0); | |
5275 | 97 |
10313 | 98 if (match) |
99 { | |
100 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) | |
101 ; // Skip whitespace and the colon. | |
5275 | 102 |
10313 | 103 is.putback (c); |
104 if (c != '\n' && c != '\r') | |
105 is >> value; | |
106 if (is) | |
107 status = true; | |
108 skip_until_newline (is, false); | |
109 break; | |
110 } | |
111 else if (next_only) | |
112 break; | |
113 } | |
5275 | 114 } |
115 return status; | |
116 } | |
117 | |
9852
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
118 template <class T> |
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
119 bool |
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
120 extract_keyword (std::istream& is, const std::string& kw, T& value, |
10313 | 121 const bool next_only = false) |
9852
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
122 { |
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
123 return extract_keyword (is, kw.c_str (), value, next_only); |
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
124 } |
aabf7a8c2e57
implement sparse logical conversion
Jaroslav Hajek <highegg@gmail.com>
parents:
8946
diff
changeset
|
125 |
5275 | 126 // Match one of the elements in KEYWORDS on stream IS, placing the |
127 // matched keyword in KW and the associated value in VALUE, | |
128 // returning TRUE if successful and FALSE otherwise. | |
129 // | |
130 // Input should look something like: | |
131 // | |
132 // [%#][ \t]*keyword[ \t]*int-value.*\n | |
133 | |
134 template <class T> | |
135 bool | |
136 extract_keyword (std::istream& is, const string_vector& keywords, | |
10313 | 137 std::string& kw, T& value, const bool next_only = false) |
5275 | 138 { |
139 bool status = false; | |
140 kw = ""; | |
141 value = 0; | |
142 | |
143 char c; | |
144 while (is.get (c)) | |
145 { | |
146 if (c == '%' || c == '#') | |
10313 | 147 { |
148 std::ostringstream buf; | |
5275 | 149 |
10313 | 150 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) |
151 ; // Skip whitespace and comment characters. | |
5275 | 152 |
10313 | 153 if (isalpha (c)) |
154 buf << c; | |
5275 | 155 |
10313 | 156 while (is.get (c) && isalpha (c)) |
157 buf << c; | |
5275 | 158 |
10313 | 159 std::string tmp = buf.str (); |
5275 | 160 |
10313 | 161 for (int i = 0; i < keywords.length (); i++) |
162 { | |
163 int match = (tmp == keywords[i]); | |
5275 | 164 |
10313 | 165 if (match) |
166 { | |
167 kw = keywords[i]; | |
5275 | 168 |
10313 | 169 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) |
170 ; // Skip whitespace and the colon. | |
5275 | 171 |
10313 | 172 is.putback (c); |
173 if (c != '\n' && c != '\r') | |
174 is >> value; | |
175 if (is) | |
176 status = true; | |
177 skip_until_newline (is, false); | |
178 return status; | |
179 } | |
180 } | |
5275 | 181 |
10313 | 182 if (next_only) |
183 break; | |
184 } | |
5275 | 185 } |
186 return status; | |
187 } | |
188 | |
4634 | 189 #endif |