SUAPI-CppWrapper
C++WrapperforSketchUpCAPI
String.cpp
1 //
2 // String.cpp
3 //
4 // Sketchup C++ Wrapper for C API
5 // MIT License
6 //
7 // Copyright (c) 2017 Tom Kaneko
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 //
27 
28 // Macro for getting rid of unused variables commonly for assert checking
29 #define _unused(x) ((void)(x))
30 
31 #include "SUAPI-CppWrapper/String.hpp"
32 
33 #include <cassert>
34 #include <vector>
35 
36 namespace CW {
37 
38 SUStringRef m_string;
39 
40 String::String():
41  m_string(create_string_ref()),
42  m_encoding(StringEncoding::UTF8)
43 {}
44 
45 
46 String::String(SUStringRef string_ref):
47  m_string(string_ref),
48  m_encoding(StringEncoding::UTF8)
49 {}
50 
51 
52 String::String(const std::string &string_input, StringEncoding enc):
53  m_string(create_string_ref(string_input, enc)),
54  m_encoding(enc)
55 {}
56 
57 
58 String::String(const char string_input[]):
59  m_string(create_string_ref(&string_input[0])),
60  m_encoding(StringEncoding::UTF8)
61 {}
62 
63 
64 String::String(const unichar string_input[]):
65  m_string(create_string_ref(&string_input[0])),
66  m_encoding(StringEncoding::UTF16)
67 {}
68 
69 
70 String::String(const String& other):
71  m_string(create_string_ref(other.std_string(), StringEncoding::UTF8))
72 {
73  // TODO support UTF16
74 }
75 
76 
78  // Release old string and create new
79  if (SUIsValid(m_string)) {
80  SUResult res = SUStringRelease(&m_string);
81  assert(res == SU_ERROR_NONE); _unused(res);
82  }
83  m_string = SU_INVALID;
84  m_string = create_string_ref(other.std_string(), StringEncoding::UTF8);
85  return *this;
86 }
87 
88 
89 bool operator==(const String &lhs, const String &rhs) {
90  // Check validity
91  // Check type
92  if (lhs.m_encoding != rhs.m_encoding) {
93  return false;
94  }
95  // Check value
96  if (lhs.std_string() == rhs.std_string()) {
97  return true;
98  }
99  return false;
100 }
101 
102 
103 SUStringRef String::create_string_ref() {
104  SUStringRef string_ref = SU_INVALID;
105  SUResult res = SUStringCreate(&string_ref);
106  assert(res == SU_ERROR_NONE); _unused(res);
107  return string_ref;
108 }
109 
110 
111 SUStringRef String::create_string_ref(std::string string_input, StringEncoding enc) {
112  SUStringRef string_ref = SU_INVALID;
113  if (enc == StringEncoding::UTF8) {
114 
115  // TODO: to reduce code, use String::create_string_ref(const char* string_input[]).
116  SUStringCreateFromUTF8(&string_ref, &string_input[0]);
117  }
118  else {
119  // TODO UTF16 to be supported.
120  //SUStringCreateFromUTF16(&string_ref, &string_input[0]);
121  }
122  return string_ref;
123 }
124 
125 
126 SUStringRef String::create_string_ref(const char string_input[]) {
127  SUStringRef string_ref = SU_INVALID;
128  SUStringCreateFromUTF8(&string_ref, &string_input[0]);
129  return string_ref;
130 }
131 
132 
133 SUStringRef String::create_string_ref(const unichar string_input[]) {
134  SUStringRef string_ref = SU_INVALID;
135  // TODO UTF16 to be supported.
136  //SUStringCreateFromUTF16(&string_ref, &string_input[0]);
137  return string_ref;
138 }
139 
140 
141 String::~String() {
142  if (SUIsValid(m_string)) {
143  SUResult res = SUStringRelease(&m_string);
144  assert(res == SU_ERROR_NONE); _unused(res);
145  }
146 }
147 
148 
149 SUStringRef String::ref() const {
150  return m_string;
151 }
152 
153 
154 std::string String::std_string() const {
155  size_t out_length = 0;
156  SUResult res = SUStringGetUTF8Length(m_string, &out_length);
157  assert(res == SU_ERROR_NONE);
158  out_length++; // Allow for null terminated string
159  std::vector<char> char_array(out_length, 0);
160  res = SUStringGetUTF8(m_string, out_length, char_array.data(), &out_length);
161  assert(res == SU_ERROR_NONE); _unused(res);
162  std::string str(char_array.data());
163  return str;
164 }
165 
166 
167 String::operator std::string() const {
168  return std_string();
169 }
170 
171 
172 size_t String::size() const {
173  size_t out_length = 0;
174  SUResult res = SUStringGetUTF8Length(m_string, &out_length);
175  assert(res == SU_ERROR_NONE); _unused(res);
176  return out_length;
177 }
178 
179 
180 bool String::empty() const {
181  // size of 1 is empty due to the \n character at the end.
182  if (size() == 1) {
183  return true;
184  }
185  return false;
186 }
187 
188 
189 } /* namespace CW */
String & operator=(String &other)
Definition: String.cpp:77
friend bool operator==(const String &lhs, const String &rhs)
Definition: String.cpp:89
bool empty() const
Definition: String.cpp:180
Definition: Color.hpp:34