src/tool/file_tools.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Refresh des fichiers.
00020  *****************************************************************************/
00021 
00022 #include "../tool/file_tools.h"
00023 #include <fstream>
00024 #ifdef _WIN32
00025    // To get SHGetSpecialFolderPath
00026 #  define _WIN32_IE   0x400
00027 #  include <shlobj.h>
00028 #else
00029 #  include <stdlib.h> // getenv
00030 #endif
00031 
00032 #include "i18n.h"
00033 
00034 // Test if a file exists
00035 bool IsFileExist(const std::string &name)
00036 {
00037   std::ifstream f(name.c_str());
00038   bool exist = f;
00039   f.close();
00040   return exist;
00041 }
00042 
00043 // Find the extension part of a filename
00044 std::string FileExtension (const std::string &name)
00045 {
00046   int pos = name.rfind('.');
00047   if (pos != -1)
00048     return name.substr(pos+1);
00049   else
00050     return "";
00051 }
00052 
00053 // Return the path to the home directory of the user
00054 #ifndef WIN32
00055 std::string GetHome()
00056 {
00057   char *txt = getenv("HOME");
00058   
00059   if (txt == NULL) 
00060     Error (_("HOME directory (environment variable $HOME) could not be found!"));
00061   
00062   return txt;
00063 }
00064 #else
00065 std::string GetHome (){
00066   TCHAR szPath[MAX_PATH];
00067 
00068   // "Documents and Settings\user" is CSIDL_PROFILE
00069   if(SHGetSpecialFolderPath(NULL, szPath,
00070                             CSIDL_APPDATA, FALSE) == TRUE)
00071   {
00072     return szPath;
00073   }
00074   return "";
00075 }
00076 #endif
00077 
00078 // Replace ~ by its true name
00079 std::string TranslateDirectory(const std::string &directory)
00080 {
00081   std::string home = GetHome();
00082   std::string txt = directory;
00083   
00084   for (int pos = txt.length()-1;
00085        (pos = txt.rfind ('~', pos)) != -1;
00086        --pos)
00087   {
00088     txt.replace(pos,1,home);
00089   }
00090   return txt;
00091 }

Generated on Mon Jan 1 13:10:59 2007 for Wormux by  doxygen 1.4.7