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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/******************************************************************************
* Warmux is a convivial mass murder game.
* Copyright (C) 2001-2011 Warmux Team.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
******************************************************************************
* Refresh des fichiers.
*****************************************************************************/
#include <fstream>
#include <sys/stat.h>
#include <errno.h>
#include <cstdlib>
#ifdef WIN32
// To get SHGetSpecialFolderPath
# define _WIN32_IE 0x400
# include <shlobj.h>
# include <io.h>
# include <fcntl.h>
# include <direct.h>
# undef DeleteFile // windows.h defines it I think
#else
# include <sys/types.h>
# include <sys/stat.h>
# include <stdlib.h> // getenv
# include <unistd.h> // not needed by mingw
#endif
#include <WARMUX_error.h>
#include <WARMUX_file_tools.h>
// Test if a file exists
bool DoesFileExist(const std::string &name)
{
struct stat stat_file;
if (stat(name.c_str(), &stat_file))
return false;
return (stat_file.st_mode & S_IFMT) == S_IFREG;
}
// Check if the folder exists
bool DoesFolderExist(const std::string &name)
{
// Is it a directory ?
struct stat stat_file;
if (stat(name.c_str(), &stat_file))
return false;
return (stat_file.st_mode & S_IFMT) == S_IFDIR;
}
#ifndef WIN32
# define MKDIR(dir) (mkdir(dir, 0750))
# if defined(GEKKO)
# define rmdir(dir) remove(dir)
# define dup(fd) fcntl(fd, F_DUPFD) // This is strictly not equivalent
# endif
#else
# define MKDIR(dir) (_mkdir(dir))
# define rmdir(dir) (_rmdir(dir))
#endif
bool CreateFolder(const std::string &name)
{
if (DoesFolderExist(name))
return true; // folder is already existing, nothing to do :-)
std::string dir = name;
std::string subdir;
std::size_t pos;
// Create the needed parent folders
pos = dir.find("/");
while (pos != dir.npos) {
subdir = dir.substr(0, pos);
printf("%s\n", subdir.c_str());
if (subdir.size() != 0) {
// Create the directory if it doesn't exist
if (MKDIR(subdir.c_str()) != 0 && errno != EEXIST)
return false;
}
pos = dir.find("/", pos+1);
}
// Create the directory if it doesn't exist
if (MKDIR(dir.c_str()) != 0 && errno != EEXIST)
return false;
return true;
}
// Delete the folder if it exists
bool DeleteFolder(const std::string &name)
{
if (DoesFolderExist(name)){
return (rmdir(name.c_str())==0);
}
return false;
}
// Delete the file if it exists
bool DeleteFile(const std::string &name)
{
return (remove(name.c_str()) == 0);
}
// Find the extension part of a filename
std::string FileExtension(const std::string &name)
{
int pos = name.rfind('.');
if (pos != -1)
return name.substr(pos+1);
else
return "";
}
#ifdef WIN32
// Return the path to the home directory of the user
static std::string GetWindowsPath(int csidl){
static TCHAR szPath[MAX_PATH];
// "Documents and Settings\user" is CSIDL_PROFILE
if (SHGetSpecialFolderPath(NULL, szPath, csidl, FALSE) == TRUE)
return szPath;
return "";
}
std::string GetOldPersonalDir() { return GetWindowsPath(CSIDL_APPDATA) + "\\Wormux\\"; }
std::string GetHome() { return GetWindowsPath(CSIDL_PERSONAL); }
bool Rename(const std::string& old_name, const std::string& new_name)
{
DWORD flags = MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH;
if (!MoveFileEx(old_name.c_str(), new_name.c_str(), flags)) {
// Don't report the error if the source doesn't exist
if (DoesFolderExist(old_name))
fprintf(stderr, "%s not moved to %s: %i\n",
old_name.c_str(), new_name.c_str(), GetLastError());
return false;
}
return true;
}
#include <windows.h>
struct _FolderSearch
{
WIN32_FIND_DATA file;
HANDLE file_search;
};
FolderSearch *OpenFolder(const std::string& dirname)
{
std::string pattern = dirname + "*.*";
FolderSearch *f = new FolderSearch;
f->file_search = FindFirstFile(pattern.c_str(), &f->file);
if (f->file_search == INVALID_HANDLE_VALUE) {
FindClose(f->file_search);
delete f;
return NULL;
}
return f;
}
const char* FolderSearchNext(FolderSearch *f, bool& file)
{
WIN32_FIND_DATA *find = &f->file;
while (FindNextFile(f->file_search, find)) {
const char *name = (find->cAlternateFileName && find->cAlternateFileName[0])
? find->cAlternateFileName : find->cFileName;
if (find->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// If we are also looking for files, report it isn't one
if (file)
file = false;
return name;
}
// We're not searching for folder, so exit with failure
if (!file)
continue;
// This is a file and we do search for files
file = true;
return name;
}
return NULL;
}
void CloseFolder(FolderSearch *f)
{
if (f) {
FindClose(f->file_search);
delete f;
}
}
std::string GetTmpDir()
{
char *txt = std::getenv("TMPDIR");
if (txt != NULL)
return txt;
txt = std::getenv("TMP");
if (txt != NULL)
return txt;
txt = std::getenv("TEMP");
if (txt != NULL)
return txt;
if (txt == NULL)
Error (_("TEMP directory could not be found!"));
return txt;
}
#else
# if defined(ANDROID)
std::string GetHome() { return "."; }
std::string GetOldPersonalDir() { return ""; }
std::string GetTmpDir() { return "."; }
# elif defined(GEKKO) // Wii
std::string GetHome() { return "sd:/apps/Warmux"; }
std::string GetOldPersonalDir() { return ""; }
std::string GetTmpDir() { return "sd:/apps/Warmux"; }
# elif defined(__SYMBIAN32__)
std::string GetHome() { return "."; }
std::string GetOldPersonalDir() { return "."; }
std::string GetTmpDir() { return "."; }
# else // Linux or Apple
std::string GetHome()
{
char *txt = std::getenv("HOME");
if (txt == NULL)
Error(_("HOME directory (environment variable $HOME) could not be found!"));
return txt;
}
std::string GetTmpDir() { return "/tmp"; }
# ifdef __APPLE__
std::string GetOldPersonalDir() { return ""; }
# else
std::string GetOldPersonalDir() { return GetHome() + "/.wormux/"; }
# endif
# endif // Linux or Apple
bool Rename(const std::string& old_name, const std::string& new_name)
{
return rename(old_name.c_str(), new_name.c_str()) == 0;
}
#include <dirent.h>
struct _FolderSearch
{
DIR *dir;
struct dirent *file;
std::string dname;
};
FolderSearch* OpenFolder(const std::string& dirname)
{
FolderSearch *f = new FolderSearch;
f->dir = opendir(dirname.c_str());
f->dname = dirname;
if (!f->dir) {
delete f;
return NULL;
}
return f;
}
const char* FolderSearchNext(FolderSearch *f, bool& file)
{
while ((f->file = readdir(f->dir)) != NULL) {
if (
#ifdef __SYMBIAN32__
f->file->d_namlen &&
#endif
DoesFolderExist(f->dname+"/"+std::string(f->file->d_name))) {
// If we are also looking for files, report it isn't one
if (file)
file = false;
return f->file->d_name;
}
// We're not searching for folder, exit with failure
if (!file)
continue;
// This is a file and we do search for file
if (
#ifdef __SYMBIAN32__
f->file->d_namlen &&
#endif
DoesFileExist(f->dname+"/"+std::string(f->file->d_name))) {
file = true;
return f->file->d_name;
}
// Not something we like, so skip it
}
return NULL;
}
void CloseFolder(FolderSearch *f)
{
if (f) {
closedir(f->dir);
delete f;
}
}
#endif
std::string CreateTmpFile(const std::string& prefix, int* fd)
{
ASSERT(fd);
char path[512];
snprintf(path, 512, "%s/%sXXXXXX", GetTmpDir().c_str(), prefix.c_str());
#ifdef WIN32
mktemp(path);
*fd = open(path, O_RDWR|O_BINARY|O_CREAT|O_EXCL|_O_SHORT_LIVED, _S_IREAD|_S_IWRITE);
#else
*fd = mkstemp(path);
#endif
return path;
}
#ifndef _WIN32
// Replace ~ by its true name
std::string TranslateDirectory(const std::string &directory)
{
std::string home = GetHome();
std::string txt = directory;
for (int pos = txt.length()-1;
(pos = txt.rfind ('~', pos)) != -1;
--pos) {
txt.replace(pos,1,home);
}
return txt;
}
#endif
std::string FormatFileName(const std::string &name)
{
std::string formated_name = name;
for (uint i=0; i<formated_name.size();i++) {
if (formated_name[i] == ' '){
formated_name[i] = '_';
}
if (formated_name[i] == '.') {
formated_name[i] = '_';
}
if (formated_name[i] == '/') {
formated_name[i] = '_';
}
if (formated_name[i] == '\\') {
formated_name[i] = '_';
}
}
return formated_name;
}
|