Mercurial > hg > octave-nkf
annotate src/ls-oct-ascii.h @ 8946:e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
author | Benjamin Lindner <lindnerb@users.sourceforge.net> |
---|---|
date | Tue, 10 Mar 2009 01:01:50 -0400 |
parents | eb63fbe60fab |
children | aabf7a8c2e57 |
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, |
46 const bool next_only = false); | |
47 | |
6109 | 48 extern OCTINTERP_API std::string |
4634 | 49 read_ascii_data (std::istream& is, const std::string& filename, bool& global, |
5754 | 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, |
6974 | 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, |
58 const std::string& name); | |
59 | |
6109 | 60 extern OCTINTERP_API bool |
5958 | 61 save_three_d (std::ostream& os, const octave_value& t, |
62 bool parametric = false); | |
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, | |
74 const bool next_only = false) | |
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 == '#') | |
83 { | |
5765 | 84 std::ostringstream buf; |
5275 | 85 |
86 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#')) | |
87 ; // Skip whitespace and comment characters. | |
88 | |
89 if (isalpha (c)) | |
90 buf << c; | |
91 | |
92 while (is.get (c) && isalpha (c)) | |
93 buf << c; | |
94 | |
5765 | 95 std::string tmp = buf.str (); |
5679 | 96 bool match = (tmp.compare (0, strlen (keyword), keyword) == 0); |
5275 | 97 |
98 if (match) | |
99 { | |
100 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) | |
101 ; // Skip whitespace and the colon. | |
102 | |
103 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
|
104 if (c != '\n' && c != '\r') |
5275 | 105 is >> value; |
106 if (is) | |
107 status = true; | |
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
|
108 skip_until_newline (is, false); |
5275 | 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 { | |
5765 | 140 std::ostringstream buf; |
5275 | 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 | |
5765 | 151 std::string tmp = buf.str (); |
5275 | 152 |
153 for (int i = 0; i < keywords.length (); i++) | |
154 { | |
155 int match = (tmp == keywords[i]); | |
156 | |
157 if (match) | |
158 { | |
159 kw = keywords[i]; | |
160 | |
161 while (is.get (c) && (c == ' ' || c == '\t' || c == ':')) | |
162 ; // Skip whitespace and the colon. | |
163 | |
164 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
|
165 if (c != '\n' && c != '\r') |
5275 | 166 is >> value; |
167 if (is) | |
168 status = true; | |
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
|
169 skip_until_newline (is, false); |
5275 | 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 |