-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
38 lines (35 loc) · 992 Bytes
/
util.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
#include "windows.h"
#include "util.h"
bool isNumeric(std::string& str)
{
for(unsigned int i=0; i < str.length(); i++) {
if (isdigit(str.at(i)) == 0)
return false;
}
return true;
}
bool WinReadFile(std::string& path, std::string& result)
{
char buffer[10000];
DWORD bytesRead;
HANDLE fileHandle;
fileHandle = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle == INVALID_HANDLE_VALUE)
return false;
for(;;)
{
if (!ReadFile(fileHandle, &buffer, sizeof(buffer), &bytesRead, NULL)) {
CloseHandle(fileHandle);
return false;
}
for(unsigned int i=0; i < bytesRead; i++) { //std::string::append does not work because it treats the buffer as C string with a NULL terminator
result += buffer[i];
}
if (bytesRead < sizeof(buffer)) {
CloseHandle(fileHandle);
return true;
}
}
CloseHandle(fileHandle);
return false; //this should never happen
}