00001 #ifndef Impala_Basis_FileNameTmp_h
00002 #define Impala_Basis_FileNameTmp_h
00003
00004 #include "Basis/String.h"
00005 #include "Basis/CmdOptions.h"
00006
00007 namespace Impala
00008 {
00009
00013 inline String
00014 GetTmpPath()
00015 {
00016 ILOG_VAR(Impala.Basis.FileNameTmp);
00017 static String tempFolder("*");
00018 if (tempFolder == "*")
00019 {
00020 CmdOptions& options = CmdOptions::GetInstance();
00021 tempFolder = options.GetString("tmpPath");
00022 if (!tempFolder.empty())
00023 {
00024 if ((tempFolder[tempFolder.size()-1] == '/') ||
00025 (tempFolder[tempFolder.size()-1] == '\\'))
00026 {
00027 tempFolder = tempFolder.substr(0, tempFolder.size() - 1);
00028 }
00029
00030
00031 #ifdef unix
00032 String cmd = "mkdir -p " + tempFolder;
00033 int sysRes = system(cmd.c_str());
00034 if (sysRes != 0)
00035 {
00036 ILOG_INFO("Can't create tempFolder " << tempFolder <<
00037 ", using .");
00038 tempFolder = ".";
00039 }
00040 #else
00041 if ((_mkdir(StringReplaceAll(tempFolder, "/", "\\").c_str()) != 0) &&
00042 (errno != EEXIST))
00043 {
00044 ILOG_INFO("Can't create tempFolder " << tempFolder <<
00045 ", using .");
00046 tempFolder = ".";
00047 }
00048 #endif
00049 }
00050 }
00051 return tempFolder;
00052 }
00053
00061 inline String
00062 PathJoin(String path1, String path2)
00063 {
00064 String a = StringReplace(path1, "\\", "/");
00065 String b = StringReplace(path2, "\\", "/");
00066 if(b[0] == '/')
00067 {
00068 return b;
00069 }
00070 if(a.empty())
00071 {
00072 return b;
00073 }
00074 if(a[a.size()-1] == '/')
00075 {
00076 return a + b;
00077 }
00078 else
00079 {
00080 return a + "/" + b;
00081 }
00082 }
00083
00084 inline String
00085 PathJoin(String path1, String path2, String path3)
00086 {
00087 return PathJoin(path1, PathJoin(path2, path3));
00088 }
00089
00090 inline String
00091 PathJoin(String path1, String path2, String path3, String path4)
00092 {
00093 return PathJoin(path1, PathJoin(path2, path3, path4));
00094 }
00095
00096 inline String
00097 PathJoin(String path1, String path2, String path3, String path4, String path5)
00098 {
00099 return PathJoin(path1, PathJoin(path2, path3, path4, path5));
00100 }
00101
00102 inline String
00103 FileNameTmp()
00104 {
00105 static int nr = 0;
00106 char hName[256];
00107 #ifdef unix
00108 gethostname(hName, 256);
00109 pid_t p = getpid();
00110 #else
00111 DWORD hNameSize = 256;
00112 GetComputerNameA(hName, &hNameSize);
00113 int p = getpid();
00114 #endif
00115 char res[256];
00116 sprintf(res, "tmpFile_%s_%d_%d", hName, p, nr++);
00117 return PathJoin(GetTmpPath(), String(res));
00118 }
00119
00120
00121 }
00122
00123 #endif