#include <winsock2.h>
#endif
using namespace std;
+
//int gethostname(char *name, size_t len);
std::string GetHostname()
return p;
}
+
+std::string OpUtil_Dir::GetTmpDirByEnv( const std::string& tmp_path_env )
+{
+ string dir;
+ char* val = getenv( tmp_path_env.c_str() );
+ val ? dir = string( val ) : "";
+ return GetTmpDirByPath( dir );
+}
+
+std::string OpUtil_Dir::GetTmpDirByPath( const std::string& tmp_path )
+{
+ string aTmpDir = tmp_path;
+ char sep =
+#ifdef WNT
+ '\\';
+#else
+ '/';
+#endif
+ /*if ( tmp_path.length() > 0 )
+ {
+ char *Tmp_dir = getenv(tmp_path_env.c_str());
+ if( Tmp_dir != NULL )
+ {
+ aTmpDir = string(Tmp_dir);
+ if(aTmpDir[aTmpDir.size()-1] != sep)
+ aTmpDir+=sep;
+ }
+ }*/
+ if ( aTmpDir == "" )
+ {
+#ifdef WNT
+ char *Tmp_dir = getenv("TEMP");
+ if( Tmp_dir == NULL )
+ {
+ Tmp_dir = getenv("TMP");
+ if (Tmp_dir == NULL)
+ aTmpDir = string("C:\\");
+ else
+ aTmpDir = string(Tmp_dir);
+ }
+ else
+ aTmpDir = string(Tmp_dir);
+#else
+ aTmpDir = string("/tmp/");
+#endif
+ }
+
+ if(aTmpDir[aTmpDir.size()-1] != sep)
+ aTmpDir+=sep;
+
+ srand((unsigned int)time(NULL));
+ int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
+ char buffer[127];
+ sprintf(buffer, "%d", aRND);
+ string aSubDir(buffer);
+ if(aSubDir.size() <= 1) aSubDir = string("123409876");
+
+ aTmpDir += aSubDir; //Get RND sub directory
+
+ string aDir = aTmpDir;
+
+ if(IsExists(aDir)) {
+ for(aRND = 0; IsExists(aDir); aRND++) {
+ sprintf(buffer, "%d", aRND);
+ aDir = aTmpDir+buffer; //Build a unique directory name
+ }
+ }
+
+ if(aDir[aDir.size()-1] != sep) aDir+=sep;
+
+#ifdef WNT
+ CreateDirectory(aDir.c_str(), NULL);
+#else
+ mkdir(aDir.c_str(), 0x1ff);
+#endif
+
+ return aDir;
+}
+
+//============================================================================
+// function : GetTempDir
+// purpose : Return a temp directory to store created files like "/tmp/sub_dir/"
+//============================================================================
+std::string OpUtil_Dir::GetTmpDir()
+{
+ return GetTmpDirByPath( "" );
+ //Find a temporary directory to store a file
+/*
+ string aTmpDir = "";
+ char sep =
+#ifdef WNT
+ '\\';
+#else
+ '/';
+#endif
+ if ( tmp_path_env.length() > 0 )
+ {
+ char *Tmp_dir = getenv(tmp_path_env.c_str());
+ if( Tmp_dir != NULL )
+ {
+ aTmpDir = string(Tmp_dir);
+ if(aTmpDir[aTmpDir.size()-1] != sep)
+ aTmpDir+=sep;
+ }
+ }
+ if ( aTmpDir == "" )
+ {
+#ifdef WNT
+ char *Tmp_dir = getenv("TEMP");
+ if( Tmp_dir == NULL )
+ {
+ Tmp_dir = getenv("TMP");
+ if (Tmp_dir == NULL)
+ aTmpDir = string("C:\\");
+ else
+ aTmpDir = string(Tmp_dir);
+ }
+ else
+ aTmpDir = string(Tmp_dir);
+#else
+ aTmpDir = string("/tmp/");
+#endif
+ }
+
+ srand((unsigned int)time(NULL));
+ int aRND = 999 + (int)(100000.0*rand()/(RAND_MAX+1.0)); //Get a random number to present a name of a sub directory
+ char buffer[127];
+ sprintf(buffer, "%d", aRND);
+ string aSubDir(buffer);
+ if(aSubDir.size() <= 1) aSubDir = string("123409876");
+
+ aTmpDir += aSubDir; //Get RND sub directory
+
+ string aDir = aTmpDir;
+
+ if(Exists(aDir)) {
+ for(aRND = 0; Exists(aDir); aRND++) {
+ sprintf(buffer, "%d", aRND);
+ aDir = aTmpDir+buffer; //Build a unique directory name
+ }
+ }
+
+ if(aDir[aDir.size()-1] != sep) aDir+=sep;
+
+#ifdef WNT
+ CreateDirectory(aDir.c_str(), NULL);
+#else
+ mkdir(aDir.c_str(), 0x1ff);
+#endif
+
+ return aDir;*/
+}
+
+bool OpUtil_Dir::IsExists(const string& thePath)
+{
+#ifdef WNT
+ if ( GetFileAttributes ( thePath.c_str() ) == 0xFFFFFFFF ) {
+ if ( GetLastError () == ERROR_FILE_NOT_FOUND ) {
+ return false;
+ }
+ }
+#else
+ int status = access ( thePath.c_str() , F_OK );
+ if (status != 0) return false;
+#endif
+ return true;
+}
\ No newline at end of file
UTILS_EXPORT std::string GetHostname();
UTILS_EXPORT const char *duplicate(const char * const);
+class UTILS_EXPORT OpUtil_Dir
+{
+public:
+ // Returns the unique temporary directory, that is defined in tmp_path_env if this variable is set
+ // otherwise return /tmp/something/ for Unix or c:\something\ for WNT
+ static std::string GetTmpDirByEnv( const std::string& tmp_path_env );
+
+ // Returns the unique temporary directory, that is defined in tmp_path if this variable is set
+ // otherwise return /tmp/something/ for Unix or c:\something\ for WNT
+ static std::string GetTmpDirByPath( const std::string& tmp_path );
+
+ // Returns the unique temporary directory in
+ // /tmp/something/ for Unix or c:\something\ for WNT
+ static std::string GetTmpDir();
+
+ // Returns True(False) if the path (not)exists
+ static bool IsExists( const std::string& path );
+
+};
+
#endif