misc.h

Go to the documentation of this file.
00001 /* Mysterious treasure box.
00002  *
00003  * PegSoft miscellaneous library (c) 2004 PegSoft
00004  * Contact us at pegsoft@pegsoft.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 as
00008  * published by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this software (the COPYING file); if not, write to the
00017  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
00018  * USA.
00019  *
00020  */
00021 
00027 #ifndef MISC_MISC_H
00028 #define MISC_MISC_H
00029 
00030 /*************************************************************************/
00031 
00032 #if HAVE_SYS_STAT_H
00033 # include <sys/stat.h>
00034 #endif
00035 
00036 #if HAVE_NETINET_IN_H
00037 # include <netinet/in.h>
00038 #endif
00039 
00040 #include <misc/string.h>
00041 
00042 /*************************************************************************/
00043 
00045 
00046 namespace misc
00047 {
00048 
00049 /*************************************************************************/
00050 
00052 
00063 extern void absolute_path(char *buf, size_t len, char const *default_dir,
00064          char const *filename);
00065 
00067 
00075 extern cstring absolute_path(char const *default_dir, char const *filename);
00076 
00078 extern std::string absolute_path(std::string const &default_dir,
00079         std::string const &filename);
00080 
00081 /*************************************************************************/
00082 
00084 
00093 size_t base64_decode(char const *input, size_t input_len,
00094         char *output);
00095 
00097 
00105 size_t base64_encode(char const *input, size_t input_len,
00106         char *output);
00107 
00109 size_t base64_decode_str(char const *input, size_t input_len,
00110         char *output);
00112 size_t base64_encode_str(char const *input, size_t input_len,
00113         char *output);
00114 
00116 
00122 static inline size_t base64_declen(size_t input_len)
00123 {
00124     return ((input_len / 4) * 3);
00125 }
00126 
00128 
00134 static inline size_t base64_enclen(size_t input_len)
00135 {
00136     return (((input_len % 3) == 0) ? ((input_len / 3) * 4) : (((input_len / 3) + 1) * 4));
00137 }
00138 
00139 /*************************************************************************/
00140 
00142 static inline uint32_t cidr4_to_netmask(uint8_t cidr)
00143 {
00144     return (cidr ? (0xFFFFFFFF - (1 << (32 - cidr)) + 1) : 0);
00145 }
00146 
00148 static inline uint8_t netmask_to_cidr4(uint32_t mask) 
00149 {
00150    uint8_t tmp = 0;
00151 
00152    while (!(mask & (1 << tmp)) && tmp < 32) 
00153       tmp++;
00154 
00155    return (32 - tmp);
00156 }
00157 
00158 /*************************************************************************/
00159 
00161 
00162 enum
00163 {
00165     ctNone = 0,
00167     ctLowerCase = 1,
00169     ctUpperCase = 2
00170 };
00171 
00173 
00182 extern uint16_t crc16(char const *buf, size_t len, uint16_t start = 0);
00183 
00185 
00192 extern uint16_t crc16_str(char const *s, int trans = ctNone);
00193 
00195 
00206 extern uint32_t crc32(char const *buf, size_t len,
00207         uint32_t start = 0xFFFFFFFF, bool last = true);
00208 
00210 
00217 extern uint32_t crc32_str(char const *s, int trans = ctNone);
00218 
00220 
00229 extern uint16_t crc_ccitt(char const *buf, size_t len,
00230         uint16_t start = 0xFFFF);
00231 
00233 
00240 extern uint16_t crc_ccitt_str(char const *s, int trans = ctNone);
00241 
00243 
00254 extern uint16_t crc_dnp(char const *buf, size_t len, uint16_t start = 0,
00255         bool last = true);
00256 
00258 
00265 extern uint16_t crc_dnp_str(char const *s, int trans = ctNone);
00266 
00267 /*************************************************************************/
00268 
00270 
00276 char *duration_expr(char *buf, size_t len, time_t secs);
00277 
00279 
00290 extern bool duration_scan(char const *expr, time_t *secs); 
00291 
00292 /*************************************************************************/
00293 
00295 
00300 extern bool dir_exists(char const *dirname);
00301 
00303 
00308 extern bool file_exists(char const *filename);
00309 
00310 /*************************************************************************/
00311 
00313 
00322 extern bool match(char const *pattern, char const *str, bool cs = false);
00323 
00325 extern bool irc_match(char const *pattern, char const *str);
00326 
00327 /*************************************************************************/
00328 
00330 
00342 extern bool simple_range(char const *expr, unsigned long *first,
00343         unsigned long *last, bool desc = false);
00344 
00346 
00362 extern bool complex_range(char const **expr, unsigned long *first,
00363         unsigned long *last, bool desc = false);
00364 
00365 /*************************************************************************/
00366 
00368 
00375 extern bool recursive_mkdir(char const *pathname, mode_t mode);
00376 
00377 /*************************************************************************/
00378 
00380 
00386 extern bool resolve_error(char *buf, size_t len, int err);  
00387 
00389 
00399 extern bool resolve_host(char const *host, struct in_addr *in, int *err);
00400 
00401 /*************************************************************************/
00402 
00403 } /* namespace misc */
00404 
00405 /*************************************************************************/
00406 
00407 #endif /* MISC_MISC_H */

Generated on Sun May 20 21:32:14 2007 for Epona API by  doxygen 1.4.6