GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ls-oct-text.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_ls_oct_text_h)
24 #define octave_ls_oct_text_h 1
25 
26 #include "octave-config.h"
27 
28 #include <limits>
29 #include <sstream>
30 #include <string>
31 
32 #include "str-vec.h"
33 
34 #include "ls-ascii-helper.h"
35 
36 class octave_value;
37 
38 // Flag for cell elements
39 #define CELL_ELT_TAG "<cell-element>"
40 
41 // Used when converting Inf to something that gnuplot can read.
42 
43 #if ! defined (OCT_RBV)
44 # define OCT_RBV (std::numeric_limits<double>::max () / 100.0)
45 #endif
46 
47 extern OCTINTERP_API std::string
48 extract_keyword (std::istream& is, const char *keyword,
49  const bool next_only = false);
50 
51 extern OCTINTERP_API std::string
52 read_text_data (std::istream& is, const std::string& filename, bool& global,
53  octave_value& tc, octave_idx_type count);
54 
55 extern OCTINTERP_API bool
56 save_text_data (std::ostream& os, const octave_value& val_arg,
57  const std::string& name, bool mark_global, int precision);
58 
59 extern OCTINTERP_API bool
60 save_text_data_for_plotting (std::ostream& os, const octave_value& t,
61  const std::string& name);
62 
63 extern OCTINTERP_API bool
64 save_three_d (std::ostream& os, const octave_value& t,
65  bool parametric = false);
66 
67 // Match KEYWORD on stream IS, placing the associated value in VALUE,
68 // returning TRUE if successful and FALSE otherwise.
69 //
70 // Input should look something like:
71 //
72 // [%#][ \t]*keyword[ \t]*int-value.*\n
73 
74 template <typename T>
75 bool
76 extract_keyword (std::istream& is, const char *keyword, T& value,
77  const bool next_only = false)
78 {
79  bool status = false;
80  value = T ();
81 
82  char c;
83  while (is.get (c))
84  {
85  if (c == '%' || c == '#')
86  {
87  std::ostringstream buf;
88 
89  while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
90  ; // Skip whitespace and comment characters.
91 
92  if (isalpha (c))
93  buf << c;
94 
95  while (is.get (c) && isalpha (c))
96  buf << c;
97 
98  std::string tmp = buf.str ();
99  bool match = (tmp.substr (0, strlen (keyword)) == keyword);
100 
101  if (match)
102  {
103  while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
104  ; // Skip whitespace and the colon.
105 
106  is.putback (c);
107  if (c != '\n' && c != '\r')
108  is >> value;
109  if (is)
110  status = true;
111  skip_until_newline (is, false);
112  break;
113  }
114  else if (next_only)
115  break;
116  }
117  }
118  return status;
119 }
120 
121 template <typename T>
122 bool
123 extract_keyword (std::istream& is, const std::string& kw, T& value,
124  const bool next_only = false)
125 {
126  return extract_keyword (is, kw.c_str (), value, next_only);
127 }
128 
129 // Match one of the elements in KEYWORDS on stream IS, placing the
130 // matched keyword in KW and the associated value in VALUE,
131 // returning TRUE if successful and FALSE otherwise.
132 //
133 // Input should look something like:
134 //
135 // [%#][ \t]*keyword[ \t]*int-value.*\n
136 
137 template <typename T>
138 bool
139 extract_keyword (std::istream& is, const string_vector& keywords,
140  std::string& kw, T& value, const bool next_only = false)
141 {
142  bool status = false;
143  kw = "";
144  value = 0;
145 
146  char c;
147  while (is.get (c))
148  {
149  if (c == '%' || c == '#')
150  {
151  std::ostringstream buf;
152 
153  while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
154  ; // Skip whitespace and comment characters.
155 
156  if (isalpha (c))
157  buf << c;
158 
159  while (is.get (c) && isalpha (c))
160  buf << c;
161 
162  std::string tmp = buf.str ();
163 
164  for (int i = 0; i < keywords.numel (); i++)
165  {
166  int match = (tmp == keywords[i]);
167 
168  if (match)
169  {
170  kw = keywords[i];
171 
172  while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
173  ; // Skip whitespace and the colon.
174 
175  is.putback (c);
176  if (c != '\n' && c != '\r')
177  is >> value;
178  if (is)
179  status = true;
180  skip_until_newline (is, false);
181  return status;
182  }
183  }
184 
185  if (next_only)
186  break;
187  }
188  }
189  return status;
190 }
191 
192 #endif
OCTINTERP_API bool save_three_d(std::ostream &os, const octave_value &t, bool parametric=false)
Definition: ls-oct-text.cc:356
void skip_until_newline(std::istream &is, bool keep_newline)
OCTINTERP_API bool save_text_data_for_plotting(std::ostream &os, const octave_value &t, const std::string &name)
Definition: ls-oct-text.cc:343
OCTINTERP_API std::string extract_keyword(std::istream &is, const char *keyword, const bool next_only=false)
Definition: ls-oct-text.cc:82
std::string filename
Definition: urlwrite.cc:121
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:997
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
nd deftypefn *std::string name
Definition: sysdep.cc:647
double tmp
Definition: data.cc:6252
T::size_type strlen(const typename T::value_type *str)
Definition: oct-string.cc:75
OCTINTERP_API bool save_text_data(std::ostream &os, const octave_value &val_arg, const std::string &name, bool mark_global, int precision)
Definition: ls-oct-text.cc:307
for i
Definition: data.cc:5264
OCTINTERP_API std::string read_text_data(std::istream &is, const std::string &filename, bool &global, octave_value &tc, octave_idx_type count)
Definition: ls-oct-text.cc:238
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
write the output to stdout if nargout is
Definition: load-save.cc:1612
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
nd group nd example For each display the value
Definition: sysdep.cc:866
octave::stream os
Definition: file-io.cc:627