00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00016 #ifndef COMPAT_HH
00017 #define COMPAT_HH
00018
00019 #include <config.h>
00020
00021 #include <string>
00022 #include <stdio.h>
00023 #include <unistd-jigdo.h>
00024 #include <sys/stat.h>
00025 #include <sys/types.h>
00026
00027 #if WINDOWS
00028 # include <windows.h>
00029 #endif
00030
00031
00032
00033 #if !HAVE_OUTUINT64
00034 #include <iostream>
00035 inline ostream& operator<<(ostream& s, const uint64 x) {
00036 s << static_cast<unsigned long>(x / 1000000000)
00037 << static_cast<unsigned long>(x % 1000000000);
00038 return s;
00039 }
00040 #endif
00041
00042
00045 #if HAVE_TRUNCATE
00046 inline int compat_truncate(const char* path, uint64 length) {
00047 return truncate(path, length);
00048 }
00049 #else
00050 int compat_truncate(const char* path, uint64 length);
00051 #endif
00052
00053
00056 #if WINDOWS
00057 int compat_rename(const char* src, const char* dst);
00058 #else
00059 inline int compat_rename(const char* src, const char* dst) {
00060 return rename(src, dst);
00061 }
00062 #endif
00063
00064
00066 #if WINDOWS
00067 inline int compat_mkdir(const char* newDir) {
00068 return mkdir(newDir);
00069 }
00070 #else
00071 inline int compat_mkdir(const char* newDir) {
00072 return mkdir(newDir, 0777);
00073 }
00074 #endif
00075
00076
00078 #if WINDOWS
00079 inline bool compat_setenv(const char* name, const char* value) {
00080 return (SetEnvironmentVariable(name, value) != 0) ? SUCCESS : FAILURE;
00081 }
00082 #elif HAVE_SETENV
00083 inline bool compat_setenv(const char* name, const char* value) {
00084 return (setenv(name, value, 1) == 0) ? SUCCESS : FAILURE;
00085 }
00086 #else
00087 bool compat_setenv(const char* name, const char* value);
00088 #endif
00089
00090
00093 #if WINDOWS
00094 inline int ttyWidth() { return 80; }
00095 #elif !HAVE_IOCTL_WINSZ || !HAVE_FILENO
00096 inline int ttyWidth() { return 0; }
00097 #else
00098 extern int ttyWidth();
00099 #endif
00100
00101
00107 inline void compat_swapFileUriChars(string& s) {
00108
00109 if (DIRSEP != '/' || EXTSEP != '.') {
00110 for (string::iterator i = s.begin(), e = s.end(); i != e; ++i) {
00111 if (DIRSEP != '/' && *i == DIRSEP) *i = '/';
00112 else if (DIRSEP != '/' && *i == '/') *i = DIRSEP;
00113 else if (EXTSEP != '.' && *i == EXTSEP) *i = '.';
00114 else if (EXTSEP != '.' && *i == '.') *i = EXTSEP;
00115 }
00116 }
00117 }
00118
00119
00121 #if HAVE_STRINGCMP
00122 inline int compat_compare(const string& s1, string::size_type pos1,
00123 string::size_type n1, const string& s2, string::size_type pos2 = 0,
00124 string::size_type n2 = string::npos) {
00125 return s1.compare(pos1, n1, s2, pos2, n2);
00126 }
00127 #else
00128 int compat_compare(const string& s1, string::size_type pos1,
00129 string::size_type n1, const string& s2, string::size_type pos2 = 0,
00130 string::size_type n2 = string::npos);
00131 #endif
00132
00133
00135 #if HAVE_STRINGSTRCMP
00136 inline int compat_compare(const string& s1, string::size_type pos1,
00137 string::size_type n1, const char* s2,
00138 string::size_type n2 = string::npos) {
00139 return s1.compare(pos1, n1, s2, n2);
00140 }
00141 #else
00142 int compat_compare(const string& s1, string::size_type pos1,
00143 string::size_type n1, const char* s2,
00144 string::size_type n2 = string::npos);
00145 #endif
00146
00147
00148 #if HAVE_LIBDB
00149 # include <db.h>
00150
00151
00152
00153
00154
00155
00156 inline int compat_dbOpen(DB* db, const char* file, const char* database,
00157 DBTYPE type, u_int32_t flags, int mode) {
00158 return db->open(db,
00159 # if (DB_VERSION_MAJOR > 4 || \
00160 (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 0))
00161 NULL,
00162 # endif
00163 file, database, type, flags, mode);
00164 }
00165 #endif
00166
00167
00168 #endif