91ea07595e973611e81c115e4853ac8b36262a9f
1 #include "Util/utils.h"
3 #include <cgds/Vector.h>
5 void free_work(Work_t
* work
)
7 free(work
->inputFileName
);
12 void free_result(Result_t
* result
)
14 free(result
->medoids_ID
);
15 free(result
->medoids_ranks
);
19 char readInt(FILE* stream
, int64_t* integer
)
22 char curChar
= fgetc(stream
);
23 int sign
= (curChar
== '-' ? -1 : 1);
24 while (curChar
< '0' || curChar
> '9')
25 curChar
= fgetc(stream
);
26 ungetc(curChar
, stream
);
27 while ((curChar
= fgetc(stream
)) >= '0' && curChar
<= '9')
28 *integer
= 10 * (*integer
) + (int64_t) (curChar
- '0');
33 char readReal(FILE* stream
, float* real
)
36 char nextChar
= readInt(stream
, &integerPart
);
37 int64_t fractionalPart
= 0;
42 while ((nextChar
= fgetc(stream
)) == '0')
44 if (nextChar
>= '0' && nextChar
<= '9')
46 ungetc(nextChar
, stream
);
47 nextChar
= readInt(stream
, &fractionalPart
);
51 if (nextChar
== 'e' || nextChar
== 'E')
52 nextChar
= readInt(stream
, &exponent
);
53 int64_t divisorForFractional
= pow(10, floor(log10(fractionalPart
> 0 ? fractionalPart
: 1))+1);
54 *real
= ( (float)integerPart
55 + (integerPart
< 0 ? -1 : 1) * (float)fractionalPart
/(divisorForFractional
*pow(10,countZeros
)) )
60 // convert n-bytes binary integers to uint32_t
61 uint32_t bInt_to_uint(Byte
* pInteger
)
64 memcpy(&res
, pInteger
, 4);
69 void write_int(uint32_t x
, Byte
* buffer
)
76 for (size_t i
= 0; i
< 4; i
++)
77 buffer
[i
] = u
.bytes
[i
];
80 //WARNING: assuming float is 32bits...
81 // convert 4-bytes binary float to float
82 float bReal_to_float(Byte
* pFloat
)
85 memcpy(&res
, pFloat
, 4);
89 //WARNING: assuming float is 32bits...
90 // serialize double with a NON-portable bytes order
91 void write_real(float x
, Byte
* buffer
)
98 for (size_t i
= 0; i
< 4; i
++)
99 buffer
[i
] = u
.bytes
[i
];
102 // Expected size of a Work message in bytes:
103 uint32_t get_packedWork_length(uint32_t nbSeriesInChunk
)
105 return NCHAR_FNAME
+ 4 + 4*nbSeriesInChunk
+ 4 + 4 + 4;
108 // Expected size of a Result message in bytes: (uint32_t is on 4 bytes)
109 uint32_t get_packedResult_length(uint32_t nbClusters
)
111 return 4 + 4 * nbClusters
+ 4 * nbClusters
;
114 // get metadata: nbSeries
115 uint32_t get_nbSeries(const char* ifileName
)
117 FILE* ifile
= fopen(ifileName
, "rb");
118 fseek(ifile
, 0, SEEK_SET
);
120 size_t lengthRead
= fread(binaryInt
, 4, 1, ifile
);
122 fprintf(stderr
,"Warning: getting nbSeries from truncated binary file.\n");
124 return bInt_to_uint(binaryInt
);
127 // get metadata: tsLength
128 uint32_t get_tsLength(const char* ifileName
)
130 FILE* ifile
= fopen(ifileName
, "rb");
131 fseek(ifile
, 4, SEEK_SET
);
133 size_t lengthRead
= fread(binaryInt
, 4, 1, ifile
);
135 fprintf(stderr
,"Warning: getting tsLength from truncated binary file.\n");
137 return bInt_to_uint(binaryInt
);