File: Util.h

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (231 lines) | stat: -rwxr-xr-x 7,200 bytes parent folder | download
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
/*
	Copyright (c) 2008 Robin Vobruba <hoijui.quaero@gmail.com>

	This program is free software; you can redistribute it 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.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _CUTILS_UTIL_H
#define _CUTILS_UTIL_H

#ifdef __cplusplus
extern "C" {
#endif

#ifndef _MSC_VER
	#include <stdbool.h>
#endif

// BEGINN: String realated functions

/**
 * Allocates fresh memory for storing a C string of the specified length.
 */
char* util_allocStr(unsigned int length);

/**
 * Allocates fresh memory and copies the supplied string into it.
 * @return  the freshly allocated memory containing the string,
 *          or NULL, if the arguemtn was NULL.
 */
char* util_allocStrCpy(const char* toCopy);

/**
 * Allocates fresh memory and copies a part of the supplied string into it.
 */
char* util_allocStrSubCpy(const char* toCopy, int fromPos, int toPos);
/**
 * Allocates fresh memory and copies a part of the supplied string into it.
 */
char* util_allocStrSubCpyByPointers(const char* toCopy,
		const char* fromPos, const char* toPos);

/**
 * Concatenates a variable number of C strings into newly allocated memory.
 * The number of strings to concatenate has to be supplied as first argument.
 */
char* util_allocStrCat(int numParts, const char* first, ...);
/**
 * Concatenates a variable number of C strings into newly allocated memory.
 * The last argument has to be NULL, as there would be no way to determine
 * the number of arguments otherwise.
 */
char* util_allocStrCat_nt(const char* first, ...);

/**
 * Concatenates a variable number of C strings,
 * which represent file system parts (eg. dirs or files)
 * into newly allocated memory.
 * The number of parts to concatenate has to be supplied as first argument.
 * sample call:
 * (3, "/home/user/tmp", "appName/", "/i/am/fine")
 * output:
 * "/home/user/tmp/appName/i/am/fine"
 */
char* util_allocStrCatFSPath(int numParts, const char* first, ...);

bool util_isWhiteSpace(char c);

void util_strLeftTrim(char* toTrim);
void util_strRightTrim(char* toTrim);
void util_strTrim(char* toTrim);
char* util_allocStrTrimed(const char* toTrim);

void util_strReplaceChar(char* toChange, char toFind, char replacer);
char* util_allocStrReplaceStr(const char* toChange, const char* toFind,
		const char* replacer);

bool util_startsWith(const char* str, const char* prefix);
bool util_endsWith(const char* str, const char* suffix);

bool util_strToBool(const char* str);

// END: String realated functions


// BEGINN: File system realated functions

/**
 * Checks whether a string contians an absolute path.
 * The corresponding matcher-regexes are:
 * on Windows: "^.:(\\|/).*"
 * otherwise:  "^/.*"
 *
 * @param  path  the dir or file path to be checked for absoluteness
 * @return  true if the path is absolute; it does not have to exist
 */
bool util_isPathAbsolute(const char* path);

/**
 * Lists all files found below a directory.
 *
 * Example:
 * char* foundFiles[64];
 * (".../AI/Interface/Java/0.1/jlib/", ".jar", foundFiles, true, 64)
 *    ".../AI/Interface/Java/0.1/jlib/vecmath.jar"
 *    ".../AI/Interface/Java/0.1/jlib/jna/jna.jar"
 *    ".../AI/Interface/Java/0.1/jlib/jna/linux-i386.jar"
 *
 * @param  suffix  files to list have to end with this suffix, eg. ".jar"
 * @param  recursive  list files recursively
 */
unsigned int util_listFiles(const char* dir, const char* suffix,
		char** fileNames, bool recursive, const unsigned int maxFileNames);

/**
 * Removes a slash ('/' or '\') from the end of a string, if there is one.
 */
void util_removeTrailingSlash(char* fsPath);

/**
 * Returns true if the file specified in filePath exists.
 */
bool util_fileExists(const char* filePath);

/**
 * Creates the dir specified in dirPath, if its parent directory exists
 * and we have write access to it.
 *
 * @param  recursive  whether ancestor dirs shall be created aswell,
 *                    if they do not yet exist
 * @return true if the dir could be created or existed already.
 */
bool util_makeDir(const char* dirPath, bool recursive);

/**
 * Saves the parent directory of a file or dir specified in path
 * into parentPath.
 *
 * Example:
 * before: "/home/user/games/spring/AI/Skirmish/RAI/0.1/log.txt"
 * after:  "/home/user/games/spring/AI/Skirmish/RAI/0.1"
 */
bool util_getParentDir(char* path);

/**
 * Finds a file or directory under dirs with the relativeFilePath
 * and saves the resulting path in absoluteFilePath.
 * If searchOnlyWriteable is set, only the first entry in dirs
 * is used for the search, as it is assumed to contain
 * the writable directory.
 *
 * @return  true if the file exists
 */
bool util_findFile(const char* dirs[], unsigned int numDirs,
		const char* relativeFilePath, char* absoluteFilePath,
		bool searchOnlyWriteable);

/**
 * Finds a directory under dirs with the relativeDirPath
 * and saves the resulting path in absoluteDirPath.
 * If searchOnlyWriteable is set, only the first entry in dirs
 * is used for the search, as it is assumed to contain
 * the writable directory.
 *
 * @return  true if the file existed or was created
 */
bool util_findDir(const char* dirs[], unsigned int numDirs,
		const char* relativeDirPath, char* absoluteDirPath,
		bool searchOnlyWriteable, bool create);

/**
 * Parses a properties file into a C [string, string] map.
 * Lines that contain only white-spaces and lines beginning
 * with '#' or ';' are ignored.
 * Regex for a valid property:
 * ^[\\w]*[^;#][^=]*=.+$
 * The strings in the \<keys\> and \<values\> arrays have to
 * be freed by the callee.
 */
int util_parsePropertiesFile(const char* propertiesFile,
		const char* keys[], const char* values[], int maxProperties);

// END: File system realated functions


/**
 * Find the value assigned to a key in a C [string, string] map.
 * @return  the assigned value or NULL if the key was not found
 */
const char* util_map_getValueByKey(
		unsigned int mapSize,
		const char** mapKeys, const char** mapValues,
		const char* key);

/**
 * Reestablishes the environment that is needed by the engine.
 * Whenever an AI Iterface, or a VM running in it,
 * changes the programm environment, this function should be called
 * before CPU controll goes back to the engine.
 * Currently handled changes:
 * - changing the state of the FPU
 *
 * For an exampe of when to call this function,
 * look for it in the Java AI Interface native code.
 * If you use this function, you will need to link against
 * the streflop static library.
 */
void util_resetEngineEnv();

/**
 * Free memory.
 * Some of the other functions will not work anymore,
 * after calling this function.
 */
void util_finalize();

#ifdef __cplusplus
} // extern "C"
#endif

#endif // _CUTILS_UTIL_H