-
Notifications
You must be signed in to change notification settings - Fork 16
/
archivefunc.cpp
153 lines (132 loc) · 4.2 KB
/
archivefunc.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
/*############################################################################
## NEONCUBE - RAGNAROK ONLINE PATCH CLIENT (GNU General Public License)
##
## http://openkore.sourceforge.net/neoncube
## (c) 2005 Ansell "Cliffe" Cruz ([email protected])
##
##############################################################################*/
#include "precompiled.h"
//########################################################################
// returns the current folder from a given string
// EG: LPTSTR folder = GetFolder("/this/is/a/folder/test.file", 3);
// folder becomes "a"
//
// @param source - [in] Pointer to a NUL terminated string which
// contains the folder name to be returned.
//
// @param index - [in] index number of the folder to be extracted
// separated by forward slash characters
//
// @return value - Pointer to a NUL terminated string where the folder
// name will be stored.
// @remark This code is not safe for calling several times consecutively.
// You should use its result immediately after calling it.
// You must not attempt any sort of deallocation on
// the returned pointer.
//########################################################################
LPTSTR GetFolder(LPCTSTR source, INT index)
{
static TCHAR localBuffer[MAX_PATH];
LPTSTR pointerCurrent = localBuffer;
INT found = 0;
while(found != index)
{
if(*source == _T('\\'))
++found;
if(*source == '\0')
return NULL;
*pointerCurrent++ = *source++;
}
*pointerCurrent = '\0';
return localBuffer;
}
//#####################################################################
// returns the number of folders from a given string
// EG: int num = CountFolders("/count/this/folder/this_is_a_file.doc");
// num becomes 3
//
// @param source - [in] NUL terminated string which is a path to a file
// or a directory
//
// @return value - number of folders counted
//#####################################################################
INT CountFolders(LPCTSTR source)
{
INT ret = 0;
while(*source != NULL)
{
if(*source == '\\')
++ret;
++source;
}
return ret;
}
//##################################################################
// recursively deletes a folder and its subfolders and files
//
// @param lpszDir - [in] NUL terminated string which contains the path
// to the folder to be deleted recursively
//
// @return value - TRUE if the operation succeeded
//##################################################################
BOOL DeleteDirectoryA(LPCSTR lpszDir)
{
size_t len = lstrlenA(lpszDir);
CHAR *pszFrom = new CHAR[len+2];
lstrcpyA(pszFrom, lpszDir);
pszFrom[len] = 0;
pszFrom[len+1] = 0; // Append extra NUL
SHFILEOPSTRUCTA fileop;
fileop.hwnd = NULL;
fileop.wFunc = FO_DELETE;
fileop.pFrom = pszFrom;
fileop.pTo = NULL;
fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;
fileop.fAnyOperationsAborted = FALSE;
fileop.lpszProgressTitle = NULL;
fileop.hNameMappings = NULL;
int ret = SHFileOperationA(&fileop);
delete [] pszFrom;
return (ret == 0);
}
//##################################################################
// See DeleteDirectoryA
//##################################################################
BOOL DeleteDirectoryW(LPCWSTR lpszDir)
{
size_t len = lstrlenW(lpszDir);
WCHAR *pszFrom = new WCHAR[len+2];
lstrcpyW(pszFrom, lpszDir);
pszFrom[len] = 0;
pszFrom[len+1] = 0; // Append extra NUL
SHFILEOPSTRUCTW fileop;
fileop.hwnd = NULL;
fileop.wFunc = FO_DELETE;
fileop.pFrom = pszFrom;
fileop.pTo = NULL;
fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;
fileop.fAnyOperationsAborted = FALSE;
fileop.lpszProgressTitle = NULL;
fileop.hNameMappings = NULL;
int ret = SHFileOperationW(&fileop);
delete [] pszFrom;
return (ret == 0);
}
//##################################################################
// gets the file extension of a given filename
// @param fname - [in] NUL terminated string which contains the filename
//
// @return value - the file extension, or NULL if not any
//##################################################################
LPCTSTR GetFileExt(LPCTSTR fname)
{
while(*fname != _T('.') && *fname)
{
++fname;
}
if(*fname == '\0')
{
return NULL;
}
return (fname+1);
}