-
Notifications
You must be signed in to change notification settings - Fork 89
/
platform_common.h
281 lines (228 loc) · 9.08 KB
/
platform_common.h
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2005 Matthias Braun <matze@braunis.de>
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _SHARED_PLATFORM_PLATFORMCOMMON_H_
#define _SHARED_PLATFORM_PLATFORMCOMMON_H_
#ifdef WIN32
#include <windows.h>
#endif
#include <string>
#include <vector>
#include <stdexcept>
#include <list>
#include "types.h"
#include "checksum.h"
#include <utility>
#include <SDL.h>
#include <map>
#include "leak_dumper.h"
#if (defined WIN32) && !(defined snprintf)
#define snprintf _snprintf
#endif
using std::string;
using std::vector;
using std::list;
using std::exception;
using Shared::Platform::int64;
using Shared::Util::Checksum;
namespace Shared { namespace PlatformCommon {
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define CODE_AT_LINE __FILE__ ":" TOSTRING(__LINE__)
#define CODE_AT_LINE_X(x) __FILE__ ":" TOSTRING(__LINE__) ":" TOSTRING(x)
static const int IGNORE_CMD_RESULT_VALUE = -999999;
// keycode constants (unfortunately designed after DirectInput and therefore not
// very specific)
// They also have to fit into a char. The positive numbers seem to be equal
// to ascii, for the rest we have to find sensefull mappings from SDL (which is
// alot more fine grained like left/right control instead of just control...)
const char vkAdd = -1;
const char vkSubtract = -2;
const char vkAlt = -3;
const char vkControl = -4;
const char vkShift = -5;
const char vkEscape = -6;
const char vkUp = -7;
const char vkLeft = -8;
const char vkRight = -9;
const char vkDown = -10;
const char vkReturn = -11;
const char vkBack = -12;
const char vkTab = -13;
const char vkF1 = -14;
const char vkF2 = -15;
const char vkF3 = -16;
const char vkF4 = -17;
const char vkF5 = -18;
const char vkF6 = -19;
const char vkF7 = -20;
const char vkF8 = -21;
const char vkF9 = -22;
const char vkF10 = -23;
const char vkF11 = -24;
const char vkF12 = -25;
const char vkDelete = -26;
const char vkPrint = -27;
const char vkPause = -29;
// =====================================================
// class PerformanceTimer
// =====================================================
class PerformanceTimer {
private:
Uint32 lastTicks;
Uint32 updateTicks;
int times; // number of consecutive times
int maxTimes; // maximum number consecutive times
public:
void init(float fps, int maxTimes= -1);
bool isTime();
void reset();
};
// =====================================================
// class Chrono
// =====================================================
class Chrono {
private:
Uint32 startCount;
Uint32 accumCount;
Uint32 freq;
bool stopped;
Uint32 lastStartCount;
Uint32 lastTickCount;
int64 lastResult;
int lastMultiplier;
bool lastStopped;
public:
Chrono(bool autoStart=false);
void start();
void stop();
void reset();
int64 getMicros();
int64 getMillis();
int64 getSeconds();
static int64 getCurTicks();
static int64 getCurMillis();
private:
int64 queryCounter(int multiplier);
};
// =====================================================
// class ModeInfo
// =====================================================
class ModeInfo {
public:
int width;
int height;
int depth;
ModeInfo(int width, int height, int depth);
bool operator< (const ModeInfo &j) const {
if(this->width < j.width) {
return true;
}
else if(this->width == j.width && this->height < j.height) {
return true;
}
else if(this->width == j.width &&
this->height == j.height &&
this->depth < j.depth) {
return true;
}
return false;
}
string getString() const;
};
// =====================================================
// Misc
// =====================================================
void Tokenize(const string& str,vector<string>& tokens,const string& delimiters = " ");
bool isdir(const char *path);
void findDirs(string path, vector<string> &results, bool errorOnNotFound,bool keepDuplicates);
void findDirs(const vector<string> &paths, vector<string> &results, bool errorOnNotFound=false,bool keepDuplicates=false);
void findAll(const vector<string> &paths, const string &fileFilter, vector<string> &results, bool cutExtension=false, bool errorOnNotFound=true,bool keepDuplicates=false);
void findAll(const string &path, vector<string> &results, bool cutExtension=false, bool errorOnNotFound=true);
vector<string> getFolderTreeContentsListRecursively(const string &path, const string &filterFileExt, bool includeFolders=false, vector<string> *recursiveMap=NULL);
string getCRCCacheFilePath();
void setCRCCacheFilePath(string path);
std::pair<string,string> getFolderTreeContentsCheckSumCacheKey(vector<string> paths, string pathSearchString, const string filterFileExt);
void clearFolderTreeContentsCheckSum(vector<string> paths, string pathSearchString, const string filterFileExt);
int32 getFolderTreeContentsCheckSumRecursively(vector<string> paths, string pathSearchString, const string filterFileExt, Checksum *recursiveChecksum,bool forceNoCache=false);
time_t getFolderTreeContentsCheckSumRecursivelyLastGenerated(vector<string> paths, string pathSearchString, const string filterFileExt);
std::pair<string,string> getFolderTreeContentsCheckSumCacheKey(const string &path, const string filterFileExt);
void clearFolderTreeContentsCheckSum(const string &path, const string filterFileExt);
int32 getFolderTreeContentsCheckSumRecursively(const string &path, const string &filterFileExt, Checksum *recursiveChecksum,bool forceNoCache=false);
std::pair<string,string> getFolderTreeContentsCheckSumListCacheKey(vector<string> paths, string pathSearchString, const string filterFileExt);
void clearFolderTreeContentsCheckSumList(vector<string> paths, string pathSearchString, const string filterFileExt);
vector<std::pair<string,int32> > getFolderTreeContentsCheckSumListRecursively(vector<string> paths, string pathSearchString, const string filterFileExt, vector<std::pair<string,int32> > *recursiveMap);
std::pair<string,string> getFolderTreeContentsCheckSumListCacheKey(const string &path, const string filterFileExt);
void clearFolderTreeContentsCheckSumList(const string &path, const string filterFileExt);
vector<std::pair<string,int32> > getFolderTreeContentsCheckSumListRecursively(const string &path, const string &filterFileExt, vector<std::pair<string,int32> > *recursiveMap);
void createDirectoryPaths(string Path);
string extractFileFromDirectoryPath(string filename);
string extractDirectoryPathFromFile(string filename);
string extractLastDirectoryFromPath(string Path);
string extractExtension(const string& filename);
void getFullscreenVideoModes(vector<ModeInfo> *modeinfos,bool isFullscreen);
void getFullscreenVideoInfo(int &colorBits,int &screenWidth,int &screenHeight,bool isFullscreen);
bool changeVideoMode(int resH, int resW, int colorBits, int refreshFrequency);
void restoreVideoMode(bool exitingApp=false);
bool StartsWith(const std::string &str, const std::string &key);
bool EndsWith(const string &str, const string& key);
void endPathWithSlash(string &path, bool requireOSSlash=false);
void trimPathWithStartingSlash(string &path);
void updatePathClimbingParts(string &path);
string formatPath(string path);
string replaceAll(string& context, const string& from, const string& to);
bool removeFile(string file);
bool renameFile(string oldFile, string newFile);
void removeFolder(const string path);
off_t getFileSize(string filename);
bool searchAndReplaceTextInFile(string fileName, string findText, string replaceText, bool simulateOnly);
void copyFileTo(string fromFileName, string toFileName);
int getScreenW();
int getScreenH();
void sleep(int millis);
bool isCursorShowing();
void showCursor(bool b);
bool isKeyDown(int virtualKey);
//bool isKeyDown(SDLKey key);
string getCommandLine();
#define SPACES " "
inline string trim_right (const string & s, const string & t = SPACES) {
string d (s);
string::size_type i (d.find_last_not_of (t));
if (i == string::npos)
return "";
else
return d.erase (d.find_last_not_of (t) + 1) ;
} // end of trim_right
inline string trim_left (const string & s, const string & t = SPACES) {
string d (s);
return d.erase (0, s.find_first_not_of (t)) ;
} // end of trim_left
inline string trim (const string & s, const string & t = SPACES) {
string d (s);
return trim_left (trim_right (d, t), t) ;
} // end of trim
string getFullFileArchiveExtractCommand(string fileArchiveExtractCommand,
string fileArchiveExtractCommandParameters, string outputpath, string archivename);
bool executeShellCommand(string cmd,int expectedResult=IGNORE_CMD_RESULT_VALUE);
string executable_path(string exeName,bool includeExeNameInPath=false);
bool valid_utf8_file(const char* file_name);
class ValueCheckerVault {
protected:
std::map<const void *,int32> vaultList;
void addItemToVault(const void *ptr,int value);
void checkItemInVault(const void *ptr,int value) const;
public:
ValueCheckerVault() {
vaultList.clear();
}
};
}}//end namespace
#endif