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_data_conv_h)
27 #define octave_data_conv_h 1
28 
29 #include "octave-config.h"
30 
31 #include "mach-info.h"
32 #include "oct-inttypes-fwd.h"
33 
34 class
35 OCTAVE_API
36 oct_data_conv
37 {
38 public:
39 
40   enum data_type
41   {
42     dt_int8      =  0,
43     dt_uint8     =  1,
44     dt_int16     =  2,
45     dt_uint16    =  3,
46     dt_int32     =  4,
47     dt_uint32    =  5,
48     dt_int64     =  6,
49     dt_uint64    =  7,
50     dt_single    =  8,
51     dt_double    =  9,
52     dt_char      = 10,
53     dt_schar     = 11,
54     dt_uchar     = 12,
55     dt_logical   = 13,
56     dt_short     = 14,
57     dt_ushort    = 15,
58     dt_int       = 16,
59     dt_uint      = 17,
60     dt_long      = 18,
61     dt_ulong     = 19,
62     dt_longlong  = 20,
63     dt_ulonglong = 21,
64     dt_float     = 22,
65     dt_unknown   = 23 // Must be last, have largest value!
66   };
67 
68   static std::size_t data_type_size (data_type dt);
69 
70   static data_type string_to_data_type (const std::string& s);
71 
72   static void string_to_data_type (const std::string& s, int& block_size,
73                                    data_type& input_type,
74                                    data_type& output_type);
75 
76   static void string_to_data_type (const std::string& s, int& block_size,
77                                    data_type& output_type);
78 
79   static std::string data_type_as_string (data_type dt);
80 };
81 
82 // Add new entries to the end of this enum, otherwise Octave will not
83 // be able to read binary data files stored in Octave's binary data
84 // format that were created with previous versions of Octave.
85 
86 enum save_type
87 {
88   LS_U_CHAR  = 0,
89   LS_U_SHORT = 1,
90   LS_U_INT   = 2,
91   LS_CHAR    = 3,
92   LS_SHORT   = 4,
93   LS_INT     = 5,
94   LS_FLOAT   = 6,
95   LS_DOUBLE  = 7,
96   LS_U_LONG  = 8,
97   LS_LONG    = 9
98 };
99 
100 extern OCTAVE_API void
101 do_double_format_conversion (void *data, octave_idx_type len,
102                              octave::mach_info::float_format from_fmt,
103                              octave::mach_info::float_format to_fmt
104                              = octave::mach_info::native_float_format ());
105 
106 extern OCTAVE_API void
107 do_float_format_conversion (void *data, octave_idx_type len,
108                             octave::mach_info::float_format from_fmt,
109                             octave::mach_info::float_format to_fmt
110                             = octave::mach_info::native_float_format ());
111 
112 extern OCTAVE_API void
113 do_float_format_conversion (void *data, std::size_t sz, octave_idx_type len,
114                             octave::mach_info::float_format from_fmt,
115                             octave::mach_info::float_format to_fmt
116                             = octave::mach_info::native_float_format ());
117 
118 extern OCTAVE_API void
119 read_doubles (std::istream& is, double *data, save_type type,
120               octave_idx_type len, bool swap, octave::mach_info::float_format fmt);
121 
122 extern OCTAVE_API void
123 write_doubles (std::ostream& os, const double *data, save_type type,
124                octave_idx_type len);
125 
126 extern OCTAVE_API void
127 read_floats (std::istream& is, float *data, save_type type,
128              octave_idx_type len, bool swap, octave::mach_info::float_format fmt);
129 
130 extern OCTAVE_API void
131 write_floats (std::ostream& os, const float *data, save_type type,
132               octave_idx_type len);
133 
134 template <typename T>
135 inline bool
is_equivalent_type(oct_data_conv::data_type)136 is_equivalent_type (oct_data_conv::data_type)
137 {
138   return false;
139 }
140 
141 template <>
142 inline bool
143 is_equivalent_type<int8_t> (oct_data_conv::data_type t)
144 {
145   return t == oct_data_conv::dt_int8;
146 }
147 
148 template <>
149 inline bool
150 is_equivalent_type<int16_t> (oct_data_conv::data_type t)
151 {
152   return t == oct_data_conv::dt_int16;
153 }
154 
155 template <>
156 inline bool
157 is_equivalent_type<int32_t> (oct_data_conv::data_type t)
158 {
159   return t == oct_data_conv::dt_int32;
160 }
161 
162 template <>
163 inline bool
164 is_equivalent_type<int64_t> (oct_data_conv::data_type t)
165 {
166   return t == oct_data_conv::dt_int64;
167 }
168 
169 template <>
170 inline bool
171 is_equivalent_type<uint8_t> (oct_data_conv::data_type t)
172 {
173   return t == oct_data_conv::dt_uint8;
174 }
175 
176 template <>
177 inline bool
178 is_equivalent_type<uint16_t> (oct_data_conv::data_type t)
179 {
180   return t == oct_data_conv::dt_uint16;
181 }
182 
183 template <>
184 inline bool
185 is_equivalent_type<uint32_t> (oct_data_conv::data_type t)
186 {
187   return t == oct_data_conv::dt_uint32;
188 }
189 
190 template <>
191 inline bool
192 is_equivalent_type<uint64_t> (oct_data_conv::data_type t)
193 {
194   return t == oct_data_conv::dt_uint64;
195 }
196 
197 template <>
198 inline bool
199 is_equivalent_type<octave_int8> (oct_data_conv::data_type t)
200 {
201   return t == oct_data_conv::dt_int8;
202 }
203 
204 template <>
205 inline bool
206 is_equivalent_type<octave_int16> (oct_data_conv::data_type t)
207 {
208   return t == oct_data_conv::dt_int16;
209 }
210 
211 template <>
212 inline bool
213 is_equivalent_type<octave_int32> (oct_data_conv::data_type t)
214 {
215   return t == oct_data_conv::dt_int32;
216 }
217 
218 template <>
219 inline bool
220 is_equivalent_type<octave_int64> (oct_data_conv::data_type t)
221 {
222   return t == oct_data_conv::dt_int64;
223 }
224 
225 template <>
226 inline bool
227 is_equivalent_type<octave_uint8> (oct_data_conv::data_type t)
228 {
229   return t == oct_data_conv::dt_uint8;
230 }
231 
232 template <>
233 inline bool
234 is_equivalent_type<octave_uint16> (oct_data_conv::data_type t)
235 {
236   return t == oct_data_conv::dt_uint16;
237 }
238 
239 template <>
240 inline bool
241 is_equivalent_type<octave_uint32> (oct_data_conv::data_type t)
242 {
243   return t == oct_data_conv::dt_uint32;
244 }
245 
246 template <>
247 inline bool
248 is_equivalent_type<octave_uint64> (oct_data_conv::data_type t)
249 {
250   return t == oct_data_conv::dt_uint64;
251 }
252 
253 template <>
254 inline bool
255 is_equivalent_type<double> (oct_data_conv::data_type t)
256 {
257   return t == oct_data_conv::dt_double;
258 }
259 
260 template <>
261 inline bool
262 is_equivalent_type<float> (oct_data_conv::data_type t)
263 {
264   return t == oct_data_conv::dt_single || t == oct_data_conv::dt_float;
265 }
266 
267 #endif
268