path.h
Go to the documentation of this file.00001
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #if !defined (INCLUDED_OS_PATH_H)
00033 #define INCLUDED_OS_PATH_H
00034
00035 #include <string>
00036
00040 namespace os
00041 {
00044 inline std::string standardPath (const std::string& inPath)
00045 {
00046 std::string newStr = inPath;
00047 std::string::size_type pos = newStr.find("\\");
00048
00049 while (std::string::npos != pos) {
00050 newStr.replace(pos, 1, "/");
00051 pos = newStr.find("\\");
00052 }
00053
00054 return newStr;
00055 }
00056
00061 inline std::string getExtension (const std::string& path)
00062 {
00063 return path.substr(path.rfind(".") + 1);
00064 }
00065
00070 inline std::string getFilenameFromPath (const std::string& path)
00071 {
00072 return path.substr(path.rfind("/") + 1);
00073 }
00074
00078 inline std::string stripFilename (const std::string& filename)
00079 {
00080 return filename.substr(0, filename.rfind("/"));
00081 }
00082
00088 inline std::string stripExtension (const std::string& filename)
00089 {
00090 std::string::size_type pos = filename.rfind(".");
00091 return filename.substr(0, pos);
00092 }
00093 }
00094
00095 #include "string/string.h"
00096
00097 #include <glib.h>
00098 #include <glib/gstdio.h>
00099
00100 #if defined(WIN32)
00101 # define OS_CASE_INSENSITIVE
00102 # ifndef PATH_MAX
00103 # define PATH_MAX 260
00104 # endif
00105 #endif
00106
00109 inline bool path_is_directory (const std::string& path)
00110 {
00111 std::size_t length = path.length();
00112 if (length > 0)
00113 return path[length - 1] == '/';
00114 return false;
00115 }
00116
00119 inline const char* path_get_filename_start (const char* path)
00120 {
00121
00122
00123 const char* last_forward_slash = strrchr(path, '/');
00124 const char* last_backward_slash = strrchr(path, '\\');
00125 if (last_forward_slash > last_backward_slash) {
00126 return last_forward_slash + 1;
00127 } else if (last_forward_slash < last_backward_slash) {
00128 return last_backward_slash + 1;
00129 }
00130
00131 return path;
00132 }
00133
00137 inline bool path_less (const char* path, const char* other)
00138 {
00139 #if defined(OS_CASE_INSENSITIVE)
00140 return string_less_nocase(path, other);
00141 #else
00142 return string_less(path, other);
00143 #endif
00144 }
00145
00150 inline int path_compare (const char* path, const char* other)
00151 {
00152 #if defined(OS_CASE_INSENSITIVE)
00153 return string_compare_nocase(path, other);
00154 #else
00155 return string_compare(path, other);
00156 #endif
00157 }
00158
00161 inline bool path_equal (const std::string& path, const std::string& other)
00162 {
00163 #if defined(OS_CASE_INSENSITIVE)
00164 return string_equal_nocase(path, other);
00165 #else
00166 return string_equal(path, other);
00167 #endif
00168 }
00169
00173 inline bool path_equal_n (const char* path, const char* other, std::size_t n)
00174 {
00175 #if defined(OS_CASE_INSENSITIVE)
00176 return string_equal_nocase_n(path, other, n);
00177 #else
00178 return string_equal_n(path, other, n);
00179 #endif
00180 }
00181
00184 inline const char* path_get_filename_base_end (const char* path)
00185 {
00186 const char* last_period = strrchr(path_get_filename_start(path), '.');
00187 return (last_period != 0) ? last_period : path + string_length(path);
00188 }
00189
00192 inline std::size_t path_get_filename_base_length (const char* path)
00193 {
00194 return path_get_filename_base_end(path) - path;
00195 }
00196
00199 inline const char* path_make_relative (const char* path, const char* base)
00200 {
00201 const std::size_t length = string_length(base);
00202 if (path_equal_n(path, base, length)) {
00203 return path + length;
00204 }
00205 return path;
00206 }
00207
00210 inline bool extension_equal (const std::string& extension, const std::string& other)
00211 {
00212 return path_equal(extension, other);
00213 }
00214
00215 template<typename Functor>
00216 class MatchFileExtension
00217 {
00218 const char* m_extension;
00219 const Functor& m_functor;
00220 public:
00221 MatchFileExtension (const char* extension, const Functor& functor) :
00222 m_extension(extension), m_functor(functor)
00223 {
00224 }
00225 void operator() (const char* name) const
00226 {
00227 const std::string extension = os::getExtension(name);
00228 if (extension_equal(extension.c_str(), m_extension)) {
00229 m_functor(name);
00230 }
00231 }
00232 };
00233
00234 class DirectoryCleaned
00235 {
00236 public:
00237 const char* m_path;
00238 DirectoryCleaned (const char* path) :
00239 m_path(path)
00240 {
00241 }
00242 };
00243
00245 template<typename TextOutputStreamType>
00246 TextOutputStreamType& ostream_write (TextOutputStreamType& ostream, const DirectoryCleaned& path)
00247 {
00248 const char* i = path.m_path;
00249 for (; *i != '\0'; ++i) {
00250 if (*i == '\\') {
00251 ostream << '/';
00252 } else {
00253 ostream << *i;
00254 }
00255 }
00256 const char c = *(i - 1);
00257 if (c != '/' && c != '\\') {
00258 ostream << '/';
00259 }
00260 return ostream;
00261 }
00262
00263 #endif