35 # define HUGE_VAL HUGE 42 #if BINIO_ENABLE_STRING 44 #define STRINGBUFSIZE 256 68 if(
sizeof(
float) == 4 &&
sizeof(double) == 8)
72 if(dat[0] == 0x40 && dat[1] == 0xD0 && !dat[2] && !dat[3])
77 if(dat[3] == 0x40 && dat[2] == 0xD0 && !dat[1] && !dat[0])
104 return (
my_flags & f ?
true :
false);
117 return (
err &
Eof ?
true :
false);
136 if(size >
sizeof(
Int)) {
138 throw runtime_error (
"The size of the integer to be read exceeds our system's biggest type");
142 for(i = 0; i < size; i++) {
167 case Single: size = 4;
break;
168 case Double: size = 8;
break;
177 if (!swap && ((size ==
sizeof (
float)) || (size ==
sizeof (
double))))
182 getRaw ((
char *)&f, size);
188 getRaw ((
char *)&d, size);
195 for(i = 0; i < size; i++)
197 in[size - i - 1] = getByte();
204 case Single:
return *(
float *)in;
205 case Double:
return *(
double *)in;
209 case Single:
return ieee_single2float(in);
210 case Double:
return ieee_double2float(in);
222 signed int sign = data[0] >> 7 ? -1 : 1;
223 unsigned int exp = ((data[0] << 1) & 0xff) | ((data[1] >> 7) & 1),
224 fracthi7 = data[1] & 0x7f;
225 Float fract = fracthi7 * 65536.0 + data[2] * 256.0 + data[3];
228 if(!exp && !fracthi7 && !data[2] && !data[3])
return sign * 0.0;
233 if(!fracthi7 && !data[2] && !data[3])
239 if(sign == -1)
return -1.0;
else return 1.0;
253 return sign * pow(2, -126.) * fract * pow(2, -23.);
255 return sign * pow(2, exp - 127.) * (fract * pow(2, -23.) + 1);
262 signed int sign = data[0] >> 7 ? -1 : 1;
263 unsigned int exp = ((
unsigned int)(data[0] & 0x7f) << 4) | (data[1] >> 4),
264 fracthi4 = data[1] & 0xf;
265 Float fract = fracthi4 * pow(2, 48.) + data[2] * pow(2, 40.) + data[3] *
266 pow(2, 32.) + data[4] * pow(2, 24.) + data[5] * pow(2, 16.) + data[6] *
267 pow(2, 8.) + data[7];
270 if(!exp && !fracthi4 && !data[2] && !data[3] && !data[4] && !data[5] &&
271 !data[6] && !data[7])
return sign * 0.0;
276 if(!fracthi4 && !data[2] && !data[3] && !data[4] && !data[5] && !data[6] &&
283 if(sign == -1)
return -1.0;
else return 1.0;
297 return sign * pow(2, -1022.) * fract * pow(2, -52.);
299 return sign * pow(2, exp - 1023.) * (fract * pow(2, -52.) + 1);
316 for(i = 1; i < (exp < 0 ? -exp : exp); i++)
319 if(exp < 0) val = 1.0 / val;
329 for(i = 0; i < maxlen; i++) {
330 str[i] = (char)getByte();
331 if(
err) { str[i] =
'\0';
return i; }
342 for(i = 0; i < maxlen; i++) {
343 str[i] = (char)getByte();
344 if(str[i] == delim ||
err) { str[i] =
'\0';
return i; }
351 #if BINIO_ENABLE_STRING 360 tempstr.append(buf, read);
369 Int val = readInt(size);
376 Float val = readFloat(ft);
402 for(i = 0; i < amount; i++)
421 if(size >
sizeof(
Int)) {
423 throw runtime_error (
"The size of the integer to be stored exceeds our system's biggest type");
427 for(i = 0; i < size; i++) {
429 putByte((
unsigned char)(val >> ((size - i - 1) * 8)) & 0xff);
431 putByte((
unsigned char)val & 0xff);
448 float outf = (float)f;
449 double outd = (double)f;
455 case Single: size = 4;
break;
456 case Double: size = 8;
break;
459 if (!swap && ((size ==
sizeof (
float)) || (size ==
sizeof (
double))))
462 { putRaw ((
char *)&outf, size);
return; }
464 { putRaw ((
char *)&outd, size);
return; }
485 case Single: size = 4; float2ieee_single(f, buf);
break;
486 case Double: size = 8; float2ieee_double(f, buf);
break;
497 if(swap) out += size - 1;
498 for(i = 0; i < size; i++) {
500 if(swap) out--;
else out++;
510 #ifdef BINIO_WITH_MATH 541 # define FloatToUnsigned(f) ((unsigned long)(f)) 543 # define FloatToUnsigned(f) ((unsigned long)(((long)((f) - 2147483648.0)) + 2147483647L + 1)) 547 #define SEXP_OFFSET 127 549 #define SEXP_POSITION (32-SEXP_SIZE-1) 569 fMant = frexp(num, &expon);
573 bits = sign | 0x7F800000;
585 mantissa = (long)(fMant * (
double)(1L << shift));
586 bits = sign | mantissa;
598 bytes[0] = (
unsigned char)(bits >> 24);
599 bytes[1] = (
unsigned char)(bits >> 16);
600 bytes[2] = (
unsigned char)(bits >> 8);
601 bytes[3] = (
unsigned char)bits;
604 #define DEXP_MAX 2047 605 #define DEXP_OFFSET 1023 607 #define DEXP_POSITION (32-DEXP_SIZE-1) 628 fMant = frexp(num, &expon);
632 first = sign | 0x7FF00000;
652 fsMant = ldexp(fMant, shift);
653 mantissa = (long)floor(fsMant);
654 first = sign | mantissa;
661 mantissa = (long)floor(fsMant);
670 bytes[0] = (
unsigned char)(first >> 24);
671 bytes[1] = (
unsigned char)(first >> 16);
672 bytes[2] = (
unsigned char)(first >> 8);
673 bytes[3] = (
unsigned char)first;
674 bytes[4] = (
unsigned char)(second >> 24);
675 bytes[5] = (
unsigned char)(second >> 16);
676 bytes[6] = (
unsigned char)(second >> 8);
677 bytes[7] = (
unsigned char)second;
680 #endif // BINIO_WITH_MATH 686 if(!amount) amount = strlen(str);
688 for(i = 0; i < amount; i++) {
696 #if BINIO_ENABLE_STRING 699 return writeString(str.c_str());
void swap(Vector &v1, Vector &v2)
Swaps two objects.
Float ieee_single2float(Byte *data)
virtual void seek(long, Offset=Set)=0
unsigned long writeString(const char *str, unsigned long amount=0)
Numeric sign(const Numeric &x)
sign
Float readFloat(FType ft)
void float2ieee_single(Float f, Byte *data)
Float peekFloat(FType ft)
unsigned long readString(char *str, unsigned long amount)
Int readInt(unsigned int size)
static const Flags system_flags
void float2ieee_double(Float f, Byte *data)
void writeFloat(Float f, FType ft)
void writeInt(Int val, unsigned int size)
void ignore(unsigned long amount=1)
static Flags detect_system_flags()
#define FloatToUnsigned(f)
void setFlag(Flag f, bool set=true)
Float ieee_double2float(Byte *data)
Int peekInt(unsigned int size)