GNU Octave  4.2.1
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-text.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2017 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_text_h)
24 #define octave_ls_oct_text_h 1
25 
26 #include "octave-config.h"
27 
28 #include <cfloat>
29 
30 #include <sstream>
31 #include <string>
32 
33 #include "str-vec.h"
34 
35 #include "ls-ascii-helper.h"
36 
37 // Flag for cell elements
38 #define CELL_ELT_TAG "<cell-element>"
39 
40 // Used when converting Inf to something that gnuplot can read.
41 
42 #if ! defined (OCT_RBV)
43 # define OCT_RBV (std::numeric_limits<double>::max () / 100.0)
44 #endif
45 
47 extract_keyword (std::istream& is, const char *keyword,
48  const bool next_only = false);
49 
51 read_text_data (std::istream& is, const std::string& filename, bool& global,
52  octave_value& tc, octave_idx_type count);
53 
54 extern OCTINTERP_API bool
55 save_text_data (std::ostream& os, const octave_value& val_arg,
56  const std::string& name, bool mark_as_global, int precision);
57 
58 extern OCTINTERP_API bool
59 save_text_data_for_plotting (std::ostream& os, const octave_value& t,
60  const std::string& name);
61 
62 extern OCTINTERP_API bool
63 save_three_d (std::ostream& os, const octave_value& t,
64  bool parametric = false);
65 
66 // Match KEYWORD on stream IS, placing the associated value in VALUE,
67 // returning TRUE if successful and FALSE otherwise.
68 //
69 // Input should look something like:
70 //
71 // [%#][ \t]*keyword[ \t]*int-value.*\n
72 
73 template <typename T>
74 bool
75 extract_keyword (std::istream& is, const char *keyword, T& value,
76  const bool next_only = false)
77 {
78  bool status = false;
79  value = T ();
80 
81  char c;
82  while (is.get (c))
83  {
84  if (c == '%' || c == '#')
85  {
86  std::ostringstream buf;
87 
88  while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
89  ; // Skip whitespace and comment characters.
90 
91  if (isalpha (c))
92  buf << c;
93 
94  while (is.get (c) && isalpha (c))
95  buf << c;
96 
97  std::string tmp = buf.str ();
98  bool match = (tmp.substr (0, strlen (keyword)) == keyword);
99 
100  if (match)
101  {
102  while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
103  ; // Skip whitespace and the colon.
104 
105  is.putback (c);
106  if (c != '\n' && c != '\r')
107  is >> value;
108  if (is)
109  status = true;
110  skip_until_newline (is, false);
111  break;
112  }
113  else if (next_only)
114  break;
115  }
116  }
117  return status;
118 }
119 
120 template <typename T>
121 bool
122 extract_keyword (std::istream& is, const std::string& kw, T& value,
123  const bool next_only = false)
124 {
125  return extract_keyword (is, kw.c_str (), value, next_only);
126 }
127 
128 // Match one of the elements in KEYWORDS on stream IS, placing the
129 // matched keyword in KW and the associated value in VALUE,
130 // returning TRUE if successful and FALSE otherwise.
131 //
132 // Input should look something like:
133 //
134 // [%#][ \t]*keyword[ \t]*int-value.*\n
135 
136 template <typename T>
137 bool
138 extract_keyword (std::istream& is, const string_vector& keywords,
139  std::string& kw, T& value, const bool next_only = false)
140 {
141  bool status = false;
142  kw = "";
143  value = 0;
144 
145  char c;
146  while (is.get (c))
147  {
148  if (c == '%' || c == '#')
149  {
150  std::ostringstream buf;
151 
152  while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
153  ; // Skip whitespace and comment characters.
154 
155  if (isalpha (c))
156  buf << c;
157 
158  while (is.get (c) && isalpha (c))
159  buf << c;
160 
161  std::string tmp = buf.str ();
162 
163  for (int i = 0; i < keywords.numel (); i++)
164  {
165  int match = (tmp == keywords[i]);
166 
167  if (match)
168  {
169  kw = keywords[i];
170 
171  while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
172  ; // Skip whitespace and the colon.
173 
174  is.putback (c);
175  if (c != '\n' && c != '\r')
176  is >> value;
177  if (is)
178  status = true;
179  skip_until_newline (is, false);
180  return status;
181  }
182  }
183 
184  if (next_only)
185  break;
186  }
187  }
188  return status;
189 }
190 
191 #endif
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
OCTINTERP_API bool save_three_d(std::ostream &os, const octave_value &t, bool parametric=false)
Definition: ls-oct-text.cc:349
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:336
OCTINTERP_API std::string extract_keyword(std::istream &is, const char *keyword, const bool next_only=false)
Definition: ls-oct-text.cc:80
std::string filename
Definition: urlwrite.cc:340
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:935
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
#define OCTINTERP_API
Definition: mexproto.h:69
double tmp
Definition: data.cc:6300
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
T::size_type strlen(const typename T::value_type *str)
Definition: oct-string.cc:75
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the IEEE symbol zero divided by nd tex zero divided by nd ifnottex and any operation involving another NaN value(5+NaN).Note that NaN always compares not equal to NaN(NaN!
OCTINTERP_API bool save_text_data(std::ostream &os, const octave_value &val_arg, const std::string &name, bool mark_as_global, int precision)
Definition: ls-oct-text.cc:300
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:236
static const char *const keywords[]
Definition: help.cc:147
write the output to stdout if nargout is
Definition: load-save.cc:1576
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:854