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 /*************************************************************************/
00051 
00053 class Trans
00054 {
00055     public:
00057         virtual ~Trans() { }
00058         
00060         virtual int operator() (int ch) const = 0;
00061 };
00062 
00064 class TransNone : public Trans
00065 {
00066     public:
00068         virtual int operator() (int ch) const { return ch; }
00069 };
00070 
00072 class TransLowerCase : public Trans
00073 {
00074     public:
00076         virtual int operator() (int ch) const { return tolower(ch); }
00077 };
00078 
00080 class TransUpperCase : public Trans
00081 {
00082     public:
00084         virtual int operator() (int ch) const { return toupper(ch); }
00085 };
00086 
00087 /*************************************************************************/
00088 /*************************************************************************/
00089 
00091 
00102 extern void absolute_path(char *buf, size_t len, char const *default_dir,
00103          char const *filename);
00104 
00106 
00114 extern cstring absolute_path(char const *default_dir, char const *filename);
00115 
00117 extern std::string absolute_path(std::string const &default_dir,
00118         std::string const &filename);
00119 
00120 /*************************************************************************/
00121 
00123 
00132 size_t base64_decode(char const *input, size_t input_len,
00133         char *output);
00134 
00136 
00144 size_t base64_encode(char const *input, size_t input_len,
00145         char *output);
00146 
00148 size_t base64_decode_str(char const *input, size_t input_len,
00149         char *output);
00151 size_t base64_encode_str(char const *input, size_t input_len,
00152         char *output);
00153 
00155 
00161 static inline size_t base64_declen(size_t input_len)
00162 {
00163     return ((input_len / 4) * 3);
00164 }
00165 
00167 
00173 static inline size_t base64_enclen(size_t input_len)
00174 {
00175     return (((input_len % 3) == 0) ? ((input_len / 3) * 4) : (((input_len / 3) + 1) * 4));
00176 }
00177 
00178 /*************************************************************************/
00179 
00181 static inline uint32_t cidr4_to_netmask(uint8_t cidr)
00182 {
00183     return (cidr ? (0xFFFFFFFF - (1 << (32 - cidr)) + 1) : 0);
00184 }
00185 
00187 static inline uint8_t netmask_to_cidr4(uint32_t mask) 
00188 {
00189    uint8_t tmp = 0;
00190 
00191    while (!(mask & (1 << tmp)) && tmp < 32) 
00192       tmp++;
00193 
00194    return (32 - tmp);
00195 }
00196 
00197 /*************************************************************************/
00198 
00200 static TransNone const ctNone;
00202 static TransLowerCase const ctLowerCase;
00204 static TransUpperCase const ctUpperCase;
00205 
00207 
00216 extern uint16_t crc16(char const *buf, size_t len, uint16_t start = 0);
00217 
00219 
00228 extern uint16_t crc16_str(char const *s, Trans const &trans = ctNone);
00229 
00231 
00242 extern uint32_t crc32(char const *buf, size_t len,
00243         uint32_t start = 0xFFFFFFFF, bool last = true);
00244 
00246 
00255 extern uint32_t crc32_str(char const *s, Trans const &trans = ctNone);
00256 
00258 
00267 extern uint16_t crc_ccitt(char const *buf, size_t len,
00268         uint16_t start = 0xFFFF);
00269 
00271 
00280 extern uint16_t crc_ccitt_str(char const *s, Trans const &trans = ctNone);
00281 
00283 
00294 extern uint16_t crc_dnp(char const *buf, size_t len, uint16_t start = 0,
00295         bool last = true);
00296 
00298 
00307 extern uint16_t crc_dnp_str(char const *s, Trans const &trans = ctNone);
00308 
00309 /*************************************************************************/
00310 
00312 
00318 char *duration_expr(char *buf, size_t len, time_t secs);
00319 
00321 
00332 extern bool duration_scan(char const *expr, time_t *secs); 
00333 
00334 /*************************************************************************/
00335 
00337 
00342 extern bool dir_exists(char const *dirname);
00343 
00345 
00350 extern bool file_exists(char const *filename);
00351 
00352 /*************************************************************************/
00353 
00355 
00365 extern bool match(char const *pattern, char const *str, 
00366         Trans const &trans = TransUpperCase());
00367 
00369 extern bool irc_match(char const *pattern, char const *str, 
00370         Trans const &trans = TransUpperCase());
00371 
00372 /*************************************************************************/
00373 
00375 
00387 extern bool simple_range(char const *expr, unsigned long *first,
00388         unsigned long *last, bool desc = false);
00389 
00391 
00407 extern bool complex_range(char const **expr, unsigned long *first,
00408         unsigned long *last, bool desc = false);
00409 
00410 /*************************************************************************/
00411 
00413 
00420 extern bool recursive_mkdir(char const *pathname, mode_t mode);
00421 
00422 /*************************************************************************/
00423 
00425 
00431 extern bool resolve_error(char *buf, size_t len, int err);  
00432 
00434 
00444 extern bool resolve_host(char const *host, struct in_addr *in, int *err);
00445 
00446 /*************************************************************************/
00447 
00448 } /* namespace misc */
00449 
00450 /*************************************************************************/
00451 
00452 #endif /* MISC_MISC_H */

Generated on Fri Apr 18 22:03:27 2008 for Epona API by  doxygen 1.5.3