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 #include "Helpers.h"
  27 #include "PlatformString.h"
  28 #include "PropertyFile.h"
  29 
  30 
  31 bool Helpers::SplitOptionIntoNameValue(
  32         TString option, TString& Name, TString& Value) {
  33     bool hasValue = false;
  34     Name = _T("");
  35     Value = _T("");
  36     unsigned int index = 0;
  37 
  38     for (; index < option.length(); index++) {
  39         TCHAR c = option[index];
  40 
  41         switch (c) {
  42             case '=': {
  43                 index++;
  44                 hasValue = true;
  45                 break;
  46             }
  47 
  48             case '\\': {
  49                 if (index + 1 < option.length()) {
  50                     c = option[index + 1];
  51 
  52                     switch (c) {
  53                         case '\\': {
  54                             index++;
  55                             Name += '\\';
  56                             break;
  57                         }
  58 
  59                         case '=': {
  60                             index++;
  61                             Name += '=';
  62                             break;
  63                         }
  64                     }
  65 
  66                 }
  67 
  68                 continue;
  69             }
  70 
  71             default: {
  72                 Name += c;
  73                 continue;
  74             }
  75         }
  76 
  77         break;
  78     }
  79 
  80     if (hasValue) {
  81         Value = option.substr(index, index - option.length());
  82     }
  83 
  84     return (option.length() > 0);
  85 }
  86 
  87 
  88 TString Helpers::ReplaceString(TString subject, const TString& search,
  89                             const TString& replace) {
  90     size_t pos = 0;
  91     while((pos = subject.find(search, pos)) != TString::npos) {
  92             subject.replace(pos, search.length(), replace);
  93             pos += replace.length();
  94     }
  95     return subject;
  96 }
  97 
  98 TString Helpers::ConvertIdToFilePath(TString Value) {
  99     TString search;
 100     search = '.';
 101     TString replace;
 102     replace = '/';
 103     TString result = ReplaceString(Value, search, replace);
 104     return result;
 105 }
 106 
 107 TString Helpers::ConvertIdToJavaPath(TString Value) {
 108     TString search;
 109     search = '.';
 110     TString replace;
 111     replace = '/';
 112     TString result = ReplaceString(Value, search, replace);
 113     search = '\\';
 114     result = ReplaceString(result, search, replace);
 115     return result;
 116 }
 117 
 118 TString Helpers::ConvertJavaPathToId(TString Value) {
 119     TString search;
 120     search = '/';
 121     TString replace;
 122     replace = '.';
 123     TString result = ReplaceString(Value, search, replace);
 124     return result;
 125 }
 126 
 127 OrderedMap<TString, TString>
 128         Helpers::GetJavaOptionsFromConfig(IPropertyContainer* config) {
 129     OrderedMap<TString, TString> result;
 130 
 131     for (unsigned int index = 0; index < config->GetCount(); index++) {
 132         TString argname =
 133                 TString(_T("jvmarg.")) + PlatformString(index + 1).toString();
 134         TString argvalue;
 135 
 136         if (config->GetValue(argname, argvalue) == false) {
 137             break;
 138         }
 139         else if (argvalue.empty() == false) {
 140             TString name;
 141             TString value;
 142             if (Helpers::SplitOptionIntoNameValue(argvalue, name, value)) {
 143                 result.Append(name, value);
 144             }
 145         }
 146     }
 147 
 148     return result;
 149 }
 150 
 151 std::list<TString> Helpers::GetArgsFromConfig(IPropertyContainer* config) {
 152     std::list<TString> result;
 153 
 154     for (unsigned int index = 0; index < config->GetCount(); index++) {
 155         TString argname = TString(_T("arg."))
 156                 + PlatformString(index + 1).toString();
 157         TString argvalue;
 158 
 159         if (config->GetValue(argname, argvalue) == false) {
 160             break;
 161         }
 162         else if (argvalue.empty() == false) {
 163             result.push_back((argvalue));
 164         }
 165     }
 166 
 167     return result;
 168 }
 169 
 170 std::list<TString>
 171         Helpers::MapToNameValueList(OrderedMap<TString, TString> Map) {
 172     std::list<TString> result;
 173     std::vector<TString> keys = Map.GetKeys();
 174 
 175     for (OrderedMap<TString, TString>::const_iterator iterator = Map.begin();
 176             iterator != Map.end(); iterator++) {
 177        JPPair<TString, TString> *item = *iterator;
 178        TString key = item->first;
 179        TString value = item->second;
 180 
 181        if (value.length() == 0) {
 182            result.push_back(key);
 183        } else {
 184            result.push_back(key + _T('=') + value);
 185         }
 186     }
 187 
 188     return result;
 189 }
 190 
 191 TString Helpers::NameValueToString(TString name, TString value) {
 192     TString result;
 193 
 194     if (value.empty() == true) {
 195         result = name;
 196     }
 197     else {
 198         result = name + TString(_T("=")) + value;
 199     }
 200 
 201     return result;
 202 }
 203 
 204 std::list<TString> Helpers::StringToArray(TString Value) {
 205     std::list<TString> result;
 206     TString line;
 207 
 208     for (unsigned int index = 0; index < Value.length(); index++) {
 209         TCHAR c = Value[index];
 210 
 211         switch (c) {
 212             case '\n': {
 213                 result.push_back(line);
 214                 line = _T("");
 215                 break;
 216             }
 217 
 218             case '\r': {
 219                 result.push_back(line);
 220                 line = _T("");
 221 
 222                 if (Value[index + 1] == '\n')
 223                     index++;
 224 
 225                 break;
 226             }
 227 
 228             default: {
 229                 line += c;
 230             }
 231         }
 232     }
 233 
 234     // The buffer may not have ended with a Carriage Return/Line Feed.
 235     if (line.length() > 0) {
 236         result.push_back(line);
 237     }
 238 
 239     return result;
 240 }