comparison liboctave/data-conv.cc @ 3358:fa7d8036d12a

[project @ 1999-11-19 00:46:31 by jwe]
author jwe
date Fri, 19 Nov 1999 00:46:32 +0000
parents 041ea33fbbf4
children 803643945da1
comparison
equal deleted inserted replaced
3357:34d512262892 3358:fa7d8036d12a
30 30
31 #include "byte-swap.h" 31 #include "byte-swap.h"
32 #include "data-conv.h" 32 #include "data-conv.h"
33 #include "lo-error.h" 33 #include "lo-error.h"
34 34
35 #define CHECK_SIZED_INT_TYPE(VAL, BITS, TQ, Q) \
36 do \
37 { \
38 int sz = BITS / CHAR_BIT; \
39 if (sizeof (TQ char) == sz) \
40 VAL = oct_data_conv::dt_ ## Q ## char; \
41 else if (sizeof (TQ short) == sz) \
42 VAL = oct_data_conv::dt_ ## Q ## short; \
43 else if (sizeof (TQ int) == sz) \
44 VAL = oct_data_conv::dt_ ## Q ## int; \
45 else if (sizeof (TQ long) == sz) \
46 VAL = oct_data_conv::dt_ ## Q ## long; \
47 else \
48 VAL = oct_data_conv::dt_unknown; \
49 } \
50 while (0)
51
52 #define CHECK_SIZED_FLOAT_TYPE(VAL, BITS) \
53 do \
54 { \
55 int sz = BITS / CHAR_BIT; \
56 if (sizeof (float) == sz) \
57 VAL = oct_data_conv::dt_float; \
58 else if (sizeof (double) == sz) \
59 VAL = oct_data_conv::dt_double; \
60 else \
61 VAL = oct_data_conv::dt_unknown; \
62 } \
63 while (0)
64
65 // I'm not sure it is worth the trouble, but let's use a lookup table
66 // for the types that are supposed to be a specific number of bits
67 // wide. Given the macros above, this should work as long as CHAR_BIT
68 // is a multiple of 8 and there are types with the right sizes.
69 //
70 // The sized data type lookup table has the following format:
71 //
72 // bits
73 // +----+----+----+----+
74 // | 8 | 16 | 32 | 64 |
75 // +----+----+----+----+
76 // signed integer | | | | |
77 // +----+----+----+----+
78 // unsigned integer | | | | |
79 // +----+----+----+----+
80 // floating point | | | | |
81 // +----+----+----+----+
82 //
83 // So, the 0,3 element is supposed to contain the oct_data_conv enum
84 // value corresponding to the correct native data type for a signed
85 // 32-bit integer.
86
87 static void
88 init_sized_type_lookup_table (oct_data_conv::data_type table[3][4])
89 {
90 int bits = 8;
91
92 for (int i = 0; i < 4; i++)
93 {
94 CHECK_SIZED_INT_TYPE (table[0][i], bits, , );
95
96 CHECK_SIZED_INT_TYPE (table[1][i], bits, unsigned, u);
97
98 CHECK_SIZED_FLOAT_TYPE (table[2][i], bits);
99
100 bits *= 2;
101 }
102 }
103
35 oct_data_conv::data_type 104 oct_data_conv::data_type
36 oct_data_conv::string_to_data_type (const string& str) 105 oct_data_conv::string_to_data_type (const string& str)
37 { 106 {
38 data_type retval = dt_unknown; 107 data_type retval = dt_unknown;
39 108
40 // XXX FIXME XXX -- finish implementing this. 109 static bool initialized = false;
41 110
42 // XXX FIXME XXX -- before checking s, need to strip spaces and downcase. 111 static data_type sized_type_table[3][4];
112
113 if (! initialized)
114 {
115 init_sized_type_lookup_table (sized_type_table);
116
117 initialized = true;
118 }
119
120 // XXX FIXME XXX -- finish implementing this.
43 121
44 int n = str.length (); 122 int n = str.length ();
45 123
46 int k = 0; 124 int k = 0;
47 125
51 if (! isspace (str[i])) 129 if (! isspace (str[i]))
52 s[k++] = tolower (str[i]); 130 s[k++] = tolower (str[i]);
53 131
54 s.resize (k); 132 s.resize (k);
55 133
56 if (s == "char" || s == "char*1" || s == "integer*1" || s == "int8") 134 if (s == "char")
57 retval = dt_char; 135 retval = dt_char;
58 else if (s == "schar" || s == "signedchar") 136 else if (s == "schar" || s == "signedchar")
59 retval = dt_schar; 137 retval = dt_schar;
60 else if (s == "uchar" || s == "unsignedchar") 138 else if (s == "uchar" || s == "unsignedchar")
61 retval = dt_uchar; 139 retval = dt_uchar;
69 retval = dt_uint; 147 retval = dt_uint;
70 else if (s == "long") 148 else if (s == "long")
71 retval = dt_long; 149 retval = dt_long;
72 else if (s == "ulong" || s == "unsignedlong") 150 else if (s == "ulong" || s == "unsignedlong")
73 retval = dt_ulong; 151 retval = dt_ulong;
74 else if (s == "float" || s == "float32" || s == "real*4") 152 else if (s == "float")
75 retval = dt_float; 153 retval = dt_float;
76 else if (s == "double" || s == "float64" || s == "real*8") 154 else if (s == "double")
77 retval = dt_double; 155 retval = dt_double;
156 else if (s == "int8" || s == "char*1" || s == "integer*1")
157 retval = sized_type_table[0][0];
78 else if (s == "int16" || s == "integer*2") 158 else if (s == "int16" || s == "integer*2")
79 { 159 retval = sized_type_table[0][1];
80 if (sizeof (short) == 2)
81 retval = dt_short;
82 else if (sizeof (int) == 2)
83 retval = dt_int;
84 else
85 (*current_liboctave_error_handler)
86 ("unable to find matching native data type for %s", s.c_str ());
87 }
88 else if (s == "int32" || s == "integer*4") 160 else if (s == "int32" || s == "integer*4")
89 { 161 retval = sized_type_table[0][2];
90 if (sizeof (int) == 4) 162 else if (s == "int64" || s == "integer*8")
91 retval = dt_int; 163 retval = sized_type_table[0][3];
92 else if (sizeof (long) == 4) 164 else if (s == "uint8")
93 retval = dt_long; 165 retval = sized_type_table[1][0];
94 else
95 (*current_liboctave_error_handler)
96 ("unable to find matching native data type for %s", s.c_str ());
97 }
98 else if (s == "uint16") 166 else if (s == "uint16")
99 { 167 retval = sized_type_table[1][1];
100 if (sizeof (unsigned short) == 2)
101 retval = dt_ushort;
102 else if (sizeof (unsigned int) == 2)
103 retval = dt_uint;
104 else
105 (*current_liboctave_error_handler)
106 ("unable to find matching native data type for %s", s.c_str ());
107 }
108 else if (s == "uint32") 168 else if (s == "uint32")
109 { 169 retval = sized_type_table[1][2];
110 if (sizeof (unsigned int) == 4) 170 else if (s == "uint64")
111 retval = dt_uint; 171 retval = sized_type_table[1][3];
112 else if (sizeof (unsigned long) == 4) 172 else if (s == "float32" || s == "real*4")
113 retval = dt_ulong; 173 retval = sized_type_table[2][2];
114 else 174 else if (s == "float64" || s == "real*8")
115 (*current_liboctave_error_handler) 175 retval = sized_type_table[2][3];
116 ("unable to find matching native data type for %s", s.c_str ());
117 }
118 else 176 else
119 (*current_liboctave_error_handler) ("invalid data type specified"); 177 (*current_liboctave_error_handler) ("invalid data type specified");
178
179 if (retval == dt_unknown)
180 (*current_liboctave_error_handler)
181 ("unable to find matching native data type for %s", s.c_str ());
120 182
121 return retval; 183 return retval;
122 } 184 }
123 185
124 #define swap_1_bytes(x, y) 186 #define swap_1_bytes(x, y)