GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-ch-mat.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 John W. Eaton
4 Copyright (C) 2009-2010 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <cctype>
29 #include <iostream>
30 
31 #include "lo-ieee.h"
32 #include "mx-base.h"
33 
34 #include "mxarray.h"
35 #include "ov-base.h"
36 #include "ov-base-mat.h"
37 #include "ov-base-mat.cc"
38 #include "ov-ch-mat.h"
39 #include "gripes.h"
40 #include "pr-output.h"
41 
42 template class octave_base_matrix<charNDArray>;
43 
45 octave_char_matrix::index_vector (bool /* require_integers */) const
46 {
47  const char *p = matrix.data ();
48  if (numel () == 1 && *p == ':')
49  return idx_vector (':');
50  else
51  return idx_vector (array_value (true));
52 }
53 
54 double
56 {
57  double retval = lo_ieee_nan_value ();
58 
59  if (rows () > 0 && columns () > 0)
60  {
61  gripe_implicit_conversion ("Octave:array-to-scalar",
62  "character matrix", "real scalar");
63 
64  retval = static_cast<unsigned char> (matrix (0, 0));
65  }
66  else
67  gripe_invalid_conversion ("character matrix", "real scalar");
68 
69  return retval;
70 }
71 
72 float
74 {
75  float retval = lo_ieee_float_nan_value ();
76 
77  if (rows () > 0 && columns () > 0)
78  {
79  gripe_implicit_conversion ("Octave:array-to-scalar",
80  "character matrix", "real scalar");
81 
82  retval = static_cast<unsigned char> (matrix (0, 0));
83  }
84  else
85  gripe_invalid_conversion ("character matrix", "real scalar");
86 
87  return retval;
88 }
89 
90 Complex
92 {
93  double tmp = lo_ieee_nan_value ();
94 
95  Complex retval (tmp, tmp);
96 
97  if (rows () > 0 && columns () > 0)
98  {
99  gripe_implicit_conversion ("Octave:array-to-scalar",
100  "character matrix", "complex scalar");
101 
102  retval = static_cast<unsigned char> (matrix (0, 0));
103  }
104  else
105  gripe_invalid_conversion ("character matrix", "complex scalar");
106 
107  return retval;
108 }
109 
112 {
113  float tmp = lo_ieee_float_nan_value ();
114 
115  FloatComplex retval (tmp, tmp);
116 
117  if (rows () > 0 && columns () > 0)
118  {
119  gripe_implicit_conversion ("Octave:array-to-scalar",
120  "character matrix", "complex scalar");
121 
122  retval = static_cast<unsigned char> (matrix (0, 0));
123  }
124  else
125  gripe_invalid_conversion ("character matrix", "complex scalar");
126 
127  return retval;
128 }
129 
130 void
132  bool pr_as_read_syntax) const
133 {
134  octave_print_internal (os, matrix, pr_as_read_syntax,
136 }
137 
138 mxArray *
140 {
141  mxArray *retval = new mxArray (mxCHAR_CLASS, dims (), mxREAL);
142 
143  mxChar *pr = static_cast<mxChar *> (retval->get_data ());
144 
145  mwSize nel = numel ();
146 
147  const char *p = matrix.data ();
148 
149  for (mwIndex i = 0; i < nel; i++)
150  pr[i] = p[i];
151 
152  return retval;
153 }
154 
155 // The C++ standard guarantees cctype defines functions, not macros (and
156 // hence macros *CAN'T* be defined if only cctype is included) so
157 // there's no need to fuck around. The exceptions are isascii and
158 // toascii, which are not C++. Oddly enough, all those character
159 // functions are int (*) (int), even in C++. Wicked!
160 static inline int xisascii (int c)
161 {
162 #ifdef HAVE_ISASCII
163  return isascii (c);
164 #else
165  return (c >= 0x00 && c <= 0x7f);
166 #endif
167 }
168 
169 static inline int xtoascii (int c)
170 {
171 #ifdef HAVE_TOASCII
172  return toascii (c);
173 #else
174  return (c & 0x7F);
175 #endif
176 }
177 
180 {
181  octave_value retval;
182 
183  switch (umap)
184  {
185 #define STRING_MAPPER(UMAP,FCN,TYPE) \
186  case umap_ ## UMAP: \
187  return octave_value (matrix.map<TYPE, int (&) (int)> (FCN))
188 
189  STRING_MAPPER (xisalnum, std::isalnum, bool);
190  STRING_MAPPER (xisalpha, std::isalpha, bool);
192  STRING_MAPPER (xiscntrl, std::iscntrl, bool);
193  STRING_MAPPER (xisdigit, std::isdigit, bool);
194  STRING_MAPPER (xisgraph, std::isgraph, bool);
195  STRING_MAPPER (xislower, std::islower, bool);
196  STRING_MAPPER (xisprint, std::isprint, bool);
197  STRING_MAPPER (xispunct, std::ispunct, bool);
198  STRING_MAPPER (xisspace, std::isspace, bool);
199  STRING_MAPPER (xisupper, std::isupper, bool);
200  STRING_MAPPER (xisxdigit, std::isxdigit, bool);
201  STRING_MAPPER (xtoascii, xtoascii, double);
202  STRING_MAPPER (xtolower, std::tolower, char);
203  STRING_MAPPER (xtoupper, std::toupper, char);
204 
205  // For Matlab compatibility, these should work on ASCII values
206  // without error or warning.
207  case umap_abs:
208  case umap_ceil:
209  case umap_fix:
210  case umap_floor:
211  case umap_imag:
212  case umap_isinf:
213  case umap_isnan:
214  case umap_real:
215  case umap_round:
216  {
217  octave_matrix m (array_value (true));
218  return m.map (umap);
219  }
220 
221  default:
222  error ("%s: expecting numeric argument", get_umap_name (umap));
223  break;
224  }
225 
226  return retval;
227 }
void gripe_implicit_conversion(const char *id, const char *from, const char *to)
Definition: gripes.cc:180
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition: ov-ch-mat.cc:131
octave_idx_type columns(void) const
Definition: ov-base.h:301
static int xtoascii(int c)
Definition: ov-ch-mat.cc:169
FloatComplex float_complex_value(bool=false) const
Definition: ov-ch-mat.cc:111
octave_idx_type numel(void) const
Definition: ov-base-mat.h:103
#define STRING_MAPPER(UMAP, FCN, TYPE)
void error(const char *fmt,...)
Definition: error.cc:476
void * get_data(void) const
Definition: mxarray.h:433
double lo_ieee_nan_value(void)
Definition: lo-ieee.cc:126
static const char * get_umap_name(unary_mapper_t)
Definition: ov-base.cc:1184
octave_value map(unary_mapper_t umap) const
Definition: ov-re-mat.cc:878
mxArray * as_mxArray(void) const
Definition: ov-ch-mat.cc:139
const T * data(void) const
Definition: Array.h:479
float lo_ieee_float_nan_value(void)
Definition: lo-ieee.cc:202
void mxArray
Definition: mex.h:53
float float_value(bool=false) const
Definition: ov-ch-mat.cc:73
dim_vector dims(void) const
Definition: ov-base-mat.h:101
octave_value map(unary_mapper_t umap) const
Definition: ov-ch-mat.cc:179
void gripe_invalid_conversion(const std::string &from, const std::string &to)
Definition: gripes.cc:99
int current_print_indent_level(void) const
Definition: ov-base.h:805
void octave_print_internal(std::ostream &, char, bool)
Definition: pr-output.cc:1715
NDArray array_value(bool=false) const
Definition: ov-ch-mat.h:114
std::complex< float > FloatComplex
Definition: oct-cmplx.h:30
Complex complex_value(bool=false) const
Definition: ov-ch-mat.cc:91
double double_value(bool=false) const
Definition: ov-ch-mat.cc:55
Definition: mxarray.h:52
std::complex< double > Complex
Definition: oct-cmplx.h:29
idx_vector index_vector(bool require_integers=false) const
Definition: ov-ch-mat.cc:45
octave_idx_type rows(void) const
Definition: ov-base.h:294
static int xisascii(int c)
Definition: ov-ch-mat.cc:160