- // Use the header to know positions of ID and rawPower
- FILE* ifile = fopen(ifileName, "r");
- uint32_t headerShift = 0;
- char curChar;
- Vector* header = vector_new(char);
- do
- {
- curChar = fgetc(ifile);
- headerShift++;
- if (curChar == '\n' || curChar == '\r')
- {
- // Flush all potential other line feeds
- while (curChar == '\n' || curChar == '\r')
- curChar = fgetc(ifile);
- ungetc(curChar, ifile);
- break;
- }
- vector_push(header, curChar);
- }
- while (1);
- char* headerString = (char*)malloc((vector_size(header) + 1)*sizeof(char));
- VectorIterator* it = vector_get_iterator(header);
- int index = 0;
- while (vectorI_has_data(it))
- {
- vectorI_get(it, headerString[index]);
- vectorI_move_next(it);
- index++;
- }
- vectorI_destroy(it);
- headerString[index] = 0;
- vector_destroy(header);
- int position = 1, posID = 0, posPower = 0;
- char* columnDescriptor = strtok(headerString, ",");
- while (columnDescriptor != NULL)
- {
- if (!strcmp(columnDescriptor,"FK_CCU_ID") || !strcmp(columnDescriptor,"fk_ccu_id"))
- posID = position;
- else if (!strcmp(columnDescriptor,"CPP_PUISSANCE_BRUTE"))
- posPower = position;
- position++;
- columnDescriptor = strtok(NULL, ",");
- }
- free(headerString);
-
- // Estimate tsLength with a scan of the 3 first series
- uint32_t ID=0, lastID=0, refTsLength=0;
- float rawPower = 0.;
- scan_line(ifile, posID, &ID, posPower, &rawPower);
- //'sl' = sample lengths (short because a lot of comparisons then)
- uint32_t* sl = (uint32_t*) calloc(3, sizeof(uint32_t));
- for (int i=0; i<3; i++)
- {
- lastID = ID;
- while (ID == lastID)
- {
- sl[i]++;
- scan_line(ifile, posID, &ID, posPower, &rawPower);
- }
- }
- if (sl[0] <= sl[1] <= sl[2] || sl[1] <= sl[0] <= sl[2])
- refTsLength = sl[2];
- else if (sl[1] <= sl[2] <= sl[0] || sl[2] <= sl[1] <= sl[0])
- refTsLength = sl[0];
- else
- refTsLength = sl[1];
- free(sl);
- //go back at the beginning of the first series (ready to read '\n'...)
- fseek(ifile, headerShift-1, SEEK_SET);