-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.c
More file actions
58 lines (52 loc) · 1.2 KB
/
csv.c
File metadata and controls
58 lines (52 loc) · 1.2 KB
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
#include "csv.h"
#include "SortList.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
CSV CSV_load_file(char* path){
FILE* File = 0;
File = fopen(path,"rb");
if(File){
int file_size=0, read_bytes;
fseek(File,0,SEEK_END);
file_size=ftell(File);
char* buff = malloc(file_size);
fseek(File,0,SEEK_SET);
read_bytes = fread(buff,1,file_size,File);
if(read_bytes==file_size)
return CSV_load_string(buff, file_size);;
}
CSV csv={0,-1};
return csv;
}
CSV CSV_load_string(char *str,int size){
return CSV_load_string_del(str,size,',');
}
CSV CSV_load_string_del(char *str,int size,char sep)
{
int count = 1;
for(int i=0; i<size; i++){
if(str[i]==sep)
count++;
}
CSV csv= {malloc(sizeof(char*)*count+1),count};
count=0;
csv.data[count]=str;
count++;
for(int i=0; i<size; i++){
if(str[i]==sep){
str[i]=0;
csv.data[count]=str+i+1;
count++;
}
}
str[size]=0;
return csv;
}
void CSV_print(CSV CSV){
for(int i=0; i< CSV.size; i++)
printf("%s ", CSV.data[i]);
}
void CSV_free(CSV CSV){
free(CSV.data);
}