Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

FsPath.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "FsPath.h"
00010 
00011 #include "Log.h"
00012 #include "PathException.h"
00013 
00014 #include <string.h>
00015 #include <errno.h>
00016 #include <sys/stat.h>
00017 #include <sys/types.h>
00018 #include <unistd.h>
00019 
00020 //-----------------------------------------------------------------
00021 /**
00022  * Get native filename.
00023  * @param file posix filename
00024  */
00025     std::string
00026 FsPath::getNative(const std::string &file)
00027 {
00028     return file;
00029 }
00030 //-----------------------------------------------------------------
00031 /**
00032  * Returns true when file or directory exists.
00033  * @param file posix filename
00034  */
00035     bool
00036 FsPath::exists(const std::string &file)
00037 {
00038     if (file.empty()) {
00039         return true;
00040     }
00041 
00042     struct stat buf;
00043     int error = stat(file.c_str(), &buf);
00044     return !error;
00045 }
00046 //-----------------------------------------------------------------
00047 /**
00048  * Joint two paths.
00049  * @param dir posix filename
00050  * @param file posix filename
00051  * @return "dir/file"
00052  */
00053     std::string
00054 FsPath::join(const std::string &dir, const std::string &file)
00055 {
00056     if (dir.empty()) {
00057         return file;
00058     }
00059 
00060     return dir + '/' + file;
00061 }
00062 
00063 //-----------------------------------------------------------------
00064 /**
00065  * Return path without file basename.
00066  * @param file posix filename
00067  */
00068 inline std::string
00069 dirPath(const std::string &file)
00070 {
00071     std::string dirs = "";
00072     std::string::size_type pos = file.rfind('/');
00073     if (pos == 0) {
00074         //NOTE: root
00075         dirs = "/";
00076     }
00077     else if (pos != std::string::npos) {
00078         dirs = file.substr(0, pos);
00079     }
00080 
00081     return dirs;
00082 }
00083 //-----------------------------------------------------------------
00084 /**
00085  * Create dir.
00086  * @param dir posix filename
00087  * @throws NameException when error occurs
00088  */
00089 inline void
00090 createDir(const std::string &dir)
00091 {
00092     if (dir.empty()) {
00093         return;
00094     }
00095 
00096 #ifdef WIN32
00097     int error = mkdir(dir.c_str());
00098 #else
00099     int error = mkdir(dir.c_str(), 0777);
00100 #endif
00101     if (error) {
00102         throw PathException(ExInfo("cannot create dir")
00103             .addInfo("stderror", strerror(errno))
00104             .addInfo("dir", dir));
00105     }
00106 }
00107 //-----------------------------------------------------------------
00108 /**
00109  * Create all directories in path (like "mkdir -p").
00110  * @param file posix filename
00111  */
00112 void
00113 FsPath::createPath(const std::string &file)
00114 {
00115     std::string parent = dirPath(file);
00116     if (!FsPath::exists(parent)) {
00117         createPath(parent);
00118         createDir(parent);
00119     }
00120 }
00121 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2