complete first draft of package
[epclust.git] / old_C_code / stage1 / src / Util / utils.c
1 #include "Util/utils.h"
2 #include <math.h>
3 #include <cgds/Vector.h>
4
5 void free_work(Work_t* work)
6 {
7 free(work->inputFileName);
8 free(work->ranks);
9 free(work);
10 }
11
12 void free_result(Result_t* result)
13 {
14 free(result->medoids_ID);
15 free(result->medoids_ranks);
16 free(result);
17 }
18
19 char readInt(FILE* stream, int64_t* integer)
20 {
21 *integer = 0;
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');
29 (*integer) *= sign;
30 return curChar;
31 }
32
33 char readReal(FILE* stream, float* real)
34 {
35 int64_t integerPart;
36 char nextChar = readInt(stream, &integerPart);
37 int64_t fractionalPart = 0;
38 int countZeros = 0;
39 if (nextChar == '.')
40 {
41 //need to count zeros
42 while ((nextChar = fgetc(stream)) == '0')
43 countZeros++;
44 if (nextChar >= '0' && nextChar <= '9')
45 {
46 ungetc(nextChar, stream);
47 nextChar = readInt(stream, &fractionalPart);
48 }
49 }
50 int64_t exponent = 0;
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)) )
56 * pow(10,exponent);
57 return nextChar;
58 }
59
60 // convert n-bytes binary integers to uint32_t
61 uint32_t bInt_to_uint(Byte* pInteger)
62 {
63 uint32_t res;
64 memcpy(&res, pInteger, 4);
65 return res;
66 }
67
68 // serialize integers
69 void write_int(uint32_t x, Byte* buffer)
70 {
71 union {
72 uint32_t i;
73 char bytes[4];
74 } u;
75 u.i = x;
76 for (size_t i = 0; i < 4; i++)
77 buffer[i] = u.bytes[i];
78 }
79
80 //WARNING: assuming float is 32bits...
81 // convert 4-bytes binary float to float
82 float bReal_to_float(Byte* pFloat)
83 {
84 float res;
85 memcpy(&res, pFloat, 4);
86 return res;
87 }
88
89 //WARNING: assuming float is 32bits...
90 // serialize double with a NON-portable bytes order
91 void write_real(float x, Byte* buffer)
92 {
93 union {
94 float d;
95 char bytes[4];
96 } u;
97 u.d = x;
98 for (size_t i = 0; i < 4; i++)
99 buffer[i] = u.bytes[i];
100 }
101
102 // Expected size of a Work message in bytes:
103 uint32_t get_packedWork_length(uint32_t nbSeriesInChunk)
104 {
105 return NCHAR_FNAME + 4 + 4*nbSeriesInChunk + 4 + 4 + 4;
106 }
107
108 // Expected size of a Result message in bytes: (uint32_t is on 4 bytes)
109 uint32_t get_packedResult_length(uint32_t nbClusters)
110 {
111 return 4 + 4 * nbClusters + 4 * nbClusters;
112 }
113
114 // get metadata: nbSeries
115 uint32_t get_nbSeries(const char* ifileName)
116 {
117 FILE* ifile = fopen(ifileName, "rb");
118 fseek(ifile, 0, SEEK_SET);
119 Byte binaryInt[4];
120 size_t lengthRead = fread(binaryInt, 4, 1, ifile);
121 if (lengthRead != 1)
122 fprintf(stderr,"Warning: getting nbSeries from truncated binary file.\n");
123 fclose(ifile);
124 return bInt_to_uint(binaryInt);
125 }
126
127 // get metadata: tsLength
128 uint32_t get_tsLength(const char* ifileName)
129 {
130 FILE* ifile = fopen(ifileName, "rb");
131 fseek(ifile, 4, SEEK_SET);
132 Byte binaryInt[4];
133 size_t lengthRead = fread(binaryInt, 4, 1, ifile);
134 if (lengthRead != 1)
135 fprintf(stderr,"Warning: getting tsLength from truncated binary file.\n");
136 fclose(ifile);
137 return bInt_to_uint(binaryInt);
138 }