GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ls-oct-ascii.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2013 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_ls_oct_ascii_h)
24 #define octave_ls_oct_ascii_h 1
25 
26 #include <cfloat>
27 
28 #include <sstream>
29 #include <string>
30 
31 #include "str-vec.h"
32 
33 #include "ls-ascii-helper.h"
34 
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 (std::numeric_limits<double>::max () / 100.0)
42 #endif
43 
44 extern OCTINTERP_API std::string
45 extract_keyword (std::istream& is, const char *keyword,
46  const bool next_only = false);
47 
48 extern OCTINTERP_API std::string
49 read_ascii_data (std::istream& is, const std::string& filename, bool& global,
50  octave_value& tc, octave_idx_type count);
51 
52 extern OCTINTERP_API bool
53 save_ascii_data (std::ostream& os, const octave_value& val_arg,
54  const std::string& name, bool mark_as_global, int precision);
55 
56 extern OCTINTERP_API bool
57 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t,
58  const std::string& name);
59 
60 extern OCTINTERP_API bool
61 save_three_d (std::ostream& os, const octave_value& t,
62  bool parametric = false);
63 
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;
77  value = T ();
78 
79  char c;
80  while (is.get (c))
81  {
82  if (c == '%' || c == '#')
83  {
84  std::ostringstream buf;
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 
95  std::string tmp = buf.str ();
96  bool match = (tmp.compare (0, strlen (keyword), keyword) == 0);
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);
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  }
114  }
115  return status;
116 }
117 
118 template <class T>
119 bool
120 extract_keyword (std::istream& is, const std::string& kw, T& value,
121  const bool next_only = false)
122 {
123  return extract_keyword (is, kw.c_str (), value, next_only);
124 }
125 
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,
137  std::string& kw, T& value, const bool next_only = false)
138 {
139  bool status = false;
140  kw = "";
141  value = 0;
142 
143  char c;
144  while (is.get (c))
145  {
146  if (c == '%' || c == '#')
147  {
148  std::ostringstream buf;
149 
150  while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
151  ; // Skip whitespace and comment characters.
152 
153  if (isalpha (c))
154  buf << c;
155 
156  while (is.get (c) && isalpha (c))
157  buf << c;
158 
159  std::string tmp = buf.str ();
160 
161  for (int i = 0; i < keywords.length (); i++)
162  {
163  int match = (tmp == keywords[i]);
164 
165  if (match)
166  {
167  kw = keywords[i];
168 
169  while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
170  ; // Skip whitespace and the colon.
171 
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  }
181 
182  if (next_only)
183  break;
184  }
185  }
186  return status;
187 }
188 
189 #endif