libH/UtilFile.cpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Licensed under the Apache License, Version 2.0 (the "License");
00016   you may not use this file except in compliance with the License.
00017   You may obtain a copy of the License at 
00018 
00019         http://www.apache.org/licenses/LICENSE-2.0
00020 
00021   Unless required by applicable law or agreed to in writing, software
00022   distributed under the License is distributed on an "AS IS" BASIS,
00023   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   See the License for the specific language governing permissions and 
00025   limitations under the License. 
00026   
00027 */
00028 
00029 #include "UtilFile.hpp"
00030 #include "Debug.hpp"
00031 #include <fstream>
00032 #include <sys/stat.h>
00033 #include <sys/types.h>
00034 #include <stdio.h>
00035 #include <stddef.h>
00036 #include <stdlib.h>
00037 #include <errno.h>
00038 #include <string.h>
00039 #ifndef WIN32
00040         #include <dirent.h>
00041         #include <unistd.h>
00042 #else
00043         #include <Windows.h>
00044         #include <shlobj.h>
00045 #endif
00046 #include <fcntl.h>
00047 #include <time.h>
00048 
00049 using namespace H;
00050 using namespace std;
00051 
00053 // Defines / Type Defs
00055 
00066 #ifdef WIN32
00067         #define DIRCHAR         '\\'
00068         #define DIRSTR          "\\"
00069 #else
00070         #define DIRCHAR         '/'
00071         #define DIRSTR          "/"
00072 #endif
00073 
00075 // Construction / Deconstruction
00077 
00081 UtilFile::UtilFile() {
00082 }
00083 
00087 UtilFile::~UtilFile() {
00088 }
00089 
00091 // Class Body
00093 
00099 bool UtilFile::createDirectory(std::string const & FileName) {
00100 #ifndef WIN32
00101         if (mkdir(FileName.c_str(), 0755) == -1)
00102                 return false;
00103         else
00104                 return true;
00105 #else
00106         return CreateDirectory(FileName, NULL) != 0;
00107 #endif
00108 }
00109 
00114 void UtilFile::relativeToAbsolute(std::string & FilePath) {
00115         size_t tPos;
00116         if ((tPos = FilePath.find("~" DIRSTR)) == string::npos)
00117                 return;         
00118         const char * home = getenv("HOME");
00119         if (!home)
00120                 return;
00121         FilePath = FilePath.substr(0, tPos) + home + DIRSTR + FilePath.substr(2);
00122 }
00123 
00129 bool UtilFile::touch(std::string const & FilePath) {
00130         // check if it's a directory
00131         if (FilePath[FilePath.length() - 1] == DIRCHAR) {
00132                 // create a dir
00133                 return createDirectory(FilePath);
00134         } else {
00135                 // create a file
00136                 ofstream oFile(FilePath.c_str(), ios::app);
00137                 if (!oFile.is_open())
00138                         return false;
00139                 return true;
00140         }
00141 }
00142 
00149 bool UtilFile::touchRecursive(std::string const & FilePath, bool DoRecursive) { 
00150         // let's try to create it!
00151         if (touch(FilePath))
00152                 return true;            
00153         // recursion requested?
00154         if (!DoRecursive)
00155                 return false;
00156         
00157         // if still no go break it down into smaller pieces
00158         string strFilePath = FilePath;
00159         int Modifier = 0;
00160         if (strFilePath[strFilePath.length() - 1] == DIRCHAR)
00161                 Modifier = 1;
00162         size_t sPos = strFilePath.rfind(DIRSTR, strFilePath.length() - Modifier - 1);
00163         if (sPos == string::npos)
00164                 return touch(FilePath);
00165                         
00166         string SubLevel = strFilePath.substr(0, sPos + 1);
00167         if (!touchRecursive(SubLevel, true))
00168                 return false;
00169                 
00170         return touch(FilePath);
00171 }

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3