ls-oct-ascii.h
Upload User: qianxunqiu
Upload Date: 2015-11-11
Package Size: 11485k
Code Size: 4k
Category:

Other systems

Development Platform:

Matlab

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