1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_ov_str_mat_h)
27 #define octave_ov_str_mat_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdlib>
32 
33 #include <iosfwd>
34 #include <string>
35 
36 #include "mx-base.h"
37 #include "str-vec.h"
38 
39 #include "error.h"
40 #include "oct-stream.h"
41 #include "ov.h"
42 #include "ov-ch-mat.h"
43 #include "ov-re-mat.h"
44 #include "ov-typeinfo.h"
45 
46 class octave_value_list;
47 
48 // Character matrix values with special properties for use as
49 // strings.
50 
51 class
52 OCTINTERP_API
53 octave_char_matrix_str : public octave_char_matrix
54 {
55 public:
56 
octave_char_matrix_str(void)57   octave_char_matrix_str (void)
58     : octave_char_matrix () { }
59 
octave_char_matrix_str(const charMatrix & chm)60   octave_char_matrix_str (const charMatrix& chm)
61     : octave_char_matrix (chm) { }
62 
octave_char_matrix_str(const charNDArray & chm)63   octave_char_matrix_str (const charNDArray& chm)
64     : octave_char_matrix (chm) { }
65 
octave_char_matrix_str(const Array<char> & chm)66   octave_char_matrix_str (const Array<char>& chm)
67     : octave_char_matrix (chm) { }
68 
octave_char_matrix_str(char c)69   octave_char_matrix_str (char c)
70     : octave_char_matrix (c) { }
71 
octave_char_matrix_str(const char * s)72   octave_char_matrix_str (const char *s)
73     : octave_char_matrix (s) { }
74 
octave_char_matrix_str(const std::string & s)75   octave_char_matrix_str (const std::string& s)
76     : octave_char_matrix (s) { }
77 
octave_char_matrix_str(const string_vector & s)78   octave_char_matrix_str (const string_vector& s)
79     : octave_char_matrix (s) { }
80 
octave_char_matrix_str(const octave_char_matrix & chm)81   octave_char_matrix_str (const octave_char_matrix& chm)
82     : octave_char_matrix (chm) { }
83 
octave_char_matrix_str(const octave_char_matrix_str & chms)84   octave_char_matrix_str (const octave_char_matrix_str& chms)
85     : octave_char_matrix (chms) { }
86 
87   ~octave_char_matrix_str (void) = default;
88 
clone(void)89   octave_base_value * clone (void) const
90   { return new octave_char_matrix_str (*this); }
empty_clone(void)91   octave_base_value * empty_clone (void) const
92   { return new octave_char_matrix_str (); }
93 
94   type_conv_info numeric_conversion_function (void) const;
95 
96   octave_value do_index_op (const octave_value_list& idx,
97                             bool resize_ok = false)
98   { return do_index_op_internal (idx, resize_ok); }
99 
squeeze(void)100   octave_value squeeze (void) const
101   { return octave_value (charNDArray (matrix.squeeze ())); }
102 
reshape(const dim_vector & new_dims)103   octave_value reshape (const dim_vector& new_dims) const
104   { return octave_value (charNDArray (matrix.reshape (new_dims))); }
105 
106   octave_value permute (const Array<int>& vec, bool inv = false) const
107   { return octave_value (charNDArray (matrix.permute (vec, inv))); }
108 
109   octave_value resize (const dim_vector& dv, bool fill = false) const;
110 
111   // We don't need to override both forms of the diag method.  The using
112   // declaration will avoid warnings about partially-overloaded virtual
113   // functions.
114   using octave_char_matrix::diag;
115 
116   octave_value diag (octave_idx_type k = 0) const
117   { return octave_value (matrix.diag (k)); }
118 
is_string(void)119   bool is_string (void) const { return true; }
120 
isnumeric(void)121   bool isnumeric (void) const { return false; }
122 
123   double double_value (bool = false) const;
124 
125   Matrix matrix_value (bool = false) const;
126 
127   NDArray array_value (bool = false) const;
128 
129   Complex complex_value (bool = false) const;
130 
131   ComplexMatrix complex_matrix_value (bool = false) const;
132 
133   ComplexNDArray complex_array_value (bool = false) const;
134 
135   string_vector string_vector_value (bool pad = false) const;
136 
137   std::string string_value (bool force = false) const;
138 
xstring_value(void)139   std::string xstring_value (void) const { return string_value (); }
140 
141   Array<std::string> cellstr_value (void) const;
142 
143   octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
144   { return octave_value (matrix.sort (dim, mode)); }
145 
146   octave_value sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0,
147                      sortmode mode = ASCENDING) const
148   { return octave_value (matrix.sort (sidx, dim, mode)); }
149 
print_as_scalar(void)150   bool print_as_scalar (void) const { return (rows () <= 1); }
151 
152   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
153 
154   void short_disp (std::ostream& os) const;
155 
156   std::string edit_display (const float_display_format& fmt,
157                             octave_idx_type i, octave_idx_type j) const;
158 
159   bool save_ascii (std::ostream& os);
160 
161   bool load_ascii (std::istream& is);
162 
163   bool save_binary (std::ostream& os, bool save_as_floats);
164 
165   bool load_binary (std::istream& is, bool swap,
166                     octave::mach_info::float_format fmt);
167 
168   bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
169 
170   bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
171 
write(octave::stream & os,int block_size,oct_data_conv::data_type output_type,int skip,octave::mach_info::float_format flt_fmt)172   int write (octave::stream& os, int block_size,
173              oct_data_conv::data_type output_type, int skip,
174              octave::mach_info::float_format flt_fmt) const
175   { return os.write (matrix, block_size, output_type, skip, flt_fmt); }
176 
177 protected:
178 
179   octave_value do_index_op_internal (const octave_value_list& idx,
180                                      bool resize_ok, char type = '"');
181 
182 private:
183 
184   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
185 };
186 
187 typedef octave_char_matrix_str octave_char_matrix_dq_str;
188 
189 class
190 octave_char_matrix_sq_str : public octave_char_matrix_str
191 {
192 public:
193 
octave_char_matrix_sq_str(void)194   octave_char_matrix_sq_str (void)
195     : octave_char_matrix_str () { }
196 
octave_char_matrix_sq_str(const charMatrix & chm)197   octave_char_matrix_sq_str (const charMatrix& chm)
198     : octave_char_matrix_str (chm) { }
199 
octave_char_matrix_sq_str(const charNDArray & chm)200   octave_char_matrix_sq_str (const charNDArray& chm)
201     : octave_char_matrix_str (chm) { }
202 
octave_char_matrix_sq_str(const Array<char> & chm)203   octave_char_matrix_sq_str (const Array<char>& chm)
204     : octave_char_matrix_str (chm) { }
205 
octave_char_matrix_sq_str(char c)206   octave_char_matrix_sq_str (char c)
207     : octave_char_matrix_str (c) { }
208 
octave_char_matrix_sq_str(const char * s)209   octave_char_matrix_sq_str (const char *s)
210     : octave_char_matrix_str (s) { }
211 
octave_char_matrix_sq_str(const std::string & s)212   octave_char_matrix_sq_str (const std::string& s)
213     : octave_char_matrix_str (s) { }
214 
octave_char_matrix_sq_str(const string_vector & s)215   octave_char_matrix_sq_str (const string_vector& s)
216     : octave_char_matrix_str (s) { }
217 
octave_char_matrix_sq_str(const octave_char_matrix_str & chm)218   octave_char_matrix_sq_str (const octave_char_matrix_str& chm)
219     : octave_char_matrix_str (chm) { }
220 
octave_char_matrix_sq_str(const octave_char_matrix_sq_str & chms)221   octave_char_matrix_sq_str (const octave_char_matrix_sq_str& chms)
222     : octave_char_matrix_str (chms) { }
223 
224   ~octave_char_matrix_sq_str (void) = default;
225 
clone(void)226   octave_base_value * clone (void) const
227   { return new octave_char_matrix_sq_str (*this); }
empty_clone(void)228   octave_base_value * empty_clone (void) const
229   { return new octave_char_matrix_sq_str (); }
230 
squeeze(void)231   octave_value squeeze (void) const
232   { return octave_value (charNDArray (matrix.squeeze ()), '\''); }
233 
reshape(const dim_vector & new_dims)234   octave_value reshape (const dim_vector& new_dims) const
235   { return octave_value (charNDArray (matrix.reshape (new_dims)), '\''); }
236 
237   octave_value permute (const Array<int>& vec, bool inv = false) const
238   { return octave_value (charNDArray (matrix.permute (vec, inv)), '\''); }
239 
240   octave_value resize (const dim_vector& dv, bool = false) const
241   {
242     charNDArray retval (matrix);
243     retval.resize (dv);
244     return octave_value (retval, '\'');
245   }
246 
247   // We don't need to override both forms of the diag method.  The using
248   // declaration will avoid warnings about partially-overloaded virtual
249   // functions.
250   using octave_char_matrix_str::diag;
251 
252   octave_value diag (octave_idx_type k = 0) const
253   { return octave_value (matrix.diag (k), '\''); }
254 
is_sq_string(void)255   bool is_sq_string (void) const { return true; }
256 
257   octave_value do_index_op (const octave_value_list& idx,
258                             bool resize_ok = false)
259   { return do_index_op_internal (idx, resize_ok, '\''); }
260 
261   octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
262   { return octave_value (matrix.sort (dim, mode), '\''); }
263 
264   octave_value sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0,
265                      sortmode mode = ASCENDING) const
266   { return octave_value (matrix.sort (sidx, dim, mode), '\''); }
267 
268 private:
269 
270   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
271 };
272 
273 #endif
274