1 /* 2 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #ifndef PLATFORMSTRING_H 27 #define PLATFORMSTRING_H 28 29 30 #include <string> 31 #include <list> 32 #include <stdio.h> 33 #include <stdlib.h> 34 35 #include "jni.h" 36 #include "Platform.h" 37 38 39 template <typename T> 40 class DynamicBuffer { 41 private: 42 T* FData; 43 size_t FSize; 44 45 public: 46 DynamicBuffer(size_t Size) { 47 FSize = 0; 48 FData = NULL; 49 Resize(Size); 50 } 51 52 ~DynamicBuffer() { 53 delete[] FData; 54 } 55 56 T* GetData() { return FData; } 57 size_t GetSize() { return FSize; } 58 59 bool Resize(size_t Size) { 60 FSize = Size; 61 62 if (FData != NULL) { 63 delete[] FData; 64 FData = NULL; 65 } 66 67 if (FSize != 0) { 68 FData = new T[FSize]; 69 if (FData != NULL) { 70 Zero(); 71 } else { 72 return false; 73 } 74 } 75 76 return true; 77 } 78 79 void Zero() { 80 memset(FData, 0, FSize * sizeof(T)); 81 } 82 83 T& operator[](size_t index) { 84 return FData[index]; 85 } 86 }; 87 88 class PlatformString { 89 private: 90 char* FData; // Stored as UTF-8 91 size_t FLength; 92 wchar_t* FWideTStringToFree; 93 94 void initialize(); 95 96 // Prohibit Heap-Based PlatformStrings 97 private: 98 static void *operator new(size_t size); 99 static void operator delete(void *ptr); 100 101 public: 102 PlatformString(void); 103 PlatformString(const PlatformString &value); 104 PlatformString(const char* value); 105 PlatformString(const wchar_t* value); 106 PlatformString(const std::string &value); 107 PlatformString(const std::wstring &value); 108 PlatformString(size_t Value); 109 110 static TString Format(const TString value, ...); 111 112 ~PlatformString(void); 113 114 size_t length(); 115 116 char* c_str(); 117 char* toMultibyte(); 118 wchar_t* toWideString(); 119 std::wstring toUnicodeString(); 120 std::string toStdString(); 121 TCHAR* toPlatformString(); 122 TString toString(); 123 124 operator char* (); 125 operator wchar_t* (); 126 operator std::wstring (); 127 128 // Caller must free result using delete[]. 129 static char* duplicate(const char* Value); 130 131 // Caller must free result using delete[]. 132 static wchar_t* duplicate(const wchar_t* Value); 133 }; 134 135 136 #endif // PLATFORMSTRING_H