-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordreader.cpp
101 lines (94 loc) · 2.75 KB
/
wordreader.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "wordreader.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <cstring>
using std::string;
using std::vector;
using std::ifstream;
WordReader::WordReader(int playerCount, std::string filename) : _playerCount(playerCount) {
sentences.resize(playerCount);
std::cout << "initializing wordreader" << std::endl;
for(int i = 1; i <= playerCount; i++){
std::stringstream ss;
ss << filename << "." << i << ".txt";
std::cout << "looking for file " << ss.str() << std::endl;
ifstream file(ss.str().c_str());
if(file.is_open()){
string line;
int sentencenumber = 1;
int wordnumber = 0;
Sentence sentence(1);
while(file.good()){
getline(file, line);
if(line.length() < 3) {
continue;
}
if(line.find_first_of("#") != string::npos){
continue;
}
char * cstr = new char [line.size()+1];
strcpy (cstr, line.c_str());
const char * part = strtok(cstr, ";");
if(part != NULL){
if(atoi(part) > sentencenumber){
// Save sentence
sentences[i-1].push_back(sentence);
sentencenumber = atoi(part);
sentence = Sentence(sentencenumber);
}
}
else{
std::cout << "Incomplete line in file targetwords." << i << ".txt" << " sentence " << sentencenumber << std::endl;
}
part = strtok(NULL, ";");
if(part != NULL){
wordnumber = atoi(part);
}
else{
std::cout << "Incomplete line in file targetwords." << i << ".txt" << " sentence " << sentencenumber << std::endl;
}
part = strtok(NULL, ";");
string word;
if(part != NULL){
word = part;
}
else{
std::cout << "Incomplete line in file targetwords." << i << ".txt" << " sentence " << sentencenumber << std::endl;
}
float width = 0.0;
part = strtok(NULL, ";");
if(part != NULL){
width = atof(part);
}
else{
std::cout << "Incomplete line in file targetwords." << i << ".txt" << " sentence " << sentencenumber << std::endl;
}
float distance = 0.0;
part = strtok(NULL, ";");
if(part != NULL){
distance = atof(part);
}
else{
std::cout << "Incomplete line in file targetwords." << i << ".txt" << " sentence " << sentencenumber << std::endl;
}
TargetWord tw = {wordnumber, word, width, distance};
sentence.add(tw);
//std::cout << "read word: " << tw.word << " w: " << tw.width << " d: " << tw.distance << std::endl;
}
file.close();
sentences[i-1].push_back(sentence);
}
else{
std::cout << "file not found" << std::endl;
}
}
};
WordReader::~WordReader(){}
// Returns null if no such word
TargetWord WordReader::getWord(int player, int sentenceNumber, int wordNumber){
return sentences[player][sentenceNumber-1][wordNumber-1];
}