jigdo API
Last update by Admin on 2010-05-23
compat.hh
Go to the documentation of this file.00001 /* $Id: compat.hh,v 1.17 2003/06/24 13:55:07 richard Exp $ -*- C++ -*- 00002 __ _ 00003 |_) /| Copyright (C) 2001-2003 | richard@ 00004 | \/¯| Richard Atterer | atterer.net 00005 ¯ '` ¯ 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License, version 2. See 00008 the file COPYING for details. 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 // No operator<< for uint64, so define our own 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 /* Linux, BSD */ 00083 inline bool compat_setenv(const char* name, const char* value) { 00084 return (setenv(name, value, 1) == 0) ? SUCCESS : FAILURE; 00085 } 00086 #else /* Solaris and other Unices without setenv() */ 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 // Need this "if" because gcc cannot optimize away loops 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 // v3, v4.0: 00151 // int (*open) __P((DB *, 00152 // const char *, const char *, DBTYPE, u_int32_t, int)); 00153 // v4.1 onwards: 00154 // int (*open) __P((DB *, DB_TXN *, 00155 // const char *, const char *, DBTYPE, u_int32_t, int)); 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
Generated on Tue Sep 23 14:27:41 2008 for jigdo by
