00001 00002 // 00003 // Copyright (C) 2000 00004 // Ralf Westram 00005 // Time-stamp: <Sun Jul/28/2002 01:48 MET Coder@ReallySoft.de> 00006 // 00007 // Permission to use, copy, modify, distribute and sell this software 00008 // and its documentation for any purpose is hereby granted without fee, 00009 // provided that the above copyright notice appear in all copies and 00010 // that both that copyright notice and this permission notice appear 00011 // in supporting documentation. Ralf Westram makes no 00012 // representations about the suitability of this software for any 00013 // purpose. It is provided "as is" without express or implied warranty. 00014 // 00015 // This code is part of my library. 00016 // You may find a more recent version at http://www.reallysoft.de/ 00017 // 00019 00020 #include "sys_dep.h" 00021 00022 #ifdef __STRICT_ANSI__ 00023 #error Cannot compile sys_dep.cpp with __STRICT_ANSI__ declared 00024 #endif 00025 00026 #include <unistd.h> 00027 #include <sys/stat.h> 00028 #include <climits> 00029 #include <cerrno> 00030 00031 #ifdef DOS 00032 00033 // ------------------------- 00034 // DOS includes: 00035 // ------------------------- 00036 # ifndef _SYS_UTIME_H 00037 # include <sys/utime.h> 00038 # endif 00039 00040 #else 00041 00042 // ----------------------- 00043 // Linux includes: 00044 // ----------------------- 00045 # ifndef __UTIME_H__ 00046 # include <utime.h> 00047 # endif 00048 00049 #endif 00050 00051 #include <Err.h> 00052 #include <Tools.h> 00053 #include <Directory.h> 00054 00055 using namespace std; 00056 using namespace rs; 00057 using namespace rs::err; 00058 using namespace rs::str; 00059 // using namespace rs::file; 00060 // using namespace rs::posix; 00061 00062 namespace rs { 00063 00064 namespace sys_dep { 00065 // -------------------------------------- 00066 // bool is_a_TTY(int filehandle) 00067 // -------------------------------------- 00068 bool is_a_TTY(int filehandle) { 00069 return isatty(filehandle); 00070 } 00071 // ------------------------------- 00072 // bool is_a_TTY(FILE *f) 00073 // ------------------------------- 00074 bool is_a_TTY(FILE *f) { 00075 return is_a_TTY(fileno(f)); 00076 } 00077 00078 // --------------------------- 00079 // string tempname() 00080 // --------------------------- 00081 #ifdef DOS 00082 string tempname() { 00083 static int counter = 0; 00084 string temp_name; 00085 while (1) { 00086 temp_name = strf("rstmp%03i", counter); 00087 if (!posix::fileExists(temp_name)) break; 00088 ++counter; 00089 if (counter>999) throw Error("Can't create temporary file"); 00090 } 00091 00092 return temp_name; 00093 } 00094 #else // LINUX 00095 string tempname() { 00096 return tmpnam(0); 00097 } 00098 #endif 00099 00100 void sleep(unsigned milliseconds) { 00101 ::sleep((milliseconds+999)/1000); 00102 } 00103 }; 00104 00105 namespace posix { 00106 // ------------------------------------------------ 00107 // off_t getFileSize(const string& fname) 00108 // ------------------------------------------------ 00109 off_t getFileSize(const string& fname) { 00110 struct stat st; 00111 if (stat(fname.c_str(), &st) != 0) throw IOError(fname, "stat"); 00112 00113 return st.st_size; 00114 } 00115 // --------------------------------------------------- 00116 // time_t getFileModTime(const string& fname) 00117 // --------------------------------------------------- 00118 time_t getFileModTime(const string& fname) { 00119 struct stat st; 00120 if (stat(fname.c_str(), &st) != 0) throw IOError(fname, "stat"); 00121 00122 return st.st_mtime; 00123 } 00124 // --------------------------------------------------------------- 00125 // void setFileModTime(const string& fname, time_t mtime) 00126 // --------------------------------------------------------------- 00127 void setFileModTime(const string& fname, time_t mtime) { 00128 struct stat st; 00129 if (stat(fname.c_str(), &st) != 0) throw IOError(fname, "stat"); 00130 struct utimbuf ut; 00131 ut.actime = st.st_atime; 00132 ut.modtime = mtime; 00133 00134 if (utime(fname.c_str(), &ut) != 0) throw IOError(fname, "utime"); 00135 } 00136 00137 // --------------------------------------------- 00138 // bool fileExists(const string& fname) 00139 // --------------------------------------------- 00140 bool fileExists(const string& fname) { 00141 struct stat st; 00142 if (stat(fname.c_str(), &st) != 0) return false; 00143 return S_ISREG(st.st_mode); 00144 } 00145 00146 00147 00148 #define BUFFERSIZE (PATH_MAX+1) 00149 00150 // ------------------------- 00151 // string getcwd() 00152 // ------------------------- 00153 string getcwd() { 00154 char buffer[BUFFERSIZE]; 00155 char *cwd = ::getcwd(buffer, BUFFERSIZE); 00156 00157 if (!cwd) throw IOError("getcwd"); 00158 00159 string result; 00160 if (strsicmp(cwd, "/cygdrive/") == 0) result = strf("%c:%s", cwd[10], cwd+11); 00161 else result = cwd; 00162 00163 #if defined(DEBUG) && 0 00164 cout << "getcwd()='" << result << "'\n"; 00165 #endif // DEBUG 00166 return result; 00167 } 00168 00169 // ---------------------------------------------------------------------------- 00170 // void create_directory(const string& dirname, bool throw_if_exists) 00171 // ---------------------------------------------------------------------------- 00172 void create_directory(const string& dirname, bool throw_if_exists) { 00173 int result = mkdir(dirname.c_str(), S_IREAD|S_IWRITE|S_IEXEC); 00174 if (result != 0 && ((errno != EEXIST) || throw_if_exists)) { 00175 throw IOError(dirname, "create_directory"); 00176 } 00177 } 00178 00179 // --------------------------------------------- 00180 // bool dirExists(const string& dname) 00181 // --------------------------------------------- 00182 bool dirExists(const string& dname) { 00183 struct stat st; 00184 if (stat(dname.c_str(), &st) != 0) return false; 00185 return S_ISDIR(st.st_mode); 00186 } 00187 00188 00189 #undef BUFFERSIZE 00190 00191 }; 00192 }; 00193
Contact me in case of errors or questions. This documentation is powered by . |
(C) 2000-2002 |