1+ #define __CASCLIB_SELF__
12#include " ../CascLib/src/CascLib.h"
23#include " ../include/SimpleOpt.h"
34
45#include < iostream>
56#include < string>
67#include < vector>
78#include < algorithm>
8- #include < unistd.h>
99#include < dirent.h>
10+
11+ #if (defined(_WIN32) || defined(_WIN64))
12+ #include < direct.h>
13+ #define mkdir (name, chmod ) _mkdir(name)
14+ #endif
15+
16+ #if defined(WIN32) || defined(_WIN32)
17+ #define PATH_SEP_STR " \\ "
18+ #define PATH_SEP_CHAR ' \\ '
19+ #else
20+ #define PATH_SEP_STR " /"
21+ #define PATH_SEP_CHAR ' /'
22+ #endif
23+
1024#include < stdlib.h>
1125#include < stdio.h>
1226#include < sys/stat.h>
@@ -17,7 +31,7 @@ using namespace std;
1731
1832
1933// All the global variables
20- string version = " 1.3 .0" ;
34+ string version = " 1.4 .0" ;
2135
2236struct tSearchResult {
2337 string strFileName;
@@ -211,40 +225,27 @@ vector<string> searchArchive() {
211225}
212226
213227size_t extractFile (string strFullPath) {
214- char buffer[0x100000 ]; // 1MB buffer
215- string strDestName = strDestination;
216-
217- {
218- strDestName += strFullPath;
219-
220- size_t offset = strDestName.find (" \\ " );
221- while (offset != string::npos)
222- {
223- strDestName = strDestName.substr (0 , offset) + " /" + strDestName.substr (offset + 1 );
224- offset = strDestName.find (" \\ " );
225- }
228+ char buffer[0x1000 ];
229+ string strDestName = strDestination + strFullPath;
230+ size_t pos;
226231
227- offset = strDestName.find_last_of (" /" );
228- if (offset != string::npos)
229- {
230- string dest = strDestName.substr (0 , offset + 1 );
231-
232- size_t start = dest.find (" /" , 0 );
233- while (start != string::npos)
234- {
235- string dirname = dest.substr (0 , start);
232+ // normalize slashes in the path
233+ std::replace (strDestName.begin (), strDestName.end (), ' \\ ' , ' /' );
236234
237- DIR* d = opendir (dirname. c_str ());
238- if (!d)
239- mkdir (dirname. c_str (), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
240- else
241- closedir (d );
235+ // ensure directory path to the file exists
236+ pos = - 1 ;
237+ while ((pos = strDestName. find ( ' / ' , pos + 1 )) != string::npos)
238+ {
239+ string dirname = strDestName. substr ( 0 , pos );
242240
243- start = dest.find (" /" , start + 1 );
244- }
245- }
241+ DIR* d = opendir (dirname.c_str ());
242+ if (!d)
243+ mkdir (dirname.c_str (), 0755 );
244+ else
245+ closedir (d);
246246 }
247247
248+ // extract data
248249 HANDLE hFile;
249250 size_t fileSize = 0 ;
250251 if (CascOpenFile (hStorage, strFullPath.c_str (), CASC_LOCALE_ALL, 0 , &hFile))
@@ -254,7 +255,7 @@ size_t extractFile(string strFullPath) {
254255 if (dest)
255256 {
256257 do {
257- if (CascReadFile (hFile, &buffer, 0x100000 , &read)) {
258+ if (CascReadFile (hFile, &buffer, sizeof (buffer) , &read)) {
258259 fileSize += fwrite (&buffer, read, 1 , dest);
259260 }
260261 } while (read > 0 );
@@ -264,7 +265,7 @@ size_t extractFile(string strFullPath) {
264265 else
265266 {
266267 cerr << " NOFILE: (" << errno << " ) Failed to extract '" << strFullPath << " ' to " << strDestName << endl;
267- return 0 ;
268+ return 0 ;
268269 }
269270 CascCloseFile (hFile);
270271 }
@@ -380,8 +381,8 @@ int main(int argc, char** argv) {
380381 int progress;
381382 echo (" Extracting files:\n " );
382383
383- if (strDestination.at (strDestination.size () - 1 ) != ' / ' )
384- strDestination += " / " ;
384+ if (strDestination.at (strDestination.size () - 1 ) != PATH_SEP_CHAR )
385+ strDestination += PATH_SEP_STR ;
385386
386387 vector<string>::iterator iter, iterEnd;
387388 for (iter = results.begin (), iterEnd = results.end (); iter != iterEnd; ++iter)
0 commit comments