00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef IXE_util_H
00026 #define IXE_util_H
00027
00028
00029 #include "include/ixe.h"
00030
00031
00032 #include "text/less.h"
00033
00034
00035 #include <cctype>
00036 #include <cerrno>
00037 #include <climits>
00038 #include <cstring>
00039 #include <iostream>
00040 #include <map>
00041 #include <string>
00042 #ifndef _WIN32
00043 # include <ctime>
00044 # include <dirent.h>
00045 # include <sys/resource.h>
00046 #endif
00047 #include <sys/stat.h>
00048
00049
00050
00051 typedef struct timeval Timeval;
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #define URL_MAX 2*2048
00072
00073 namespace IXE {
00074
00093 #ifdef _SYS_RESOURCE_H
00094 template <class T>
00095 rlim_t max_out_limit(T resource)
00096 {
00097 struct rlimit r;
00098 ::getrlimit(resource, &r);
00099 r.rlim_cur = r.rlim_max;
00100 ::setrlimit(resource, &r);
00101 return r.rlim_max;
00102 }
00103 #endif
00104
00105
00109
00110
00111 inline bool file_empty(char const* path) {
00112 struct stat stat_buf;
00113 ::stat(path, &stat_buf);
00114 return stat_buf.st_size == 0;
00115 }
00116
00117 inline off_t file_size(char const* path) {
00118 struct stat stat_buf;
00119 ::stat(path, &stat_buf);
00120 return stat_buf.st_size;
00121 }
00122
00123 inline bool file_exists(char const* path) {
00124 struct stat stat_buf;
00125 return ::stat(path, &stat_buf) != -1;
00126 }
00127 inline bool file_exists(std::string const &path) {
00128 return file_exists(path.c_str());
00129 }
00130
00131 inline bool is_directory(char const* path) {
00132 struct stat stat_buf;
00133 return ::stat(path, &stat_buf) != -1 && S_ISDIR(stat_buf.st_mode);
00134 }
00135
00136 inline bool is_directory(std::string const& path) {
00137 return is_directory(path.c_str());
00138 }
00139
00140 inline bool is_plain_file(char const* path) {
00141 struct stat stat_buf;
00142 return ::stat(path, &stat_buf) != -1 && S_ISREG(stat_buf.st_mode);
00143 }
00144 inline bool is_plain_file(std::string const& path) {
00145 return is_plain_file(path.c_str());
00146 }
00147
00148 bool is_indexable_file(char const* path);
00149
00150 #ifndef IXE_NO_SYMBOLIC_LINKS
00151 inline bool is_symbolic_link(char const* path) {
00152 struct stat lstat_buf;
00153 return ::lstat(path, &lstat_buf) != -1
00154 && S_ISLNK(lstat_buf.st_mode & S_IFLNK);
00155 }
00156 inline bool is_symbolic_link(std::string const& path) {
00157 return is_symbolic_link(path.c_str());
00158 }
00159 #endif
00160
00161
00165
00166
00167 struct FileAction {
00168 virtual void operator() (char const* pathname) = 0;
00169 };
00170
00171 void
00172 mapDir(char const* pathname, FileAction& action,
00173 bool recurse_subdirectories = true, bool follow_symbolic_links = true,
00174 int verbosity = 0);
00175
00176
00182
00183
00184 #define FOR_EACH(T, C, I) \
00185 for (T::const_iterator I = (C).begin(); I != (C).end(); ++I)
00186
00187 #define TO_EACH(T, C, I) \
00188 for (T::iterator I = (C).begin(); I != (C).end(); ++I)
00189
00190 #define TRANSFORM_EACH(T, C, I) \
00191 for (T::iterator I = (C).begin(); I != (C).end(); ++I)
00192
00193 void showTime(char const* msg, Timeval& l0, Timeval& l1,
00194 std::ostream& out = std::cerr);
00195
00196 int url_decode(char* dest, char const* src, int len = 0);
00197 char* url_encode(char const* s);
00198 int url_encode(char* dst, char const* s);
00199 void reverseURLdomain(char* revDomain, char const* url, Size len);
00200 void unreverseURLdomain(char* domain, char const* revDomain);
00201 Size availableMemory();
00202 void cgi_parse(std::map<char const*, char const*>& keyMap, char* qstart);
00203
00204
00205
00206 #ifdef BYTE_ALIGN
00207 #define PAD_FILE(o, size)
00208 #else
00209 #define PAD_FILE(o, size) \
00210 { int pad = (size - (o.tellp() % size)) % size; \
00211 if (pad) { const long z = 0; \
00212 o.write(reinterpret_cast<const char*>(&z), pad); } }
00213 #endif
00214
00215 }
00216
00217 #endif