-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
61 lines (58 loc) · 1.65 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "matlib.h"
Matrix loadCsv(FILE* file, int bufferSize, int numBufferSize, int nElements);
Matrix loadCsv(FILE* file,int bufferSize,int numBufferSize, int nElements){
if(bufferSize == 0)
bufferSize = 32000;
if(numBufferSize ==0)
numBufferSize = 20;
if(nElements ==0)
nElements = 1024;
char buffer[bufferSize], numBuffer[numBufferSize];
int numIndex = 0, width = 0,digitIdx = 0 ,nread;
bool hitNew = false;
float* elements = (float*) malloc(sizeof(float)*nElements);
while((nread = fread(buffer, 1, bufferSize, file))>0){
for(int i =0; i<bufferSize; i++){
if(isdigit(buffer[i]) || buffer[i] == '.'){
if(digitIdx<numBufferSize){
numBuffer[digitIdx] = buffer[i];
digitIdx++;
}else
printf("numBuffer too small!\n");
}
else if(buffer[i] == ',' || buffer[i] == '\n'){
if(!hitNew)
switch(buffer[i]){
case '\n':
hitNew = true;
case ',':
width++;
break;
}
numBuffer[digitIdx] = '\0';
if(numIndex>=nElements){
nElements += (int)(nElements/4);
elements = (float*)realloc(elements, sizeof(float)*nElements);
}
elements[numIndex] = strtof(numBuffer,NULL);
memset(numBuffer, 0, numBufferSize);
digitIdx = 0;
numIndex++;
}else if(buffer[i]!=' ')
break;
}
}
if(nElements>(numIndex)){
elements = (float*)realloc(elements, (numIndex+1)*sizeof(float));
}
int height = (numIndex)/width;
if((numIndex)%width>0)
printf("Matrix loaded is not even, mod %i\n", numIndex%width);
Matrix mat = {.elements = elements, .height = height, .width = width};
return mat;
}