ov-ch-mat.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 Copyright (C) 2009-2010 VZLU Prague
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include <cctype>
00029 #include <iostream>
00030 
00031 #include "lo-ieee.h"
00032 #include "mx-base.h"
00033 
00034 #include "ov-base.h"
00035 #include "ov-base-mat.h"
00036 #include "ov-base-mat.cc"
00037 #include "ov-ch-mat.h"
00038 #include "gripes.h"
00039 #include "pr-output.h"
00040 
00041 template class octave_base_matrix<charNDArray>;
00042 
00043 idx_vector
00044 octave_char_matrix::index_vector (void) const
00045 {
00046   const char *p = matrix.data ();
00047   if (numel () == 1 && *p == ':')
00048     return idx_vector (':');
00049   else
00050     return idx_vector (array_value (true));
00051 }
00052 
00053 double
00054 octave_char_matrix::double_value (bool) const
00055 {
00056   double retval = lo_ieee_nan_value ();
00057 
00058   if (rows () > 0 && columns () > 0)
00059     {
00060       gripe_implicit_conversion ("Octave:array-as-scalar",
00061                                  "character matrix", "real scalar");
00062 
00063       retval = static_cast<unsigned char> (matrix (0, 0));
00064     }
00065   else
00066     gripe_invalid_conversion ("character matrix", "real scalar");
00067 
00068   return retval;
00069 }
00070 
00071 float
00072 octave_char_matrix::float_value (bool) const
00073 {
00074   float retval = lo_ieee_float_nan_value ();
00075 
00076   if (rows () > 0 && columns () > 0)
00077     {
00078       gripe_implicit_conversion ("Octave:array-as-scalar",
00079                                  "character matrix", "real scalar");
00080 
00081       retval = static_cast<unsigned char> (matrix (0, 0));
00082     }
00083   else
00084     gripe_invalid_conversion ("character matrix", "real scalar");
00085 
00086   return retval;
00087 }
00088 
00089 Complex
00090 octave_char_matrix::complex_value (bool) const
00091 {
00092   double tmp = lo_ieee_nan_value ();
00093 
00094   Complex retval (tmp, tmp);
00095 
00096   if (rows () > 0 && columns () > 0)
00097     {
00098       gripe_implicit_conversion ("Octave:array-as-scalar",
00099                                  "character matrix", "complex scalar");
00100 
00101       retval = static_cast<unsigned char> (matrix (0, 0));
00102     }
00103   else
00104     gripe_invalid_conversion ("character matrix", "complex scalar");
00105 
00106   return retval;
00107 }
00108 
00109 FloatComplex
00110 octave_char_matrix::float_complex_value (bool) const
00111 {
00112   float tmp = lo_ieee_float_nan_value ();
00113 
00114   FloatComplex retval (tmp, tmp);
00115 
00116   if (rows () > 0 && columns () > 0)
00117     {
00118       gripe_implicit_conversion ("Octave:array-as-scalar",
00119                                  "character matrix", "complex scalar");
00120 
00121       retval = static_cast<unsigned char> (matrix (0, 0));
00122     }
00123   else
00124     gripe_invalid_conversion ("character matrix", "complex scalar");
00125 
00126   return retval;
00127 }
00128 
00129 void
00130 octave_char_matrix::print_raw (std::ostream& os,
00131                                bool pr_as_read_syntax) const
00132 {
00133   octave_print_internal (os, matrix, pr_as_read_syntax,
00134                          current_print_indent_level ());
00135 }
00136 
00137 mxArray *
00138 octave_char_matrix::as_mxArray (void) const
00139 {
00140   mxArray *retval = new mxArray (mxCHAR_CLASS, dims (), mxREAL);
00141 
00142   mxChar *pr = static_cast<mxChar *> (retval->get_data ());
00143 
00144   mwSize nel = numel ();
00145 
00146   const char *p = matrix.data ();
00147 
00148   for (mwIndex i = 0; i < nel; i++)
00149     pr[i] = p[i];
00150 
00151   return retval;
00152 }
00153 
00154 // The C++ standard guarantees cctype defines functions, not macros (and hence macros *CAN'T*
00155 // be defined if only cctype is included)
00156 // so there's no need to f*ck around. The exceptions are isascii and toascii,
00157 // which are not C++.
00158 // Oddly enough, all those character functions are int (*) (int), even
00159 // in C++. Wicked!
00160 static inline int xisascii (int c)
00161 { return isascii (c); }
00162 
00163 static inline int xtoascii (int c)
00164 { return toascii (c); }
00165 
00166 octave_value
00167 octave_char_matrix::map (unary_mapper_t umap) const
00168 {
00169   switch (umap)
00170     {
00171 #define STRING_MAPPER(UMAP,FCN,TYPE) \
00172     case umap_ ## UMAP: \
00173       return octave_value (matrix.map<TYPE, int (&) (int)> (FCN))
00174 
00175     STRING_MAPPER (xisalnum, std::isalnum, bool);
00176     STRING_MAPPER (xisalpha, std::isalpha, bool);
00177     STRING_MAPPER (xisascii, xisascii, bool);
00178     STRING_MAPPER (xiscntrl, std::iscntrl, bool);
00179     STRING_MAPPER (xisdigit, std::isdigit, bool);
00180     STRING_MAPPER (xisgraph, std::isgraph, bool);
00181     STRING_MAPPER (xislower, std::islower, bool);
00182     STRING_MAPPER (xisprint, std::isprint, bool);
00183     STRING_MAPPER (xispunct, std::ispunct, bool);
00184     STRING_MAPPER (xisspace, std::isspace, bool);
00185     STRING_MAPPER (xisupper, std::isupper, bool);
00186     STRING_MAPPER (xisxdigit, std::isxdigit, bool);
00187     STRING_MAPPER (xtoascii, xtoascii, double);
00188     STRING_MAPPER (xtolower, std::tolower, char);
00189     STRING_MAPPER (xtoupper, std::toupper, char);
00190 
00191     default:
00192       {
00193         octave_matrix m (array_value (true));
00194         return m.map (umap);
00195       }
00196     }
00197 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines