src/ls-oct-ascii.h
author Benjamin Lindner <lindnerb@users.sourceforge.net>
Wed Mar 18 15:23:14 2009 +0100 (2009-03-18)
changeset 7685 34b75a47e712
parent 7566 fd669d081438
permissions -rw-r--r--
fix leaving stray '\r' in stream when reading from CRLF data file
* * *
fix CRLF issues with text-mode reading in windows when loading ascii data
     1 /*
     2 
     3 Copyright (C) 2003, 2004, 2005, 2006, 2007 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 #include "ls-ascii-helper.h"
    33 
    34 // Flag for cell elements
    35 #define CELL_ELT_TAG "<cell-element>"
    36 
    37 // Used when converting Inf to something that gnuplot can read.
    38 
    39 #ifndef OCT_RBV
    40 #define OCT_RBV DBL_MAX / 100.0
    41 #endif
    42 
    43 extern OCTINTERP_API std::string
    44 extract_keyword (std::istream& is, const char *keyword, 
    45 		 const bool next_only = false);
    46 
    47 extern OCTINTERP_API std::string
    48 read_ascii_data (std::istream& is, const std::string& filename, bool& global,
    49 		 octave_value& tc, octave_idx_type count);
    50 
    51 extern OCTINTERP_API bool
    52 save_ascii_data (std::ostream& os, const octave_value& val_arg,
    53 		 const std::string& name, bool mark_as_global, int precision);
    54 
    55 extern OCTINTERP_API bool
    56 save_ascii_data_for_plotting (std::ostream& os, const octave_value& t,
    57 			      const std::string& name);
    58 
    59 extern OCTINTERP_API bool
    60 save_three_d (std::ostream& os, const octave_value& t,
    61 	      bool parametric = false);
    62 
    63 // Match KEYWORD on stream IS, placing the associated value in VALUE,
    64 // returning TRUE if successful and FALSE otherwise.
    65 //
    66 // Input should look something like:
    67 //
    68 //  [%#][ \t]*keyword[ \t]*int-value.*\n
    69 
    70 template <class T>
    71 bool
    72 extract_keyword (std::istream& is, const char *keyword, T& value, 
    73 		 const bool next_only = false)
    74 {
    75   bool status = false;
    76   value = 0;
    77 
    78   char c;
    79   while (is.get (c))
    80     {
    81       if (c == '%' || c == '#')
    82 	{
    83 	  std::ostringstream buf;
    84 
    85 	  while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
    86 	    ; // Skip whitespace and comment characters.
    87 
    88 	  if (isalpha (c))
    89 	    buf << c;
    90 
    91 	  while (is.get (c) && isalpha (c))
    92 	    buf << c;
    93 
    94 	  std::string tmp = buf.str ();
    95 	  bool match = (tmp.compare (0, strlen (keyword), keyword) == 0);
    96 
    97 	  if (match)
    98 	    {
    99 	      while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
   100 		; // Skip whitespace and the colon.
   101 
   102 	      is.putback (c);
   103 	      if (c != '\n' && c != '\r')
   104 		is >> value;
   105 	      if (is)
   106 		status = true;
   107 	      // Skip to beginning of next line;
   108 	      skip_until_newline (is, false);
   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 	{
   140 	  std::ostringstream buf;
   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 
   151 	  std::string tmp = buf.str ();
   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);
   165 		  if (c != '\n' && c != '\r')
   166 		    is >> value;
   167 		  if (is)
   168 		    status = true;
   169 		  // Skip to beginning of next line;
   170 		  skip_until_newline (is, false);
   171 		  return status;
   172 		}
   173 	    }
   174 
   175 	  if (next_only)
   176 	    break;
   177 	}
   178     }
   179   return status;
   180 }
   181 
   182 #endif
   183 
   184 /*
   185 ;;; Local Variables: ***
   186 ;;; mode: C++ ***
   187 ;;; End: ***
   188 */
   189