return curChar;
}
-char readReal(FILE* stream, Real* real)
+char readReal(FILE* stream, float* real)
{
int64_t integerPart;
char nextChar = readInt(stream, &integerPart);
if (nextChar == 'e' || nextChar == 'E')
nextChar = readInt(stream, &exponent);
int64_t divisorForFractional = pow(10, floor(log10(fractionalPart > 0 ? fractionalPart : 1))+1);
- *real = ( (Real)integerPart
- + (integerPart < 0 ? -1 : 1) * (Real)fractionalPart/(divisorForFractional*pow(10,countZeros)) )
+ *real = ( (float)integerPart
+ + (integerPart < 0 ? -1 : 1) * (float)fractionalPart/(divisorForFractional*pow(10,countZeros)) )
* pow(10,exponent);
return nextChar;
}
// convert n-bytes binary integers to uint32_t
-uint32_t bInt_to_uint(Byte* pInteger, size_t bytesCount)
+uint32_t bInt_to_uint(Byte* pInteger)
{
- uint32_t integer = 0;
- for (size_t i = 0; i < bytesCount; i++)
- integer += ((uint32_t) (pInteger[i])) << (i << 3);
- return integer;
+ uint32_t res;
+ memcpy(&res, pInteger, 4);
+ return res;
}
-// serialize integers with a portable bytes order
-void write_int(uint32_t integer, size_t bytesCount, Byte* buffer)
+// serialize integers
+void write_int(uint32_t x, Byte* buffer)
{
- Byte chunk;
- // write from left to right, from least to most significative bit
- for (size_t i = 0; i < bytesCount; i++)
- {
- chunk = (integer >> (i << 3)) & 0xFF;
- buffer[i] = chunk;
- }
+ union {
+ uint32_t i;
+ char bytes[4];
+ } u;
+ u.i = x;
+ for (size_t i = 0; i < 4; i++)
+ buffer[i] = u.bytes[i];
+}
+
+//WARNING: assuming float is 32bits...
+// convert 4-bytes binary float to float
+float bReal_to_float(Byte* pFloat)
+{
+ float res;
+ memcpy(&res, pFloat, 4);
+ return res;
+}
+
+//WARNING: assuming float is 32bits...
+// serialize double with a NON-portable bytes order
+void write_real(float x, Byte* buffer)
+{
+ union {
+ float d;
+ char bytes[4];
+ } u;
+ u.d = x;
+ for (size_t i = 0; i < 4; i++)
+ buffer[i] = u.bytes[i];
}
// Expected size of a Work message in bytes:
if (lengthRead != 1)
fprintf(stderr,"Warning: getting nbSeries from truncated binary file.\n");
fclose(ifile);
- return bInt_to_uint(binaryInt, 4);
+ return bInt_to_uint(binaryInt);
}
// get metadata: tsLength
if (lengthRead != 1)
fprintf(stderr,"Warning: getting tsLength from truncated binary file.\n");
fclose(ifile);
- return bInt_to_uint(binaryInt, 4);
+ return bInt_to_uint(binaryInt);
}