• R/O
  • SSH
  • HTTPS

clxcpp: コミット


コミットメタ情報

リビジョン1 (tree)
日時2008-07-12 01:27:14
作者cielquis

ログメッセージ

Initial import

変更サマリ

差分

--- trunk/scanner.h (nonexistent)
+++ trunk/scanner.h (revision 1)
@@ -0,0 +1,135 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * scanner.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Tue 16 Jan 2007 04:56:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_SCANNER_H
36+#define CLX_SCANNER_H
37+
38+#include <istream>
39+#include <string>
40+#include "tokenizer.h"
41+#include "lexical_cast.h"
42+
43+namespace clx {
44+ /* --------------------------------------------------------------------- */
45+ // basic_scanner
46+ /* --------------------------------------------------------------------- */
47+ template <
48+ class CharT,
49+ class Traits = std::char_traits<CharT>
50+ >
51+ class basic_scanner {
52+ public:
53+ typedef CharT char_type;
54+ typedef unsigned int size_type;
55+ typedef typename std::basic_string<CharT, Traits> string_type;
56+ typedef typename std::basic_istream<CharT, Traits> istream;
57+
58+ explicit basic_scanner(istream& sin, const char_type* fmt) :
59+ f_(separator(fmt)) { this->scan(sin); }
60+
61+ explicit basic_scanner(istream& sin, const string_type& fmt) :
62+ f_(separator(fmt)) { this->scan(sin); }
63+
64+ explicit basic_scanner(const char_type* src, const char_type* fmt) :
65+ f_(separator(fmt)) { this->scan(src); }
66+
67+ explicit basic_scanner(const string_type& src, const string_type& fmt) :
68+ f_(separator(fmt)) { this->scan(src); }
69+
70+ ~basic_scanner() {}
71+
72+ template <class ValueT>
73+ basic_scanner& operator%(ValueT& dest) {
74+ return this->assign(dest);
75+ }
76+
77+ template <class ValueT>
78+ basic_scanner& operator()(size_type pos, ValueT& dest) {
79+ return this->assign(pos, dest);
80+ }
81+
82+ template <class ValueT, class Assign>
83+ basic_scanner& operator()(size_type pos, ValueT& dest, Assign f) {
84+ return this->assign(pos, dest, f);
85+ }
86+
87+ private:
88+ typedef format_separator<CharT, Traits> separator;
89+ typedef basic_tokenizer<separator, string_type> functor;
90+ typedef typename functor::const_iterator const_iterator;
91+
92+ functor f_;
93+ const_iterator cur_;
94+
95+ void scan(const char_type* src) {
96+ f_.assign(src);
97+ cur_ = f_.begin();
98+ }
99+
100+ void scan(const string_type& src) {
101+ this->scan(src.c_str());
102+ }
103+
104+ void scan(istream& sin) {
105+ std::string tmp;
106+ std::getline(sin, tmp);
107+ this->scan(tmp);
108+ }
109+
110+ template <class ValueT>
111+ basic_scanner& assign(ValueT& dest) {
112+ if (cur_ != f_.end()) {
113+ dest = lexical_cast<ValueT>(*cur_);
114+ cur_++;
115+ }
116+ return *this;
117+ }
118+
119+ template <class ValueT>
120+ basic_scanner& assign(size_type pos, ValueT& dest) {
121+ if (pos < f_.size()) dest = lexical_cast<ValueT>(f_.at(pos));
122+ return *this;
123+ }
124+
125+ template <class ValueT, class Assign>
126+ basic_scanner& assign(size_type pos, ValueT& dest, Assign f) {
127+ if (pos < f_.size()) f(f_.at(pos), dest);
128+ return *this;
129+ }
130+ };
131+
132+ typedef basic_scanner<char> scanner;
133+}
134+
135+#endif // CLX_SCANNER_H
Added: svn:executable
--- trunk/ioftime.h (nonexistent)
+++ trunk/ioftime.h (revision 1)
@@ -0,0 +1,186 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * ioftime.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 14 Jun 2007 11:00:02 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_IOFTIME_H
36+#define CLX_IOFTIME_H
37+
38+#include <ctime>
39+#include <string>
40+#include <stdexcept>
41+#include <ostream>
42+#include "date_time.h"
43+
44+namespace clx {
45+ /* --------------------------------------------------------------------- */
46+ // basic_iftime
47+ /* --------------------------------------------------------------------- */
48+ template <
49+ class CharT,
50+ class Traits = std::char_traits<CharT>
51+ >
52+ class basic_iftime {
53+ public:
54+ typedef CharT char_type;
55+ typedef std::basic_string<CharT, Traits> string_type;
56+ typedef date_time date_type;
57+ typedef typename date_type::tm_type tm_type;
58+
59+ // constructor and destructor
60+ basic_iftime() : date_() {}
61+
62+ explicit basic_iftime(const char_type* s, const char_type* fmt) :
63+ date_() { this->assign(s, fmt); }
64+
65+ explicit basic_iftime(const string_type& s, const string_type& fmt) :
66+ date_() { this->assign(s, fmt); }
67+
68+ virtual ~basic_iftime() {}
69+
70+ // operator
71+ const date_type& assign(const char_type* s, const char_type* fmt) {
72+ tm_type tmp;
73+ char_type* p = ::strptime(s, fmt, &tmp);
74+ if (*p != '\0') throw std::runtime_error("string does not match the format");
75+ date_ = tmp;
76+ return date_;
77+ }
78+
79+ const date_type& assign(const string_type& s, const string_type& fmt) {
80+ return this->assign(s.c_str(), fmt.c_str());
81+ }
82+
83+ // accessor
84+ const date_type& data() const { return date_; }
85+
86+ private:
87+ date_type date_;
88+ };
89+
90+ /* --------------------------------------------------------------------- */
91+ // basic_oftime
92+ /* --------------------------------------------------------------------- */
93+ template <
94+ class CharT,
95+ class Traits = std::char_traits<CharT>,
96+ int BufferSize = 1024
97+ >
98+ class basic_oftime {
99+ public:
100+ typedef CharT char_type;
101+ typedef std::basic_string<CharT, Traits> string_type;
102+ typedef date_time date_type;
103+ typedef typename date_type::tm_type tm_type;
104+
105+ // constructor and destructor
106+ basic_oftime() : date_(), str_() {}
107+
108+ explicit basic_oftime(const char_type* fmt) :
109+ date_(), str_() {
110+ this->assign(fmt);
111+ }
112+
113+ template <class Date>
114+ explicit basic_oftime(const char_type* fmt, const Date& t) :
115+ date_(t), str_() {
116+ this->assign(fmt);
117+ }
118+
119+ explicit basic_oftime(const string_type& fmt) :
120+ date_(), str_() {
121+ this->assign(fmt);
122+ }
123+
124+ template <class Date>
125+ explicit basic_oftime(const string_type& fmt, const Date& t) :
126+ date_(t), str_() {
127+ this->assign(fmt);
128+ }
129+
130+ template <class Date>
131+ basic_oftime& operator=(const Date& t) {
132+ date_ = t;
133+ return *this;
134+ }
135+
136+ virtual ~basic_oftime() {}
137+
138+ // operator
139+ virtual void reset() {
140+ str_.clear();
141+ }
142+
143+ const string_type& assign(const char_type* fmt) {
144+ char_type tmp[BufferSize];
145+ int n = std::strftime(tmp, sizeof(tmp), fmt, date_.data());
146+ if (n == 0) throw std::runtime_error("temporary buffer is overflow");
147+ str_ = tmp;
148+ return str_;
149+ }
150+
151+ template <class Date>
152+ const string_type& assign(const char_type* fmt, const Date& t) {
153+ date_ = t;
154+ return this->assign(fmt);
155+ }
156+
157+ const string_type& assign(const string_type& fmt) {
158+ return this->assign(fmt.c_str());
159+ }
160+
161+ template <class Date>
162+ const string_type& assign(const string_type& fmt, const Date& t) {
163+ date_ = t;
164+ return this->assign(fmt);
165+ }
166+
167+ // accessor
168+ const string_type& str() const { return str_; }
169+
170+ template <class Ch, class Tr>
171+ friend std::basic_ostream<Ch, Tr>& operator<<(
172+ std::basic_ostream<Ch, Tr>& sout, const basic_oftime<Ch, Tr>& f) {
173+ sout << f.str_;
174+ return sout;
175+ }
176+
177+ private:
178+ date_type date_;
179+ string_type str_;
180+ };
181+
182+ typedef basic_iftime<char> iftime;
183+ typedef basic_oftime<char> oftime;
184+}
185+
186+#endif // CXL_IOFTIME_H
Added: svn:executable
--- trunk/remove.h (nonexistent)
+++ trunk/remove.h (revision 1)
@@ -0,0 +1,159 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * remove.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 26 Apr 2007 17:55:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_REMOVE_H
36+#define CLX_REMOVE_H
37+
38+#include <string>
39+#include <algorithm>
40+#include <functional>
41+#include "predicate.h"
42+
43+namespace clx {
44+ /* --------------------------------------------------------------------- */
45+ // remove_if
46+ /* --------------------------------------------------------------------- */
47+ template <class CharT, class Traits, class PredicateFunc>
48+ std::basic_string<CharT, Traits>& remove_if(
49+ std::basic_string<CharT, Traits>& s, PredicateFunc f) {
50+ typename std::basic_string<CharT, Traits>::iterator last;
51+ last = std::remove_if(s.begin(), s.end(), f);
52+ if (last != s.end()) s.erase(last, s.end());
53+ return s;
54+ }
55+
56+ /* --------------------------------------------------------------------- */
57+ // remove_copy_if
58+ /* --------------------------------------------------------------------- */
59+ template <class CharT, class Traits, class PredicateFunc>
60+ std::basic_string<CharT, Traits> remove_copy_if(
61+ const std::basic_string<CharT, Traits>& s, PredicateFunc f) {
62+ std::basic_string<CharT, Traits> tmp(s);
63+ return remove_if(tmp, f);
64+ }
65+
66+ /* --------------------------------------------------------------------- */
67+ // remove
68+ /* --------------------------------------------------------------------- */
69+ template <class CharT, class Traits>
70+ std::basic_string<CharT, Traits>& remove(
71+ std::basic_string<CharT, Traits>& s, CharT c) {
72+ return remove_if(s, std::bind2nd(std::equal_to<CharT>(), c));
73+ }
74+
75+ /* --------------------------------------------------------------------- */
76+ // remove_copy
77+ /* --------------------------------------------------------------------- */
78+ template <class CharT, class Traits>
79+ std::basic_string<CharT, Traits> remove_copy(
80+ const std::basic_string<CharT, Traits>& s, CharT c) {
81+ std::basic_string<CharT, Traits> tmp(s);
82+ return remove(tmp, c);
83+ }
84+
85+ /* --------------------------------------------------------------------- */
86+ // unique
87+ /* --------------------------------------------------------------------- */
88+ template <class CharT, class Traits>
89+ std::basic_string<CharT, Traits>& unique(std::basic_string<CharT, Traits>& s) {
90+ typename std::basic_string<CharT, Traits>::iterator last;
91+ last = std::unique(s.begin(), s.end());
92+ if (last != s.end()) s.erase(last, s.end());
93+ return s;
94+ }
95+
96+ /* --------------------------------------------------------------------- */
97+ // unique_copy
98+ /* --------------------------------------------------------------------- */
99+ template <class CharT, class Traits>
100+ std::basic_string<CharT, Traits> unique_copy(const std::basic_string<CharT, Traits>& s) {
101+ std::basic_string<CharT, Traits> tmp(s);
102+ return unique(tmp);
103+ }
104+
105+ /* --------------------------------------------------------------------- */
106+ // squeeze_if
107+ /* --------------------------------------------------------------------- */
108+ template <class CharT, class Traits, class PredicateFunc>
109+ std::basic_string<CharT, Traits>& squeeze_if(
110+ std::basic_string<CharT, Traits>& s, PredicateFunc f) {
111+ bool x = false;
112+ typename std::basic_string<CharT, Traits>::iterator pos = s.begin();
113+ while (pos != s.end()) {
114+ if (f(*pos)) {
115+ if (x) pos = s.erase(pos);
116+ else {
117+ x = true;
118+ pos++;
119+ }
120+ }
121+ else {
122+ x = false;
123+ pos++;
124+ }
125+ }
126+
127+ return s;
128+ }
129+
130+ /* --------------------------------------------------------------------- */
131+ // squeeze_copy_if
132+ /* --------------------------------------------------------------------- */
133+ template <class CharT, class Traits, class PredicateFunc>
134+ std::basic_string<CharT, Traits> squeeze_copy_if(
135+ const std::basic_string<CharT, Traits>& s, PredicateFunc f) {
136+ std::basic_string<CharT, Traits> tmp(s);
137+ return squeeze_if(tmp, f);
138+ }
139+
140+ /* --------------------------------------------------------------------- */
141+ // squeeze
142+ /* --------------------------------------------------------------------- */
143+ template <class CharT, class Traits>
144+ std::basic_string<CharT, Traits>& squeeze(
145+ std::basic_string<CharT, Traits>& s, CharT c) {
146+ return squeeze_if(s, std::bind2nd(std::equal_to<CharT>(), c));
147+ }
148+
149+ /* --------------------------------------------------------------------- */
150+ // squeeze_copy
151+ /* --------------------------------------------------------------------- */
152+ template <class CharT, class Traits>
153+ std::basic_string<CharT, Traits> squeeze_copy(
154+ const std::basic_string<CharT, Traits> s, CharT c) {
155+ std::basic_string<CharT, Traits> tmp(s);
156+ return squeeze(tmp, c);
157+ }
158+}
159+#endif // CLX_REMOVE_H
Added: svn:executable
--- trunk/stats.h (nonexistent)
+++ trunk/stats.h (revision 1)
@@ -0,0 +1,161 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * stats.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 30 Aug 2007 11:53:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_STATS_H
36+#define CLX_STATS_H
37+
38+#include <utility>
39+#include <cmath>
40+#include "lexical_cast.h"
41+#include "quantile.h"
42+
43+namespace clx {
44+ /* --------------------------------------------------------------------- */
45+ // basic_stats
46+ /* --------------------------------------------------------------------- */
47+ template <class Type>
48+ class basic_stats {
49+ public:
50+ typedef Type value_type;
51+ typedef unsigned int size_type;
52+
53+ basic_stats() : sample_(0), exp_(0.0), var_(0.0), unbias_(0.0) {}
54+ virtual ~basic_stats() {}
55+
56+ template <class InputIterator>
57+ basic_stats& assign(InputIterator first, InputIterator last) {
58+ value_type sum = 0.0;
59+ value_type val = 0.0;
60+
61+ for (InputIterator p = first; p != last; p++, sample_++) {
62+ value_type tmp = lexical_cast<value_type>(*p);
63+ sum += tmp;
64+ val += std::pow(tmp, 2);
65+ }
66+
67+ if (sample_ > 0) {
68+ exp_ = sum / static_cast<value_type>(sample_);
69+ var_ = val / static_cast<value_type>(sample_) - std::pow(exp_, 2);
70+ if (sample_ > 1) unbias_ = var_ * static_cast<value_type>(sample_)
71+ / static_cast<value_type>(sample_ - 1);
72+ }
73+
74+ return *this;
75+ }
76+
77+ size_type sample() const { return sample_; }
78+ value_type mean() const { return exp_; }
79+ value_type variance() const { return var_; }
80+ value_type unbiasvar() const { return unbias_; }
81+
82+ private:
83+ size_type sample_;
84+ value_type exp_;
85+ value_type var_;
86+ value_type unbias_;
87+ };
88+
89+ /* --------------------------------------------------------------------- */
90+ // mean
91+ /* --------------------------------------------------------------------- */
92+ template <class InputIterator>
93+ double mean(InputIterator first, InputIterator last) {
94+ basic_stats<double> x;
95+ x.assign(first, last);
96+ return x.mean();
97+ }
98+
99+ /* --------------------------------------------------------------------- */
100+ // variance
101+ /* --------------------------------------------------------------------- */
102+ template <class InputIterator>
103+ double variance(InputIterator first, InputIterator last) {
104+ basic_stats<double> x;
105+ x.assign(first, last);
106+ return x.variance();
107+ }
108+
109+ /* --------------------------------------------------------------------- */
110+ // stddev
111+ /* --------------------------------------------------------------------- */
112+ template <class InputIterator>
113+ double stddev(InputIterator first, InputIterator last) {
114+ return std::sqrt(variance(first, last));
115+ }
116+
117+ /* --------------------------------------------------------------------- */
118+ // unbiasvar
119+ /* --------------------------------------------------------------------- */
120+ template <class InputIterator>
121+ double unbiasvar(InputIterator first, InputIterator last) {
122+ basic_stats<double> x;
123+ x.assign(first, last);
124+ return x.unbiasvar();
125+ }
126+
127+ /* --------------------------------------------------------------------- */
128+ // confidence_interval
129+ /* --------------------------------------------------------------------- */
130+ template <class InputIterator, class Type>
131+ std::pair<double, double> confidence_interval(InputIterator first, InputIterator last, Type t) {
132+ basic_stats<double> x;
133+ x.assign(first, last);
134+ double interval = t * std::sqrt(x.unbiasvar() / static_cast<double>(x.sample()));
135+ return std::pair<double, double>(x.mean() - interval, x.mean() + interval);
136+ }
137+
138+ /* --------------------------------------------------------------------- */
139+ // mse
140+ /* --------------------------------------------------------------------- */
141+ template <class InputIterator, class Type>
142+ double mse(InputIterator first, InputIterator last, Type correct) {
143+ double sum = 0.0;
144+ unsigned int n = 0;
145+ for (InputIterator p = first; p != last; p++, n++) {
146+ double tmp = lexical_cast<double>(*p) - static_cast<double>(correct);
147+ sum += std::pow(tmp, 2.0);
148+ }
149+ return sum / static_cast<double>(n);
150+ }
151+
152+ /* --------------------------------------------------------------------- */
153+ // rmse
154+ /* --------------------------------------------------------------------- */
155+ template <class InputIterator, class Type>
156+ double rmse(InputIterator first, InputIterator last, Type correct) {
157+ return std::sqrt(mse(first, last, correct));
158+ }
159+};
160+
161+#endif // CLX_STATS_H
Added: svn:executable
--- trunk/syntax_error.h (nonexistent)
+++ trunk/syntax_error.h (revision 1)
@@ -0,0 +1,61 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * syntax_error.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Fri 16 Mar 2007 17:52:03 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_SYNTAX_ERROR_H
36+#define CLX_SYNTAX_ERROR_H
37+
38+#include <stdexcept>
39+#include <string>
40+
41+namespace clx {
42+ /* --------------------------------------------------------------------- */
43+ // syntax_error
44+ /* --------------------------------------------------------------------- */
45+ class syntax_error : public std::runtime_error {
46+ public:
47+ typedef unsigned int size_type;
48+ typedef char char_type;
49+ typedef std::string string_type;
50+
51+ explicit syntax_error(size_type n, const string_type& what_arg) :
52+ std::runtime_error(what_arg), line_(n) {}
53+
54+ size_type line() { return line_; }
55+
56+ private:
57+ size_type line_;
58+ };
59+}
60+
61+#endif // CLX_SYNTAX_ERROR_H
Added: svn:executable
--- trunk/resolver.h (nonexistent)
+++ trunk/resolver.h (revision 1)
@@ -0,0 +1,116 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * resolver.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Fri 01 Feb 2008 12:02:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_RESOLVER_H
36+#define CLX_RESOLVER_H
37+
38+#include <string>
39+#include <vector>
40+#include "socket.h"
41+
42+namespace clx {
43+ /* --------------------------------------------------------------------- */
44+ // basic_resolver
45+ /* --------------------------------------------------------------------- */
46+ template <
47+ int Family,
48+ class CharT = char,
49+ class Traits = std::char_traits<CharT>
50+ >
51+ class basic_resolver {
52+ public:
53+ typedef struct in_addr address_type;
54+ typedef CharT char_type;
55+ typedef std::basic_string<CharT, Traits> string_type;
56+ typedef unsigned int size_type;
57+ typedef std::vector<address_type> address_list;
58+ typedef std::vector<string_type> alias_list;
59+
60+ // constructor and destructor
61+ basic_resolver() : name_(), addr_(), aliases_(), len_() {}
62+
63+ explicit basic_resolver(const char* s) :
64+ name_(), addr_(), aliases_(), len_() { this->lookup(s); }
65+
66+ explicit basic_resolver(const string_type& s) :
67+ name_(), addr_(), aliases_(), len_() { this->lookup(s); }
68+
69+ virtual ~basic_resolver() {}
70+
71+ basic_resolver& operator=(const char_type* s) {
72+ this->lookup(s);
73+ return *this;
74+ }
75+
76+ basic_resolver& operator=(const string_type& s) {
77+ this->lookup(s);
78+ return *this;
79+ }
80+
81+ // operator
82+ bool lookup(const char_type* s) {
83+ address_type addr;
84+ struct hostent* entry;
85+
86+ if (!::inet_aton(s, &addr)) entry = ::gethostbyname(s);
87+ else entry = ::gethostbyaddr((const char*)&addr, sizeof(address_type), Family);
88+
89+ if (entry == NULL) return false;
90+ name_ = entry->h_name;
91+ len_ = entry->h_length;
92+ for (int i = 0; entry->h_aliases[i]; i++) aliases_.push_back(entry->h_aliases[i]);
93+ for (int i = 0; entry->h_addr_list[i]; i++) addr_.push_back(*((address_type*)entry->h_addr_list[i]));
94+
95+ return true;
96+ }
97+
98+ bool lookup(const string_type& s) { return this->lookup(s.c_str()); }
99+
100+ // accessor
101+ const address_list& addresses() const { return addr_; }
102+ const string_type& hostname() const { return name_; }
103+ const alias_list& aliases() const { return aliases_; }
104+ int length() const { return len_; }
105+
106+ private:
107+ string_type name_;
108+ address_list addr_;
109+ alias_list aliases_;
110+ int len_;
111+ };
112+
113+ typedef basic_resolver<AF_INET> resolver;
114+};
115+
116+#endif // CLX_RESOLVER_H
Added: svn:executable
--- trunk/sockaddress.h (nonexistent)
+++ trunk/sockaddress.h (revision 1)
@@ -0,0 +1,230 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * sockaddress.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 10 Jul 2008 07:23:03 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_SOCKADDRESS_H
36+#define CLX_SOCKADDRESS_H
37+
38+#include <cstdlib>
39+#include <cstring>
40+#include <string>
41+#include <map>
42+#include <exception>
43+#include "socket.h"
44+#include "resolver.h"
45+#include "lexical_cast.h"
46+
47+namespace clx {
48+ /* --------------------------------------------------------------------- */
49+ // sockaddress_error
50+ /* --------------------------------------------------------------------- */
51+ class sockaddress_error : public std::exception {
52+ public:
53+ typedef char char_type;
54+ typedef std::string string_type;
55+
56+ sockaddress_error() : std::exception(), factor_() {}
57+
58+ explicit sockaddress_error(const string_type& factor) :
59+ std::exception(), factor_(factor) {}
60+
61+ explicit sockaddress_error(const char_type* factor) :
62+ std::exception(), factor_(factor) {}
63+
64+ virtual ~sockaddress_error() throw() {}
65+
66+ virtual const char_type* what() {
67+ string_type tmp;
68+ tmp = factor_ + ": Non-existent address or service";
69+ return tmp.c_str();
70+ }
71+
72+ const string_type& factor() const { return factor_; }
73+
74+ private:
75+ string_type factor_;
76+ };
77+
78+ /* --------------------------------------------------------------------- */
79+ // basic_sockaddress
80+ /* --------------------------------------------------------------------- */
81+ template <
82+ int Family,
83+ int Protocol,
84+ class CharT = char,
85+ class Traits = std::char_traits<CharT>
86+ >
87+ class basic_sockaddress {
88+ public:
89+ typedef basic_resolver<Family> resolver;
90+ typedef struct sockaddr_in inet_type;
91+ typedef struct sockaddr base_type;
92+ typedef struct in_addr address_type;
93+ typedef CharT char_type;
94+ typedef std::basic_string<CharT, Traits> string_type;
95+ typedef unsigned int size_type;
96+
97+ // constructor and destructor
98+ basic_sockaddress() : addr_() {}
99+
100+ explicit basic_sockaddress(const inet_type& addr) : addr_(addr) {
101+ this->init();
102+ }
103+
104+ explicit basic_sockaddress(int port) : addr_() {
105+ this->init();
106+ this->assign(port);
107+ }
108+
109+ explicit basic_sockaddress(const char_type* service) : addr_() {
110+ this->init();
111+ this->assign(service);
112+ }
113+
114+ explicit basic_sockaddress(const char_type* host, int port) : addr_() {
115+ this->init();
116+ this->assign(host, port);
117+ }
118+
119+ explicit basic_sockaddress(const string_type& host, int port) : addr_() {
120+ this->init();
121+ this->assign(host, port);
122+ }
123+
124+ explicit basic_sockaddress(const char_type* host, const char_type* service) : addr_() {
125+ this->init();
126+ this->assign(host, service);
127+ }
128+
129+ explicit basic_sockaddress(const string_type& host, const string_type& service) : addr_() {
130+ this->init();
131+ this->assign(host, service);
132+ }
133+
134+ virtual ~basic_sockaddress() {}
135+
136+ basic_sockaddress& operator=(const inet_type& addr) {
137+ return this->assign(addr);
138+ }
139+
140+ // operator
141+ basic_sockaddress& assign(const inet_type& addr) {
142+ addr_ = addr;
143+ return *this;
144+ }
145+
146+ basic_sockaddress& assign(int port) {
147+ std::memset((char*)&addr_, 0, sizeof(addr_));
148+ addr_.sin_family = Family;
149+ addr_.sin_port = ::htons(port);
150+#ifdef CLX_WIN32
151+ addr_.sin_addr.S_un.S_addr = ::htonl(INADDR_ANY);
152+#else
153+ addr_.sin_addr.s_addr = ::htonl(INADDR_ANY);
154+#endif // CLX_WIN32
155+ return *this;
156+ }
157+
158+ basic_sockaddress& assign(const char_type* service) {
159+ struct servent* serv = ::getservbyname(service, pname_[Protocol].c_str());
160+ if (serv == NULL) {
161+ try {
162+ return this->assign(lexical_cast<int>(service));
163+ }
164+ catch (bad_lexical_cast& e) {
165+ throw sockaddress_error(service);
166+ }
167+ }
168+ return this->assign(::ntohs(serv->s_port));
169+ }
170+
171+ basic_sockaddress& assign(const char_type* host, int port, int n = 0) {
172+ address_type tmp;
173+ if (!::inet_aton(host, &tmp)) {
174+ resolver inet;
175+ inet.lookup(host);
176+ if (inet.addresses().empty()) throw sockaddress_error(host);
177+ tmp = inet.addresses().at(n);
178+ }
179+
180+ std::memset(&addr_, 0, sizeof(addr_));
181+ addr_.sin_family = Family;
182+ addr_.sin_port = ::htons(port);
183+ std::memcpy((char*)&addr_.sin_addr, &tmp, sizeof(tmp));
184+ return *this;
185+ }
186+
187+ basic_sockaddress& assign(const string_type& host, int port, int n = 0) {
188+ return this->assign(host.c_str(), port, n);
189+ }
190+
191+ basic_sockaddress& assign(const char_type* host, const char_type* service, int n = 0) {
192+ struct servent* serv = ::getservbyname(service, pname_[Protocol].c_str());
193+ if (serv == NULL) {
194+ try {
195+ return this->assign(host, lexical_cast<int>(service), n);
196+ }
197+ catch (bad_lexical_cast& e) {
198+ throw sockaddress_error(service);
199+ }
200+ }
201+ return this->assign(host, ::ntohs(serv->s_port), n);
202+ }
203+
204+ basic_sockaddress& assign(const string_type& host, const string_type& service, int n = 0) {
205+ return this->assign(host.c_str(), service.c_str(), n);
206+ }
207+
208+ // get information about socket address
209+ string_type ipaddr() const {
210+ string_type tmp(::inet_ntoa(addr_.sin_addr));
211+ return tmp;
212+ }
213+
214+ int port() const { return ::ntohs(addr_.sin_port); }
215+
216+ size_type size() const { return sizeof(addr_); }
217+ const inet_type* data() const { return &addr_; }
218+
219+ private:
220+ inet_type addr_;
221+ std::map<int, string_type> pname_;
222+
223+ void init() {
224+ pname_[IPPROTO_TCP] = "tcp";
225+ pname_[IPPROTO_UDP] = "udp";
226+ }
227+ };
228+}
229+
230+#endif // CLX_SOCKADDRESS_H
Added: svn:executable
--- trunk/constrained_value.h (nonexistent)
+++ trunk/constrained_value.h (revision 1)
@@ -0,0 +1,206 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * constrained_value.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Reference: D. Ryan Stephens, Christopher Diggins, Janathan Turkanis,
33+ * and Jeff Cogswell, ``C++ Cookbook,'' O'Reilly Media, Inc, 2006.
34+ *
35+ * Last-modified: Mon 07 Jul 2008 10:24:00 JST
36+ */
37+/* ------------------------------------------------------------------------- */
38+#ifndef CLX_CONSTRAINED_VALUE_H
39+#define CLX_CONSTRAINED_VALUE_H
40+
41+#include <cstdlib>
42+#include <iostream>
43+#include "constrained_value_func.h"
44+
45+namespace clx {
46+ template <class ConstrainedFunc>
47+ class constrained_value {
48+ public:
49+ typedef ConstrainedFunc func;
50+ typedef typename ConstrainedFunc::value_type value_type;
51+
52+ constrained_value() : value_(func::default_value) {}
53+ explicit constrained_value(const constrained_value& x) : value_(x.value_) {}
54+ explicit constrained_value(const value_type& x) { func::assign(value_, x); }
55+ virtual ~constrained_value() {}
56+
57+ const value_type& value() const { return value_; }
58+
59+ // assignment operator
60+ constrained_value& operator=(const constrained_value& x) {
61+ this->assign(x.value_);
62+ return *this;
63+ }
64+
65+ constrained_value& operator+=(const constrained_value& x) {
66+ this->assign(value_ + x.value_);
67+ return *this;
68+ }
69+
70+ constrained_value& operator-=(const constrained_value& x) {
71+ this->assign(value_ - x.value_);
72+ return *this;
73+ }
74+
75+ constrained_value& operator*=(const constrained_value& x) {
76+ this->assign(value_ * x.value_);
77+ return *this;
78+ }
79+
80+ constrained_value& operator/=(const constrained_value& x) {
81+ this->assign(value_ / x.value_);
82+ return *this;
83+ }
84+
85+ constrained_value& operator%=(const constrained_value& x) {
86+ this->assign(value_ % x.value_);
87+ return *this;
88+ }
89+
90+ constrained_value& operator=(const value_type& x) {
91+ this->assign(x);
92+ return *this;
93+ }
94+
95+ constrained_value& operator+=(const value_type& x) {
96+ this->assign(value_ + x);
97+ return *this;
98+ }
99+
100+ constrained_value& operator-=(const value_type& x) {
101+ this->assign(value_ - x);
102+ return *this;
103+ }
104+
105+ constrained_value& operator*=(const value_type& x) {
106+ this->assign(value_ * x);
107+ return *this;
108+ }
109+
110+ constrained_value& operator/=(const value_type& x) {
111+ this->assign(value_ / x);
112+ return *this;
113+ }
114+
115+ constrained_value& operator%=(const value_type& x) {
116+ this->assign(value_ % x);
117+ return *this;
118+ }
119+
120+ constrained_value& operator>>=(int x) {
121+ this->assign(value_ >> x);
122+ return *this;
123+ }
124+
125+ constrained_value& operator<<=(int x) {
126+ this->assign(value_ << x);
127+ return *this;
128+ }
129+
130+ // unary operator
131+ constrained_value operator-() { return constrained_value(-value_); }
132+ constrained_value operator+() { return constrained_value(+value_); }
133+ constrained_value operator!() { return constrained_value(!value_); }
134+ constrained_value operator~() { return constrained_value(~value_); }
135+
136+ // binary operator
137+ friend constrained_value operator+(constrained_value x, const constrained_value& y) { return x += y; }
138+ friend constrained_value operator-(constrained_value x, const constrained_value& y) { return x -= y; }
139+ friend constrained_value operator*(constrained_value x, const constrained_value& y) { return x *= y; }
140+ friend constrained_value operator/(constrained_value x, const constrained_value& y) { return x /= y; }
141+ friend constrained_value operator%(constrained_value x, const constrained_value& y) { return x %= y; }
142+
143+ friend constrained_value operator+(constrained_value x, const value_type& y) { return x += y; }
144+ friend constrained_value operator-(constrained_value x, const value_type& y) { return x -= y; }
145+ friend constrained_value operator*(constrained_value x, const value_type& y) { return x *= y; }
146+ friend constrained_value operator/(constrained_value x, const value_type& y) { return x /= y; }
147+ friend constrained_value operator%(constrained_value x, const value_type& y) { return x %= y; }
148+ friend constrained_value operator+(const value_type& y, constrained_value x) { return x += y; }
149+ friend constrained_value operator-(const value_type& y, constrained_value x) { return x -= y; }
150+ friend constrained_value operator*(const value_type& y, constrained_value x) { return x *= y; }
151+ friend constrained_value operator/(const value_type& y, constrained_value x) { return x /= y; }
152+ friend constrained_value operator%(const value_type& y, constrained_value x) { return x %= y; }
153+
154+ friend constrained_value operator>>(constrained_value x, int y) { return x >>= y; }
155+ friend constrained_value operator<<(constrained_value x, int y) { return x <<= y; }
156+
157+ // stream operator
158+ template <class Ch, class Tr>
159+ friend std::basic_ostream<Ch, Tr>& operator<<(
160+ std::basic_ostream<Ch, Tr>& sout, constrained_value x) {
161+ sout << x.value_;
162+ return sout;
163+ }
164+
165+ template <class Ch, class Tr>
166+ friend std::basic_istream<Ch, Tr>& operator>>(
167+ std::basic_istream<Ch, Tr>& sin, constrained_value x) {
168+ value_type tmp;
169+ sin >> tmp;
170+ x.assign(tmp);
171+ return sin;
172+ }
173+
174+ // comparison operator
175+ friend bool operator<(const constrained_value& x, const constrained_value& y) { return x.value_ < y.value_; }
176+ friend bool operator>(const constrained_value& x, const constrained_value& y) { return x.value_ > y.value_; }
177+ friend bool operator<=(const constrained_value& x, const constrained_value& y) { return x.value_ <= y.value_; }
178+ friend bool operator>=(const constrained_value& x, const constrained_value& y) { return x.value_ >= y.value_; }
179+ friend bool operator==(const constrained_value& x, const constrained_value& y) { return x.value_ == y.value_; }
180+ friend bool operator!=(const constrained_value& x, const constrained_value& y) { return x.value_ != y.value_; }
181+
182+ friend bool operator<(const constrained_value& x, const value_type& y) { return x.value_ < y; }
183+ friend bool operator>(const constrained_value& x, const value_type& y) { return x.value_ > y; }
184+ friend bool operator<=(const constrained_value& x, const value_type& y) { return x.value_ <= y; }
185+ friend bool operator>=(const constrained_value& x, const value_type& y) { return x.value_ >= y; }
186+ friend bool operator==(const constrained_value& x, const value_type& y) { return x.value_ == y; }
187+ friend bool operator!=(const constrained_value& x, const value_type& y) { return x.value_ != y; }
188+ friend bool operator<(const value_type& y, const constrained_value& x) { return x.value_ < y; }
189+ friend bool operator>(const value_type& y, const constrained_value& x) { return x.value_ > y; }
190+ friend bool operator<=(const value_type& y, const constrained_value& x) { return x.value_ <= y; }
191+ friend bool operator>=(const value_type& y, const constrained_value& x) { return x.value_ >= y; }
192+ friend bool operator==(const value_type& y, const constrained_value& x) { return x.value_ == y; }
193+ friend bool operator!=(const value_type& y, const constrained_value& x) { return x.value_ != y; }
194+
195+ private:
196+ value_type value_;
197+
198+ void assign(const value_type& x) {
199+ func::assign(value_, x);
200+ }
201+
202+
203+ };
204+};
205+
206+#endif // CLX_CONSTRAINED_VALUE_H
Added: svn:executable
--- trunk/winsock_util.h (nonexistent)
+++ trunk/winsock_util.h (revision 1)
@@ -0,0 +1,79 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * winsock_util.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Fri 08 Feb 2008 01:23:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_WINSOCK_UTIL_H
36+#define CLX_WINSOCK_UTIL_H
37+
38+#include <winsock2.h>
39+#pragma comment(lib, "ws2_32.lib")
40+
41+/* ------------------------------------------------------------------------- */
42+/*
43+ * inet_aton
44+ *
45+ * WinSock does not have the inet_aton() function.
46+ */
47+/* ------------------------------------------------------------------------- */
48+int inet_aton(const char* s, struct in_addr* addr) {
49+ int tmp = ::inet_addr(s);
50+ if (tmp != INADDR_NONE || ::strcmp(s, "255.255.255.255") == 0) {
51+ if (addr) addr->s_addr = tmp;
52+ return 1;
53+ }
54+ return 0;
55+}
56+
57+namespace clx {
58+ /* --------------------------------------------------------------------- */
59+ // winsock_init
60+ /* --------------------------------------------------------------------- */
61+ class winsock_init {
62+ public:
63+ winsock_init() {
64+ WSADATA dat;
65+ ::WSAStartup(MAKEWORD(2, 0), &dat);
66+ }
67+
68+ ~winsock_init() {
69+ ::WSACleanup();
70+ }
71+ private:
72+ explicit winsock_init(const winsock_init& cp);
73+ winsock_init& operator=(const winsock_init& cp);
74+ };
75+
76+ static winsock_init ws_init_;
77+}
78+
79+#endif // CLX_WINSOCK_UTIL_H
Added: svn:executable
--- trunk/sockstream.h (nonexistent)
+++ trunk/sockstream.h (revision 1)
@@ -0,0 +1,187 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * sockstream.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Mon 05 Feb 2008 15:27:03 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_SOCKSTREAM_H
36+#define CLX_SOCKSTREAM_H
37+
38+#include <iostream>
39+#include <streambuf>
40+#include <string>
41+#include <cstring>
42+#include <vector>
43+
44+namespace clx {
45+ /* --------------------------------------------------------------------- */
46+ // basic_sockbuf
47+ /* --------------------------------------------------------------------- */
48+ template <
49+ class Socket,
50+ class CharT,
51+ class Traits = std::char_traits<CharT>
52+ >
53+ class basic_sockbuf : public std::basic_streambuf<CharT, Traits> {
54+ public:
55+ typedef Socket descriptor;
56+ typedef CharT char_type;
57+ typedef Traits traits;
58+ typedef typename std::vector<CharT> container;
59+ typedef typename std::basic_string<CharT, Traits> string_type;
60+ typedef typename container::size_type size_type;
61+ typedef typename Traits::int_type int_type;
62+
63+ static const int npback = 8;
64+
65+ // constructor and destructor
66+ explicit basic_sockbuf(const descriptor& s, size_type n) :
67+ super(), sock_(s), buffer_(n) {}
68+
69+ explicit basic_sockbuf(const string_type& host, int port, size_type n) :
70+ super(), sock_(host, port), buffer_(n) {}
71+
72+ explicit basic_sockbuf(const char_type* host, int port, size_type n) :
73+ super(), sock_(host, port), buffer_(n) {}
74+
75+ explicit basic_sockbuf(const char_type* host, const char_type* service, size_type n) :
76+ super(), sock_(host, service), buffer_(n) {}
77+
78+ explicit basic_sockbuf(const string_type& host, const string_type& service, size_type n) :
79+ super(), sock_(host, service), buffer_(n) {}
80+
81+ virtual ~basic_sockbuf() {}
82+
83+ // operator
84+ const descriptor& socket() const { return sock_; }
85+
86+ protected:
87+ virtual int_type overflow(int_type c) {
88+ if (!traits::not_eof(c)) return traits::eof();
89+ char tmp = static_cast<char>(c);
90+ if (sock_.send(&tmp, 1) <= 0) return traits::eof();
91+ return c;
92+ }
93+
94+ virtual std::streamsize xsputn(const char_type* s, std::streamsize n) {
95+ int l = sock_.send(s, n);
96+ return (l < 0) ? 0 : l;
97+ }
98+
99+ virtual int_type underflow() {
100+ if (this->gptr() == this->egptr()) {
101+ // copy the previous read string to the putback field
102+ int n = this->gptr() - this->eback();
103+ if (n > npback) n = npback;
104+ std::memcpy(&buffer_[npback-n], this->gptr() - n, n);
105+
106+ int l = sock_.recv(&buffer_[npback], static_cast<int>(buffer_.size() - npback));
107+ if (l <= 0) return traits::eof();
108+ this->setg(&buffer_[npback-n], &buffer_[npback], &buffer_[npback + l]);
109+ return *this->gptr();
110+ }
111+ else return traits::eof();
112+ }
113+
114+ virtual std::streamsize xsgetn(char_type* s, std::streamsize n) {
115+ int l = sock_.recv(s, n);
116+ return (l < 0) ? 0 : l;
117+ }
118+
119+ private:
120+ typedef std::basic_streambuf<CharT, Traits> super;
121+
122+ descriptor sock_;
123+ container buffer_;
124+ };
125+
126+ /* --------------------------------------------------------------------- */
127+ // basic_sockstream
128+ /* --------------------------------------------------------------------- */
129+ template <
130+ class Socket,
131+ class CharT = char,
132+ class Traits = std::char_traits<CharT>
133+ >
134+ class basic_sockstream : public std::basic_iostream<CharT, Traits> {
135+ public:
136+ typedef Socket descriptor;
137+ typedef CharT char_type;
138+ typedef std::basic_string<CharT, Traits> string_type;
139+ typedef basic_sockbuf<Socket, CharT, Traits> sockbuf;
140+ typedef typename sockbuf::size_type size_type;
141+
142+ static const size_type nbuf = 65536;
143+
144+ // constructor and destructor
145+ explicit basic_sockstream(const descriptor& s, size_type n = nbuf) :
146+ super(0), sbuf_(s, n) {
147+ this->init();
148+ }
149+
150+ explicit basic_sockstream(const char_type* host, int port, size_type n = nbuf) :
151+ super(0), sbuf_(host, port, n) {
152+ this->init();
153+ }
154+
155+ explicit basic_sockstream(const string_type& host, int port, size_type n = nbuf) :
156+ super(0), sbuf_(host, port, n) {
157+ this->init();
158+ }
159+
160+ explicit basic_sockstream(const char_type* host, const char_type* service, size_type n = nbuf) :
161+ super(0), sbuf_(host, service, n) {
162+ this->init();
163+ }
164+
165+ explicit basic_sockstream(const string_type& host, const string_type& service, size_type n = nbuf) :
166+ super(0), sbuf_(host, service, n) {
167+ this->init();
168+ }
169+
170+ virtual ~basic_sockstream() { this->rdbuf(0); }
171+
172+ // operator
173+ const descriptor& socket() const { return sbuf_.socket(); }
174+
175+ private:
176+ typedef std::basic_iostream<CharT, Traits> super;
177+
178+ void init() {
179+ this->rdbuf(&sbuf_);
180+ if (!sbuf_.socket().is_open()) this->setstate(std::ios::badbit);
181+ }
182+
183+ sockbuf sbuf_;
184+ };
185+}
186+
187+#endif // CLX_SOCKSTREAM_H
Added: svn:executable
--- trunk/ini.h (nonexistent)
+++ trunk/ini.h (revision 1)
@@ -0,0 +1,197 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * ini.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Fri 27 Feb 2008 04:13:16 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_INI_H
36+#define CLX_INI_H
37+
38+#include <iostream>
39+#include <string>
40+#include <vector>
41+#include <map>
42+#include <fstream>
43+#include <stdexcept>
44+#include "lexical_cast.h"
45+#include "salgorithm.h"
46+#include "syntax_error.h"
47+
48+namespace clx {
49+ /* --------------------------------------------------------------------- */
50+ // ini_container
51+ /* --------------------------------------------------------------------- */
52+ template <
53+ class Type,
54+ class CharT,
55+ class Traits = std::char_traits<CharT>
56+ >
57+ class ini_container : public std::map<std::basic_string<CharT, Traits>, Type> {
58+ public:
59+ typedef CharT char_type;
60+ typedef std::basic_string<CharT, Traits> string_type;
61+ typedef std::map<string_type, Type> container;
62+ typedef typename container::key_type key_type;
63+ typedef typename container::mapped_type mapped_type;
64+ typedef typename container::value_type value_type;
65+ typedef typename container::iterator iterator;
66+ typedef typename container::const_iterator const_iterator;
67+
68+ ini_container() : container() {}
69+
70+ std::pair<iterator, bool> insert(const string_type& s) {
71+ std::vector<string_type> v;
72+ split_if(s, v, is_any_of("="));
73+ if (v.empty()) return std::pair<iterator, bool>(this->end(), false);
74+ string_type key = v.at(0);
75+ string_type val;
76+ if (v.size() >= 2) val = v.at(1);
77+ return container::insert(value_type(key, lexical_cast<mapped_type>(val)));
78+ }
79+
80+ std::pair<iterator, bool> insert(const char_type* s) {
81+ string_type tmp(s);
82+ return this->insert(tmp);
83+ }
84+ };
85+
86+ /* --------------------------------------------------------------------- */
87+ // basic_ini
88+ /* --------------------------------------------------------------------- */
89+ template <
90+ class Type,
91+ class CharT = char,
92+ class Traits = std::char_traits<CharT>
93+ >
94+ class basic_ini : public std::map<std::basic_string<CharT, Traits>,
95+ ini_container<Type, CharT, Traits> > {
96+ public:
97+ typedef CharT char_type;
98+ typedef std::basic_string<CharT, Traits> string_type;
99+ typedef std::map<string_type, ini_container<Type, CharT, Traits> > container;
100+ typedef std::basic_istream<CharT, Traits> istream;
101+ typedef typename container::key_type key_type;
102+ typedef typename container::mapped_type mapped_type;
103+ typedef typename container::value_type value_type;
104+ typedef typename container::iterator iterator;
105+ typedef typename container::const_iterator const_iterator;
106+
107+ basic_ini() : container() {}
108+
109+ explicit basic_ini(istream& sin) : container() {
110+ this->read(sin);
111+ }
112+
113+ explicit basic_ini(const string_type& path) : container() {
114+ this->read(path);
115+ }
116+
117+ explicit basic_ini(const char_type* path) : container() {
118+ this->read(path);
119+ }
120+
121+ void read(istream& sin) {
122+ string_type cur;
123+ size_t n = 0;
124+
125+ string_type tmp;
126+ while (std::getline(sin, tmp)) {
127+ chomp(tmp);
128+ n++;
129+
130+ if (tmp.empty() || tmp.at(0) == ';') continue;
131+ else if (tmp.at(0) == '[') {
132+ size_t last = tmp.find(']');
133+ if (last == string_type::npos) throw syntax_error(n, "expected ']' token");
134+ cur = tmp.substr(1, last - 1);
135+ this->insert(cur);
136+ }
137+ else {
138+ if (cur.empty()) throw syntax_error(n, "expected section name");
139+ std::pair<typename mapped_type::iterator, bool> ret;
140+ ret = (*this)[cur].insert(tmp);
141+ if (!ret.second) throw syntax_error(n, "unknown error");
142+ }
143+ }
144+ }
145+
146+ void read(const string_type& path) {
147+ this->read(path.c_str());
148+ }
149+
150+ void read(const char_type* path) {
151+ std::basic_ifstream<CharT, Traits> fs(path);
152+ if (fs.fail()) {
153+ std::basic_stringstream<CharT, Traits> msg;
154+ msg << path << ": no such file or directory";
155+ throw std::runtime_error(msg.str());
156+ }
157+ this->read(fs);
158+ }
159+
160+ void insert(const string_type& section) {
161+ container::insert(value_type(s, mapped_type()));
162+ }
163+
164+ void insert(const char_type* section) {
165+ string_type tmp(s);
166+ this->insert(tmp);
167+ }
168+
169+ bool exist(const string_type& section) {
170+ if (this->find(section) == this->end()) return false;
171+ return true;
172+ }
173+
174+ bool exist(const char_type* section) {
175+ string_type tmp(section);
176+ return this->exist(tmp);
177+ }
178+
179+ bool exist(const string_type& section, const string_type& entry) {
180+ iterator p = this->find(section);
181+ if (p == this->end()) return false;
182+ typename mapped_type::iterator q = p->second.find(entry);
183+ if (q == p->second.end()) return false;
184+ return true;
185+ }
186+
187+ bool exist(const char_type* section, const char_type* entry) {
188+ string_type tmp1(section);
189+ string_type tmp2(entry);
190+ return this->exist(tmp1, tmp2);
191+ }
192+ };
193+
194+ typedef basic_ini<std::string> ini;
195+}
196+
197+#endif // CLX_INI_H
Added: svn:executable
--- trunk/constrained_value_func.h (nonexistent)
+++ trunk/constrained_value_func.h (revision 1)
@@ -0,0 +1,59 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * constrained_value_func.h
4+ *
5+ * Copyright (c) 2004 - 2006, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Reference: D. Ryan Stephens, Christopher Diggins, Janathan Turkanis,
33+ * and Jeff Cogswell, ``C++ Cookbook,'' O'Reilly Media, Inc, 2006.
34+ *
35+ * Last-modified: Mon 11 Dec 2006 04:02:00 JST
36+ */
37+/* ------------------------------------------------------------------------- */
38+#ifndef CLX_CONSTRAINED_VALUE_FUNC_H
39+#define CLX_CONSTRAINED_VALUE_FUNC_H
40+
41+#include <stdexcept>
42+
43+namespace clx {
44+ template <int Min, int Max>
45+ class ranged_integer {
46+ public:
47+ typedef int value_type;
48+ const static value_type default_value = Min;
49+
50+ static void assign(value_type& dest, const value_type& src) {
51+ if ((src < Min) || (src > Max)) {
52+ throw std::range_error("out of valied range");
53+ }
54+ dest = src;
55+ }
56+ };
57+};
58+
59+#endif // CLX_CONSTRAINED_VALUE_FUNC_H
Added: svn:executable
--- trunk/argument.h (nonexistent)
+++ trunk/argument.h (revision 1)
@@ -0,0 +1,253 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * argument.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Fri 08 Feb 2008 04:09:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_ARGUMENT_H
36+#define CLX_ARGUMENT_H
37+
38+#include <string>
39+#include <vector>
40+#include <map>
41+#include "lexical_cast.h"
42+#include "tokenizer.h"
43+
44+namespace clx {
45+ /* --------------------------------------------------------------------- */
46+ /*!
47+ * basic_argument_container
48+ *
49+ * BNF description of the argument is as follow:
50+ * <program> [<nokey_param>]* (['-'<key>] | ['-'+<key> [<param>]*])*
51+ *
52+ * The basic_argument_container class has two containers. One is
53+ * std::map<key, param>, and the other is std::vector<nokey_param>.
54+ * This class provides the interface that the upper class can control
55+ * these two containers as one container.
56+ */
57+ /* --------------------------------------------------------------------- */
58+ template <
59+ class CharT,
60+ class Traits = std::char_traits<CharT>
61+ >
62+ class basic_argument_container {
63+ public:
64+ typedef CharT char_type;
65+ typedef unsigned int size_type;
66+ typedef std::basic_string<CharT, Traits> string_type;
67+ typedef std::map<string_type, std::vector<string_type> > container;
68+ typedef typename container::key_type key_type;
69+ typedef typename container::mapped_type mapped_type;
70+ typedef typename container::value_type value_type;
71+
72+ // constructor and destructor
73+ basic_argument_container() : v_(), head_() {}
74+
75+ virtual ~basic_argument_container() {}
76+
77+ // operator
78+ bool insert(const key_type& x, const string_type& s) {
79+ typedef typename container::iterator iterator;
80+
81+ // if the argument is <nokey_param>, x is empty.
82+ if (x.empty()) {
83+ head_.push_back(s);
84+ return true;
85+ }
86+
87+ iterator pos = v_.find(x);
88+ if (pos == v_.end()) {
89+ mapped_type v;
90+ v.push_back(s);
91+ std::pair<iterator, bool> r = v_.insert(value_type(x, v));
92+ return r.second;
93+ }
94+ if (!s.empty()) pos->second.push_back(s);
95+ return true;
96+ }
97+
98+ // access
99+ mapped_type operator[](const key_type& x) {
100+ if (x.empty()) return head_;
101+ else return v_[x];
102+ }
103+
104+ bool exist(const key_type& x) {
105+ if (v_.find(x) != v_.end()) return true;
106+ else return false;
107+ }
108+
109+ private:
110+ container v_;
111+ mapped_type head_;
112+ };
113+
114+ /* --------------------------------------------------------------------- */
115+ // basic_argument
116+ /* --------------------------------------------------------------------- */
117+ template <
118+ class CharT,
119+ class Traits = std::char_traits<CharT>
120+ >
121+ class basic_argument {
122+ public:
123+ typedef CharT char_type;
124+ typedef int int_type;
125+ typedef unsigned int size_type;
126+ typedef std::basic_string<CharT, Traits> string_type;
127+ typedef basic_argument_container<CharT, Traits> container;
128+ typedef typename container::mapped_type param_list;
129+
130+ // constructor and destructor
131+ explicit basic_argument(char_type c = '-') :
132+ identifier_(c), program_(), v_() {}
133+
134+ explicit basic_argument(int_type argc, char_type* argv[], char_type c = '-') :
135+ identifier_(c), program_(), v_() {
136+ this->scan(argc, argv);
137+ }
138+
139+ virtual ~basic_argument() {}
140+
141+ // operator
142+ basic_argument& assign(int_type argc, char_type* argv[]) {
143+ program_ = argv[0];
144+ string_type key;
145+ bool eoh = false; // End of head parameter
146+ for (int_type i = 1; i < argc; i++) {
147+ string_type tmp(argv[i]);
148+ if (tmp.at(0) == identifier_) {
149+ eoh = true;
150+ if (tmp.at(1) != identifier_ && tmp.size() > 2) {
151+ for (size_type j = 1; j < tmp.size(); j++) {
152+ key = tmp.at(j);
153+ v_.insert(key, "");
154+ }
155+ key = "";
156+ }
157+ else {
158+ key = tmp.substr(tmp.find_first_not_of(identifier_));
159+ if (i + 1 == argc || argv[i+1][0] == identifier_) v_.insert(key, "");
160+ }
161+ }
162+ else {
163+ if (eoh && key.empty()) break;
164+ v_.insert(key, tmp);
165+ }
166+ }
167+ return *this;
168+ }
169+
170+ // access
171+ string_type program() { return program_; }
172+ param_list head() { return v_[string_type("")]; }
173+ param_list operator[](const string_type& x) { return v_[x]; }
174+
175+ param_list operator[](const char_type* x) {
176+ string_type tmp(x);
177+ return (*this)[tmp];
178+ }
179+
180+ param_list operator[](char_type x) {
181+ string_type tmp(1, x);
182+ return (*this)[tmp];
183+ }
184+
185+ bool exist(const string_type& x) {
186+ functor f(x);
187+ for (size_type i = 0; i < f.size(); i++) {
188+ if (v_.exist(f.at(i))) return true;
189+ }
190+ return false;
191+ }
192+
193+ bool exist(const char_type* x) {
194+ string_type tmp(x);
195+ return this->exist(tmp);
196+ }
197+
198+ bool exist(char_type x) {
199+ string_type tmp(1, x);
200+ return v_.exist(tmp);
201+ }
202+
203+ template <class ValueT>
204+ basic_argument& operator()(ValueT& dest, size_type n = 0) {
205+ return this->get_param(dest, n);
206+ }
207+
208+ template <class ValueT>
209+ basic_argument& operator()(const string_type& key, ValueT& dest, size_type n = 0) {
210+ return this->get_param(key, dest, n);
211+ }
212+
213+ template <class ValueT>
214+ basic_argument& operator()(const char_type* key, ValueT& dest, size_type n = 0) {
215+ string_type tmp(key);
216+ return (*this)(tmp, dest, n);
217+ }
218+
219+ template <class ValueT>
220+ basic_argument& operator()(char_type key, ValueT& dest, size_type n = 0) {
221+ string_type tmp(1, key);
222+ return (*this)(tmp, dest, n);
223+ }
224+
225+ private:
226+ typedef basic_tokenizer<escape_separator<CharT, Traits>, string_type> functor;
227+
228+ char_type identifier_;
229+ string_type program_;
230+ container v_;
231+
232+ template <class ValueT>
233+ basic_argument& get_param(ValueT& dest, size_type n) {
234+ if (n < this->head().size()) dest = lexical_cast<ValueT>(this->head().at(n));
235+ return *this;
236+ }
237+
238+ template <class ValueT>
239+ basic_argument& get_param(const string_type& key, ValueT& dest, size_type n) {
240+ functor f(key);
241+ for (size_type i = 0; i < f.size(); i++) {
242+ if (v_.exist(f.at(i)) && n < v_[f.at(i)].size() && !v_[f.at(i)].at(0).empty()) {
243+ dest = lexical_cast<ValueT>(v_[f.at(i)][n]);
244+ }
245+ }
246+ return *this;
247+ }
248+ };
249+
250+ typedef basic_argument<char> argument;
251+}
252+
253+#endif // CLX_ARGUMENT_H
Added: svn:executable
--- trunk/tokenizer_func.h (nonexistent)
+++ trunk/tokenizer_func.h (revision 1)
@@ -0,0 +1,289 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * tokenizer_func.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Fri 16 Mar 2007 23:50:41 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_TOKENIZER_FUNC_H
36+#define CLX_TOKENIZER_FUNC_H
37+
38+#include <algorithm>
39+#include <cstdlib>
40+#include <string>
41+#include "lexical_cast.h"
42+
43+namespace clx {
44+ /* --------------------------------------------------------------------- */
45+ /*!
46+ * char_separator
47+ *
48+ * The char_separator class breaks a sequence of characters into
49+ * tokens based on the character delimiters.
50+ */
51+ /* --------------------------------------------------------------------- */
52+ template <
53+ class CharT,
54+ class Traits = std::char_traits<CharT>
55+ >
56+ class char_separator {
57+ public:
58+ typedef CharT char_type;
59+ typedef typename std::basic_string<CharT, Traits> string_type;
60+
61+ // constructor and destructor
62+ char_separator() : c_(1, ' '), emtok_(false) {}
63+
64+ explicit char_separator(char_type c, bool x = false) :
65+ c_(1, c), emtok_(false) {}
66+
67+ explicit char_separator(const char_type* c, bool x = false) :
68+ c_(c), emtok_(x) {}
69+
70+ explicit char_separator(const string_type& c, bool x = false) :
71+ c_(c), emtok_(x) {}
72+
73+ // operator
74+ void reset() { return; }
75+
76+ bool empty_token(bool x) {
77+ emtok_ = x;
78+ return emtok_;
79+ }
80+
81+ template <class InputIterator, class Token>
82+ bool operator()(InputIterator& next, InputIterator last, Token& dest) {
83+ dest = Token();
84+
85+ if (next == last) return 0;
86+ if (!emtok_) { // invalid empty token, so skip continuous delimiters.
87+ while (std::find(c_.begin(), c_.end(), *next) != c_.end()) {
88+ if (++next == last) return false;
89+ }
90+ }
91+
92+ while (next != last) { // obtain the next token.
93+ if (std::find(c_.begin(), c_.end(), *next) != c_.end()) break;
94+ dest += *next;
95+ next++;
96+ }
97+ return true;
98+ }
99+
100+ private:
101+ string_type c_;
102+ bool emtok_;
103+
104+ };
105+
106+ /* --------------------------------------------------------------------- */
107+ /*!
108+ * escape_separator
109+ *
110+ * The escape_separator class is a super-set of what is commonly known
111+ * as a comma separated value (csv) list.
112+ */
113+ /* --------------------------------------------------------------------- */
114+ template <
115+ class CharT,
116+ class Traits = std::char_traits<CharT>
117+ >
118+ class escape_separator {
119+ public:
120+ typedef CharT char_type;
121+ typedef typename std::basic_string<CharT, Traits> string_type;
122+
123+ // constructor and destructor
124+ escape_separator() : c_(1, ','), quote_(1, '\\'), esc_(1, '\"'), emtok_(false) {}
125+
126+ explicit escape_separator(char_type c, char_type q, char_type e, bool x = false) :
127+ c_(1, c), quote_(1, q), esc_(1, e), emtok_(x) {}
128+
129+ explicit escape_separator(const char_type* c, const char_type* q, const char_type* e, bool x = false) :
130+ c_(c), quote_(q), esc_(e), emtok_(x) {}
131+
132+ explicit escape_separator(const string_type& c, const string_type& q, const string_type& e, bool x = false) :
133+ c_(c), quote_(q), esc_(e), emtok_(x) {}
134+
135+ // operator
136+ void reset() { return; }
137+
138+ bool empty_token(bool x) {
139+ emtok_ = x;
140+ return emtok_;
141+ }
142+
143+ template <class InputIterator, class Token>
144+ bool operator()(InputIterator& next, InputIterator last, Token& dest) {
145+ bool inq = false;
146+ dest = Token();
147+
148+ if (next == last) return 0;
149+ if (!emtok_) { // invalid empty token, so skip continuous delimiters.
150+ while (std::find(c_.begin(), c_.end(), *next) != c_.end()) {
151+ if (++next == last) return false;
152+ }
153+ }
154+
155+ while (next != last) { // obtain the next token.
156+ if (std::find(quote_.begin(), quote_.end(), *next) != quote_.end()) inq = !inq;
157+ else if (!inq && std::find(c_.begin(), c_.end(), *next) != c_.end()) break;
158+ else {
159+ if (std::find(esc_.begin(), esc_.end(), *next) != esc_.end()) next++;
160+ dest += *next;
161+ }
162+ next++;
163+ }
164+ return true;
165+ }
166+
167+ private:
168+ string_type c_;
169+ string_type quote_;
170+ string_type esc_;
171+ bool emtok_;
172+ };
173+
174+ /* --------------------------------------------------------------------- */
175+ /*!
176+ * format_separator
177+ *
178+ * The format separator class is split a string according to the
179+ * given format. The behavior of this class is similar to scanf()
180+ * function.
181+ */
182+ /* --------------------------------------------------------------------- */
183+ template <
184+ class CharT,
185+ class Traits = std::char_traits<CharT>
186+ >
187+ class format_separator {
188+ public:
189+ typedef CharT char_type;
190+ typedef typename std::basic_string<CharT, Traits> string_type;
191+ typedef typename string_type::iterator iterator;
192+
193+ // constructor and destructor
194+ format_separator() : fmt_("%s"), ids_(1, 's'), skipws_(true) {}
195+
196+ explicit format_separator(const char_type* fmt, char_type idset = 's', bool x = true) :
197+ fmt_(fmt), ids_(1, idset), skipws_(x) {
198+ cur_ = fmt_.begin();
199+ }
200+
201+ explicit format_separator(const string_type& fmt, char_type idset = 's', bool x = true) :
202+ fmt_(fmt), ids_(1, idset), skipws_(x) {
203+ cur_ = fmt_.begin();
204+ }
205+
206+ explicit format_separator(const char_type* fmt, const char_type* idset, bool x = true) :
207+ fmt_(fmt), ids_(idset), skipws_(x) {
208+ cur_ = fmt_.begin();
209+ }
210+
211+ explicit format_separator(const string_type& fmt, const string_type& idset, bool x = true) :
212+ fmt_(fmt), ids_(idset), skipws_(x) {
213+ cur_ = fmt_.begin();
214+ }
215+
216+ virtual ~format_separator() {}
217+
218+ // operator
219+ void reset() { cur_ = fmt_.begin(); }
220+
221+ bool skipws(bool x) {
222+ skipws_ = x;
223+ return skipws_;
224+ }
225+
226+ template <class InputIterator, class Token>
227+ bool operator()(InputIterator& next, InputIterator last, Token& dest) {
228+ char_type c;
229+ return this->next_token(next, last, c, dest);
230+ }
231+
232+ // method for basic_tokenmap<>
233+ template <class InputIterator, class Key, class Token>
234+ bool operator()(InputIterator& next, InputIterator last, Key& key, Token& dest) {
235+ return this->next_token(next, last, key, dest);
236+ }
237+
238+ private:
239+ static const char_type rep = '%';
240+ static const char_type eos = '\0';
241+ static const char_type ws = ' ';
242+
243+ string_type fmt_;
244+ string_type ids_;
245+ bool skipws_;
246+ iterator cur_;
247+
248+ template <class InputIterator, class Key, class Token>
249+ bool next_token(InputIterator& next, InputIterator last, Key& key, Token& dest) {
250+ while (next != last && cur_ != fmt_.end()) {
251+ if (skipws_ && *next == ws && *cur_ != ws) next++;
252+ else if (*next == rep) {
253+ if (cur_ + 1 != fmt_.end() && *cur_ == rep && *(cur_ + 1) == rep) {
254+ next++;
255+ cur_ += 2;
256+ }
257+ else break;
258+ }
259+ else if (*cur_ == rep) {
260+ string_type flag;
261+ while (++cur_ != fmt_.end() && ids_.find(*cur_) == string_type::npos) flag += *cur_;
262+ if (cur_ == fmt_.end()) break;
263+ key = *cur_++;
264+ char_type c = (cur_ != fmt_.end()) ? *cur_++ : eos;
265+ dest = this->get(next, last, c, flag);
266+ return true;
267+ }
268+ else if (*cur_ == *next) { next++; cur_++; }
269+ else break;
270+ }
271+ return false;
272+ }
273+
274+ template <class InputIterator>
275+ string_type get(InputIterator& next, InputIterator last, char_type c, const string_type& flag) {
276+ int n = -1;
277+ if (!flag.empty() && isdigit(flag.at(0))) n = lexical_cast<int>(flag);
278+
279+ string_type dest;
280+ while (next != last && *next != c && n-- != 0) {
281+ dest += *next;
282+ next++;
283+ }
284+ return dest;
285+ }
286+ };
287+}
288+
289+#endif // CLX_TOKENIZER_FUNC_H
Added: svn:executable
--- trunk/udp.h (nonexistent)
+++ trunk/udp.h (revision 1)
@@ -0,0 +1,199 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * udp.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 10 Jul 2008 08:38:23 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_UDP_H
36+#define CLX_UDP_H
37+
38+#include <cstdlib>
39+#include <cstring>
40+#include <string>
41+#include "socket.h"
42+#include "sockaddress.h"
43+#include "sockstream.h"
44+
45+namespace clx {
46+ namespace udp {
47+ /* ----------------------------------------------------------------- */
48+ // basic_socket
49+ /* ----------------------------------------------------------------- */
50+ template <
51+ int Family,
52+ class CharT = char,
53+ class Traits = std::char_traits<CharT>
54+ >
55+ class basic_socket : public basic_rawsocket<SOCK_DGRAM, Family> {
56+ public:
57+ typedef basic_rawsocket<SOCK_DGRAM, Family> rawsocket;
58+ typedef basic_sockaddress<Family, IPPROTO_UDP> sockaddress;
59+ typedef CharT char_type;
60+ typedef std::basic_string<CharT, Traits> string_type;
61+
62+ // constructor and destructor
63+ basic_socket() : rawsocket(), from_(), to_(), is_bind_(false) {}
64+
65+ explicit basic_socket(const char_type* host, int port) :
66+ rawsocket(), from_(), to_(), is_bind_(false) {
67+ this->connect(host, port);
68+ }
69+
70+ explicit basic_socket(const string_type& host, int port) :
71+ rawsocket(), from_(), to_(), is_bind_(false) {
72+ this->connect(host, port);
73+ }
74+
75+ explicit basic_socket(const char_type* host, const char_type* service) :
76+ rawsocket(), from_(), to_(), is_bind_(false) {
77+ this->connect(host, service);
78+ }
79+
80+ explicit basic_socket(const string_type& host, const string_type& service) :
81+ rawsocket(), from_(), to_(), is_bind_(false) {
82+ this->connect(host, service);
83+ }
84+
85+ explicit basic_socket(int port) :
86+ rawsocket(), from_(), to_(), is_bind_(false) {
87+ this->bind(port);
88+ }
89+
90+ explicit basic_socket(const char_type* service) :
91+ rawsocket(), from_(), to_(), is_bind_(false) {
92+ this->bind(service);
93+ }
94+
95+ virtual ~basic_socket() {}
96+
97+ // operator
98+ basic_socket& connect(const char_type* host, int port) {
99+ to_.assign(host, port);
100+ this->open();
101+ return *this;
102+ }
103+
104+ basic_socket& connect(const string_type& host, int port) {
105+ return this->connect(host.c_str(), port);
106+ }
107+
108+ basic_socket& connect(const char_type* host, const char_type* service) {
109+ to_.assign(host, service);
110+ this->open();
111+ return *this;
112+ }
113+
114+ basic_socket& connect(const string_type& host, const string_type& service) {
115+ return this->connect(host.c_str(), service.c_str());
116+ }
117+
118+ int bind() {
119+ this->open();
120+ if (::bind(this->socket(), (struct sockaddr*)to_.data(), to_.size()) == -1) return -1;
121+ is_bind_ = true;
122+ return 0;
123+ }
124+
125+ int bind(int port) {
126+ to_.assign(port);
127+ return this->bind();
128+ }
129+
130+ int bind(const char_type* service) {
131+ to_.assign(service);
132+ return this->bind();
133+ }
134+
135+ int bind(const string_type& service) {
136+ to_.assign(service);
137+ return this->bind();
138+ }
139+
140+ int send_to(const char_type* src, int n, const sockaddress& addr) {
141+ return ::sendto(this->socket(), src, n, 0, (struct sockaddr*)addr.data(), addr.size());
142+ }
143+
144+ int send_to(const string_type& src, const sockaddress& addr) {
145+ return this->send_to(src.c_str(), src.size(), addr);
146+ }
147+
148+ int send_to(const char_type* src, int n, const char_type* host, int port) {
149+ sockaddress addr(host, port);
150+ return this->send_to(src, n, addr);
151+ }
152+
153+ int send_to(const string_type& src, const string_type& host, int port) {
154+ sockaddress addr(host, port);
155+ return this->send_to(src, addr);
156+ }
157+
158+ int send(const char_type* src, int n) {
159+ int len = -1;
160+
161+ if (!this->is_bind()) len = this->send_to(src, n, to_);
162+ else len = this->send_to(src, n, from_);
163+ return len;
164+ }
165+
166+ int send(const string_type& src) {
167+ return this->send(src.c_str(), src.size());
168+ }
169+
170+ int recv(char_type* dest, int n) {
171+ if (!this->is_bind()) return -1;
172+
173+ typename sockaddress::inet_type tmp;
174+ int len = sizeof(tmp);
175+ std::memset(dest, 0, n);
176+
177+ int ret = ::recvfrom(this->socket(), dest, n, 0, (struct sockaddr*)&tmp, &len);
178+ if (ret > 0) from_ = tmp;
179+ return ret;
180+ }
181+
182+ // accessor
183+ bool is_bind() const { return is_bind_; }
184+ const sockaddress& from() const { return from_; }
185+ const sockaddress& to() const { return to_; }
186+
187+ private:
188+ sockaddress from_;
189+ sockaddress to_;
190+ bool is_bind_;
191+ };
192+
193+ typedef basic_socket<AF_INET> socket;
194+ typedef basic_sockaddress<AF_INET, IPPROTO_UDP> sockaddress;
195+ typedef basic_sockstream<socket> sockstream;
196+ };
197+}
198+
199+#endif // CLX_UDP_H
Added: svn:executable
--- trunk/adjust.h (nonexistent)
+++ trunk/adjust.h (revision 1)
@@ -0,0 +1,108 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * adjust.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 26 Apr 2007 19:55:04 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_ADJUST_H
36+#define CLX_ADJUST_H
37+
38+#include <string>
39+
40+namespace clx {
41+ /* --------------------------------------------------------------------- */
42+ // ljust
43+ /* --------------------------------------------------------------------- */
44+ template <class CharT, class Traits>
45+ std::basic_string<CharT, Traits>& ljust(std::basic_string<CharT, Traits>& s,
46+ unsigned int n, CharT c = ' ') {
47+ if (s.length() < n) s.append(n - s.length(), c);
48+ return s;
49+ }
50+
51+ /* --------------------------------------------------------------------- */
52+ // ljust_copy
53+ /* --------------------------------------------------------------------- */
54+ template <class CharT, class Traits>
55+ std::basic_string<CharT, Traits> ljust_copy(const std::basic_string<CharT, Traits>& s,
56+ unsigned int n, CharT c = ' ') {
57+ std::basic_string<CharT, Traits> tmp(s);
58+ return ljust(tmp, n, c);
59+ }
60+
61+ /* --------------------------------------------------------------------- */
62+ // rjust
63+ /* --------------------------------------------------------------------- */
64+ template <class CharT, class Traits>
65+ std::basic_string<CharT, Traits>& rjust(std::basic_string<CharT, Traits>& s,
66+ unsigned int n, CharT c = ' ') {
67+ if (s.length() < n) s.insert(s.begin(), n - s.length(), c);
68+ return s;
69+ }
70+
71+ /* --------------------------------------------------------------------- */
72+ // rjust_copy
73+ /* --------------------------------------------------------------------- */
74+ template <class CharT, class Traits>
75+ std::basic_string<CharT, Traits> rjust_copy(const std::basic_string<CharT, Traits>& s,
76+ unsigned int n, CharT c = ' ') {
77+ std::basic_string<CharT, Traits> tmp(s);
78+ return rjust(tmp, n, c);
79+ }
80+
81+ /* --------------------------------------------------------------------- */
82+ // center
83+ /* --------------------------------------------------------------------- */
84+ template <class CharT, class Traits>
85+ std::basic_string<CharT, Traits>& center(std::basic_string<CharT, Traits>& s,
86+ unsigned int n, CharT c = ' ') {
87+ if (s.length() < n) {
88+ unsigned int quo = (n - s.length()) / 2;
89+ unsigned int odd = (n - s.length()) % 2;
90+ ljust(s, quo + s.length(), c);
91+ if (odd != 0) quo++;
92+ rjust(s, quo + s.length(), c);
93+ }
94+ return s;
95+ }
96+
97+ /* --------------------------------------------------------------------- */
98+ // center_copy
99+ /* --------------------------------------------------------------------- */
100+ template <class CharT, class Traits>
101+ std::basic_string<CharT, Traits> center_copy(const std::basic_string<CharT, Traits>& s,
102+ unsigned int n, CharT c = ' ') {
103+ std::basic_string<CharT, Traits> tmp(s);
104+ return center(tmp, n, c);
105+ }
106+}
107+
108+#endif // CLX_ADJUST_H
Added: svn:executable
--- trunk/lexical_cast.h (nonexistent)
+++ trunk/lexical_cast.h (revision 1)
@@ -0,0 +1,144 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * lexical_cast.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Distributed under the Boost Software License, Version 1.0. (See
33+ * accompanying file LICENSE_1_0.txt or copy at
34+ * http://www.boost.org/LICENSE_1_0.txt)
35+ *
36+ * Last-modified: Fri 11 Jul 2008 08:38:00 JST
37+ */
38+/* ------------------------------------------------------------------------- */
39+#ifndef CLX_LEXICAL_CAST_H
40+#define CLX_LEXICAL_CAST_H
41+
42+#include <sstream>
43+#include <string>
44+#include <iomanip>
45+#include <iostream>
46+
47+namespace clx {
48+ /* --------------------------------------------------------------------- */
49+ // bad_lexical_cast
50+ /* --------------------------------------------------------------------- */
51+ class bad_lexical_cast : public std::bad_cast {
52+ public:
53+ bad_lexical_cast() : source_(&typeid(void)), target_(&typeid(void)) {}
54+ explicit bad_lexical_cast(const std::type_info& source, const std::type_info& target) :
55+ source_(&source), target_(&target) {}
56+ virtual ~bad_lexical_cast() throw() {}
57+
58+ const std::type_info& source_type() const { return *source_; }
59+ const std::type_info& target_type() const { return *target_; }
60+
61+ virtual const char* what() const throw() {
62+ return "bad lexical cast";
63+ }
64+
65+ private:
66+ const std::type_info* source_;
67+ const std::type_info* target_;
68+ };
69+
70+ /* --------------------------------------------------------------------- */
71+ // cast_stream
72+ /* --------------------------------------------------------------------- */
73+ template <
74+ class Type,
75+ class Source
76+ >
77+ class cast_stream {
78+ public:
79+ typedef Source src_type;
80+ typedef Type dest_type;
81+ typedef std::string string_type;
82+ typedef std::stringstream internal_stream;
83+
84+ // constructor and destructor
85+ explicit cast_stream(std::ios::fmtflags base = std::ios::dec) : ss_() {
86+ ss_.setf(base, std::ios::basefield);
87+ }
88+
89+ virtual ~cast_stream() {}
90+
91+ // operator
92+ bool operator<<(const src_type& src) {
93+ ss_ << std::setiosflags(std::ios::fixed);
94+ return !(ss_ << src).fail();
95+ }
96+
97+ template <class ValueT>
98+ bool operator>>(ValueT& dest) {
99+ return ss_ >> dest;
100+ }
101+
102+ // speciallized for string_type;
103+ bool operator>>(string_type& dest) {
104+ dest = ss_.str();
105+ return true;
106+ }
107+
108+ private:
109+ internal_stream ss_;
110+ };
111+
112+ /* --------------------------------------------------------------------- */
113+ /*!
114+ * lexical_cast
115+ *
116+ * The lexical_cast class is a sub-set class of boost::lexical_cast.
117+ * Scale back the function of wide string support.
118+ */
119+ /* --------------------------------------------------------------------- */
120+ template <class Type, class Source>
121+ Type lexical_cast(const Source& src, std::ios::fmtflags base = std::ios::dec) {
122+ cast_stream<Type, Source> ss(base);
123+ Type dest;
124+
125+ if(!(ss << src && ss >> dest)) {
126+ throw(bad_lexical_cast(typeid(Source), typeid(Type)));
127+ }
128+ return dest;
129+ };
130+
131+ /* --------------------------------------------------------------------- */
132+ // specialized lexical_cast
133+ /* --------------------------------------------------------------------- */
134+ template <> char lexical_cast(const char& src, std::ios::fmtflags base) { return src; }
135+ template <> int lexical_cast(const int& src, std::ios::fmtflags base) { return src; }
136+ template <> long lexical_cast(const long& src, std::ios::fmtflags base) { return src; }
137+ template <> unsigned char lexical_cast(const unsigned char& src, std::ios::fmtflags base) { return src; }
138+ template <> unsigned int lexical_cast(const unsigned int& src, std::ios::fmtflags base) { return src; }
139+ template <> unsigned long lexical_cast(const unsigned long& src, std::ios::fmtflags base) { return src; }
140+ template <> double lexical_cast(const double& src, std::ios::fmtflags base) { return src; }
141+ template <> std::string lexical_cast(const std::string& src, std::ios::fmtflags base) { return src; }
142+}
143+
144+#endif // CLX_LEXICAL_CAST_H
Added: svn:executable
--- trunk/date_time.h (nonexistent)
+++ trunk/date_time.h (revision 1)
@@ -0,0 +1,204 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * date_time.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 14 Jun 2007 14:38:02 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_DATE_TIME_H
36+#define CLX_DATE_TIME_H
37+
38+#include <ctime>
39+#include <stdexcept>
40+#include "time_duration.h"
41+
42+namespace clx {
43+ /* --------------------------------------------------------------------- */
44+ // date_time
45+ /* --------------------------------------------------------------------- */
46+ class date_time {
47+ public:
48+ typedef time_value value_type;
49+ typedef std::tm tm_type;
50+
51+ // constructor and destructor
52+ date_time() {
53+ std::time_t t = std::time(NULL);
54+ this->assign(t);
55+ }
56+
57+ explicit date_time(const tm_type& t) { this->assign(t); }
58+ explicit date_time(const std::time_t& t) { this->assign(t); }
59+
60+ explicit date_time(value_type& y, value_type& m, value_type& d,
61+ value_type& h, value_type& mm, value_type& s) {
62+ this->assign(y, m, d, h, mm, s);
63+ }
64+
65+ date_time& operator=(const date_time& t) { return this->assign(t); }
66+ date_time& operator=(const tm_type& t) { return this->assign(t); }
67+ date_time& operator=(const std::time_t& t) { return this->assign(t); }
68+
69+ date_time& operator+=(value_type t) { return this->after(t); }
70+ date_time& operator+=(const date_duration& t) { return this->after(t); }
71+ date_time& operator-=(value_type t) { return this->before(t); }
72+ date_time& operator-=(const date_duration& t) { return this->before(t); }
73+
74+ double operator-(const date_time& t) const { return this->difference(t); }
75+ double operator-(const std::time_t& t) const { return this->difference(t); }
76+ double operator-(const tm_type& t) const { return this->difference(t); }
77+
78+ // binary operator
79+ friend date_time operator+(date_time x, value_type y) { return x += y; }
80+ friend date_time operator+(date_time x, const date_duration& y) { return x += y; }
81+ friend date_time operator-(date_time x, value_type y) { return x -= y; }
82+ friend date_time operator-(date_time x, const date_duration& y) { return x -= y; }
83+ friend date_time operator+(value_type y, date_time x) { return x += y; }
84+ friend date_time operator+(const date_duration& y, date_time x) { return x += y; }
85+ friend date_time operator-(value_type y, date_time x) { return x -= y; }
86+ friend date_time operator-(const date_duration& y, date_time x) { return x -= y; }
87+
88+ // comparison operator
89+ friend bool operator<(const date_time& x, const date_time& y) {
90+ if (std::difftime(y.c_time(), x.c_time()) > 0.01) return true;
91+ return false;
92+ }
93+
94+ friend bool operator>(const date_time& x, const date_time& y) {
95+ if (std::difftime(x.c_time(), y.c_time()) > 0.01) return true;
96+ return false;
97+ }
98+
99+ friend bool operator<=(const date_time& x, const date_time& y) {
100+ return !(x > y);
101+ }
102+
103+ friend bool operator>=(const date_time& x, const date_time& y) {
104+ return !(x < y);
105+ }
106+
107+ friend bool operator==(const date_time& x, const date_time& y) {
108+ if (!(x < y) && !(x > y)) return true;
109+ return false;
110+ }
111+
112+ friend bool operator!=(const date_time& x, const date_time& y) {
113+ return !(x == y);
114+ }
115+
116+ // accessor
117+ value_type year() const { return date_.tm_year + 1900; }
118+ value_type mon() const { return date_.tm_mon + 1; }
119+ value_type day() const { return date_.tm_mday; }
120+ value_type hour() const { return date_.tm_hour; }
121+ value_type min() const { return date_.tm_min; }
122+ value_type sec() const { return date_.tm_sec; }
123+ value_type totaldays() const { return date_.tm_yday; }
124+
125+ // get information as the traditional std::tm type
126+ const tm_type& reference() const { return date_; }
127+ const tm_type* data() const { return &date_; }
128+
129+ std::time_t c_time() const {
130+ tm_type tmp = date_;
131+ return std::mktime(&tmp);
132+ }
133+
134+ date_time& assign(const date_time& t) {
135+ return this->assign(t.date_);
136+ }
137+
138+ date_time& assign(const tm_type& t) {
139+ date_ = t;
140+ return *this;
141+ }
142+
143+ date_time& assign(const std::time_t& t) {
144+ struct tm tval;
145+ ::localtime_r(&t, &tval);
146+ return this->assign(tval);
147+ }
148+
149+ date_time& assign(value_type y, value_type m, value_type d,
150+ value_type h, value_type mm, value_type s) {
151+ date_.tm_year = y - 1900;
152+ date_.tm_mon = m - 1;
153+ date_.tm_mday = d;
154+ date_.tm_hour = h;
155+ date_.tm_min = mm;
156+ date_.tm_sec = s;
157+ return *this;
158+ }
159+
160+ date_time& after(value_type t) {
161+ return this->assign(this->c_time() + t);
162+ }
163+
164+ date_time& after(const date_duration& t) {
165+ date_.tm_year += t.years();
166+ date_.tm_mon += t.months();
167+ date_.tm_mday += t.days();
168+
169+ return this->assign(this->c_time());
170+ }
171+
172+ date_time& before(value_type t) {
173+ std::time_t tmp = this->c_time() - t;
174+ if (tmp < 0) throw std::range_error("negative value");
175+ return this->assign(tmp);
176+ }
177+
178+ date_time& before(const date_duration& t) {
179+ date_.tm_year -= t.years();
180+ date_.tm_mon -= t.months();
181+ date_.tm_mday -= t.days();
182+
183+ return this->assign(this->c_time());
184+ }
185+
186+ double difference(const date_time& t) const {
187+ return std::difftime(this->c_time(), t.c_time());
188+ }
189+
190+ double difference(const std::time_t& t) const {
191+ return std::difftime(this->c_time(), t);
192+ }
193+
194+ double difference(const std::tm& t) const {
195+ std::time_t tmp = std::mktime(const_cast<std::tm*>(&t));
196+ return std::difftime(this->c_time(), tmp);
197+ }
198+
199+ private:
200+ tm_type date_;
201+ };
202+}
203+
204+#endif // CLX_DATE_TIME_H
Added: svn:executable
--- trunk/split.h (nonexistent)
+++ trunk/split.h (revision 1)
@@ -0,0 +1,101 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * split.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 26 Apr 2007 19:12:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_SPLIT_H
36+#define CLX_SPLIT_H
37+
38+#include <iterator>
39+#include "predicate.h"
40+#include "lexical_cast.h"
41+
42+namespace clx {
43+ /* --------------------------------------------------------------------- */
44+ // split_if
45+ /* --------------------------------------------------------------------- */
46+ template <class CharT, class Traits, class Container, class PredicateFunc>
47+ Container& split_if(const std::basic_string<CharT, Traits>& s, Container& result,
48+ PredicateFunc f, bool emtok = false) {
49+ typedef typename Container::value_type value_type;
50+ typedef typename std::basic_string<CharT, Traits>::const_iterator const_iterator;
51+
52+ std::basic_string<CharT, Traits> tmp;
53+ std::insert_iterator<Container> it(result, result.begin());
54+ for (const_iterator pos = s.begin(); pos != s.end(); pos++) {
55+ if (!f(*pos)) tmp += *pos;
56+ else if (emtok || !tmp.empty()) {
57+ *it++ = lexical_cast<value_type>(tmp);
58+ tmp.erase();
59+ }
60+ }
61+ if (!tmp.empty()) *it++ = lexical_cast<value_type>(tmp);
62+
63+ return result;
64+ }
65+
66+ /* --------------------------------------------------------------------- */
67+ // split
68+ /* --------------------------------------------------------------------- */
69+ template <class CharT, class Traits, class Container>
70+ Container& split(const std::basic_string<CharT, Traits>& s, Container& result,
71+ bool emtok = false, const std::locale& loc = std::locale()) {
72+ return split_if(result, s, is_space(loc), emtok);
73+ }
74+
75+ /* --------------------------------------------------------------------- */
76+ // join
77+ /* --------------------------------------------------------------------- */
78+ template <class CharT, class Traits, class Container>
79+ std::basic_string<CharT, Traits>& join(const Container& v,
80+ std::basic_string<CharT, Traits>& result, const std::basic_string<CharT, Traits>& delim) {
81+ typename Container::const_iterator pos = v.begin();
82+ if (pos != v.end()) result += *pos++;
83+ while (pos != v.end()) {
84+ result += delim;
85+ result += *pos++;
86+ }
87+ return result;
88+ }
89+
90+ /* --------------------------------------------------------------------- */
91+ // join
92+ /* --------------------------------------------------------------------- */
93+ template <class CharT, class Traits, class Container>
94+ std::basic_string<CharT, Traits>& join(const Container& v,
95+ std::basic_string<CharT, Traits>& result, const CharT* delim) {
96+ std::basic_string<CharT, Traits> tmp(delim);
97+ return join(v, result, tmp);
98+ }
99+}
100+
101+#endif // CLX_SPLIT_H
Added: svn:executable
--- trunk/time.h (nonexistent)
+++ trunk/time.h (revision 1)
@@ -0,0 +1,43 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * time.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 14 Jun 2007 10:12:02 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_TIME_H
36+#define CLX_TIME_H
37+
38+#include "date_time.h"
39+#include "time_duration.h"
40+#include "ioftime.h"
41+#include "timer.h"
42+
43+#endif // CLX_TIME_H
Added: svn:executable
--- trunk/uri.h (nonexistent)
+++ trunk/uri.h (revision 1)
@@ -0,0 +1,107 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * uri.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Tue 06 Nov 2007 02:17:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_URI_H
36+#define CLX_URI_H
37+
38+#include <string>
39+#include <sstream>
40+#include "lexical_cast.h"
41+#include "predicate.h"
42+
43+namespace clx {
44+ namespace uri {
45+ /* ----------------------------------------------------------------- */
46+ // is_reserved
47+ /* ----------------------------------------------------------------- */
48+ template <class CharT>
49+ bool is_reserved(CharT c) {
50+ if (is_alpha()(c) || is_any_of("-_.~ ")(c)) return false;
51+ return true;
52+ }
53+
54+ /* ----------------------------------------------------------------- */
55+ // encode
56+ /* ----------------------------------------------------------------- */
57+ template <class CharT, class Traits>
58+ std::basic_string<CharT, Traits> encode(const std::basic_string<CharT, Traits>& s) {
59+ std::basic_ostringstream<CharT, Traits> ss;
60+ for (unsigned int i = 0; i < s.size(); i++) {
61+ if (s.at(i) == ' ') ss << '+';
62+ else if (is_reserved(s.at(i))) {
63+ ss << '%' << std::hex << static_cast<unsigned int>(s.at(i));
64+ }
65+ else ss << s.at(i);
66+ }
67+ return ss.str();
68+ }
69+
70+ /* ----------------------------------------------------------------- */
71+ // encode
72+ /* ----------------------------------------------------------------- */
73+ template <class CharT>
74+ std::basic_string<CharT> encode(const CharT* s) {
75+ std::basic_string<CharT> tmp(s);
76+ return encode(tmp);
77+ }
78+
79+ /* ----------------------------------------------------------------- */
80+ // decode
81+ /* ----------------------------------------------------------------- */
82+ template <class CharT, class Traits>
83+ std::basic_string<CharT, Traits> decode(const std::basic_string<CharT, Traits>& s) {
84+ std::basic_ostringstream<CharT, Traits> ss;
85+ for (unsigned int i = 0; i < s.size(); i++) {
86+ if (s.at(i) == '%') {
87+ unsigned int c = lexical_cast<unsigned int>(s.substr(i + 1, 2), std::ios::hex);
88+ ss << static_cast<CharT>(c);
89+ i += 2;
90+ }
91+ else ss << s.at(i);
92+ }
93+ return ss.str();
94+ }
95+
96+ /* ----------------------------------------------------------------- */
97+ // decode
98+ /* ----------------------------------------------------------------- */
99+ template <class CharT>
100+ std::basic_string<CharT> decode(const CharT* s) {
101+ std::basic_string<CharT> tmp(s);
102+ return decode(tmp);
103+ }
104+ };
105+}
106+
107+#endif
Added: svn:executable
--- trunk/predicate.h (nonexistent)
+++ trunk/predicate.h (revision 1)
@@ -0,0 +1,167 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * predicate.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Mon 23 Apr 2007 18:34:15 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_PREDICATE_H
36+#define CLX_PREDICATE_H
37+
38+#include <locale>
39+#include <set>
40+
41+namespace clx {
42+ /* --------------------------------------------------------------------- */
43+ // classified_functor
44+ /* --------------------------------------------------------------------- */
45+ class classified_functor {
46+ public:
47+ explicit classified_functor(std::ctype_base::mask m, const std::locale& loc = std::locale()) :
48+ mask_(m), loc_(loc) {}
49+
50+ template <class CharT>
51+ bool operator()(CharT c) const {
52+ return std::use_facet<std::ctype<CharT> >(loc_).is(mask_, c);
53+ }
54+
55+ private:
56+ const std::ctype_base::mask mask_;
57+ const std::locale loc_;
58+ };
59+
60+ /* --------------------------------------------------------------------- */
61+ // charset_functor
62+ /* --------------------------------------------------------------------- */
63+ template <class CharT>
64+ class charset_functor {
65+ public:
66+ typedef CharT char_type;
67+ typedef unsigned int size_type;
68+
69+ explicit charset_functor(const char_type* s) : charset_() {
70+ while (s != NULL && *s != '\0') charset_.insert(*s++);
71+ }
72+
73+ bool operator()(char_type c) const {
74+ return charset_.find(c) != charset_.end();
75+ }
76+
77+ private:
78+ std::set<char_type> charset_;
79+ };
80+
81+ /* --------------------------------------------------------------------- */
82+ // is_any_of
83+ /* --------------------------------------------------------------------- */
84+ template <class CharT>
85+ charset_functor<CharT> is_any_of(const CharT* s) {
86+ return charset_functor<CharT>(s);
87+ }
88+
89+ /* --------------------------------------------------------------------- */
90+ // is_space
91+ /* --------------------------------------------------------------------- */
92+ classified_functor is_space(const std::locale& loc = std::locale()) {
93+ return classified_functor(std::ctype_base::space, loc);
94+ }
95+
96+ /* --------------------------------------------------------------------- */
97+ // is_alnum
98+ /* --------------------------------------------------------------------- */
99+ classified_functor is_alnum(const std::locale& loc = std::locale()) {
100+ return classified_functor(std::ctype_base::alnum, loc);
101+ }
102+
103+ /* --------------------------------------------------------------------- */
104+ // is_alpha
105+ /* --------------------------------------------------------------------- */
106+ classified_functor is_alpha(const std::locale& loc = std::locale()) {
107+ return classified_functor(std::ctype_base::alpha, loc);
108+ }
109+
110+ /* --------------------------------------------------------------------- */
111+ // is_cntrl
112+ /* --------------------------------------------------------------------- */
113+ classified_functor is_cntrl(const std::locale& loc = std::locale()) {
114+ return classified_functor(std::ctype_base::cntrl, loc);
115+ }
116+
117+ /* --------------------------------------------------------------------- */
118+ // is_digit
119+ /* --------------------------------------------------------------------- */
120+ classified_functor is_digit(const std::locale& loc = std::locale()) {
121+ return classified_functor(std::ctype_base::digit, loc);
122+ }
123+
124+ /* --------------------------------------------------------------------- */
125+ // is_graph
126+ /* --------------------------------------------------------------------- */
127+ classified_functor is_graph(const std::locale& loc = std::locale()) {
128+ return classified_functor(std::ctype_base::graph, loc);
129+ }
130+
131+ /* --------------------------------------------------------------------- */
132+ // is_lower
133+ /* --------------------------------------------------------------------- */
134+ classified_functor is_lower(const std::locale& loc = std::locale()) {
135+ return classified_functor(std::ctype_base::lower, loc);
136+ }
137+
138+ /* --------------------------------------------------------------------- */
139+ // is_upper
140+ /* --------------------------------------------------------------------- */
141+ classified_functor is_upper(const std::locale& loc = std::locale()) {
142+ return classified_functor(std::ctype_base::upper, loc);
143+ }
144+
145+ /* --------------------------------------------------------------------- */
146+ // is_print
147+ /* --------------------------------------------------------------------- */
148+ classified_functor is_print(const std::locale& loc = std::locale()) {
149+ return classified_functor(std::ctype_base::print, loc);
150+ }
151+
152+ /* --------------------------------------------------------------------- */
153+ // is_punct
154+ /* --------------------------------------------------------------------- */
155+ classified_functor is_punct(const std::locale& loc = std::locale()) {
156+ return classified_functor(std::ctype_base::punct, loc);
157+ }
158+
159+ /* --------------------------------------------------------------------- */
160+ // is_xdigit
161+ /* --------------------------------------------------------------------- */
162+ classified_functor is_xdigit(const std::locale& loc = std::locale()) {
163+ return classified_functor(std::ctype_base::xdigit, loc);
164+ }
165+}
166+
167+#endif // CLX_PREDICATE_H
Added: svn:executable
--- trunk/case_conv.h (nonexistent)
+++ trunk/case_conv.h (revision 1)
@@ -0,0 +1,226 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * case_conv.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Sat 17 Mar 2007 20:07:23 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_CASE_CONV_H
36+#define CLX_CASE_CONV_H
37+
38+#include <string>
39+#include <locale>
40+#include "predicate.h"
41+
42+namespace clx {
43+ /* --------------------------------------------------------------------- */
44+ // upcase
45+ /* --------------------------------------------------------------------- */
46+ template <class CharT, class Traits>
47+ std::basic_string<CharT, Traits>& upcase(
48+ std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
49+ typename std::basic_string<CharT, Traits>::iterator p;
50+ for (p = s.begin(); p != s.end(); p++) {
51+ *p = std::use_facet<std::ctype<CharT> >(loc).toupper(*p);
52+ }
53+ return s;
54+ }
55+
56+ /* --------------------------------------------------------------------- */
57+ // upcase_copy
58+ /* --------------------------------------------------------------------- */
59+ template <class CharT, class Traits>
60+ std::basic_string<CharT, Traits> upcase_copy(
61+ const std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
62+ std::basic_string<CharT, Traits> tmp(s);
63+ return upcase(tmp, loc);
64+ }
65+
66+ /* --------------------------------------------------------------------- */
67+ // upcase_if
68+ /* --------------------------------------------------------------------- */
69+ template <class CharT, class Traits, class PredicateFunc>
70+ std::basic_string<CharT, Traits>& upcase_if(std::basic_string<CharT, Traits>& s,
71+ PredicateFunc f, const std::locale& loc = std::locale()) {
72+ typename std::basic_string<CharT, Traits>::iterator p;
73+ for (p = s.begin(); p != s.end(); p++) {
74+ if (f(*p)) *p = std::use_facet<std::ctype<CharT> >(loc).toupper(*p);
75+ }
76+ return s;
77+ }
78+
79+ /* --------------------------------------------------------------------- */
80+ // upcase_copy_if
81+ /* --------------------------------------------------------------------- */
82+ template <class CharT, class Traits, class PredicateFunc>
83+ std::basic_string<CharT, Traits> upcase_copy_if(const std::basic_string<CharT, Traits>& s,
84+ PredicateFunc f, const std::locale& loc = std::locale()) {
85+ std::basic_string<CharT, Traits> tmp(s);
86+ return upcase_if(tmp, f, loc);
87+ }
88+
89+ /* --------------------------------------------------------------------- */
90+ // downcase
91+ /* --------------------------------------------------------------------- */
92+ template <class CharT, class Traits>
93+ std::basic_string<CharT, Traits>& downcase(
94+ std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
95+ typename std::basic_string<CharT, Traits>::iterator p;
96+ for (p = s.begin(); p != s.end(); p++) {
97+ *p = std::use_facet<std::ctype<CharT> >(loc).tolower(*p);
98+ }
99+ return s;
100+ }
101+
102+ /* --------------------------------------------------------------------- */
103+ // downcase_copy
104+ /* --------------------------------------------------------------------- */
105+ template <class CharT, class Traits>
106+ std::basic_string<CharT, Traits> downcase_copy(
107+ const std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
108+ std::basic_string<CharT, Traits> tmp(s);
109+ return downcase(tmp);
110+ }
111+
112+ /* --------------------------------------------------------------------- */
113+ // downcase_if
114+ /* --------------------------------------------------------------------- */
115+ template <class CharT, class Traits, class PredicateFunc>
116+ std::basic_string<CharT, Traits>& downcase_if(std::basic_string<CharT, Traits>& s,
117+ PredicateFunc f, const std::locale& loc = std::locale()) {
118+ typename std::basic_string<CharT, Traits>::iterator p;
119+ for (p = s.begin(); p != s.end(); p++) {
120+ if (f(*p)) *p = std::use_facet<std::ctype<CharT> >(loc).tolower(*p);
121+ }
122+ return s;
123+ }
124+
125+ /* --------------------------------------------------------------------- */
126+ // downcase_copy_if
127+ /* --------------------------------------------------------------------- */
128+ template <class CharT, class Traits, class PredicateFunc>
129+ std::basic_string<CharT, Traits> downcase_copy_if(const std::basic_string<CharT, Traits>& s,
130+ PredicateFunc f, const std::locale& loc = std::locale()) {
131+ std::basic_string<CharT, Traits> tmp(s);
132+ return downcase_if(tmp, f, loc);
133+ }
134+
135+ /* --------------------------------------------------------------------- */
136+ // swapcase
137+ /* --------------------------------------------------------------------- */
138+ template <class CharT, class Traits>
139+ std::basic_string<CharT, Traits>& swapcase(
140+ std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
141+ typename std::basic_string<CharT, Traits>::iterator p;
142+ for (p = s.begin(); p != s.end(); p++) {
143+ if (std::isupper(*p, loc)) *p = std::use_facet<std::ctype<CharT> >(loc).tolower(*p);
144+ else if (std::islower(*p, loc)) *p = std::use_facet<std::ctype<CharT> >(loc).toupper(*p);
145+ }
146+ return s;
147+ }
148+
149+ /* --------------------------------------------------------------------- */
150+ // swapcase_copy
151+ /* --------------------------------------------------------------------- */
152+ template <class CharT, class Traits>
153+ std::basic_string<CharT, Traits> swapcase_copy(
154+ const std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
155+ std::basic_string<CharT, Traits> tmp(s);
156+ return swapcase(tmp, loc);
157+ }
158+
159+ /* --------------------------------------------------------------------- */
160+ // swapcase_if
161+ /* --------------------------------------------------------------------- */
162+ template <class CharT, class Traits, class PredicateFunc>
163+ std::basic_string<CharT, Traits>& swapcase_if(std::basic_string<CharT, Traits>& s,
164+ PredicateFunc f, const std::locale& loc = std::locale()) {
165+ typename std::basic_string<CharT, Traits>::iterator p;
166+ for (p = s.begin(); p != s.end(); p++) {
167+ if (std::isupper(*p, loc) && f(*p)) *p = std::use_facet<std::ctype<CharT> >(loc).tolower(*p);
168+ else if (std::islower(*p, loc) && f(*p)) *p = std::use_facet<std::ctype<CharT> >(loc).toupper(*p);
169+ }
170+ return s;
171+ }
172+
173+ /* --------------------------------------------------------------------- */
174+ // swapcase_copy_if
175+ /* --------------------------------------------------------------------- */
176+ template <class CharT, class Traits, class PredicateFunc>
177+ std::basic_string<CharT, Traits> swapcase_copy_if(const std::basic_string<CharT, Traits>& s,
178+ PredicateFunc f, const std::locale& loc = std::locale()) {
179+ std::basic_string<CharT, Traits> tmp(s);
180+ return swapcase_if(s, f, loc);
181+ }
182+
183+ /* --------------------------------------------------------------------- */
184+ // capitalize
185+ /* --------------------------------------------------------------------- */
186+ template <class CharT, class Traits>
187+ std::basic_string<CharT, Traits>& capitalize(
188+ std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
189+ typename std::basic_string<CharT, Traits>::iterator first = s.begin();
190+ if (first != s.end()) *first = std::use_facet<std::ctype<CharT> >(loc).toupper(*first);
191+ return s;
192+ }
193+
194+ /* --------------------------------------------------------------------- */
195+ // capitalize_copy
196+ /* --------------------------------------------------------------------- */
197+ template <class CharT, class Traits>
198+ std::basic_string<CharT, Traits> capitalize_copy(
199+ const std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
200+ std::basic_string<CharT, Traits> tmp(s);
201+ return capitalize(tmp);
202+ }
203+
204+ /* --------------------------------------------------------------------- */
205+ // capitalize_if
206+ /* --------------------------------------------------------------------- */
207+ template <class CharT, class Traits, class PredicateFunc>
208+ std::basic_string<CharT, Traits>& capitalize(std::basic_string<CharT, Traits>& s,
209+ PredicateFunc f, const std::locale& loc = std::locale()) {
210+ typename std::basic_string<CharT, Traits>::iterator first = s.begin();
211+ if (first != s.end() && f(*first)) *first = std::use_facet<std::ctype<CharT> >(loc).toupper(*first);
212+ return s;
213+ }
214+
215+ /* --------------------------------------------------------------------- */
216+ // capitalize_copy_if
217+ /* --------------------------------------------------------------------- */
218+ template <class CharT, class Traits, class PredicateFunc>
219+ std::basic_string<CharT, Traits> capitalize_copy(const std::basic_string<CharT, Traits>& s,
220+ PredicateFunc f, const std::locale& loc = std::locale()) {
221+ std::basic_string<CharT, Traits> tmp(s);
222+ return capitalize_if(tmp, f);
223+ }
224+}
225+
226+#endif // CLX_CASE_CONV_H
Added: svn:executable
--- trunk/strip.h (nonexistent)
+++ trunk/strip.h (revision 1)
@@ -0,0 +1,227 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * strip.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Mon 23 Apr 2007 13:45:25 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_STRIP_H
36+#define CLX_STRIP_H
37+
38+#include <string>
39+#include <locale>
40+#include "predicate.h"
41+
42+namespace clx {
43+ /* --------------------------------------------------------------------- */
44+ // lstrip_if
45+ /* --------------------------------------------------------------------- */
46+ template <class CharT, class Traits, class PredicateFunc>
47+ std::basic_string<CharT, Traits>& lstrip_if(
48+ std::basic_string<CharT, Traits>& s, PredicateFunc f) {
49+ typename std::basic_string<CharT, Traits>::size_type i;
50+ for (i = 0; i < s.size(); i++) {
51+ if (!f(s.at(i))) break;
52+ }
53+ if (i > 0) s.erase(0, i);
54+ return s;
55+ }
56+
57+ /* --------------------------------------------------------------------- */
58+ // lstrip_copy_if
59+ /* --------------------------------------------------------------------- */
60+ template <class CharT, class Traits, class PredicateFunc>
61+ std::basic_string<CharT, Traits> lstrip_copy_if(
62+ const std::basic_string<CharT, Traits>& s, PredicateFunc f) {
63+ std::basic_string<CharT, Traits> tmp(s);
64+ return lstrip_if(tmp);
65+ }
66+
67+ /* --------------------------------------------------------------------- */
68+ // rstrip_if
69+ /* --------------------------------------------------------------------- */
70+ template <class CharT, class Traits, class PredicateFunc>
71+ std::basic_string<CharT, Traits>& rstrip_if(
72+ std::basic_string<CharT, Traits>& s, PredicateFunc f) {
73+ typename std::basic_string<CharT, Traits>::size_type i;
74+ for (i = s.size(); i > 0; i--) {
75+ if (!f(s.at(i - 1))) break;
76+ }
77+ if (i < s.size()) s.erase(i);
78+ return s;
79+ }
80+
81+ /* --------------------------------------------------------------------- */
82+ // rstrip_copy_if
83+ /* --------------------------------------------------------------------- */
84+ template <class CharT, class Traits, class PredicateFunc>
85+ std::basic_string<CharT, Traits> rstrip_copy_if(
86+ const std::basic_string<CharT, Traits>& s, PredicateFunc f) {
87+ std::basic_string<CharT, Traits> tmp(s);
88+ return rstrip_if(tmp);
89+ }
90+
91+ /* --------------------------------------------------------------------- */
92+ // strip_if
93+ /* --------------------------------------------------------------------- */
94+ template <class CharT, class Traits, class PredicateFunc>
95+ std::basic_string<CharT, Traits>& strip_if(
96+ std::basic_string<CharT, Traits>& s, PredicateFunc f) {
97+ lstrip_if(s, f);
98+ rstrip_if(s, f);
99+ return s;
100+ }
101+
102+ /* --------------------------------------------------------------------- */
103+ // strip_copy_if
104+ /* --------------------------------------------------------------------- */
105+ template <class CharT, class Traits, class PredicateFunc>
106+ std::basic_string<CharT, Traits> strip_copy_if(
107+ const std::basic_string<CharT, Traits>& s, PredicateFunc f) {
108+ std::basic_string<CharT, Traits> tmp(s);
109+ return strip_if(tmp, f);
110+ }
111+
112+ /* --------------------------------------------------------------------- */
113+ // lstrip
114+ /* --------------------------------------------------------------------- */
115+ template <class CharT, class Traits>
116+ std::basic_string<CharT, Traits>& lstrip(
117+ std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
118+ return lstrip_if(s, is_space(loc));
119+ }
120+
121+ /* --------------------------------------------------------------------- */
122+ // lstrip_copy
123+ /* --------------------------------------------------------------------- */
124+ template <class CharT, class Traits>
125+ std::basic_string<CharT, Traits> lstrip_copy(
126+ const std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
127+ std::basic_string<CharT, Traits> tmp(s);
128+ return lstrip(tmp, loc);
129+ }
130+
131+ /* --------------------------------------------------------------------- */
132+ // rstrip
133+ /* --------------------------------------------------------------------- */
134+ template <class CharT, class Traits>
135+ std::basic_string<CharT, Traits>& rstrip(
136+ std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
137+ return rstrip_if(s, is_space(loc));
138+ }
139+
140+ /* --------------------------------------------------------------------- */
141+ // rstrip_copy
142+ /* --------------------------------------------------------------------- */
143+ template <class CharT, class Traits>
144+ std::basic_string<CharT, Traits> rstrip_copy(
145+ const std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
146+ std::basic_string<CharT, Traits> tmp(s);
147+ return rstrip(tmp, loc);
148+ }
149+
150+ /* --------------------------------------------------------------------- */
151+ // strip
152+ /* --------------------------------------------------------------------- */
153+ template <class CharT, class Traits>
154+ std::basic_string<CharT, Traits>& strip(
155+ std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
156+ return strip_if(s, is_space(loc));
157+ }
158+
159+ /* --------------------------------------------------------------------- */
160+ // strip_copy
161+ /* --------------------------------------------------------------------- */
162+ template <class CharT, class Traits>
163+ std::basic_string<CharT, Traits> strip_copy(
164+ const std::basic_string<CharT, Traits>& s, const std::locale& loc = std::locale()) {
165+ std::basic_string<CharT, Traits> tmp(s);
166+ return strip(tmp, loc);
167+ }
168+
169+ /* --------------------------------------------------------------------- */
170+ // chop_if
171+ /* --------------------------------------------------------------------- */
172+ template <class CharT, class Traits, class PredicateFunc>
173+ std::basic_string<CharT, Traits>& chop_if(
174+ std::basic_string<CharT, Traits>& s, PredicateFunc f) {
175+ if (!s.empty() && f(s.at(s.size() - 1))) s.erase(s.size() - 1);
176+ return s;
177+ }
178+
179+ /* --------------------------------------------------------------------- */
180+ // chop_copy_if
181+ /* --------------------------------------------------------------------- */
182+ template <class CharT, class Traits, class PredicateFunc>
183+ std::basic_string<CharT, Traits> chop_copy_if(
184+ const std::basic_string<CharT, Traits>& s, PredicateFunc f) {
185+ std::basic_string<CharT, Traits> tmp(s);
186+ return chop_if(tmp, f);
187+ }
188+
189+ /* --------------------------------------------------------------------- */
190+ // chop
191+ /* --------------------------------------------------------------------- */
192+ template <class CharT, class Traits>
193+ std::basic_string<CharT, Traits>& chop(std::basic_string<CharT, Traits>& s) {
194+ if (!s.empty()) s.erase(s.size() - 1);
195+ return s;
196+ }
197+
198+ /* --------------------------------------------------------------------- */
199+ // chop_copy
200+ /* --------------------------------------------------------------------- */
201+ template <class CharT, class Traits>
202+ std::basic_string<CharT, Traits> chop_copy(const std::basic_string<CharT, Traits>& s) {
203+ std::basic_string<CharT, Traits> tmp(s);
204+ return chop(tmp);
205+ }
206+
207+ /* --------------------------------------------------------------------- */
208+ // chomp
209+ /* --------------------------------------------------------------------- */
210+ template <class CharT, class Traits>
211+ std::basic_string<CharT, Traits>& chomp(std::basic_string<CharT, Traits>& s) {
212+ chop_if(s, is_any_of("\n"));
213+ chop_if(s, is_any_of("\r"));
214+ return s;
215+ }
216+
217+ /* --------------------------------------------------------------------- */
218+ // chomp_copy
219+ /* --------------------------------------------------------------------- */
220+ template <class CharT, class Traits>
221+ std::basic_string<CharT, Traits>& chomp_copy(const std::basic_string<CharT, Traits>& s) {
222+ std::basic_string<CharT, Traits> tmp(s);
223+ return chomp(tmp);
224+ }
225+}
226+
227+#endif // CLX_STRIP_H
Added: svn:executable
--- trunk/time_duration.h (nonexistent)
+++ trunk/time_duration.h (revision 1)
@@ -0,0 +1,104 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * time_duration.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 14 Jun 2007 14:38:02 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_TIME_DURATION_H
36+#define CLX_TIME_DURATION_H
37+
38+namespace clx {
39+ typedef long time_value;
40+
41+ namespace time_unit {
42+ enum {
43+ minute = 60, // 60 sec
44+ hour = 3600, // 60 * 60 sec
45+ day = 86400, // 60 * 60 * 24 sec
46+ week = 604800 // 60 * 60 * 24 * 7 sec
47+ };
48+ };
49+
50+ /* --------------------------------------------------------------------- */
51+ // date_duration
52+ /* --------------------------------------------------------------------- */
53+ class date_duration {
54+ public:
55+ typedef long value_type;
56+
57+ explicit date_duration(value_type y = 0, value_type m = 0, value_type d = 0) :
58+ years_(y), months_(m), days_(d) {}
59+
60+ date_duration& operator+=(const date_duration& t) {
61+ years_ += t.years_;
62+ months_ += t.months_;
63+ days_ += t.days_;
64+ return *this;
65+ }
66+
67+ date_duration& operator-=(const date_duration& t) {
68+ years_ -= t.years_;
69+ months_ -= t.months_;
70+ days_ -= t.days_;
71+ return *this;
72+ }
73+
74+ // binary operator
75+ friend date_duration operator+(date_duration x, const date_duration& y) { return x += y; }
76+ friend date_duration operator-(date_duration x, const date_duration& y) { return x -= y; }
77+ friend date_duration operator+(const date_duration& y, date_duration x) { return x += y; }
78+ friend date_duration operator-(const date_duration& y, date_duration x) { return x -= y; }
79+
80+ // accessor
81+ value_type years() const { return years_; }
82+ value_type months() const { return months_; }
83+ value_type days() const { return days_; }
84+
85+ private:
86+ value_type years_;
87+ value_type months_;
88+ value_type days_;
89+ };
90+
91+ /* --------------------------------------------------------------------- */
92+ // helper_functions
93+ /* --------------------------------------------------------------------- */
94+ date_duration years(time_value t) { return date_duration(t); }
95+ date_duration months(time_value t) { return date_duration(0, t); }
96+
97+ time_value days(time_value t) { return t * time_unit::day; }
98+ time_value weeks(time_value t) { return t * time_unit::week; }
99+ time_value hours(time_value t) { return t * time_unit::hour; }
100+ time_value minutes(time_value t) { return t * time_unit::minute; }
101+ time_value seconds(time_value t) { return t; }
102+}
103+
104+#endif // CLX_TIME_DURATION_H
Added: svn:executable
--- trunk/salgorithm.h (nonexistent)
+++ trunk/salgorithm.h (revision 1)
@@ -0,0 +1,45 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * salgorithm.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 26 Apr 2007 04:30:16 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_SALGORITHM_H
36+#define CLX_SALGORITHM_H
37+
38+#include "adjust.h"
39+#include "case_conv.h"
40+#include "remove.h"
41+#include "replace.h"
42+#include "split.h"
43+#include "strip.h"
44+
45+#endif // CLX_SALGORITHM_H
Added: svn:executable
--- trunk/tokenizer.h (nonexistent)
+++ trunk/tokenizer.h (revision 1)
@@ -0,0 +1,124 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * tokenizer.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Tue 16 Jan 2007 04:56:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_TOKENIZER_H
36+#define CLX_TOKENIZER_H
37+
38+#include <string>
39+#include <vector>
40+#include "tokenizer_func.h"
41+#include "lexical_cast.h"
42+
43+namespace clx {
44+ /* --------------------------------------------------------------------- */
45+ // basic_tokenizer
46+ /* --------------------------------------------------------------------- */
47+ template <
48+ class TokenizerFunc,
49+ class Type = std::string,
50+ template <class> class Container = std::vector
51+ >
52+ class basic_tokenizer : public Container<Type> {
53+ public:
54+ typedef TokenizerFunc functor;
55+ typedef Type value_type;
56+ typedef typename functor::char_type char_type;
57+ typedef std::basic_string<char_type> string_type;
58+ typedef Container<Type> container;
59+ typedef typename container::size_type size_type;
60+
61+ // constructor and destructor
62+ explicit basic_tokenizer(const functor& f = functor()) :
63+ super(), f_(f) {}
64+
65+ template <class InputIterator>
66+ explicit basic_tokenizer(InputIterator first, InputIterator last, const functor& f = functor()) :
67+ super(), f_(f) { this->assign(first, last); }
68+
69+ explicit basic_tokenizer(const string_type& s, const functor& f = functor()) :
70+ super(), f_(f) { this->assign(s); }
71+
72+ explicit basic_tokenizer(const char_type* s, const functor& f = functor()) :
73+ super(), f_(f) { this->assign(s); }
74+
75+ virtual ~basic_tokenizer() {}
76+
77+ //operator
78+ virtual void reset() {
79+ f_.reset();
80+ this->clear();
81+ }
82+
83+ template <class InputIterator>
84+ basic_tokenizer& assign(InputIterator first, InputIterator last) {
85+ string_type token;
86+
87+ this->reset();
88+ while (f_(first, last, token)) {
89+ this->insert(this->end(), lexical_cast<value_type>(token));
90+ if (first++ == last) break;
91+ }
92+ return *this;
93+ }
94+
95+ virtual basic_tokenizer& assign(const string_type& s) {
96+ typename string_type::const_iterator pos = s.begin();
97+ return this->assign(pos, s.end());
98+ }
99+
100+ virtual basic_tokenizer& assign(const char_type* s) {
101+ string_type tmp(s);
102+ return this->assign(tmp);
103+ }
104+
105+ template <class InputIterator>
106+ basic_tokenizer& operator()(InputIterator first, InputIterator last) {
107+ return this->assign(first, last);
108+ }
109+
110+ basic_tokenizer& operator()(const string_type& s) { return this->assign(s); }
111+ basic_tokenizer& operator()(const char_type* s) { return this->assign(s); }
112+
113+ private:
114+ typedef Container<Type> super;
115+
116+ functor f_;
117+ };
118+
119+ typedef basic_tokenizer<char_separator<char> > tokenizer;
120+ typedef basic_tokenizer<escape_separator<char> > csv;
121+ typedef basic_tokenizer<format_separator<char> > strftokenizer;
122+}
123+
124+#endif // CLX_TOKENIZER_H
Added: svn:executable
--- trunk/replace.h (nonexistent)
+++ trunk/replace.h (revision 1)
@@ -0,0 +1,146 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * replace.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 26 Apr 2007 19:25:08 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_REPLACE_H
36+#define CLX_REPLACE_H
37+
38+#include <string>
39+
40+namespace clx {
41+ /* --------------------------------------------------------------------- */
42+ // replace
43+ /* --------------------------------------------------------------------- */
44+ template <class CharT, class Traits>
45+ std::basic_string<CharT, Traits>& replace(std::basic_string<CharT, Traits>& s,
46+ const std::basic_string<CharT, Traits>& sch,
47+ const std::basic_string<CharT, Traits>& rep,
48+ unsigned int nth = 1) {
49+ unsigned int i = 0;
50+ typename std::basic_string<CharT, Traits>::size_type pos = 0;
51+ while ((pos = s.find(sch, pos)) != std::basic_string<CharT, Traits>::npos) {
52+ if (++i == nth) {
53+ s.replace(pos, sch.length(), rep);
54+ break;
55+ }
56+ pos += sch.length();
57+ }
58+ return s;
59+ }
60+
61+ /* --------------------------------------------------------------------- */
62+ // replace
63+ /* --------------------------------------------------------------------- */
64+ template <class CharT, class Traits>
65+ std::basic_string<CharT, Traits>& replace(std::basic_string<CharT, Traits>& s,
66+ const CharT* sch, const CharT* rep, unsigned int nth = 1) {
67+ std::basic_string<CharT, Traits> sch_tmp(sch);
68+ std::basic_string<CharT, Traits> rep_tmp(rep);
69+ return replace(s, sch_tmp, rep_tmp, nth);
70+ }
71+
72+ /* --------------------------------------------------------------------- */
73+ // replace_copy
74+ /* --------------------------------------------------------------------- */
75+ template <class CharT, class Traits>
76+ std::basic_string<CharT, Traits> replace_copy(
77+ const std::basic_string<CharT, Traits>& s,
78+ const std::basic_string<CharT, Traits>& sch,
79+ const std::basic_string<CharT, Traits>& rep,
80+ unsigned int nth = 1) {
81+ std::basic_string<CharT, Traits> tmp(s);
82+ return replace(tmp, sch, rep, nth);
83+ }
84+
85+ /* --------------------------------------------------------------------- */
86+ // replace_copy
87+ /* --------------------------------------------------------------------- */
88+ template <class CharT, class Traits>
89+ std::basic_string<CharT, Traits> replace_copy(
90+ const std::basic_string<CharT, Traits>& s,
91+ const CharT* sch, const CharT* rep, unsigned int nth = 1) {
92+ std::basic_string<CharT, Traits> tmp(s);
93+ return replace(tmp, sch, rep, nth);
94+ }
95+
96+ /* --------------------------------------------------------------------- */
97+ // replace_all
98+ /* --------------------------------------------------------------------- */
99+ template <class CharT, class Traits>
100+ std::basic_string<CharT, Traits>& replace_all(std::basic_string<CharT, Traits>& s,
101+ const std::basic_string<CharT, Traits>& sch,
102+ const std::basic_string<CharT, Traits>& rep) {
103+ typename std::basic_string<CharT, Traits>::size_type pos = 0;
104+ while ((pos = s.find(sch, pos)) != std::basic_string<CharT, Traits>::npos) {
105+ s.replace(pos, sch.length(), rep);
106+ pos += rep.length();
107+ }
108+ return s;
109+ }
110+
111+ /* --------------------------------------------------------------------- */
112+ // replace_all
113+ /* --------------------------------------------------------------------- */
114+ template <class CharT, class Traits>
115+ std::basic_string<CharT, Traits>& replace_all(std::basic_string<CharT, Traits>& s,
116+ const CharT* sch, const CharT* rep) {
117+ std::basic_string<CharT, Traits> sch_tmp(sch);
118+ std::basic_string<CharT, Traits> rep_tmp(rep);
119+ return replace_all(s, sch_tmp, rep_tmp);
120+ }
121+
122+ /* --------------------------------------------------------------------- */
123+ // replace_all_copy
124+ /* --------------------------------------------------------------------- */
125+ template <class CharT, class Traits>
126+ std::basic_string<CharT, Traits> replace_all_copy(
127+ const std::basic_string<CharT, Traits>& s,
128+ const std::basic_string<CharT, Traits>& sch,
129+ const std::basic_string<CharT, Traits>& rep) {
130+ std::basic_string<CharT, Traits> tmp(s);
131+ return replace_all(tmp, sch, rep);
132+ }
133+
134+ /* --------------------------------------------------------------------- */
135+ // replace_all_copy
136+ /* --------------------------------------------------------------------- */
137+ template <class CharT, class Traits>
138+ std::basic_string<CharT, Traits> replace_all_copy(
139+ const std::basic_string<CharT, Traits>& s,
140+ const CharT* sch, const CharT* rep) {
141+ std::basic_string<CharT, Traits> tmp(s);
142+ return replace_all(tmp, sch, rep);
143+ }
144+}
145+
146+#endif // CLX_REPLACE_H
Added: svn:executable
--- trunk/tokenmap.h (nonexistent)
+++ trunk/tokenmap.h (revision 1)
@@ -0,0 +1,125 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * tokenmap.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Fri 16 Mar 2007 23:31:14 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_TOKENMAP_H
36+#define CLX_TOKENMAP_H
37+
38+#include <string>
39+#include <map>
40+#include "tokenizer_func.h"
41+#include "lexical_cast.h"
42+
43+namespace clx {
44+ /* --------------------------------------------------------------------- */
45+ // basic_tokenmap
46+ /* --------------------------------------------------------------------- */
47+ template <
48+ class TokenizerFunc,
49+ class Container = std::map<char, std::string>
50+ >
51+ class basic_tokenmap : public Container {
52+ public:
53+ typedef TokenizerFunc functor;
54+ typedef Container container;
55+ typedef typename container::value_type value_type;
56+ typedef typename container::key_type key_type;
57+ typedef typename container::mapped_type mapped_type;
58+ typedef typename functor::char_type char_type;
59+ typedef std::basic_string<char_type> string_type;
60+
61+ // constructor and destructor
62+ explicit basic_tokenmap(const functor& f = functor()) :
63+ super(), f_(f) {}
64+
65+ template <class InputIterator>
66+ explicit basic_tokenmap(InputIterator first, InputIterator last, const functor& f = functor()) :
67+ super(), f_(f) { this->assign(first, last); }
68+
69+ explicit basic_tokenmap(const string_type& s, const functor& f = functor()) :
70+ super(), f_(f) { this->assign(s); }
71+
72+ explicit basic_tokenmap(const char_type* s, const functor& f = functor()) :
73+ super(), f_(f) { this->assign(s); }
74+
75+ virtual ~basic_tokenmap() {}
76+
77+ //operator
78+ virtual void reset() {
79+ f_.reset();
80+ this->clear();
81+ }
82+
83+ template <class InputIterator>
84+ virtual basic_tokenmap& assign(InputIterator first, InputIterator last) {
85+ string_type tmp;
86+ string_type token;
87+
88+ this->reset();
89+ while (f_(first, last, tmp, token)) {
90+ key_type key = lexical_cast<key_type>(tmp);
91+ mapped_type val = lexical_cast<mapped_type>(token);
92+ this->insert(value_type(key, val));
93+ if (first++ == last) break;
94+ }
95+ return *this;
96+ }
97+
98+ virtual basic_tokenmap& assign(const string_type& s) {
99+ typename string_type::const_iterator pos = s.begin();
100+ return this->assign(pos, s.end());
101+ }
102+
103+ virtual basic_tokenmap& assign(const char_type* s) {
104+ string_type tmp(s);
105+ return this->assign(tmp);
106+ }
107+
108+ template <class InputIterator>
109+ basic_tokenmap& operator()(InputIterator first, InputIterator last) {
110+ return this->assign(first, last);
111+ }
112+
113+ basic_tokenmap& operator()(const string_type& s) { return this->assign(s); }
114+ basic_tokenmap& operator()(const char_type* s) { return this->assign(s); }
115+
116+ private:
117+ typedef Container super;
118+
119+ functor f_;
120+ };
121+
122+ typedef basic_tokenmap<format_separator<char> > tokenmap;
123+}
124+
125+#endif // CLX_TOKENMAP_H
Added: svn:executable
--- trunk/timer.h (nonexistent)
+++ trunk/timer.h (revision 1)
@@ -0,0 +1,117 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * timer.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Fri 01 Feb 2008 11:51:16 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_TIMER_H
36+#define CLX_TIMER_H
37+
38+#ifndef CLX_WIN32
39+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
40+#define CLX_WIN32
41+#endif
42+#endif // CLX_WIN32
43+
44+#ifdef CLX_WIN32
45+#include <windows.h>
46+#pragma comment(lib, "winmm.lib")
47+#else
48+#include <sys/time.h>
49+#endif
50+
51+namespace clx {
52+ namespace detail {
53+ /* ----------------------------------------------------------------- */
54+ // timeval_type
55+ /* ----------------------------------------------------------------- */
56+#ifdef CLX_WIN32
57+ struct use_timeGetTime {
58+ static double gettime() {
59+ return static_cast<double>(::timeGetTime()) * 1e-3;
60+ }
61+
62+ static double precision() { return 1.0 * 1e-3; }
63+ };
64+
65+ typedef use_timeGetTime timeval_type;
66+#else
67+ struct use_gettimeofday {
68+ static double gettime() {
69+ struct timeval tv;
70+ ::gettimeofday(&tv, NULL);
71+
72+ double sec = static_cast<double>(tv.tv_sec);
73+ double usec = static_cast<double>(tv.tv_usec) * 1e-6;
74+ return sec + usec;
75+ }
76+
77+ static double precision() { return 1.0 * 1e-6; }
78+ };
79+
80+ typedef use_gettimeofday timeval_type;
81+#endif
82+ };
83+
84+ /* --------------------------------------------------------------------- */
85+ // basic_timer
86+ /* --------------------------------------------------------------------- */
87+ template <class GetTimePolicy = detail::timeval_type>
88+ class basic_timer {
89+ public:
90+ typedef GetTimePolicy policy;
91+
92+ basic_timer() { this->restart(); }
93+
94+ void restart() {
95+ start_ = policy::gettime();
96+ point_ = start_;
97+ }
98+
99+ double update() {
100+ double tmp = point_;
101+ point_ = policy::gettime();
102+ return point_ - tmp;
103+ }
104+
105+ double total_elapsed() const { return policy::gettime() - start_; }
106+ double elapsed() const { return policy::gettime() - point_; }
107+ double precision() const { return policy::precision(); }
108+
109+ private:
110+ double start_;
111+ double point_;
112+ };
113+
114+ typedef basic_timer<> timer;
115+}
116+
117+#endif // CLX_TIMER_H
Added: svn:executable
--- trunk/quantile.h (nonexistent)
+++ trunk/quantile.h (revision 1)
@@ -0,0 +1,115 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * quantile.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Tue 23 Jan 2007 00:16:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_QUANTILE_H
36+#define CLX_QUANTILE_H
37+
38+
39+#include <algorithm>
40+#include <vector>
41+#include <cmath>
42+#include "lexical_cast.h"
43+
44+namespace clx {
45+ /* --------------------------------------------------------------------- */
46+ // quantile
47+ /* --------------------------------------------------------------------- */
48+ template <class InputIterator>
49+ double quantile(InputIterator first, InputIterator last, unsigned int k, unsigned int q) {
50+ if (k > q) return static_cast<double>(0);
51+
52+ std::vector<double> v;
53+ while (first != last) v.push_back(lexical_cast<double>(*first++));
54+ std::sort(v.begin(), v.end());
55+
56+ unsigned int idx = q + k * v.size() - k;
57+ if (idx % q == 0) return v.at(idx / q - 1);
58+
59+ double t = idx / static_cast<double>(q);
60+ unsigned int ceil = static_cast<unsigned int>(std::ceil(t));
61+ unsigned int floor = static_cast<unsigned int>(std::floor(t));
62+
63+ return (ceil - t) * v.at(floor - 1) + (t - floor) * v.at(ceil - 1);
64+ }
65+
66+ /* --------------------------------------------------------------------- */
67+ // median
68+ /* --------------------------------------------------------------------- */
69+ template <class InputIterator>
70+ double median(InputIterator first, InputIterator last) {
71+ return quantile(first, last, 1, 2);
72+ }
73+
74+ /* --------------------------------------------------------------------- */
75+ // tertile
76+ /* --------------------------------------------------------------------- */
77+ template <class InputIterator>
78+ double tertile(InputIterator first, InputIterator last, unsigned int k) {
79+ return quantile(first, last, k, 3);
80+ }
81+
82+ /* --------------------------------------------------------------------- */
83+ // quartile
84+ /* --------------------------------------------------------------------- */
85+ template <class InputIterator>
86+ double quartile(InputIterator first, InputIterator last, unsigned int k) {
87+ return quantile(first, last, k, 4);
88+ }
89+
90+ /* --------------------------------------------------------------------- */
91+ // quintile
92+ /* --------------------------------------------------------------------- */
93+ template <class InputIterator>
94+ double quintile(InputIterator first, InputIterator last, unsigned int k) {
95+ return quantile(first, last, k, 5);
96+ }
97+
98+ /* --------------------------------------------------------------------- */
99+ // decile
100+ /* --------------------------------------------------------------------- */
101+ template <class InputIterator>
102+ double decile(InputIterator first, InputIterator last, unsigned int k) {
103+ return quantile(first, last, k, 10);
104+ }
105+
106+ /* --------------------------------------------------------------------- */
107+ // percentile
108+ /* --------------------------------------------------------------------- */
109+ template <class InputIterator>
110+ double percentile(InputIterator first, InputIterator last, unsigned int k) {
111+ return quantile(first, last, k, 100);
112+ }
113+};
114+
115+#endif // CLX_QUANTILE_H
Added: svn:executable
--- trunk/tcp.h (nonexistent)
+++ trunk/tcp.h (revision 1)
@@ -0,0 +1,210 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * tcp.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Wed 09 Jul 2008 12:23:03 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_TCP_H
36+#define CLX_TCP_H
37+
38+#include <string>
39+#include <stdexcept>
40+#include "socket.h"
41+#include "sockaddress.h"
42+#include "sockstream.h"
43+
44+namespace clx {
45+ namespace tcp {
46+ /* ----------------------------------------------------------------- */
47+ // basic_socket
48+ /* ----------------------------------------------------------------- */
49+ template <
50+ int Family,
51+ class CharT = char,
52+ class Traits = std::char_traits<CharT>
53+ >
54+ class basic_socket : public basic_rawsocket<SOCK_STREAM, Family> {
55+ public:
56+ typedef basic_rawsocket<SOCK_STREAM, Family> rawsocket;
57+ typedef basic_sockaddress<Family, IPPROTO_TCP> sockaddress;
58+ typedef CharT char_type;
59+ typedef typename std::basic_string<CharT, Traits> string_type;
60+
61+ // constructor and destructor
62+ basic_socket() : rawsocket(), addr_() {}
63+
64+ explicit basic_socket(const socket_type& s, const sockaddress& addr) :
65+ rawsocket(s), addr_(addr) {}
66+
67+ explicit basic_socket(const char_type* host, int port) :
68+ rawsocket(), addr_(host, port) { this->connect(); }
69+
70+ explicit basic_socket(const string_type& host, int port) :
71+ rawsocket(), addr_(host, port) { this->connect(); }
72+
73+ explicit basic_socket(const char_type* host, const char_type* service) :
74+ rawsocket(), addr_(host, service) { this->connect(); }
75+
76+ explicit basic_socket(const string_type& host, const string_type& service) :
77+ rawsocket(), addr_(host, service) { this->connect(); }
78+
79+ virtual ~basic_socket() {}
80+
81+ // operator
82+ basic_socket& connect(const char_type* host, int port) {
83+ addr_.assign(host, port);
84+ return this->connect();
85+ }
86+
87+ basic_socket& connect(const string_type& host, int port) {
88+ return this->connect(host.c_str(), port);
89+ }
90+
91+ basic_socket& connect(const char_type* host, const char_type* service) {
92+ addr_.assign(host, service);
93+ return this->connect();
94+ }
95+
96+ basic_socket& connect(const string_type& host, const string_type& service) {
97+ return this->connect(host.c_str(), service.c_str());
98+ }
99+
100+ int send(const char_type* dat, int n) {
101+ return ::send(this->socket(), dat, n, 0);
102+ }
103+
104+ int send(const string_type& dat) {
105+ return this->send(dat.c_str(), dat.size());
106+ }
107+
108+ int recv(char_type* buf, int n) {
109+ return ::recv(this->socket(), buf, n, 0);
110+ }
111+
112+ const sockaddress& address() const { return addr_; }
113+
114+ private:
115+ sockaddress addr_;
116+
117+ basic_socket& connect() {
118+ if (this->open() < 0) throw clx::socket_error("socket");
119+ if (::connect(this->socket(), (struct sockaddr*)addr_.data(), addr_.size()) == -1) {
120+ throw clx::socket_error("connect");
121+ }
122+ return *this;
123+ }
124+ };
125+
126+ /* ----------------------------------------------------------------- */
127+ // basic_acceptor
128+ /* ----------------------------------------------------------------- */
129+ template <
130+ int Family,
131+ class CharT = char,
132+ class Traits = std::char_traits<CharT>
133+ >
134+ class basic_acceptor : public basic_rawsocket<SOCK_STREAM, Family> {
135+ public:
136+ typedef basic_rawsocket<SOCK_STREAM, Family> rawsocket;
137+ typedef basic_sockaddress<Family, IPPROTO_TCP> sockaddress;
138+ typedef CharT char_type;
139+ typedef typename std::basic_string<CharT, Traits> string_type;
140+
141+ static const int default_nlisten = 100;
142+
143+ // constructor and destructor
144+ basic_acceptor() : rawsocket(), nlisten_(default_nlisten) {}
145+
146+ explicit basic_acceptor(int port, int n = default_nlisten) :
147+ rawsocket(), addr_(port), nlisten_(default_nlisten) { this->open(); }
148+
149+ explicit basic_acceptor(const char_type* service, int n = default_nlisten) :
150+ rawsocket(), addr_(service), nlisten_(default_nlisten) { this->open(); }
151+
152+ explicit basic_acceptor(const string_type& service, int n = default_nlisten) :
153+ rawsocket(), addr_(service), nlisten_(default_nlisten) { this->open(); }
154+
155+ virtual ~basic_acceptor() {}
156+
157+ // operator
158+ basic_acceptor& open(int port, int n = default_nlisten) {
159+ addr_.assign(port);
160+ nlisten_ = n;
161+ return this->open();
162+ }
163+
164+ basic_acceptor& open(const char_type* service, int n = default_nlisten) {
165+ addr_.assign(service);
166+ nlisten_ = n;
167+ return this->open();
168+ }
169+
170+ basic_acceptor& open(const string_type& service, int n = default_nlisten) {
171+ return this->open(service.c_str(), n);
172+ }
173+
174+ basic_socket<Family> accept(int retry = 10) {
175+ socket_type s = -1;
176+ sockaddress addr;
177+ typename sockaddress::inet_type tmp;
178+ socklen_type len = sizeof(tmp);
179+
180+ for (int i = 0; i < retry; i++) {
181+ s = ::accept(this->socket(), (struct sockaddr*)&tmp, &len);
182+ if (s == -1) ::sleep(1);
183+ else break;
184+ }
185+ addr = tmp;
186+ return basic_socket<Family>(s, addr);
187+ }
188+
189+ private:
190+ sockaddress addr_;
191+ int nlisten_;
192+
193+ basic_acceptor& open() {
194+ if (rawsocket::open() < 0) throw socket_error("socket");
195+ if (::bind(this->socket(), (struct sockaddr*)addr_.data(), addr_.size()) == -1) {
196+ throw socket_error("bind");
197+ }
198+ if (::listen(this->socket(), nlisten_) == -1) throw socket_error("listen");
199+ return *this;
200+ }
201+ };
202+
203+ typedef basic_socket<AF_INET> socket;
204+ typedef basic_acceptor<AF_INET> acceptor;
205+ typedef basic_sockaddress<AF_INET, IPPROTO_TCP> sockaddress;
206+ typedef basic_sockstream<socket> sockstream;
207+ };
208+}
209+
210+#endif // CLX_TCP_H
Added: svn:executable
--- trunk/table.h (nonexistent)
+++ trunk/table.h (revision 1)
@@ -0,0 +1,127 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * table.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Thu 30 Aug 2007 11:32:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_TABLE_H
36+#define CLX_TABLE_H
37+
38+#include <string>
39+#include <vector>
40+#include <istream>
41+#include <fstream>
42+#include <stdexcept>
43+#include "tokenizer.h"
44+
45+namespace clx {
46+ /* --------------------------------------------------------------------- */
47+ // table
48+ /* --------------------------------------------------------------------- */
49+ template <
50+ class TokenizerFunc,
51+ class Type = std::string,
52+ template <class> class Container = std::vector
53+ >
54+ class table : public Container<Container<Type> > {
55+ public:
56+ typedef Type value_type;
57+ typedef TokenizerFunc functor;
58+ typedef Container<Container<Type> > container;
59+ typedef basic_tokenizer<TokenizerFunc, Type> parser;
60+ typedef typename parser::char_type char_type;
61+ typedef typename parser::string_type string_type;
62+ typedef std::basic_istream<char_type> istream;
63+
64+ // constructor and destructor
65+ explicit table(const functor& f = functor()) :
66+ container(), parser_(f) { this->reset(); }
67+
68+ explicit table(istream& sin, const functor& f = functor()) :
69+ container(), parser_(f) {
70+ this->reset();
71+ this->read(sin);
72+ }
73+
74+ explicit table(const char_type* path, const functor& f = functor()) :
75+ container(), parser_(f) {
76+ this->reset();
77+ this->read(path);
78+ }
79+
80+ explicit table(const string_type& path, const functor& f = functor()) :
81+ container(), parser_(f) {
82+ this->reset();
83+ this->read(path);
84+ }
85+
86+ virtual ~table() {}
87+
88+ // operator
89+ virtual void reset() {
90+ this->clear();
91+ parser_.reset();
92+ }
93+
94+ virtual bool readline(istream& sin) {
95+ string_type tmp;
96+ if (!std::getline(sin, tmp)) return false;
97+ this->insert(this->end(), parser_(tmp));
98+ return true;
99+ }
100+
101+ table& read(istream& sin) {
102+ while (this->readline(sin));
103+ return *this;
104+ }
105+
106+ table& read(const char_type* path) {
107+ std::basic_ifstream<char_type> fp(path);
108+ if (!fp.is_open()) throw std::runtime_error("No such file or directory");
109+ this->read(fp);
110+ fp.close();
111+ return *this;
112+ }
113+
114+ table& read(const string_type& path) {
115+ return this->read(path.c_str());
116+ }
117+
118+ private:
119+ parser parser_;
120+
121+ // invalid copy constructor and self-assign operator
122+ explicit table(const table& cp);
123+ table& operator=(const table& cp);
124+ };
125+}
126+
127+#endif // CLX_TABLE_H
Added: svn:executable
--- trunk/socket.h (nonexistent)
+++ trunk/socket.h (revision 1)
@@ -0,0 +1,181 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * socket.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Fri 16 Mar 2007 17:52:03 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_SOCKET_H
36+#define CLX_SOCKET_H
37+
38+#ifndef CLX_WIN32
39+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
40+#define CLX_WIN32
41+#endif
42+#endif // CLX_WIN32
43+
44+#include <cerrno>
45+#include <cstdlib>
46+#include <cstring>
47+#include <string>
48+#include <sstream>
49+#include <stdexcept>
50+
51+#ifdef CLX_WIN32
52+#include "winsock_util.h"
53+#include <windows.h>
54+#include <io.h>
55+#else
56+#include <sys/socket.h>
57+#include <sys/types.h>
58+#include <netinet/in.h>
59+#include <arpa/inet.h>
60+#include <netdb.h>
61+#include <unistd.h>
62+#endif // CLX_WIN32
63+
64+namespace clx {
65+#ifdef CLX_WIN32
66+ typedef SOCKET socket_type;
67+ typedef int socklen_type;
68+ typedef unsigned long sockint_type;
69+#else
70+ typedef int socket_type;
71+ typedef socklen_t socklen_type;
72+ typedef u_int32_t sockint_type;
73+#endif // CLX_WIN32
74+
75+ /* --------------------------------------------------------------------- */
76+ // socket_error
77+ /* --------------------------------------------------------------------- */
78+ class socket_error : public std::runtime_error {
79+ public:
80+ typedef char char_type;
81+ typedef std::string string_type;
82+ typedef std::stringstream sstream_type;
83+
84+ static const int nbuf = 1024;
85+
86+ explicit socket_error(const string_type& what_arg) :
87+ std::runtime_error(what_arg), errno_(errno) {}
88+
89+ virtual const char_type* what() {
90+ sstream_type tmp;
91+ char message[nbuf];
92+#ifdef CLX_WIN32
93+ strerror_s(message, sizeof(message), errno_);
94+#else
95+ strerror_r(errno_, message, sizeof(message));
96+#endif
97+ tmp << std::runtime_error::what() << ": " << message;
98+ return tmp.str().c_str();
99+ }
100+
101+ private:
102+ int errno_;
103+ };
104+
105+ /* --------------------------------------------------------------------- */
106+ // basic_rawsocket
107+ /* --------------------------------------------------------------------- */
108+ template <
109+ int Type,
110+ int Family
111+ >
112+ class basic_rawsocket {
113+ public:
114+ // constructor and destructor
115+ basic_rawsocket() : s_(-1) {}
116+
117+ explicit basic_rawsocket(const basic_rawsocket& cp) : s_(-1) {
118+ this->assign(cp);
119+ }
120+
121+ explicit basic_rawsocket(const socket_type& s) : s_(-1) {
122+ this->assign(s);
123+ }
124+
125+ virtual ~basic_rawsocket() { this->close(); }
126+
127+ basic_rawsocket& operator=(const basic_rawsocket& cp) {
128+ return this->assign(cp);
129+ }
130+
131+ basic_rawsocket& operator=(const socket_type& s) {
132+ return this->assign(s);
133+ }
134+
135+ // operator
136+ basic_rawsocket& assign(const basic_rawsocket& cp) {
137+ if (&cp == this) return *this;
138+ this->close();
139+#ifdef CLX_WIN32
140+ if (cp.s_ != (socket_type)-1) {
141+ ::DuplicateHandle(::GetCurrentProcess(), (HANDLE)cp.s_,
142+ ::GetCurrentProcess(), (LPHANDLE)&s_,
143+ 0, TRUE, DUPLICATE_SAME_ACCESS);
144+ }
145+#else
146+ if (cp.s_ != -1) s_ = ::dup(cp.s_);
147+#endif
148+ return *this;
149+ }
150+
151+ basic_rawsocket& assign(const socket_type& s) {
152+ this->close();
153+ s_ = s;
154+ return *this;
155+ }
156+
157+ socket_type open() {
158+ s_ = ::socket(Family, Type, 0);
159+ return s_;
160+ }
161+
162+ void close() {
163+ if (s_ >= 0) {
164+#ifdef CLX_WIN32
165+ ::closesocket(s_);
166+#else
167+ ::close(s_);
168+#endif
169+ s_ = -1;
170+ }
171+ }
172+
173+ bool is_open() const { return s_ >= 0; }
174+ socket_type socket() const { return s_; }
175+
176+ private:
177+ socket_type s_;
178+ };
179+}
180+
181+#endif // CLX_SOCKET_H
Added: svn:executable
--- trunk/format.h (nonexistent)
+++ trunk/format.h (revision 1)
@@ -0,0 +1,197 @@
1+/* ------------------------------------------------------------------------- */
2+/*
3+ * format.h
4+ *
5+ * Copyright (c) 2004 - 2008, clown. All rights reserved.
6+ *
7+ * Redistribution and use in source and binary forms, with or without
8+ * modification, are permitted provided that the following conditions
9+ * are met:
10+ *
11+ * - Redistributions of source code must retain the above copyright
12+ * notice, this list of conditions and the following disclaimer.
13+ * - Redistributions in binary form must reproduce the above copyright
14+ * notice, this list of conditions and the following disclaimer in the
15+ * documentation and/or other materials provided with the distribution.
16+ * - No names of its contributors may be used to endorse or promote
17+ * products derived from this software without specific prior written
18+ * permission.
19+ *
20+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+ *
32+ * Last-modified: Sat 10 Mar 2007 20:58:00 JST
33+ */
34+/* ------------------------------------------------------------------------- */
35+#ifndef CLX_FORMAT_H
36+#define CLX_FORMAT_H
37+
38+#include <cstdlib>
39+#include <cctype>
40+#include <iomanip>
41+#include <iostream>
42+#include <sstream>
43+#include <ostream>
44+#include <string>
45+#include "lexical_cast.h"
46+
47+namespace clx {
48+ /* --------------------------------------------------------------------- */
49+ // basic_format
50+ /* --------------------------------------------------------------------- */
51+ template <
52+ class CharT,
53+ class Traits = std::char_traits<CharT>
54+ >
55+ class basic_format {
56+ public:
57+ typedef CharT char_type;
58+ typedef unsigned int size_type;
59+ typedef typename std::basic_string<CharT, Traits> string_type;
60+
61+ // constructor and destructor
62+ basic_format() : str_(), fmt_() {}
63+
64+ explicit basic_format(const char_type* fmt) :
65+ str_(), fmt_(fmt) { this->reset(); }
66+
67+ explicit basic_format(const string_type& fmt) :
68+ str_(), fmt_(fmt) { this->reset(); }
69+
70+ // operator
71+ template <class ValueT>
72+ basic_format& operator%(const ValueT& x) {
73+ if (*pos_ == rep && pos_ + 1 != fmt_.end() && *(pos_ + 1) != rep) {
74+ string_type flag;
75+ while (++pos_ != fmt_.end() && types_.find(*pos_) == string_type::npos) flag += *pos_;
76+ if (pos_ == fmt_.end()) return *this; // uncorrect format
77+ str_ += this->put(x, *pos_++, flag);
78+ }
79+ while (pos_ != fmt_.end() && *pos_ != rep) str_ += *pos_++;
80+ return *this;
81+ }
82+
83+ template <class Ch, class Tr>
84+ friend std::basic_ostream<Ch, Tr>& operator<<(
85+ std::basic_ostream<Ch, Tr>& sout, const basic_format<Ch, Tr>& f) {
86+ sout << f.str_;
87+ return sout;
88+ }
89+
90+ private:
91+ typedef typename string_type::const_iterator const_iterator;
92+ typedef typename std::basic_stringstream<CharT, Traits> internal_stream;
93+
94+ static const char_type rep = '%';
95+ static const char_type lpos = '-';
96+ static const char_type ws = ' ';
97+ static const char_type sign = '+';
98+ static const char_type zero = '0';
99+ static const char_type prefix = '#';
100+ static const char_type dot = '.';
101+
102+ string_type types_;
103+ string_type str_;
104+ string_type fmt_;
105+ const_iterator pos_;
106+
107+ void reset() {
108+ str_.clear();
109+ pos_ = fmt_.begin();
110+ types_ = "diouxXeEfFgGcs";
111+ while (pos_ != fmt_.end() && *pos_ != rep) str_ += *pos_++;
112+ }
113+
114+ template <class ValueT>
115+ string_type put(const ValueT& x, char_type c, const string_type& flag) {
116+ internal_stream ss;
117+
118+ this->setopt(ss, flag);
119+ this->settype(ss, c);
120+ ss << x;
121+
122+ return ss.str();
123+ }
124+
125+ void setopt(internal_stream& ss, const string_type& flag) {
126+ bool lp = false;
127+ for (const_iterator pos = flag.begin(); pos != flag.end(); pos++) {
128+ switch (*pos) {
129+ case lpos:
130+ lp = true;
131+ ss << std::setfill(ws);
132+ ss << std::left;
133+ break;
134+ case prefix:
135+ ss << std::showbase;
136+ break;
137+ case sign:
138+ ss << std::showpos;
139+ break;
140+ case ws:
141+ ss << std::setfill(*pos);
142+ break;
143+ case zero:
144+ if (!lp) ss << std::setfill(*pos);
145+ break;
146+ case dot:
147+ this->setprecision(ss, ++pos, flag.end());
148+ if (pos == flag.end()) return;
149+ break;
150+ default:
151+ if (isdigit(*pos)) this->setwidth(ss, pos, flag.end());
152+ if (pos == flag.end()) return;
153+ break; /* ignore unknown flags */
154+ }
155+ }
156+ }
157+
158+ void setwidth(internal_stream& ss, const_iterator& cur, const_iterator last) {
159+ std::string digit;
160+ for (; cur != last && isdigit(*cur); cur++) digit += *cur;
161+ if (!digit.empty()) ss << std::setw(lexical_cast<int>(digit));
162+ if (cur != last && *cur == dot) this->setprecision(ss, ++cur, last);
163+ }
164+
165+ void setprecision(internal_stream& ss, const_iterator& cur, const_iterator last) {
166+ std::string digit;
167+ for (; cur != last && isdigit(*cur); cur++) digit += *cur;
168+ if (!digit.empty()) ss << std::setprecision(lexical_cast<int>(digit));
169+ }
170+
171+ void settype(internal_stream& ss, char_type c) {
172+ char_type ctype;
173+ if (isupper(c)) ss << std::uppercase;
174+ ctype = tolower(c);
175+
176+ switch (ctype) {
177+ case 'o':
178+ ss << std::oct;
179+ break;
180+ case 'x':
181+ ss << std::hex;
182+ break;
183+ case 'e':
184+ ss << std::scientific;
185+ break;
186+ case 'f':
187+ ss << std::setiosflags(std::ios::fixed);
188+ break;
189+ default:
190+ break;
191+ }
192+ }
193+ };
194+
195+ typedef basic_format<char> format;
196+}
197+#endif // CLX_FORMAT_H
Added: svn:executable
旧リポジトリブラウザで表示