1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 #ifndef PLATFORMSTRING_H 35 #define PLATFORMSTRING_H 36 37 38 #include <string> 39 #include <list> 40 #include <stdio.h> 41 #include <stdlib.h> 42 43 #include "jni.h" 44 #include "Platform.h" 45 46 47 struct WideString { 48 size_t length; 49 wchar_t* data; 50 51 WideString() { length = 0; data = NULL; } 52 }; 53 54 struct MultibyteString { 55 size_t length; 56 char* data; 57 58 MultibyteString() { length = 0; data = NULL; } 59 }; 60 61 62 template <typename T> 63 class DynamicBuffer { 64 private: 65 T* FData; 66 size_t FSize; 67 68 public: 69 DynamicBuffer(size_t Size) { 70 FSize = 0; 71 FData = NULL; 72 Resize(Size); 73 } 74 75 ~DynamicBuffer() { 76 delete[] FData; 77 } 78 79 T* GetData() { return FData; } 80 size_t GetSize() { return FSize; } 81 82 void Resize(size_t Size) { 83 FSize = Size; 84 85 if (FData != NULL) { 86 delete[] FData; 87 FData = NULL; 88 } 89 90 if (FSize != 0) { 91 FData = new T[FSize]; 92 Zero(); 93 } 94 } 95 96 void Zero() { 97 memset(FData, 0, FSize * sizeof(T)); 98 } 99 100 T& operator[](size_t index) { 101 return FData[index]; 102 } 103 }; 104 105 106 #ifdef MAC 107 // StringToFileSystemString is a stack object. It's usage is simply inline to convert a 108 // TString to a file system string. Example: 109 // 110 // return dlopen(StringToFileSystemString(FileName), RTLD_LAZY); 111 // 112 class StringToFileSystemString { 113 // Prohibit Heap-Based StringToFileSystemString 114 private: 115 static void *operator new(size_t size); 116 static void operator delete(void *ptr); 117 118 private: 119 TCHAR* FData; 120 bool FRelease; 121 122 public: 123 StringToFileSystemString(const TString &value); 124 ~StringToFileSystemString(); 125 126 operator TCHAR* (); 127 }; 128 129 130 // FileSystemStringToString is a stack object. It's usage is simply inline to convert a 131 // file system string to a TString. Example: 132 // 133 // DynamicBuffer<TCHAR> buffer(MAX_PATH); 134 // if (readlink("/proc/self/exe", buffer.GetData(), MAX_PATH) != -1) 135 // result = FileSystemStringToString(buffer.GetData()); 136 // 137 class FileSystemStringToString { 138 // Prohibit Heap-Based FileSystemStringToString 139 private: 140 static void *operator new(size_t size); 141 static void operator delete(void *ptr); 142 143 private: 144 TString FData; 145 146 public: 147 FileSystemStringToString(const TCHAR* value); 148 149 operator TString (); 150 }; 151 #endif //MAC 152 153 #ifdef LINUX 154 #define StringToFileSystemString PlatformString 155 #define FileSystemStringToString PlatformString 156 #endif //LINUX 157 158 159 class PlatformString { 160 private: 161 char* FData; // Stored as UTF-8 162 size_t FLength; 163 wchar_t* FWideTStringToFree; 164 165 void initialize(); 166 167 // Caller must free result using delete[]. 168 static void CopyString(char *Destination, size_t NumberOfElements, const char *Source); 169 170 // Caller must free result using delete[]. 171 static void CopyString(wchar_t *Destination, size_t NumberOfElements, const wchar_t *Source); 172 173 static WideString MultibyteStringToWideString(const char* value); 174 static MultibyteString WideStringToMultibyteString(const wchar_t* value); 175 176 // Prohibit Heap-Based PlatformStrings 177 private: 178 static void *operator new(size_t size); 179 static void operator delete(void *ptr); 180 181 public: 182 PlatformString(void); 183 PlatformString(const PlatformString &value); 184 PlatformString(const char* value); 185 PlatformString(const wchar_t* value); 186 PlatformString(const std::string &value); 187 PlatformString(const std::wstring &value); 188 PlatformString(JNIEnv *env, jstring value); 189 PlatformString(size_t Value); 190 191 static std::string Format(std::string value, ...); 192 193 ~PlatformString(void); 194 195 size_t length(); 196 197 char* c_str(); 198 char* toMultibyte(); 199 wchar_t* toWideString(); 200 std::wstring toUnicodeString(); 201 std::string toStdString(); 202 jstring toJString(JNIEnv *env); 203 TCHAR* toPlatformString(); 204 TString toString(); 205 206 operator char* (); 207 operator wchar_t* (); 208 operator std::wstring (); 209 210 // Caller must free result using delete[]. 211 static char* duplicate(const char* Value); 212 213 // Caller must free result using delete[]. 214 static wchar_t* duplicate(const wchar_t* Value); 215 }; 216 217 218 #endif //PLATFORMSTRING_H