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
jwe@4634
     1
/*
jwe@4634
     2
jwe@7017
     3
Copyright (C) 2003, 2004, 2005, 2006, 2007 John W. Eaton
jwe@4634
     4
jwe@4634
     5
This file is part of Octave.
jwe@4634
     6
jwe@4634
     7
Octave is free software; you can redistribute it and/or modify it
jwe@4634
     8
under the terms of the GNU General Public License as published by the
jwe@7016
     9
Free Software Foundation; either version 3 of the License, or (at your
jwe@7016
    10
option) any later version.
jwe@4634
    11
jwe@4634
    12
Octave is distributed in the hope that it will be useful, but WITHOUT
jwe@4634
    13
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jwe@4634
    14
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jwe@4634
    15
for more details.
jwe@4634
    16
jwe@4634
    17
You should have received a copy of the GNU General Public License
jwe@7016
    18
along with Octave; see the file COPYING.  If not, see
jwe@7016
    19
<http://www.gnu.org/licenses/>.
jwe@4634
    20
jwe@4634
    21
*/
jwe@4634
    22
jwe@4634
    23
#if !defined (octave_ls_oct_ascii_h)
jwe@4634
    24
#define octave_ls_oct_ascii_h 1
jwe@4634
    25
jwe@4687
    26
#include <cfloat>
jwe@4687
    27
jwe@5765
    28
#include <sstream>
jwe@5099
    29
#include <string>
jwe@5099
    30
jwe@5099
    31
#include "str-vec.h"
lindnerb@7685
    32
#include "ls-ascii-helper.h"
jwe@5099
    33
jwe@4687
    34
// Flag for cell elements
jwe@4687
    35
#define CELL_ELT_TAG "<cell-element>"
jwe@4687
    36
jwe@4687
    37
// Used when converting Inf to something that gnuplot can read.
jwe@4687
    38
jwe@4687
    39
#ifndef OCT_RBV
jwe@4687
    40
#define OCT_RBV DBL_MAX / 100.0
jwe@4687
    41
#endif
jwe@4687
    42
jwe@6109
    43
extern OCTINTERP_API std::string
jwe@4687
    44
extract_keyword (std::istream& is, const char *keyword, 
jwe@4687
    45
		 const bool next_only = false);
jwe@4687
    46
jwe@6109
    47
extern OCTINTERP_API std::string
jwe@4634
    48
read_ascii_data (std::istream& is, const std::string& filename, bool& global,
jwe@5754
    49
		 octave_value& tc, octave_idx_type count);
jwe@4634
    50
jwe@6109
    51
extern OCTINTERP_API bool
jwe@4634
    52
save_ascii_data (std::ostream& os, const octave_value& val_arg,
jwe@6974
    53
		 const std::string& name, bool mark_as_global, int precision);
jwe@4634
    54
jwe@6109
    55
extern OCTINTERP_API bool
jwe@4634
    56
save_ascii_data_for_plotting (std::ostream& os, const octave_value& t,
jwe@4634
    57
			      const std::string& name);
jwe@4634
    58
jwe@6109
    59
extern OCTINTERP_API bool
jwe@5958
    60
save_three_d (std::ostream& os, const octave_value& t,
jwe@5958
    61
	      bool parametric = false);
jwe@5958
    62
jwe@5275
    63
// Match KEYWORD on stream IS, placing the associated value in VALUE,
jwe@5275
    64
// returning TRUE if successful and FALSE otherwise.
jwe@5275
    65
//
jwe@5275
    66
// Input should look something like:
jwe@5275
    67
//
jwe@5275
    68
//  [%#][ \t]*keyword[ \t]*int-value.*\n
jwe@5275
    69
jwe@5275
    70
template <class T>
jwe@5275
    71
bool
jwe@5275
    72
extract_keyword (std::istream& is, const char *keyword, T& value, 
jwe@5275
    73
		 const bool next_only = false)
jwe@5275
    74
{
jwe@5275
    75
  bool status = false;
jwe@5275
    76
  value = 0;
jwe@5275
    77
jwe@5275
    78
  char c;
jwe@5275
    79
  while (is.get (c))
jwe@5275
    80
    {
jwe@5275
    81
      if (c == '%' || c == '#')
jwe@5275
    82
	{
jwe@5765
    83
	  std::ostringstream buf;
jwe@5275
    84
jwe@5275
    85
	  while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
jwe@5275
    86
	    ; // Skip whitespace and comment characters.
jwe@5275
    87
jwe@5275
    88
	  if (isalpha (c))
jwe@5275
    89
	    buf << c;
jwe@5275
    90
jwe@5275
    91
	  while (is.get (c) && isalpha (c))
jwe@5275
    92
	    buf << c;
jwe@5275
    93
jwe@5765
    94
	  std::string tmp = buf.str ();
jwe@5679
    95
	  bool match = (tmp.compare (0, strlen (keyword), keyword) == 0);
jwe@5275
    96
jwe@5275
    97
	  if (match)
jwe@5275
    98
	    {
jwe@5275
    99
	      while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
jwe@5275
   100
		; // Skip whitespace and the colon.
jwe@5275
   101
jwe@5275
   102
	      is.putback (c);
jwe@7566
   103
	      if (c != '\n' && c != '\r')
jwe@5275
   104
		is >> value;
jwe@5275
   105
	      if (is)
jwe@5275
   106
		status = true;
lindnerb@7685
   107
	      // Skip to beginning of next line;
lindnerb@7685
   108
	      skip_until_newline (is, false);
jwe@5275
   109
	      break;
jwe@5275
   110
	    }
jwe@5275
   111
	  else if (next_only)
jwe@5275
   112
	    break;
jwe@5275
   113
	}
jwe@5275
   114
    }
jwe@5275
   115
  return status;
jwe@5275
   116
}
jwe@5275
   117
jwe@5275
   118
// Match one of the elements in KEYWORDS on stream IS, placing the
jwe@5275
   119
// matched keyword in KW and the associated value in VALUE,
jwe@5275
   120
// returning TRUE if successful and FALSE otherwise.
jwe@5275
   121
//
jwe@5275
   122
// Input should look something like:
jwe@5275
   123
//
jwe@5275
   124
//  [%#][ \t]*keyword[ \t]*int-value.*\n
jwe@5275
   125
jwe@5275
   126
template <class T>
jwe@5275
   127
bool
jwe@5275
   128
extract_keyword (std::istream& is, const string_vector& keywords,
jwe@5275
   129
		 std::string& kw, T& value, const bool next_only = false)
jwe@5275
   130
{
jwe@5275
   131
  bool status = false;
jwe@5275
   132
  kw = "";
jwe@5275
   133
  value = 0;
jwe@5275
   134
jwe@5275
   135
  char c;
jwe@5275
   136
  while (is.get (c))
jwe@5275
   137
    {
jwe@5275
   138
      if (c == '%' || c == '#')
jwe@5275
   139
	{
jwe@5765
   140
	  std::ostringstream buf;
jwe@5275
   141
jwe@5275
   142
	  while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
jwe@5275
   143
	    ; // Skip whitespace and comment characters.
jwe@5275
   144
jwe@5275
   145
	  if (isalpha (c))
jwe@5275
   146
	    buf << c;
jwe@5275
   147
jwe@5275
   148
	  while (is.get (c) && isalpha (c))
jwe@5275
   149
	    buf << c;
jwe@5275
   150
jwe@5765
   151
	  std::string tmp = buf.str ();
jwe@5275
   152
jwe@5275
   153
	  for (int i = 0; i < keywords.length (); i++)
jwe@5275
   154
	    {
jwe@5275
   155
	      int match = (tmp == keywords[i]);
jwe@5275
   156
jwe@5275
   157
	      if (match)
jwe@5275
   158
		{
jwe@5275
   159
		  kw = keywords[i];
jwe@5275
   160
jwe@5275
   161
		  while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
jwe@5275
   162
		    ; // Skip whitespace and the colon.
jwe@5275
   163
jwe@5275
   164
		  is.putback (c);
jwe@7566
   165
		  if (c != '\n' && c != '\r')
jwe@5275
   166
		    is >> value;
jwe@5275
   167
		  if (is)
jwe@5275
   168
		    status = true;
lindnerb@7685
   169
		  // Skip to beginning of next line;
lindnerb@7685
   170
		  skip_until_newline (is, false);
jwe@5275
   171
		  return status;
jwe@5275
   172
		}
jwe@5275
   173
	    }
jwe@5275
   174
jwe@5275
   175
	  if (next_only)
jwe@5275
   176
	    break;
jwe@5275
   177
	}
jwe@5275
   178
    }
jwe@5275
   179
  return status;
jwe@5275
   180
}
jwe@5275
   181
jwe@4634
   182
#endif
jwe@4634
   183
jwe@4634
   184
/*
jwe@4634
   185
;;; Local Variables: ***
jwe@4634
   186
;;; mode: C++ ***
jwe@4634
   187
;;; End: ***
jwe@4634
   188
*/
jwe@4634
   189