]> git.stg.codes - stg.git/blob - libs/srvconf/parsers/optional_utils.h
Public interfaces: part 1
[stg.git] / libs / srvconf / parsers / optional_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 #pragma once
22
23 #include "stg/optional.h"
24 #include "stg/common.h"
25
26 #include <string>
27 #include <ostream>
28
29 namespace STG
30 {
31
32 template <typename T>
33 inline
34 void appendTag(std::ostream& stream, const std::string& name, const T& value)
35 {
36     stream << "<" << name << " value=\"" << value << "\"/>";
37 }
38
39 template <typename T>
40 inline
41 void appendTag(std::ostream& stream, const std::string& name, size_t suffix, const T& value)
42 {
43     stream << "<" << name << suffix << " value=\"" << value << "\"/>";
44 }
45
46 template <>
47 inline
48 void appendTag<uint8_t>(std::ostream& stream, const std::string& name, const uint8_t& value)
49 {
50     stream << "<" << name << " value=\"" << static_cast<unsigned>(value) << "\"/>";
51 }
52
53 template <>
54 inline
55 void appendTag<int8_t>(std::ostream& stream, const std::string& name, const int8_t& value)
56 {
57     stream << "<" << name << " value=\"" << static_cast<int>(value) << "\"/>";
58 }
59
60 template <typename T>
61 inline
62 void appendAttr(std::ostream& stream, const std::string& name, const T& value)
63 {
64     stream << " " << name << "=\"" << value << "\"";
65 }
66
67 template <typename T>
68 inline
69 void appendAttr(std::ostream& stream, const std::string& name, size_t suffix, const T& value)
70 {
71     stream << " " << name << suffix << "=\"" << value << "\"";
72 }
73
74 template <>
75 inline
76 void appendAttr<uint8_t>(std::ostream& stream, const std::string& name, const uint8_t& value)
77 {
78     stream << " " << name << "=\"" << static_cast<unsigned>(value) << "\"";
79 }
80
81 template <>
82 inline
83 void appendAttr<int8_t>(std::ostream& stream, const std::string& name, const int8_t& value)
84 {
85     stream << " " << name << "=\"" << static_cast<int>(value) << "\"";
86 }
87
88 template <typename T>
89 inline
90 void appendResetableTag(std::ostream& stream, const std::string& name, const T& value)
91 {
92     if (!value.empty())
93         appendTag(stream, name, value.const_data());
94 }
95
96 template <typename T>
97 inline
98 void appendResetableTag(std::ostream& stream, const std::string& name, size_t suffix, const T& value)
99 {
100     if (!value.empty())
101         appendTag(stream, name, suffix, value.const_data());
102 }
103
104 template <typename T>
105 inline
106 void appendResetableAttr(std::ostream& stream, const std::string& name, const T& value)
107 {
108     if (!value.empty())
109         appendAttr(stream, name, value.const_data());
110 }
111
112 template <typename T>
113 inline
114 void appendResetableAttr(std::ostream& stream, const std::string& name, size_t suffix, const T& value)
115 {
116     if (!value.empty())
117         appendAttr(stream, name, suffix, value.const_data());
118 }
119
120 inline
121 Optional<std::string> maybeEncode(const Optional<std::string>& value)
122 {
123     Optional<std::string> res;
124     if (!value.empty())
125         res = Encode12str(value.data());
126     return res;
127 }
128
129 inline
130 Optional<std::string> maybeIconv(const Optional<std::string>& value, const std::string& fromEncoding, const std::string& toEncoding)
131 {
132     Optional<std::string> res;
133     if (!value.empty())
134         res = IconvString(value.data(), fromEncoding, toEncoding);
135     return res;
136 }
137
138 } // namespace STG