]> git.stg.codes - stg.git/blob - libs/srvconf/parsers/resetable_utils.h
Port to CMake, get rid of os_int.h.
[stg.git] / libs / srvconf / parsers / resetable_utils.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #ifndef __STG_STGLIBS_SRVCONF_RESETABLE_UTILS_H__
22 #define __STG_STGLIBS_SRVCONF_RESETABLE_UTILS_H__
23
24 #include "stg/resetable.h"
25 #include "stg/common.h"
26
27 #include <string>
28 #include <ostream>
29
30 namespace STG
31 {
32
33 template <typename T>
34 inline
35 void appendTag(std::ostream & stream, const std::string & name, const T & value)
36 {
37     stream << "<" << name << " value=\"" << value << "\"/>";
38 }
39
40 template <typename T>
41 inline
42 void appendTag(std::ostream & stream, const std::string & name, size_t suffix, const T & value)
43 {
44     stream << "<" << name << suffix << " value=\"" << value << "\"/>";
45 }
46
47 template <>
48 inline
49 void appendTag<uint8_t>(std::ostream & stream, const std::string & name, const uint8_t & value)
50 {
51     stream << "<" << name << " value=\"" << static_cast<unsigned>(value) << "\"/>";
52 }
53
54 template <>
55 inline
56 void appendTag<int8_t>(std::ostream & stream, const std::string & name, const int8_t & value)
57 {
58     stream << "<" << name << " value=\"" << static_cast<int>(value) << "\"/>";
59 }
60
61 template <typename T>
62 inline
63 void appendAttr(std::ostream & stream, const std::string & name, const T & value)
64 {
65     stream << " " << name << "=\"" << value << "\"";
66 }
67
68 template <typename T>
69 inline
70 void appendAttr(std::ostream & stream, const std::string & name, size_t suffix, const T & value)
71 {
72     stream << " " << name << suffix << "=\"" << value << "\"";
73 }
74
75 template <>
76 inline
77 void appendAttr<uint8_t>(std::ostream & stream, const std::string & name, const uint8_t & value)
78 {
79     stream << " " << name << "=\"" << static_cast<unsigned>(value) << "\"";
80 }
81
82 template <>
83 inline
84 void appendAttr<int8_t>(std::ostream & stream, const std::string & name, const int8_t & value)
85 {
86     stream << " " << name << "=\"" << static_cast<int>(value) << "\"";
87 }
88
89 template <typename T>
90 inline
91 void appendResetableTag(std::ostream & stream, const std::string & name, const T & value)
92 {
93 if (!value.empty())
94     appendTag(stream, name, value.const_data());
95 }
96
97 template <typename T>
98 inline
99 void appendResetableTag(std::ostream & stream, const std::string & name, size_t suffix, const T & value)
100 {
101 if (!value.empty())
102     appendTag(stream, name, suffix, value.const_data());
103 }
104
105 template <typename T>
106 inline
107 void appendResetableAttr(std::ostream & stream, const std::string & name, const T & value)
108 {
109 if (!value.empty())
110     appendAttr(stream, name, value.const_data());
111 }
112
113 template <typename T>
114 inline
115 void appendResetableAttr(std::ostream & stream, const std::string & name, size_t suffix, const T & value)
116 {
117 if (!value.empty())
118     appendAttr(stream, name, suffix, value.const_data());
119 }
120
121 inline
122 RESETABLE<std::string> MaybeEncode(const RESETABLE<std::string> & value)
123 {
124 RESETABLE<std::string> res;
125 if (!value.empty())
126     res = Encode12str(value.data());
127 return res;
128 }
129
130 inline
131 RESETABLE<std::string> MaybeIconv(const RESETABLE<std::string> & value, const std::string & fromEncoding, const std::string & toEncoding)
132 {
133 RESETABLE<std::string> res;
134 if (!value.empty())
135     res = IconvString(value.data(), fromEncoding, toEncoding);
136 return res;
137 }
138
139 } // namespace STG
140
141 #endif