javaruntimes/starterutils/src/ueiargsparser.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Converts Universal Emulator Interface (UEI) arguments to
       
    15 *              JVM arguments
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <sstream>
       
    21 
       
    22 #include "ueiargsparser.h"
       
    23 
       
    24 #include "logger.h"
       
    25 
       
    26 const wchar_t* const SUPPORTED_UEI_ARGS[] = {L"-Xverbose", L"-Xrunjdwp", 0};
       
    27 const wchar_t* const SUPPORTED_JVM_ARGS[] = {L"-verbose", L"-Xrunjdwp", 0};
       
    28 const int INDEX_VERBOSE = 0;
       
    29 const int INDEX_RUNJDWP = 1;
       
    30 
       
    31 const wchar_t* const ALL = L"all";
       
    32 const wchar_t* const GC = L"gc";
       
    33 const wchar_t* const CLASS = L"class";
       
    34 const wchar_t* const JVM_SUPPORTED_VERBOSE_OPTIONS = L"gc,class,stack,sizes";
       
    35 
       
    36 const wchar_t* const OPTION_SEPARATOR = L":";
       
    37 const wchar_t* const SPACE = L" ";
       
    38 const wchar_t* const COMMA = L",";
       
    39 
       
    40 using namespace java::runtime;
       
    41 
       
    42 std::wstring UeiArgsParser::convertUeiArgsToJvmArgs(const std::wstring& aUeiParameters)
       
    43 {
       
    44     std::vector<std::wstring> args = getArgs(aUeiParameters);
       
    45 
       
    46     std::wostringstream jvmArgs;
       
    47     for (int i = 0; i < args.size(); ++i)
       
    48     {
       
    49         LOG2(EDebugApi, EInfo,"args(%S): %d", args.at(i).c_str(), i);
       
    50         if (isVerbose(args.at(i)))
       
    51         {
       
    52             jvmArgs << convertVerbose(args.at(i)) << SPACE;
       
    53         }
       
    54         else if (isRundjwp(args.at(i)))
       
    55         {
       
    56             jvmArgs << convertRundjwp(args.at(i)) << SPACE;
       
    57         }
       
    58     }
       
    59 
       
    60     std::wstring result = jvmArgs.str();
       
    61     trimTrailingSpaces(result, SPACE);
       
    62 
       
    63     LOG2(EDebugApi, EInfo,"convertUeiArgsToJvmArgs(%S): %S", aUeiParameters.c_str(), result.c_str());
       
    64     return result;
       
    65 }
       
    66 
       
    67 std::vector<std::wstring> UeiArgsParser::getArgs(const std::wstring& aUeiParameters)
       
    68 {
       
    69     std::vector<std::wstring> result;
       
    70     std::wstringstream stream;
       
    71     stream << aUeiParameters; // codescanner::leave
       
    72     std::wstring arg;
       
    73     while (stream.good())
       
    74     {
       
    75         stream >> arg; // codescanner::leave
       
    76         if (arg.size() != 0)
       
    77         {
       
    78             result.push_back(arg);
       
    79         }
       
    80     }
       
    81 
       
    82     LOG2(EDebugApi, EInfo,"getArgs(%S): count=%d", aUeiParameters.c_str(), result.size());
       
    83     return result;
       
    84 }
       
    85 
       
    86 bool UeiArgsParser::isVerbose(const std::wstring& aUeiArg)
       
    87 {
       
    88     std::wstring verbose(SUPPORTED_UEI_ARGS[INDEX_VERBOSE]);
       
    89     if (aUeiArg.compare(0, verbose.length(), verbose) == 0)
       
    90     {
       
    91         return true;
       
    92     }
       
    93     return false;
       
    94 }
       
    95 
       
    96 bool UeiArgsParser::isRundjwp(const std::wstring& aUeiArg)
       
    97 {
       
    98     std::wstring rundjwp(SUPPORTED_UEI_ARGS[INDEX_RUNJDWP]);
       
    99     if (aUeiArg.compare(0, rundjwp.length(), rundjwp) == 0)
       
   100     {
       
   101         return true;
       
   102     }
       
   103     return false;
       
   104 }
       
   105 
       
   106 // hasOption() assumes that options are separated by ','
       
   107 bool UeiArgsParser::hasOption(const std::wstring& aArg, const std::wstring& aOption)
       
   108 {
       
   109     // option is in the middle: option,zzz
       
   110     size_t found = aArg.find(aOption + COMMA);
       
   111     if (found != std::wstring::npos)
       
   112     {
       
   113         return true;
       
   114     }
       
   115 
       
   116     // option is the last one: zzz,option
       
   117     found = aArg.find(aOption);
       
   118     if (found != std::wstring::npos)
       
   119     {
       
   120         // avoid false alarm: zzz,optionz
       
   121         int expectedSize = found + aOption.size();
       
   122         if (expectedSize == aArg.size())
       
   123         {
       
   124             return true;
       
   125         }
       
   126     }
       
   127 
       
   128     return false;
       
   129 }
       
   130 
       
   131 std::wstring UeiArgsParser::convertVerbose(const std::wstring& aVerbose)
       
   132 {
       
   133     std::wostringstream arg;
       
   134     arg << SUPPORTED_JVM_ARGS[INDEX_VERBOSE];
       
   135 
       
   136     if (aVerbose.find(OPTION_SEPARATOR) != std::wstring::npos)
       
   137     {
       
   138         arg << OPTION_SEPARATOR;
       
   139         if (hasOption(aVerbose, ALL))
       
   140         {
       
   141             arg << JVM_SUPPORTED_VERBOSE_OPTIONS;
       
   142         }
       
   143         else
       
   144         {
       
   145             if (hasOption(aVerbose, GC))
       
   146             {
       
   147                 arg << GC << COMMA;
       
   148             }
       
   149             if (hasOption(aVerbose, CLASS))
       
   150             {
       
   151                 arg << CLASS << COMMA;
       
   152             }
       
   153         }
       
   154     }
       
   155 
       
   156     std::wstring result = arg.str();
       
   157     std::wostringstream whitespaces;
       
   158     whitespaces << OPTION_SEPARATOR << COMMA;
       
   159     trimTrailingSpaces(result, whitespaces.str());
       
   160 
       
   161     LOG2(EDebugApi, EInfo,"convertVerbose(%S): %S", aVerbose.c_str(), result.c_str());
       
   162     return result;
       
   163 }
       
   164 
       
   165 std::wstring UeiArgsParser::convertRundjwp(const std::wstring& aRunjdwp)
       
   166 {
       
   167     return aRunjdwp;
       
   168 }
       
   169 
       
   170 std::wstring UeiArgsParser::trimTrailingSpaces(std::wstring& aString, const std::wstring& aWhitespaces)
       
   171 {
       
   172     size_t trailingSpace = aString.find_last_not_of(aWhitespaces);
       
   173     if (trailingSpace != std::wstring::npos)
       
   174     {
       
   175         aString.erase(trailingSpace + 1);
       
   176     }
       
   177     return aString;
       
   178 }
       
   179