-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.cpp
224 lines (185 loc) · 3.66 KB
/
misc.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
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
#include "misc.h"
#include "stdafx.h"
#include <sys/stat.h>
#include <direct.h>
#include "iniparser/iniparser.h"
#include "iniparser/dictionary.h"
#include "launcher_settings.h"
/**
* Checks if a file exists using fopen
*
* @param char *path - Full path and name of exe
* @return bool - true if file exists, otherwise false (also if file is not avaliable, ie currently open)
*/
bool file_exists(const char *path)
{
if(strlen(path) == 0) return false;
if(path != NULL)
{
FILE *fp = fopen(path, "r");
if(fp != NULL)
{
fclose(fp);
return true;
}
}
return false;
}
bool check_cfg_file(char *dest_buffer, bool create_dir)
{
strcpy(dest_buffer, LauncherSettings::get_exe_pathonly());
strcat(dest_buffer, "\\data");
if (create_dir) {
_mkdir(dest_buffer);
}
if (LauncherSettings::get_exe_type() != EXE_TYPE_CUSTOM) {
strcat(dest_buffer, "\\cmdline.cfg");
} else {
strcat(dest_buffer, "\\cmdline_fso.cfg");
}
return file_exists(dest_buffer);
}
/**
* Gets a files size using stat
*
* @param char *path - Full path of file to check
* @return - Size of file or -1 on error
*/
int get_file_size(const char *path)
{
// This helps us find the size of the exe
struct _stat stat_buffer;
if(_stat( path, &stat_buffer ) == -1)
{
return -1;
}
return stat_buffer.st_size;
}
/**
* Find the file name given a path
*
* @param char *path - Path of file
* @return - Pointer to position in GIVEN PATH (CHAR *) where the actual filename starts
*
*/
const char *get_filename_from_path(const char *path)
{
if(path == NULL)
{
return NULL;
}
// Find the exe name only
char *filename = strrchr(path, '\\');
// No last slash is found, assume full path is filename
if(filename == NULL)
{
return path;
}
else
{
return filename+1;
}
}
/**
* Find the file name given a path
*
* @param char *path - Path of file
* @return - Pointer to position in GIVEN PATH (CHAR *) where the actual filename starts
*
*/
void remove_file_from_path(const char *path)
{
if(path == NULL)
{
return;
}
// Find the exe name only
char *filename = strrchr(path, '\\');
// No last slash is found, assume full path is filename
if(filename != NULL)
{
*filename = '\0';
}
}
bool is_whitespace(char ch)
{
return ch == ' ' || ch == '\n' || ch == '\t';
}
/**
* Remove whitespace surrounding a string, in-place.
*/
void trim(char *str)
{
// shortcut if the first char isn't whitespace
if (!is_whitespace(*str))
{
char *ch = str + strlen(str) - 1;
while (is_whitespace(*ch))
ch--;
*(ch+1) = 0;
}
// otherwise copy the non-whitespace part
else
{
char *temp = strdup(str);
char *start_ch = temp;
while (is_whitespace(*start_ch))
start_ch++;
char *end_ch = temp + strlen(temp) - 1;
while (is_whitespace(*end_ch))
end_ch--;
int len = end_ch - start_ch + 1;
strncpy(str, start_ch, len);
str[len] = 0;
free(temp);
}
}
FILE *ini_open_for_write(const char *filepath, bool append, const char *comment)
{
char *open_type = append ? "a" : "w";
FILE *fp = fopen(filepath, open_type);
if(fp && comment)
{
fprintf(fp, "\n"
"#\n"
"# %s\n"
"#\n", comment);
}
return fp;
}
void ini_write_type(FILE *fp, const char *type)
{
if(type)
{
fprintf(fp, "%s\n", type);
}
}
void ini_write_comment(FILE *fp, const char *comment)
{
if(comment)
{
fprintf(fp, "\n"
"#\n"
"# %s\n"
"#\n", comment);
}
}
void ini_write_data(FILE *fp, const char *type, const char *data)
{
if(type)
{
if(data == NULL)
{
data = "";
}
fprintf(fp, "%s = %s;\n", type, data);
}
}
void ini_write_data(FILE *fp, const char *type, bool data)
{
ini_write_data(fp, type, data ? "true" : "false");
}
void ini_close(FILE *fp)
{
fclose(fp);
}