ls-oct-ascii.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2003-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_ls_oct_ascii_h)
00024 #define octave_ls_oct_ascii_h 1
00025 
00026 #include <cfloat>
00027 
00028 #include <sstream>
00029 #include <string>
00030 
00031 #include "str-vec.h"
00032 
00033 #include "ls-ascii-helper.h"
00034 
00035 // Flag for cell elements
00036 #define CELL_ELT_TAG "<cell-element>"
00037 
00038 // Used when converting Inf to something that gnuplot can read.
00039 
00040 #ifndef OCT_RBV
00041 #define OCT_RBV DBL_MAX / 100.0
00042 #endif
00043 
00044 extern OCTINTERP_API std::string
00045 extract_keyword (std::istream& is, const char *keyword,
00046                  const bool next_only = false);
00047 
00048 extern OCTINTERP_API std::string
00049 read_ascii_data (std::istream& is, const std::string& filename, bool& global,
00050                  octave_value& tc, octave_idx_type count);
00051 
00052 extern OCTINTERP_API bool
00053 save_ascii_data (std::ostream& os, const octave_value& val_arg,
00054                  const std::string& name, bool mark_as_global, int precision);
00055 
00056 extern OCTINTERP_API bool
00057 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t,
00058                               const std::string& name);
00059 
00060 extern OCTINTERP_API bool
00061 save_three_d (std::ostream& os, const octave_value& t,
00062               bool parametric = false);
00063 
00064 // Match KEYWORD on stream IS, placing the associated value in VALUE,
00065 // returning TRUE if successful and FALSE otherwise.
00066 //
00067 // Input should look something like:
00068 //
00069 //  [%#][ \t]*keyword[ \t]*int-value.*\n
00070 
00071 template <class T>
00072 bool
00073 extract_keyword (std::istream& is, const char *keyword, T& value,
00074                  const bool next_only = false)
00075 {
00076   bool status = false;
00077   value = T();
00078 
00079   char c;
00080   while (is.get (c))
00081     {
00082       if (c == '%' || c == '#')
00083         {
00084           std::ostringstream buf;
00085 
00086           while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
00087             ; // Skip whitespace and comment characters.
00088 
00089           if (isalpha (c))
00090             buf << c;
00091 
00092           while (is.get (c) && isalpha (c))
00093             buf << c;
00094 
00095           std::string tmp = buf.str ();
00096           bool match = (tmp.compare (0, strlen (keyword), keyword) == 0);
00097 
00098           if (match)
00099             {
00100               while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
00101                 ; // Skip whitespace and the colon.
00102 
00103               is.putback (c);
00104               if (c != '\n' && c != '\r')
00105                 is >> value;
00106               if (is)
00107                 status = true;
00108               skip_until_newline (is, false);
00109               break;
00110             }
00111           else if (next_only)
00112             break;
00113         }
00114     }
00115   return status;
00116 }
00117 
00118 template <class T>
00119 bool
00120 extract_keyword (std::istream& is, const std::string& kw, T& value,
00121                  const bool next_only = false)
00122 {
00123   return extract_keyword (is, kw.c_str (), value, next_only);
00124 }
00125 
00126 // Match one of the elements in KEYWORDS on stream IS, placing the
00127 // matched keyword in KW and the associated value in VALUE,
00128 // returning TRUE if successful and FALSE otherwise.
00129 //
00130 // Input should look something like:
00131 //
00132 //  [%#][ \t]*keyword[ \t]*int-value.*\n
00133 
00134 template <class T>
00135 bool
00136 extract_keyword (std::istream& is, const string_vector& keywords,
00137                  std::string& kw, T& value, const bool next_only = false)
00138 {
00139   bool status = false;
00140   kw = "";
00141   value = 0;
00142 
00143   char c;
00144   while (is.get (c))
00145     {
00146       if (c == '%' || c == '#')
00147         {
00148           std::ostringstream buf;
00149 
00150           while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
00151             ; // Skip whitespace and comment characters.
00152 
00153           if (isalpha (c))
00154             buf << c;
00155 
00156           while (is.get (c) && isalpha (c))
00157             buf << c;
00158 
00159           std::string tmp = buf.str ();
00160 
00161           for (int i = 0; i < keywords.length (); i++)
00162             {
00163               int match = (tmp == keywords[i]);
00164 
00165               if (match)
00166                 {
00167                   kw = keywords[i];
00168 
00169                   while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
00170                     ; // Skip whitespace and the colon.
00171 
00172                   is.putback (c);
00173                   if (c != '\n' && c != '\r')
00174                     is >> value;
00175                   if (is)
00176                     status = true;
00177                   skip_until_newline (is, false);
00178                   return status;
00179                 }
00180             }
00181 
00182           if (next_only)
00183             break;
00184         }
00185     }
00186   return status;
00187 }
00188 
00189 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines