1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "nel/misc/common.h"
22 // ---------------------------------------------------------------------------
25 using namespace NLMISC
;
27 // ---------------------------------------------------------------------------
28 void CTools::mkdir (const string
&dirName
)
32 // Does the directory exist ?
33 string newDir
= pwd();
34 if (SetCurrentDirectory (dirName
.c_str()))
36 SetCurrentDirectory (newDir
.c_str());
39 SetCurrentDirectory (newDir
.c_str());
40 // Create upper levels
42 string::size_type pos
= dirName
.rfind('\\');
43 if (pos
!= string::npos
)
45 for (uint i
= 0; i
< pos
; ++i
)
50 if (!CreateDirectory(dirName
.c_str(),NULL
))
51 throw Exception(string("Cannot create directory ")+dirName
);
54 // ---------------------------------------------------------------------------
55 void CTools::chdir (const std::string
&newDir
)
57 if (!SetCurrentDirectory (newDir
.c_str()))
58 throwError ((newDir
+" : ").c_str ());
61 // ---------------------------------------------------------------------------
62 std::string
CTools::pwd ()
65 if (GetCurrentDirectory (512, sTmp
) == 0)
67 throwError ("Get current directory : ");
73 // ---------------------------------------------------------------------------
74 std::string
CTools::normalizePath (const std::string
&path
)
76 // Convert slash to anti-slash
77 string retPath
= path
;
78 for (uint32 i
= 0; i
< retPath
.size(); ++i
)
79 if (retPath
[i
] == '/')
84 // ---------------------------------------------------------------------------
85 bool CTools::fileExist (const std::string
&sFileName
)
87 HANDLE hFile
= CreateFile (sFileName
.c_str(), GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
88 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
89 if (hFile
== INVALID_HANDLE_VALUE
)
95 // ---------------------------------------------------------------------------
96 int CTools::fileDateCmp (const std::string
&file1
, const std::string
&file2
)
98 HANDLE hFile1
= CreateFile (file1
.c_str(), GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
99 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
100 HANDLE hFile2
= CreateFile (file2
.c_str(), GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
101 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
102 if ((hFile1
== INVALID_HANDLE_VALUE
) && (hFile2
== INVALID_HANDLE_VALUE
))
104 if (hFile1
== INVALID_HANDLE_VALUE
)
109 if (hFile2
== INVALID_HANDLE_VALUE
)
115 FILETIME CreationTime1
, LastAccessTime1
, LastWrite1
;
116 GetFileTime (hFile1
, &CreationTime1
, &LastAccessTime1
, &LastWrite1
);
117 FILETIME CreationTime2
, LastAccessTime2
, LastWrite2
;
118 GetFileTime (hFile2
, &CreationTime2
, &LastAccessTime2
, &LastWrite2
);
120 LONG nRet
= CompareFileTime (&LastWrite1
, &LastWrite2
);
128 // ---------------------------------------------------------------------------
129 int CTools::fileDateCmp (const std::string
&file1
, uint32 nDateLow
, uint32 nDateHigh
)
131 HANDLE hFile1
= CreateFile (file1
.c_str(), GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
132 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
133 if (hFile1
== INVALID_HANDLE_VALUE
)
137 FILETIME CreationTime1
, LastAccessTime1
, LastWrite1
;
138 GetFileTime (hFile1
, &CreationTime1
, &LastAccessTime1
, &LastWrite1
);
140 Date
.dwLowDateTime
= nDateLow
;
141 Date
.dwHighDateTime
= nDateHigh
;
142 LONG nRet
= CompareFileTime (&LastWrite1
, &Date
);
148 // ---------------------------------------------------------------------------
149 void CTools::dir (const std::string
&sFilter
, std::vector
<std::string
> &sAllFiles
, bool bFullPath
)
151 WIN32_FIND_DATA findData
;
153 char sCurDir
[MAX_PATH
];
155 GetCurrentDirectory (MAX_PATH
, sCurDir
);
156 hFind
= FindFirstFile (sFilter
.c_str(), &findData
);
157 while (hFind
!= INVALID_HANDLE_VALUE
)
159 if (!(GetFileAttributes(findData
.cFileName
)&FILE_ATTRIBUTE_DIRECTORY
))
162 sAllFiles
.push_back(string(sCurDir
) + "\\" + findData
.cFileName
);
164 sAllFiles
.push_back(findData
.cFileName
);
166 if (FindNextFile (hFind
, &findData
) == 0)
172 // ---------------------------------------------------------------------------
173 void CTools::dirSub (const std::string
&sFilter
, std::vector
<std::string
> &sAllFiles
, bool bFullPath
)
177 for (uint32 i
= 0; i
< sFilter
.size(); ++i
)
178 if (sFilter
[i
] != '*')
180 dirSubRecurse (sTmp
, sAllFiles
, bFullPath
);
183 // ---------------------------------------------------------------------------
184 void CTools::copy (const std::string
&DstFile
, const std::string
&SrcFile
)
186 if (!CopyFile (SrcFile
.c_str(), DstFile
.c_str(), false))
188 throw Exception(string("Cannot copy ")+SrcFile
+" to "+DstFile
);
196 // ---------------------------------------------------------------------------
197 void CTools::throwError (const char *message
)
200 FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER
|
201 FORMAT_MESSAGE_FROM_SYSTEM
|
202 FORMAT_MESSAGE_IGNORE_INSERTS
,
205 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), // Default language
209 string sTmp
= (char*)lpMsgBuf
;
210 sTmp
= message
+ sTmp
;
211 LocalFree (lpMsgBuf
);
212 throw Exception (sTmp
);
215 // ---------------------------------------------------------------------------
216 void CTools::dirSubRecurse (const std::string
&sFilter
, std::vector
<std::string
> &sAllFiles
, bool bFullPath
)
218 WIN32_FIND_DATA findData
;
220 char sCurDir
[MAX_PATH
];
222 GetCurrentDirectory (MAX_PATH
, sCurDir
);
223 hFind
= FindFirstFile ("*.*", &findData
);
224 while (hFind
!= INVALID_HANDLE_VALUE
)
226 if (!(GetFileAttributes(findData
.cFileName
)&FILE_ATTRIBUTE_DIRECTORY
))
228 string sTmp
= findData
.cFileName
;
229 if (sTmp
.size() > sFilter
.size())
230 if (strcmp(sTmp
.c_str()+sTmp
.size()-sFilter
.size(), sFilter
.c_str()) == 0)
233 sAllFiles
.push_back(string(sCurDir
) + "\\" + findData
.cFileName
);
235 sAllFiles
.push_back(findData
.cFileName
);
238 else if ((strcmp(findData
.cFileName
, ".") != 0) && (strcmp(findData
.cFileName
, "..") != 0))
240 SetCurrentDirectory (findData
.cFileName
);
241 dirSubRecurse (sFilter
, sAllFiles
, bFullPath
);
242 SetCurrentDirectory (sCurDir
);
244 if (FindNextFile (hFind
, &findData
) == 0)