| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					
						
							|  |  |  | // of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  | // in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  | // copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  | // furnished to do so, subject to the following conditions:
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					
						
							|  |  |  | // THE SOFTWARE.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | #include <kiwano/macros.h>
 | 
					
						
							|  |  |  | #include <kiwano/core/String.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace kiwano | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-23 22:08:14 +08:00
										 |  |  | namespace strings | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(KGE_PLATFORM_WINDOWS)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String Format(const char* format, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     String result; | 
					
						
							|  |  |  |     if (format) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         va_list args = nullptr; | 
					
						
							|  |  |  |         va_start(args, format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const auto len = static_cast<size_t>(::_vscprintf(format, args) + 1); | 
					
						
							|  |  |  |         if (len) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-04-07 16:35:26 +08:00
										 |  |  |             result.resize(len - 1); | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |             ::_vsnprintf_s(&result[0], len, len, format, args); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         va_end(args); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  | WideString Format(const wchar_t* format, ...) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     WideString result; | 
					
						
							|  |  |  |     if (format) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         va_list args = nullptr; | 
					
						
							|  |  |  |         va_start(args, format); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const auto len = static_cast<size_t>(::_vscwprintf(format, args) + 1); | 
					
						
							|  |  |  |         if (len) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-04-07 16:35:26 +08:00
										 |  |  |             result.resize(len - 1); | 
					
						
							| 
									
										
										
										
											2020-03-19 14:28:50 +08:00
										 |  |  |             ::_vsnwprintf_s(&result[0], len, len, format, args); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         va_end(args); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-14 12:28:29 +08:00
										 |  |  | String WideToNarrow(const WideString& str) | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     if (str.empty()) | 
					
						
							|  |  |  |         return String(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 16:35:26 +08:00
										 |  |  |     int len = ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL); | 
					
						
							|  |  |  |     if (len > 0) | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         String result; | 
					
						
							| 
									
										
										
										
											2020-04-07 16:35:26 +08:00
										 |  |  |         result.resize(len - 1); | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 16:35:26 +08:00
										 |  |  |         ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, &result[0], len, NULL, NULL); | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |         return result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return String(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-14 12:28:29 +08:00
										 |  |  | WideString NarrowToWide(const String& str) | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     if (str.empty()) | 
					
						
							|  |  |  |         return WideString(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 16:35:26 +08:00
										 |  |  |     int len = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); | 
					
						
							|  |  |  |     if (len > 0) | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         WideString result; | 
					
						
							| 
									
										
										
										
											2020-04-07 16:35:26 +08:00
										 |  |  |         result.resize(len - 1); | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 16:35:26 +08:00
										 |  |  |         ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &result[0], len); | 
					
						
							| 
									
										
										
										
											2020-02-15 17:32:32 +08:00
										 |  |  |         return result; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return WideString(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif  // KGE_PLATFORM_WINDOWS
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace string
 | 
					
						
							|  |  |  | }  // namespace kiwano
 |