-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstdafx.cpp
133 lines (105 loc) · 2.66 KB
/
stdafx.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
#include "stdafx.h"
void Q_strcat(char *dest, int size, const char *src);
void Q_strncpyz(char *dest, const char *src, int destsize);
#include <ShlObj.h>
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "Comdlg32.lib")
#include "Commdlg.h"
int codversion = COD_UNKNOWN;
std::string file_get_contents(const char *fn) {
FILE *fp = NULL;
fp = fopen(fn, "r");
if (fp == NULL)
return "";
char *buf = NULL;
size_t fs = 0;
fseek(fp, 0, SEEK_END);
fs = ftell(fp);
rewind(fp);
buf = (char*)malloc(fs);
if (fread(buf, 1, fs, fp) != fs) {
fclose(fp);
return "";
}
std::string res(buf);
free(buf);
fclose(fp);
return res;
}
std::string GetOpenFileNameS(HWND owner)
{
char buffer[MAX_PATH] = "";
OPENFILENAME ofn = { 0 };
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = owner;
ofn.lpstrFilter = "JPG\0*.jpg\0All Files\0*\0\0";
ofn.nFilterIndex = 1;
ofn.lpstrFile = buffer;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;
if (!GetOpenFileName(&ofn))
return "";
return buffer;
}
std::string GetLastErrorAsString()
{
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return "";
LPSTR messageBuffer = NULL;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return message;
}
int Sys_GetModulePathInfo(HMODULE module, char **path, char **filename, char **extension) {
int sep = '/';
static char szFileName[MAX_PATH + 1];
if (path)
*path = NULL;
if (filename)
*filename = NULL;
if (extension)
*extension = NULL;
GetModuleFileNameA(module, szFileName, MAX_PATH);
char *fn = strrchr(szFileName, sep);
if (fn == nullptr) {
sep = '\\';
fn = strrchr(szFileName, sep);
}
if (fn != NULL) {
*fn++ = 0;
char *ext = strrchr(fn, '.');
if (ext != NULL) {
if (fn != ext) {
if (extension)
*ext++ = 0;
if (path)
*path = szFileName;
if (filename)
*filename = fn;
if (extension)
*extension = ext;
}
}
}
return sep;
}
const char *Sys_GetAppDataFolder(char *szPath, int dwLen, bool create) {
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, szPath))) {
Q_strncpyz(szPath, szPath, dwLen);
Q_strcat(szPath, dwLen, "\\CoDExtended");
if (!create)
return szPath;
if (!PathFileExists(szPath)) {
if (ERROR_SUCCESS != SHCreateDirectoryEx(NULL, szPath, NULL)) {
return NULL;
}
}
}
return szPath;
}