00001 #ifndef Impala_Basis_FileName_h
00002 #define Impala_Basis_FileName_h
00003
00004 #ifdef unix
00005 #include <sys/types.h>
00006 #include <unistd.h>
00007 #else
00008 #include <process.h>
00009
00010
00011
00012 #include <winsock2.h>
00013
00014 #include <windows.h>
00015 #endif
00016
00017 #include "Basis/String.h"
00018
00019 namespace Impala
00020 {
00021
00022
00026 inline String::size_type
00027 FileNameLastPathSepPos(CString filePath)
00028 {
00029 const String::size_type b1 = filePath.rfind('/');
00030 const String::size_type b2 = filePath.rfind('\\');
00031 if (b1 == b2)
00032 return String::npos;
00033 if (b1 == String::npos)
00034 return b2;
00035 if (b2 == String::npos)
00036 return b1;
00037 return (b1 > b2) ? b1 : b2;
00038 }
00039
00042 inline String
00043 FileNameExt(CString filePath, bool doLowerCase = false)
00044 {
00045 const String::size_type dotPos = filePath.rfind('.');
00046 if (dotPos == String::npos || dotPos + 1 == filePath.size())
00047 return String("");
00048
00049 const String::size_type sepPos = FileNameLastPathSepPos(filePath);
00050 if (sepPos != String::npos && sepPos > dotPos)
00051 return String("");
00052
00053 String ext = filePath.substr(dotPos + 1);
00054 if (doLowerCase)
00055 for (String::size_type i = 0 ; i < ext.size() ; i++)
00056 ext[i] = tolower(ext[i]);
00057 return ext;
00058 }
00059
00062 inline String
00063 FileNameBase(CString filePath)
00064 {
00065 String::size_type startPos = 0;
00066
00067 {
00068 const String::size_type sepPos = FileNameLastPathSepPos(filePath);
00069 if (sepPos != String::npos)
00070 {
00071 if (sepPos + 1 == filePath.size())
00072 return String("");
00073 startPos = sepPos + 1;
00074 }
00075 }
00076
00077 String::size_type dotPos = filePath.rfind('.');
00078 if (dotPos == String::npos)
00079 dotPos = filePath.size();
00080
00081 if (dotPos >= startPos)
00082 return filePath.substr(startPos, dotPos - startPos);
00083
00084 return filePath.substr(startPos);
00085 }
00086
00089 inline String
00090 FileNameTail(CString filePath)
00091 {
00092 const String::size_type sepPos = FileNameLastPathSepPos(filePath);
00093 if (sepPos == String::npos)
00094 return filePath;
00095
00096 if (sepPos + 1 == filePath.size())
00097 return String("");
00098
00099 return filePath.substr(sepPos + 1);
00100 }
00101
00104 inline String
00105 FileNamePath(CString filePath)
00106 {
00107 const String::size_type sepPos = FileNameLastPathSepPos(filePath);
00108 if (sepPos == String::npos)
00109 return String("");
00110
00111 return filePath.substr(0, sepPos + 1);
00112 }
00113
00116 inline String
00117 FileNamePath2(CString filePath)
00118 {
00119 const String::size_type sepPos = FileNameLastPathSepPos(filePath);
00120 if (sepPos == String::npos)
00121 return String("");
00122
00123 return filePath.substr(0, sepPos);
00124 }
00125
00129 inline String
00130 FileNameConcat(CString fileName1, CString fileName2)
00131 {
00132 if (fileName1.empty())
00133 return fileName2;
00134 if (fileName2.empty())
00135 return fileName1;
00136 if (StringEndsWith(fileName1, "/"))
00137 return fileName1 + fileName2;
00138 return fileName1 + "/" + fileName2;
00139 }
00140
00142
00143 template <class C1, class C2>
00144 inline String
00145 FileNameCtor(C1 a1, C2 a2, CString dotExt)
00146 {
00147 return MakeString(a1) + "_" + MakeString(a2) + dotExt;
00148 }
00149
00150 template <class C1, class C2, class C3>
00151 inline String
00152 FileNameCtor(C1 a1, C2 a2, C3 a3, CString dotExt)
00153 {
00154 return MakeString(a1) + "_" + MakeString(a2) + "_" + MakeString(a3)
00155 + dotExt;
00156 }
00157
00158 template <class C1, class C2, class C3, class C4>
00159 inline String
00160 FileNameCtor(C1 a1, C2 a2, C3 a3, C4 a4, CString dotExt)
00161 {
00162 return MakeString(a1) + "_" + MakeString(a2) + "_" + MakeString(a3)
00163 + "_" + MakeString(a4) + dotExt;
00164 }
00165
00166 template <class C1, class C2, class C3, class C4, class C5>
00167 inline String
00168 FileNameCtor(C1 a1, C2 a2, C3 a3, C4 a4, C5 a5, CString dotExt)
00169 {
00170 return MakeString(a1) + "_" + MakeString(a2) + "_" + MakeString(a3)
00171 + "_" + MakeString(a4) + "_" + MakeString(a5) + dotExt;
00172 }
00173
00174 template <class C1, class C2, class C3, class C4, class C5, class C6>
00175 inline String
00176 FileNameCtor(C1 a1, C2 a2, C3 a3, C4 a4, C5 a5, C6 a6, CString dotExt)
00177 {
00178 return MakeString(a1) + "_" + MakeString(a2) + "_" + MakeString(a3)
00179 + "_" + MakeString(a4) + "_" + MakeString(a5)
00180 + "_" + MakeString(a6) + dotExt;
00181 }
00182
00183 template <class C1, class C2, class C3, class C4, class C5, class C6, class C7>
00184 inline String
00185 FileNameCtor(C1 a1, C2 a2, C3 a3, C4 a4, C5 a5, C6 a6, C7 a7, CString dotExt)
00186 {
00187 return MakeString(a1) + "_" + MakeString(a2) + "_" + MakeString(a3)
00188 + "_" + MakeString(a4) + "_" + MakeString(a5)
00189 + "_" + MakeString(a6) + "_" + MakeString(a7) + dotExt;
00190 }
00191
00192 }
00193
00194 #endif