26#if defined (HAVE_CONFIG_H)
49#if defined (OCTAVE_HAVE_LONG_LONG_INT)
50# define FIND_SIZED_INT_TYPE(VAL, BITS, TQ, Q) \
53 int sz = BITS / std::numeric_limits<unsigned char>::digits; \
54 if (sizeof (TQ char) == sz) \
55 VAL = oct_data_conv::dt_ ## Q ## char; \
56 else if (sizeof (TQ short) == sz) \
57 VAL = oct_data_conv::dt_ ## Q ## short; \
58 else if (sizeof (TQ int) == sz) \
59 VAL = oct_data_conv::dt_ ## Q ## int; \
60 else if (sizeof (TQ long) == sz) \
61 VAL = oct_data_conv::dt_ ## Q ## long; \
62 else if (sizeof (TQ long long) == sz) \
63 VAL = oct_data_conv::dt_ ## Q ## longlong; \
65 VAL = oct_data_conv::dt_unknown; \
69# define FIND_SIZED_INT_TYPE(VAL, BITS, TQ, Q) \
72 int sz = BITS / std::numeric_limits<unsigned char>::digits; \
73 if (sizeof (TQ char) == sz) \
74 VAL = oct_data_conv::dt_ ## Q ## char; \
75 else if (sizeof (TQ short) == sz) \
76 VAL = oct_data_conv::dt_ ## Q ## short; \
77 else if (sizeof (TQ int) == sz) \
78 VAL = oct_data_conv::dt_ ## Q ## int; \
79 else if (sizeof (TQ long) == sz) \
80 VAL = oct_data_conv::dt_ ## Q ## long; \
82 VAL = oct_data_conv::dt_unknown; \
87#define FIND_SIZED_FLOAT_TYPE(VAL, BITS) \
90 int sz = BITS / std::numeric_limits<unsigned char>::digits; \
91 if (sizeof (float) == sz) \
92 VAL = oct_data_conv::dt_float; \
93 else if (sizeof (double) == sz) \
94 VAL = oct_data_conv::dt_double; \
96 VAL = oct_data_conv::dt_unknown; \
128 for (
int i = 0; i < 4; i++)
141strip_spaces (
const std::string& str)
143 std::size_t n = str.length ();
147 std::string s (n,
'\0');
149 for (std::size_t i = 0; i < n; i++)
150 if (! isspace (str[i]))
151 s[k++] = tolower (str[i]);
158#define GET_SIZED_INT_TYPE(T, U) \
161 switch (sizeof (T)) \
164 retval = dt_ ## U ## int8; \
168 retval = dt_ ## U ## int16; \
172 retval = dt_ ## U ## int32; \
176 retval = dt_ ## U ## int64; \
180 retval = dt_unknown; \
189 std::size_t retval = -1;
194 retval =
sizeof (int8_t);
198 retval =
sizeof (uint8_t);
202 retval =
sizeof (int16_t);
206 retval =
sizeof (uint16_t);
210 retval =
sizeof (int32_t);
214 retval =
sizeof (uint32_t);
218 retval =
sizeof (int64_t);
222 retval =
sizeof (uint64_t);
227 retval =
sizeof (
float);
235 retval =
sizeof (char);
239 retval =
sizeof (
signed char);
243 retval =
sizeof (
unsigned char);
247 retval =
sizeof (short);
251 retval =
sizeof (
unsigned short);
255 retval =
sizeof (
int);
259 retval =
sizeof (
unsigned int);
263 retval =
sizeof (long);
267 retval =
sizeof (
unsigned long);
271 retval =
sizeof (
long long);
275 retval =
sizeof (
unsigned long long);
279 retval =
sizeof (
bool);
284 (*current_liboctave_error_handler)
285 (
"oct_data_conv::data_type_size: unknown data type");
297 static bool initialized =
false;
303 init_sized_type_lookup_table (sized_type_table);
308 std::string s = strip_spaces (str);
313 else if (s ==
"double" || s ==
"float64" || s ==
"real*8")
315 else if (s ==
"single" || s ==
"float" || s ==
"float32" || s ==
"real*4")
317 else if (s ==
"char" || s ==
"char*1")
321 else if (s ==
"uchar" || s ==
"unsignedchar")
323 else if (s ==
"schar" || s ==
"signedchar")
325 else if (s ==
"int8" || s ==
"integer*1")
327 else if (s ==
"int16" || s ==
"integer*2")
329 else if (s ==
"uint16")
331 else if (s ==
"int32" || s ==
"integer*4")
333 else if (s ==
"uint32")
335 else if (s ==
"int64" || s ==
"integer*8")
337 else if (s ==
"uint64")
339 else if (s ==
"short")
341 else if (s ==
"ushort" || s ==
"unsignedshort")
343 else if (s ==
"uint" || s ==
"unsignedint")
345 else if (s ==
"long")
347 else if (s ==
"ulong" || s ==
"unsignedlong")
350 else if (s ==
"longlong")
352 else if (s ==
"ulonglong" || s ==
"unsignedlonglong")
354 else if (s ==
"logical")
360 (*current_liboctave_error_handler)
361 (
"unable to find matching native data type for %s", s.c_str ());
375 bool input_is_output =
false;
377 std::string s = strip_spaces (str);
382 input_is_output =
true;
385 std::size_t
len = s.length ();
387 while (pos <
len && isdigit (s[pos]))
396 block_size = std::stoi (s);
397 s = s.substr (pos+1);
399 catch (
const std::invalid_argument&)
401 (*current_liboctave_error_handler)
402 (
"invalid repeat count in '%s'", s.c_str ());
404 catch (
const std::out_of_range&)
406 (*current_liboctave_error_handler)
407 (
"repeat count out of range in '%s'", s.c_str ());
412 (
"invalid repeat count in '%s'", str.c_str ());
418 if (pos != std::string::npos)
426 s1 = s.substr (1, pos-1);
428 (*current_liboctave_warning_with_id_handler)
429 (
"Octave:fread-precision-syntax",
430 "warning: ignoring leading * in fread precision");
433 s1 = s.substr (0, pos);
440 (
"fread: invalid precision specified");
450 output_type = input_type;
461 std::string s = strip_spaces (str);
465 std::size_t
len = s.length ();
467 while (pos <
len && isdigit (s[pos]))
476 block_size = std::stoi (s);
477 s = s.substr (pos+1);
479 catch (
const std::invalid_argument&)
481 (*current_liboctave_error_handler)
482 (
"invalid repeat count in '%s'", s.c_str ());
484 catch (
const std::out_of_range&)
486 (*current_liboctave_error_handler)
487 (
"repeat count out of range in '%s'", s.c_str ());
492 (
"invalid repeat count in '%s'", str.c_str ());
550 retval =
"signed char";
554 retval =
"unsigned char";
562 retval =
"unsigned short";
570 retval =
"unsigned int";
578 retval =
"unsigned long";
582 retval =
"long long";
586 retval =
"unsigned long long";
606#define LS_DO_READ(TYPE, swap, data, size, len, stream) \
611 OCTAVE_LOCAL_BUFFER (TYPE, ptr, len); \
612 std::streamsize n_bytes = size * static_cast<std::streamsize> (len); \
613 stream.read (reinterpret_cast<char *> (ptr), n_bytes); \
615 swap_bytes< size > (ptr, len); \
616 for (octave_idx_type i = 0; i < len; i++) \
625#define LS_DO_WRITE(TYPE, data, size, len, stream) \
630 char tmp_type = type; \
631 stream.write (&tmp_type, 1); \
632 OCTAVE_LOCAL_BUFFER (TYPE, ptr, len); \
633 for (octave_idx_type i = 0; i < len; i++) \
634 ptr[i] = static_cast<TYPE> (data[i]); \
635 std::streamsize n_bytes = size * static_cast<std::streamsize> (len); \
636 stream.write (reinterpret_cast<char *> (ptr), n_bytes); \
643OCTAVE_NORETURN
static
645err_unrecognized_float_fmt ()
647 (*current_liboctave_error_handler)
648 (
"unrecognized floating point format requested");
686 octave::mach_info::float_format from_fmt,
687 octave::mach_info::float_format to_fmt)
691 case octave::mach_info::flt_fmt_ieee_little_endian:
694 case octave::mach_info::flt_fmt_ieee_little_endian:
697 case octave::mach_info::flt_fmt_ieee_big_endian:
698 IEEE_big_double_to_IEEE_little_double (data,
len);
702 err_unrecognized_float_fmt ();
707 case octave::mach_info::flt_fmt_ieee_big_endian:
710 case octave::mach_info::flt_fmt_ieee_little_endian:
711 IEEE_little_double_to_IEEE_big_double (data,
len);
714 case octave::mach_info::flt_fmt_ieee_big_endian:
718 err_unrecognized_float_fmt ();
724 (*current_liboctave_error_handler)
725 (
"impossible state reached in file '%s' at line %d",
733 octave::mach_info::float_format from_fmt,
734 octave::mach_info::float_format to_fmt)
738 case octave::mach_info::flt_fmt_ieee_little_endian:
741 case octave::mach_info::flt_fmt_ieee_little_endian:
744 case octave::mach_info::flt_fmt_ieee_big_endian:
745 IEEE_big_float_to_IEEE_little_float (data,
len);
749 err_unrecognized_float_fmt ();
754 case octave::mach_info::flt_fmt_ieee_big_endian:
757 case octave::mach_info::flt_fmt_ieee_little_endian:
758 IEEE_little_float_to_IEEE_big_float (data,
len);
761 case octave::mach_info::flt_fmt_ieee_big_endian:
765 err_unrecognized_float_fmt ();
771 (*current_liboctave_error_handler)
772 (
"impossible state reached in file '%s' at line %d",
780 octave::mach_info::float_format from_fmt,
781 octave::mach_info::float_format to_fmt)
794 (*current_liboctave_error_handler)
795 (
"impossible state reached in file '%s' at line %d",
804 octave::mach_info::float_format fmt)
835 std::streamsize n_bytes = 4 *
static_cast<std::streamsize
> (
len);
836 is.read (
reinterpret_cast<char *
> (ptr), n_bytes);
845 std::streamsize n_bytes = 8 *
static_cast<std::streamsize
> (
len);
846 is.read (
reinterpret_cast<char *
> (data), n_bytes);
856 is.clear (std::ios::failbit | is.rdstate ());
864 octave::mach_info::float_format fmt)
894 std::streamsize n_bytes = 4 *
static_cast<std::streamsize
> (
len);
895 is.read (
reinterpret_cast<char *
> (data), n_bytes);
903 std::streamsize n_bytes = 8 *
static_cast<std::streamsize
> (
len);
904 is.read (
reinterpret_cast<char *
> (ptr), n_bytes);
912 is.clear (std::ios::failbit | is.rdstate ());
953 char tmp_type =
static_cast<char> (type);
954 os.write (&tmp_type, 1);
955 std::streamsize n_bytes = 8 *
static_cast<std::streamsize
> (
len);
956 os.write (
reinterpret_cast<const char *
> (data), n_bytes);
961 (*current_liboctave_error_handler)
962 (
"unrecognized data format requested");
999 char tmp_type =
static_cast<char> (type);
1000 os.write (&tmp_type, 1);
1001 std::streamsize n_bytes = 4 *
static_cast<std::streamsize
> (
len);
1002 os.write (
reinterpret_cast<const char *
> (data), n_bytes);
1011 (*current_liboctave_error_handler)
1012 (
"unrecognized data format requested");
void swap_bytes< 8 >(void *ptr)
void swap_bytes< 4 >(void *ptr)
static std::size_t data_type_size(data_type dt)
static std::string data_type_as_string(data_type dt)
static data_type string_to_data_type(const std::string &s)
void do_float_format_conversion(void *data, octave_idx_type len, octave::mach_info::float_format from_fmt, octave::mach_info::float_format to_fmt)
#define LS_DO_READ(TYPE, swap, data, size, len, stream)
#define FIND_SIZED_INT_TYPE(VAL, BITS, TQ, Q)
#define GET_SIZED_INT_TYPE(T, U)
void read_doubles(std::istream &is, double *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
void write_doubles(std::ostream &os, const double *data, save_type type, octave_idx_type len)
void write_floats(std::ostream &os, const float *data, save_type type, octave_idx_type len)
#define LS_DO_WRITE(TYPE, data, size, len, stream)
void read_floats(std::istream &is, float *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
void do_double_format_conversion(void *data, octave_idx_type len, octave::mach_info::float_format from_fmt, octave::mach_info::float_format to_fmt)
#define FIND_SIZED_FLOAT_TYPE(VAL, BITS)
OCTAVE_NORETURN liboctave_error_handler current_liboctave_error_handler
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_LOCAL_BUFFER(T, buf, size)