Slightly better Utils::push_sorted
[clinton/bobotpp.git] / source / Utils.H
1 // Utils.H -*- C++ -*-
2 // Copyright (c) 1997, 1998 Etienne BERNARD
3 // Copyright (c) 2002,2005,2008 Clinton Ebadi
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 // 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., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19
20 #ifndef UTILS_H
21 #define UTILS_H
22
23 #ifndef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <algorithm>
28 #include <list>
29 #include <string>
30
31 #include <ctime>
32
33 class Bot;
34
35 #ifdef USESCRIPTS
36 #include <libguile.h>
37 #endif
38
39 namespace Utils {
40 std::string get_nick (std::string);
41 std::string get_userhost (std::string);
42 std::string get_key();
43
44 int get_level (Bot *, std::string);
45 int get_level (Bot *, std::string, std::string);
46
47 std::string make_wildcard (std::string);
48
49 // predicates
50 bool channel_p (std::string);
51 bool wildcard_p (std::string);
52 bool valid_channel_name_p (std::string);
53 bool valid_nickname_p (const Bot *, std::string);
54 bool IP_p (std::string);
55
56 std::string level2str (int);
57 std::string prot2str (int);
58 std::string bool2str (bool);
59 std::string long2str (long);
60
61 std::time_t str2time(std::string);
62
63 // string case conversion
64 std::string to_lower (std::string);
65 std::string to_upper (std::string);
66
67 std::string trim_str (std::string);
68
69 #ifdef USESCRIPTS
70 std::string scm2str (SCM);
71 SCM str2scm (std::string);
72 #endif
73
74 // Class to compare *i1 / *i2 in heaps etc.
75 template<typename T, typename C> class IndirectPred
76 {
77 C compare;
78 public:
79 bool operator() (const T * i1, const T * i2) const
80 { return compare (*i1, *i2); }
81
82 typedef T* first_argument_type;
83 typedef T* second_argument_type;
84 typedef bool result_type;
85 };
86
87 template<typename T, typename C>
88 void push_sorted (std::list<T> & storage, T item, C compare)
89 {
90 storage.insert (std::find_if (storage.begin (), storage.end (),
91 std::bind1st (compare, item)),
92 item);
93
94 }
95 }
96
97 #endif