HoverRace  2.0
Str.h
Go to the documentation of this file.
1 
2 // Str.h
3 //
4 // Copyright (c) 2009, 2014, 2016 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include "OS.h"
25 
26 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
27 # ifdef MR_ENGINE
28 # define MR_DllDeclare __declspec( dllexport )
29 # else
30 # define MR_DllDeclare __declspec( dllimport )
31 # endif
32 #else
33 # define MR_DllDeclare
34 #endif
35 
36 namespace HoverRace {
37 namespace Util {
38 
39 namespace Str {
40 
41 MR_DllDeclare wchar_t *Utf8ToWide(const char *s);
42 MR_DllDeclare char *WideToUtf8(const wchar_t *ws);
43 
44 MR_DllDeclare void Append(std::string &dest, const std::string &src,
45  size_t len);
46 MR_DllDeclare size_t Assign(std::string &dest, const std::string &src,
47  size_t len);
48 
51 {
52  wchar_t *cs;
53 public:
54  UW(const char *s = nullptr) noexcept : cs(Utf8ToWide(s)) { }
55  explicit UW(const std::string &s) noexcept : cs(Utf8ToWide(s.c_str())) { }
56  ~UW() noexcept { free(cs); }
57  operator const wchar_t*() const noexcept { return cs; }
58  operator const std::wstring() const { return cs; }
59 };
60 
63 {
64  char *cs;
65 public:
66  WU(const wchar_t *ws = nullptr) noexcept : cs(WideToUtf8(ws)) { }
67  explicit WU(const std::wstring &s) noexcept : cs(WideToUtf8(s.c_str())) { }
68  ~WU() noexcept { free(cs); }
69  operator const char*() const noexcept { return cs; }
70  operator const std::string() const { return cs; }
71 };
72 
73 // Convert UTF-8 to path_t.
74 #ifdef WITH_WIDE_PATHS
75  inline OS::path_t UP(const char *s = nullptr) noexcept { return OS::path_t((const wchar_t*)Str::UW(s)); }
76  inline OS::path_t UP(const std::string &s) noexcept { return OS::path_t((const wchar_t*)Str::UW(s)); }
77 #else
78  inline OS::path_t UP(const char *s = nullptr) noexcept { return OS::path_t(s); }
79  inline OS::path_t UP(const std::string &s) noexcept { return OS::path_t(s); }
80 #endif
81 
82 // Convert path_t to UTF-8.
84 {
85 # ifdef WITH_WIDE_PATHS
86  WU cs;
87  public:
88  explicit PU(const OS::path_t &path) noexcept : cs(path.wstring()) { }
89  operator const char*() const noexcept { return (const char*)cs; }
90  operator const std::string() const { return std::string((const char*)cs); }
91 # else
92  std::string cs;
93  public:
94  explicit PU(const OS::path_t &path) noexcept : cs(path.string()) { }
95  operator const char*() const noexcept { return cs.c_str(); }
96  operator const std::string() const { return cs; }
97 # endif
98 };
99 
100  // Convert wide strings to path_t.
101 #ifdef WITH_WIDE_PATHS
102  inline OS::path_t WP(const wchar_t *s = nullptr) noexcept { return OS::path_t(s); }
103  inline OS::path_t WP(const std::wstring &s) noexcept { return OS::path_t(s); }
104 #else
105  inline OS::path_t WP(const wchar_t *s=NULL) noexcept { return OS::path_t((const char*)Str::WU(s)); }
106  inline OS::path_t WP(const std::wstring &s) noexcept { return OS::path_t((const char*)Str::WU(s)); }
107 #endif
108 
109 // Convert path_t to wide string.
111 {
112 # ifdef WITH_WIDE_PATHS
113  std::wstring cs;
114  public:
115  explicit PW(const OS::path_t &path) noexcept : cs(path.wstring()) { }
116  operator const wchar_t*() const noexcept { return cs.c_str(); }
117  operator const std::wstring() const { return cs; }
118 # else
120  public:
121  explicit PW(const OS::path_t &path) noexcept : cs(path.string()) { }
122  operator const wchar_t*() const noexcept { return (const wchar_t*)cs; }
123  operator const std::wstring() const { return cs; }
124 # endif
125 };
126 
139 {
140 public:
141  explicit Lua(const std::string &s) : s(s) { }
142 
143 public:
144  std::ostream &StreamOut(std::ostream &os) const;
145 
146 private:
147  std::string s;
148 };
149 
150 inline std::ostream &operator<<(std::ostream &os, const Lua &lua)
151 {
152  return lua.StreamOut(os);
153 }
154 
155 } // namespace Str
156 
157 } // namespace Util
158 } // namespace HoverRace
159 
160 #undef MR_DllDeclare
Definition: Str.h:83
MR_DllDeclare wchar_t * Utf8ToWide(const char *s)
Convert a UTF-8 string to a wide string.
Definition: Str.cpp:83
boost::filesystem::path path_t
Definition: OS.h:57
Definition: Str.h:39
Utility class for easy conversion of wide strings to UTF-8.
Definition: Str.h:62
UW(const std::string &s) noexcept
Definition: Str.h:55
Lua(const std::string &s)
Definition: Str.h:141
WU(const std::wstring &s) noexcept
Definition: Str.h:67
OS::path_t UP(const char *s=nullptr) noexcept
Definition: Str.h:78
~UW() noexcept
Definition: Str.h:56
MR_DllDeclare size_t Assign(std::string &dest, const std::string &src, size_t len)
Assign a UTF-8 string to another, with a maximum length.
Definition: Str.cpp:165
Mark a string to be streamed out escaped for Lua.
Definition: Str.h:138
Definition: Str.h:110
OS::path_t WP(const wchar_t *s=NULL) noexcept
Definition: Str.h:105
Utility class for easy conversion of UTF-8 to wide strings.
Definition: Str.h:50
wchar_t * cs
Definition: Str.h:52
std::ostream & StreamOut(std::ostream &os) const
Definition: Str.cpp:181
MR_DllDeclare char * WideToUtf8(const wchar_t *ws)
Convert a wide string to a UTF-8 string.
Definition: Str.cpp:114
#define MR_DllDeclare
Definition: Str.h:33
PU(const OS::path_t &path) noexcept
Definition: Str.h:94
WU(const wchar_t *ws=nullptr) noexcept
Definition: Str.h:66
~WU() noexcept
Definition: Str.h:68
MR_DllDeclare void Append(std::string &dest, const std::string &src, size_t len)
Append a string onto another string with a specific maximum length.
Definition: Str.cpp:146
std::string s
Definition: Str.h:147
UW(const char *s=nullptr) noexcept
Definition: Str.h:54
PW(const OS::path_t &path) noexcept
Definition: Str.h:121
Definition: Announcement.h:24
std::string cs
Definition: Str.h:92
std::ostream & operator<<(std::ostream &os, const Lua &lua)
Definition: Str.h:150
UW cs
Definition: Str.h:119
char * cs
Definition: Str.h:64