-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadInputData.cpp
More file actions
69 lines (64 loc) · 1.75 KB
/
readInputData.cpp
File metadata and controls
69 lines (64 loc) · 1.75 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
59
60
61
62
63
64
65
66
67
68
69
#include "readInputData.h"
using namespace std;
int read_sizes(int *linecounter,int *maxlength,char* docfile){
FILE *file = fopen(docfile, "r");
if(file == NULL){
cout<<"Error opening file"<<endl;
return -1;
}
char *line = NULL;
size_t falsebuffer = 0;
int curLength;
while((curLength=getline(&line, &falsebuffer, file))!=-1){
if(*maxlength < curLength)
*maxlength = curLength;
(*linecounter)++;
delete line;
line = NULL;
falsebuffer = 0;
}
fclose(file);
delete line;
if(*linecounter == 0 || *maxlength < 3){
cout<<"Document is too emty and does not meet requirements"<<endl;
return -1;
}
return 1;
}
void split(char* temp, int id, Trienode* trie, Mymap *mymap){
char* token;
token = strtok(temp," \t");
int i = 0;
while(token != NULL){
i++;
trie->insert(token, id);
token = strtok(NULL, " \t");
}
mymap->setlength(id, i);
free(token);
}
int read_input(Mymap *mymap, Trienode* trie, char* docfile){
FILE* file = fopen(docfile, "r");
char* line = NULL;
size_t falsebuffer=0;
int curLength = 0;
char* temp = (char*)malloc(mymap->getbuffersize()*sizeof(char));
for(int i = 0; i < mymap->getsize(); i++){
getline(&line, &falsebuffer, file);
if(mymap->insert(line, i) == -1){
cout << "Document does no meet the requirements!" << endl;
fclose(file);
free(line);
free(temp);
return -1;
}
strcpy(temp, mymap->getDocument(i));
split(temp, i, trie, mymap);
free(line);
line = NULL;
falsebuffer = 0;
}
fclose(file);
free(temp);
return 1;
}