1 /*
2     misc.* - misc functions
3     Copyright (C) 1999-2004  Matthew Mueller <donut AT dakotacom.net>
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #ifndef _MISC_H_
20 #define _MISC_H_
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <time.h>
26 #include <sys/types.h>
27 #include <stdio.h>
28 
29 #ifdef HAVE_LIMITS
30 #include <limits>
31 #endif
32 #include <stdexcept>
33 #include <string>
34 #include "status.h"
35 #include "_sstream.h"
36 #include "log.h"
37 
38 
39 #define TCONV_DEF_BUF_LEN 60
40 size_t tconv(char * timestr, int max, time_t *curtime,const char * formatstr="%Y%m%dT%H%M%S", int local=1);
41 
42 string hexstr(const string &s);
43 
44 template <class Type>
tostr(const Type & arg)45 string tostr(const Type &arg){
46     ostringstream buffer;
47     buffer << arg;
48     return buffer.str();
49 }
50 
51 class parse_error : public runtime_error {
52     public:
parse_error(const string & arg)53         explicit parse_error(const string &arg) : runtime_error(arg) {}
54 };
55 
56 // make these seperate functions to cut down how much has to be inlined.
57 void parsestr_valcheck(const string &val, bool is_signed);
58 void parsestr_isscheck(istringstream &iss);
59 
60 template <class T>
parsestr(const string & val,T & dest)61 void parsestr(const string &val, T &dest) {
62 #ifdef HAVE_LIMITS
63     parsestr_valcheck(val, numeric_limits<T>::is_signed);
64 #else
65     parsestr_valcheck(val, true);
66 #endif
67     istringstream iss(val);
68     T v;
69     iss >> v;
70     parsestr_isscheck(iss);
71     dest = v;
72 }
73 
74 template <class T>
parsestr(const string & val,T & dest,const char * name)75 bool parsestr(const string &val, T &dest, const char *name) {
76     try {
77         parsestr(val, dest);
78         return true;
79     } catch (parse_error &e) {
80         PERROR("%s: %s", name, e.what());
81         set_user_error_status_and_do_fatal_user_error();
82     }
83     return false;
84 }
85 
86 template <class T>
parsestr(const string & val,T & dest,T min,T max)87 void parsestr(const string &val, T &dest, T min, T max) {
88     T v;
89     parsestr(val, v);
90     if (v<min || v>max)
91         throw parse_error("not in range "+tostr(min)+".."+tostr(max));
92     dest = v;
93 }
94 
95 template <class T>
parsestr(const string & val,T & dest,T min,T max,const char * name)96 bool parsestr(const string &val, T &dest, T min, T max, const char *name) {
97     try {
98         parsestr(val, dest, min, max);
99         return true;
100     } catch (parse_error &e) {
101         PERROR("%s: %s", name, e.what());
102         set_user_error_status_and_do_fatal_user_error();
103     }
104     return false;
105 }
106 
107 
108 string strtolower(const string &s);
109 void lowerstr(string &s);
110 
111 bool strstartswith(const string &s, const string &t);
112 
113 string regex2wildmat(const string &repat, bool ignorecase=false);
114 
115 template <class int_type>
durationstr(int_type duration)116 string durationstr(int_type duration){
117 	int_type s = duration%60, m = duration/60%60, h = duration/60/60;
118 	ostringstream oss;
119 	if (h)
120 		oss << h << 'h';
121 	if (h || m)
122 		oss << m << 'm';
123 	oss << s << 's';
124 	return oss.str();
125 }
126 
127 
128 time_t decode_textdate(const char * cbuf, bool local=true);
129 time_t decode_textage(const char *cbuf);
130 int decode_textmonth(const char * buf);
131 int decode_texttz(const char * buf);
132 
133 int filecompare(const char *old_fn,const char *nfn);
134 
135 #endif
136