1 /*
2  * Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #pragma once
20 
21 #include "debug.h"
22 #include "typedefs.h"
23 #include "noexcept.h"
24 
25 namespace dcpp {
26 
27 /**
28  * Text handling routines for DC++. DC++ internally uses UTF-8 for
29  * (almost) all string:s, hence all foreign text must be converted
30  * appropriately...
31  * acp - ANSI code page used by the system
32  * wide - wide unicode string
33  * utf8 - UTF-8 representation of the string
34  * t - current GUI text format
35  * string - UTF-8 string (most of the time)
36  * wstring - Wide string
37  * tstring - GUI type string (acp string or wide string depending on build type)
38  */
39 namespace Text {
40     extern const string utf8;
41     extern string systemCharset;
42     extern string hubDefaultCharset;
43 
44     void initialize();
45 
46     const string& acpToUtf8(const string& str, string& tmp) noexcept;
acpToUtf8(const string & str)47     inline string acpToUtf8(const string& str) noexcept {
48         string tmp;
49         return acpToUtf8(str, tmp);
50     }
51 
52     const wstring& acpToWide(const string& str, wstring& tmp) noexcept;
acpToWide(const string & str)53     inline wstring acpToWide(const string& str) noexcept {
54         wstring tmp;
55         return acpToWide(str, tmp);
56     }
57 
58     const string& utf8ToAcp(const string& str, string& tmp) noexcept;
utf8ToAcp(const string & str)59     inline string utf8ToAcp(const string& str) noexcept {
60         string tmp;
61         return utf8ToAcp(str, tmp);
62     }
63 
64     const wstring& utf8ToWide(const string& str, wstring& tmp) noexcept;
utf8ToWide(const string & str)65     inline wstring utf8ToWide(const string& str) noexcept {
66         wstring tmp;
67         return utf8ToWide(str, tmp);
68     }
69 
70     const string& wideToAcp(const wstring& str, string& tmp) noexcept;
wideToAcp(const wstring & str)71     inline string wideToAcp(const wstring& str) noexcept {
72         string tmp;
73         return wideToAcp(str, tmp);
74     }
75 
76     const string& wideToUtf8(const wstring& str, string& tmp) noexcept;
wideToUtf8(const wstring & str)77     inline string wideToUtf8(const wstring& str) noexcept {
78         string tmp;
79         return wideToUtf8(str, tmp);
80     }
81 
82     int utf8ToWc(const char* str, wchar_t& c);
83     void wcToUtf8(wchar_t c, string& str);
84 
85 #ifdef UNICODE
toT(const string & str,tstring & tmp)86     inline const tstring& toT(const string& str, tstring& tmp) noexcept { return utf8ToWide(str, tmp); }
toT(const string & str)87     inline tstring toT(const string& str) noexcept { return utf8ToWide(str); }
88 
fromT(const tstring & str,string & tmp)89     inline const string& fromT(const tstring& str, string& tmp) noexcept { return wideToUtf8(str, tmp); }
fromT(const tstring & str)90     inline string fromT(const tstring& str) noexcept { return wideToUtf8(str); }
91 #else
toT(const string & str,tstring & tmp)92     inline const tstring& toT(const string& str, tstring& tmp) noexcept { return utf8ToAcp(str, tmp); }
toT(const string & str)93     inline tstring toT(const string& str) noexcept { return utf8ToAcp(str); }
94 
fromT(const tstring & str,string & tmp)95     inline const string& fromT(const tstring& str, string& tmp) noexcept { return acpToUtf8(str, tmp); }
fromT(const tstring & str)96     inline string fromT(const tstring& str) noexcept { return acpToUtf8(str); }
97 #endif
98 
toT(const StringList & lst,TStringList & tmp)99     inline const TStringList& toT(const StringList& lst, TStringList& tmp) noexcept {
100         for(StringIterC i = lst.begin(), iend = lst.end(); i != iend; ++i)
101             tmp.push_back(toT(*i));
102         return tmp;
103     }
104 
fromT(const TStringList & lst,StringList & tmp)105     inline const StringList& fromT(const TStringList& lst, StringList& tmp) noexcept {
106         for(TStringIterC i = lst.begin(), iend = lst.end(); i != iend; ++i)
107             tmp.push_back(fromT(*i));
108         return tmp;
109     }
110 
isAscii(const string & str)111     inline bool isAscii(const string& str) noexcept { return isAscii(str.c_str()); }
112     bool isAscii(const char* str) noexcept;
113 
114     bool validateUtf8(const string& str) noexcept;
115 
asciiToLower(char c)116     inline char asciiToLower(char c) { dcassert((((uint8_t)c) & 0x80) == 0); return (char)tolower(c); }
117 
118     wchar_t toLower(wchar_t c) noexcept;
119 
120     const wstring& toLower(const wstring& str, wstring& tmp) noexcept;
toLower(const wstring & str)121     inline wstring toLower(const wstring& str) noexcept {
122         wstring tmp;
123         return toLower(str, tmp);
124     }
125 
126     const string& toLower(const string& str, string& tmp) noexcept;
toLower(const string & str)127     inline string toLower(const string& str) noexcept {
128         string tmp;
129         return toLower(str, tmp);
130     }
131 
132     const string& convert(const string& str, string& tmp, const string& fromCharset, const string& toCharset) noexcept;
convert(const string & str,const string & fromCharset,const string & toCharset)133     inline string convert(const string& str, const string& fromCharset, const string& toCharset) noexcept {
134         string tmp;
135         return convert(str, tmp, fromCharset, toCharset);
136     }
137 
138     const string& toUtf8(const string& str, const string& fromCharset, string& tmp) noexcept;
139     inline string toUtf8(const string& str, const string& fromCharset = systemCharset) {
140         string tmp;
141         return toUtf8(str, fromCharset, tmp);
142     }
143 
144     const string& fromUtf8(const string& str, const string& toCharset, string& tmp) noexcept;
145     inline string fromUtf8(const string& str, const string& toCharset = systemCharset) noexcept {
146         string tmp;
147         return fromUtf8(str, toCharset, tmp);
148     }
149 
150     string toDOS(string tmp);
151     wstring toDOS(wstring tmp);
152 
153 }
154 
155 } // namespace dcpp
156