-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
71 lines (64 loc) · 1.49 KB
/
utils.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
//
// Created by Ian Parker on 23/10/2024.
//
#include "utils.h"
using namespace std;
vector<string> splitString(string line, char splitChar)
{
vector<string> parts;
while (!line.empty())
{
size_t pos = line.find(splitChar);
if (pos == string::npos)
{
pos = line.length();
if (pos == 0)
{
break;
}
}
if (pos >= 1)
{
string part = line.substr(0, pos);
trim(part);
parts.push_back(part);
}
if (pos == line.length())
{
break;
}
line = line.substr(pos + 1);
}
return parts;
}
vector<vector<string>> readTextFile(string filename, bool split)
{
vector<vector<string>> result;
FILE* fd = fopen(filename.c_str(), "r");
if (fd == nullptr)
{
printf("readTextFile: Failed to open file %s\n", filename.c_str());
return result;
}
char lineBuffer[2048];
while (fgets(lineBuffer, 2048, fd) != nullptr)
{
int len = strlen(lineBuffer);
while (len > 0 && (lineBuffer[len-1] == '\r' || lineBuffer[len-1] == '\n'))
{
lineBuffer[len - 1] = 0;
len--;
}
printf("Line: %s\n", lineBuffer);
if (split)
{
result.push_back(splitString(lineBuffer, ' '));
}
else
{
result.push_back({lineBuffer});
}
}
fclose(fd);
return result;
}