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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#include <common.hpp>
#include <cstring>
#include <cstdarg>
#include <cstdio>
#include <chrono>
#include <fstream>
#include <iostream>
#include <list>
#include <sstream>
#include <vector>
#include <error.hpp>
#include <texture.hpp>
#ifndef __WIN32__
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#endif // __WIN32__
unsigned int millis(void) {
using namespace std::chrono;
auto now = system_clock::now();
return duration_cast<milliseconds>(now.time_since_epoch()).count();
}
vec2 str2coord(std::string s)
{
auto cpos = s.find(',');
s[cpos] = '\0';
return vec2 (std::stof(s), std::stof(s.substr(cpos + 1)));
}
std::vector<std::string> StringTokenizer(const std::string& str, char delim)
{
std::vector<std::string> tokens;
std::istringstream is (str);
std::string token;
while (getline(is, token, delim))
tokens.emplace_back(token);
return tokens;
}
void DEBUG_prints(const char* file, int line, const char *s,...)
{
va_list args;
printf("%s:%d: ", file, line);
va_start(args, s);
vprintf(s, args);
va_end(args);
}
int getdir(std::string dir, std::list<std::string>& files)
{
#ifndef __WIN32__
auto dp = opendir(dir.c_str());
UserAssert(dp != nullptr, "Couldn\'t open folder: " + dir);
auto dirp = readdir(dp);
while (dirp != nullptr) {
files.emplace_back(dirp->d_name);
dirp = readdir(dp);
}
closedir(dp);
#else
WIN32_FIND_DATA fileData;
auto dirh = FindFirstFile((dir + "/*").c_str(), &fileData);
UserAssert(dirh != INVALID_HANDLE_VALUE, "Couldn\'t open folder: " + dir);
do {
auto fileName = fileData.cFileName;
if (fileName[0] == '.')
continue;
if (!(fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
files.emplace_back(fileName);
} while (FindNextFile(dirh, &fileData));
FindClose(dirh);
#endif // __WIN32__
files.sort();
return 0;
}
std::string readFile(const std::string& path)
{
std::ifstream in (path, std::ios::in);
std::string buffer;
UserAssert(in.is_open(), "Error reading file " + path);
in.seekg(0, in.end);
buffer.resize(in.tellg());
in.seekg(0, in.beg);
in.read(&buffer[0], buffer.size());
in.close();
return buffer;
}
std::vector<std::string> readFileA(const std::string& path)
{
std::ifstream in (path, std::ios::in);
std::vector<std::string> lines;
std::string line;
UserAssert(in.is_open(), "Error reading file " + path);
while(std::getline(in, line))
lines.push_back(line);
in.close();
return lines;
}
|