-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_loader.cpp
More file actions
110 lines (93 loc) · 3.08 KB
/
Copy pathtext_loader.cpp
File metadata and controls
110 lines (93 loc) · 3.08 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
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
102
103
104
105
106
107
108
109
110
#include "text_loader.h"
#include <fstream>
#include <iostream>
static inline std::vector<std::string> SplitString(const std::string &s, char delim);
FloatData::FloatData(float* floats, unsigned int count, const std::string& tag) :
floats(floats),
count(count),
tag(tag) { }
std::string FloatData::print() {
std::string str = "";
for (unsigned int i = 0; i < count; i++)
str += std::to_string(floats[i]) + " ";
return str;
}
UIntData::UIntData(unsigned short* ints, unsigned int count, const std::string& tag) :
ints(ints),
count(count),
tag(tag) { }
std::string UIntData::print() {
std::string str = "";
for (unsigned int i = 0; i < count; i++)
str += std::to_string(ints[i]) + " ";
return str;
}
TextData::TextData(const std::string& fileName) {
std::string fullFileName = "./Resources/" + fileName + ".tvd";
std::ifstream file;
file.open(fullFileName.c_str());
std::string line;
if (file.is_open()) {
while (file.good()) {
getline(file, line);
if (line.length() < 2)
continue;
std::vector<std::string> parts = SplitString(line, ' ');
if (parts[0] == "float") {
unsigned int floatCount = static_cast<unsigned int>(parts.size() - 2);
float* floats = new float[floatCount];
for (unsigned int i = 0; i < floatCount; ++i)
floats[i] = std::stof(parts[i + 2], nullptr);
floatData.push_back(FloatData(floats, floatCount, parts[1]));
} else if (parts[0] == "uint") {
unsigned int intCount = static_cast<unsigned int>(parts.size() - 2);
unsigned short* ints = new unsigned short[intCount];
for (unsigned int i = 0; i < intCount; ++i)
ints[i] = static_cast<unsigned short>(std::stoul(parts[i + 2], nullptr, 10));
uintData.push_back(UIntData(ints, intCount, parts[1]));
} else {
std::cerr << "Unknown data type \"" << parts[0] << "\", skipping entry..." << std::endl;
}
}
} else {
std::cerr << "Unable to load mesh: " << fullFileName << std::endl;
}
}
TextData::~TextData() {
for (unsigned int i = 0; i < floatData.size(); ++i)
delete[] floatData[i].floats;
for (unsigned int i = 0; i < uintData.size(); ++i)
delete[] uintData[i].ints;
}
FloatData TextData::GetFloats(const std::string& tag) const {
for (unsigned int i = 0; i < floatData.size(); ++i)
if (floatData[i].tag == tag)
return floatData[i];
//std::cerr << "No float array with tag " << tag << " found!" << std::endl;
return FloatData(nullptr, 0, "nullptr");
}
UIntData TextData::GetUInts(const std::string& tag) const {
for (unsigned int i = 0; i < uintData.size(); ++i)
if (uintData[i].tag == tag)
return uintData[i];
//std::cerr << "No int array with tag " << tag << " found!" << std::endl;
return UIntData(nullptr, 0, "nullptr");
}
static inline std::vector<std::string> SplitString(const std::string &s, char delim) {
std::vector<std::string> elems;
const char* cstr = s.c_str();
size_t strLength = s.length();
size_t start = 0;
size_t end = 0;
while (end <= strLength) {
while (end <= strLength) {
if (cstr[end] == delim)
break;
end++;
}
elems.push_back(s.substr(start, end - start));
start = end + 1;
end = start;
}
return elems;
}