/
futils.cpp
87 lines (72 loc) · 1.44 KB
/
futils.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
// FUTILS.CPP
// Copyright © 1999 Joe Riedel, Nick Randal.
// Author: Joe Riedel.
#include "futils.h"
#include <io.h>
bool bFirstFind=true;
struct _finddata_t fileinfo;
int nHandle = -1;
// Bloads a file into memory.
unsigned char* BloadFile(CString sFilename, int& nNumBytes)
{
CFile hFile;
nNumBytes = 0;
if(!hFile.Open(sFilename, CFile::modeRead | CFile::shareDenyWrite | CFile::typeBinary))
return NULL;
nNumBytes = hFile.GetLength();
if(nNumBytes == 0)
{
hFile.Close();
return NULL;
}
// Allocate.
unsigned char* pData = new unsigned char[nNumBytes];
if(pData == NULL)
{
hFile.Close();
nNumBytes = 0;
return NULL;
}
// Load the file.
if(hFile.Read(pData, nNumBytes) != (unsigned int)nNumBytes)
{
hFile.Close();
nNumBytes = 0;
delete[] pData;
return NULL;
}
hFile.Close();
return pData;
}
// Begins a file search.
bool BeginFileSearch(CString sFileExt)
{
bFirstFind = true;
nHandle = _findfirst(sFileExt, &fileinfo);
if(nHandle == -1)
return false;
return true;
}
// Find the next file.
bool GetNextFile(CString& sFilename)
{
if(bFirstFind)
{
bFirstFind = false;
sFilename = fileinfo.name;
return (fileinfo.attrib & _A_SUBDIR) != 0 ? true : false;
}
if(_findnext(nHandle, &fileinfo) == -1)
{
sFilename = "";
return false;
}
sFilename = fileinfo.name;
return (fileinfo.attrib & _A_SUBDIR) != 0 ? true : false;
}
// End search.
void EndFileSearch(void)
{
if(nHandle > -1)
_findclose(nHandle);
}