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 #include "GenericPlatform.h" 35 36 37 #include <fstream> 38 #include <locale> 39 40 #ifdef WINDOWS 41 #include <codecvt> 42 #endif //WINDOWS 43 44 45 GenericPlatform::GenericPlatform(void) { 46 } 47 48 GenericPlatform::~GenericPlatform(void) { 49 } 50 51 TString GenericPlatform::GetConfigFileName() { 52 TString result; 53 TString basedir = GetPackageAppDirectory(); 54 55 if (basedir.empty() == false) { 56 basedir = FilePath::IncludeTrailingSlash(basedir); 57 TString appConfig = basedir + GetAppName() + _T(".cfg"); 58 59 if (FilePath::FileExists(appConfig) == true) { 60 result = appConfig; 61 } 62 else { 63 result = basedir + _T("package.cfg"); 64 65 if (FilePath::FileExists(result) == false) { 66 result = _T(""); 67 } 68 } 69 } 70 71 return result; 72 } 73 74 TString GenericPlatform::GetPackageAppDirectory() { 75 #if defined(WINDOWS) || defined(LINUX) 76 return FilePath::IncludeTrailingSlash(GetPackageRootDirectory()) + _T("app"); 77 #endif //WINDOWS || LINUX 78 #ifdef MAC 79 return FilePath::IncludeTrailingSlash(GetPackageRootDirectory()) + _T("Java"); 80 #endif 81 } 82 83 TString GenericPlatform::GetPackageLauncherDirectory() { 84 #if defined(WINDOWS) || defined(LINUX) 85 return GetPackageRootDirectory(); 86 #endif //WINDOWS || LINUX 87 #ifdef MAC 88 return FilePath::IncludeTrailingSlash(GetPackageRootDirectory()) + _T("MacOS"); 89 #endif 90 } 91 92 std::list<TString> GenericPlatform::LoadFromFile(TString FileName) { 93 std::list<TString> result; 94 95 if (FilePath::FileExists(FileName) == true) { 96 std::wifstream stream(FileName.data()); 97 98 #ifdef WINDOWS 99 const std::locale empty_locale = std::locale::empty(); 100 #endif //WINDOWS 101 #ifdef POSIX 102 const std::locale empty_locale = std::locale::classic(); 103 #endif //POSIX 104 #if defined(WINDOWS) 105 const std::locale utf8_locale = std::locale(empty_locale, new std::codecvt_utf8<wchar_t>()); 106 stream.imbue(utf8_locale); 107 #endif //WINDOWS 108 109 if (stream.is_open() == true) { 110 while (stream.eof() == false) { 111 std::wstring line; 112 std::getline(stream, line); 113 114 // # at the first character will comment out the line. 115 if (line.empty() == false && line[0] != '#') { 116 result.push_back(PlatformString(line).toString()); 117 } 118 } 119 } 120 } 121 122 return result; 123 } 124 125 void GenericPlatform::SaveToFile(TString FileName, std::list<TString> Contents, bool ownerOnly) { 126 TString path = FilePath::ExtractFilePath(FileName); 127 128 if (FilePath::DirectoryExists(path) == false) { 129 FilePath::CreateDirectory(path, ownerOnly); 130 } 131 132 std::wofstream stream(FileName.data()); 133 134 FilePath::ChangePermissions(FileName.data(), ownerOnly); 135 136 #ifdef WINDOWS 137 const std::locale empty_locale = std::locale::empty(); 138 #endif //WINDOWS 139 #ifdef POSIX 140 const std::locale empty_locale = std::locale::classic(); 141 #endif //POSIX 142 #if defined(WINDOWS) 143 const std::locale utf8_locale = std::locale(empty_locale, new std::codecvt_utf8<wchar_t>()); 144 stream.imbue(utf8_locale); 145 #endif //WINDOWS || MAC 146 147 if (stream.is_open() == true) { 148 for (std::list<TString>::const_iterator iterator = Contents.begin(); iterator != Contents.end(); iterator++) { 149 TString line = *iterator; 150 stream << PlatformString(line).toUnicodeString() << std::endl; 151 } 152 } 153 } 154 155 #if defined(WINDOWS) || defined(LINUX) 156 TString GenericPlatform::GetAppName() { 157 TString result = GetModuleFileName(); 158 result = FilePath::ExtractFileName(result); 159 result = FilePath::ChangeFileExt(result, _T("")); 160 return result; 161 } 162 #endif //WINDOWS || LINUX 163 164 std::map<TString, TString> GenericPlatform::GetKeys() { 165 std::map<TString, TString> keys; 166 keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINJAR_KEY, _T("app.mainjar"))); 167 keys.insert(std::map<TString, TString>::value_type(CONFIG_MAINCLASSNAME_KEY, _T("app.mainclass"))); 168 keys.insert(std::map<TString, TString>::value_type(CONFIG_CLASSPATH_KEY, _T("app.classpath"))); 169 keys.insert(std::map<TString, TString>::value_type(APP_NAME_KEY, _T("app.name"))); 170 keys.insert(std::map<TString, TString>::value_type(CONFIG_SPLASH_KEY, _T("app.splash"))); 171 keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_ID_KEY, _T("app.preferences.id"))); 172 keys.insert(std::map<TString, TString>::value_type(CONFIG_APP_MEMORY, _T("app.memory"))); 173 keys.insert(std::map<TString, TString>::value_type(JVM_RUNTIME_KEY, _T("app.runtime"))); 174 keys.insert(std::map<TString, TString>::value_type(PACKAGER_APP_DATA_DIR, _T("app.preferences.id"))); 175 return keys; 176 } 177 178 #ifdef DEBUG 179 DebugState GenericPlatform::GetDebugState() { 180 DebugState result = dsNone; 181 182 if (IsNativeDebuggerPresent() == true) { 183 result = dsNative; 184 } 185 186 return result; 187 } 188 #endif //DEBUG