logsui/tsrc/logdbmodifier/addeventtest/src/argstest.cpp
changeset 24 0ba2181d7c28
equal deleted inserted replaced
0:e686773b3f54 24:0ba2181d7c28
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Argument parser
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "argstest.h"
       
    22 
       
    23 // EXTERNAL DATA STRUCTURES
       
    24 // none
       
    25 
       
    26 // EXTERNAL FUNCTION PROTOTYPES  
       
    27 // none
       
    28 
       
    29 // CONSTANTS
       
    30 // none
       
    31 
       
    32 // MACROS
       
    33 // none
       
    34 
       
    35 // LOCAL CONSTANTS AND MACROS
       
    36 // none
       
    37 
       
    38 // MODULE DATA STRUCTURES
       
    39 // none
       
    40 
       
    41 // LOCAL FUNCTION PROTOTYPES
       
    42 // none
       
    43 
       
    44 // FORWARD DECLARATIONS
       
    45 // none
       
    46 
       
    47 // ============================= LOCAL FUNCTIONS ===============================
       
    48 
       
    49 // none
       
    50 
       
    51 // ============================ MEMBER FUNCTIONS ===============================
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 //  CArgs::CArgs
       
    55 //  C++ default constructor can NOT contain any code, that
       
    56 //  might leave.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 EXPORT_C CArgs::CArgs() 
       
    60     {
       
    61     }
       
    62 
       
    63 // Destructor
       
    64 EXPORT_C CArgs::~CArgs()
       
    65     {
       
    66 
       
    67     ClearArgs();
       
    68 
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 //  CArgs::ArgCount
       
    73 //  Returns count of parsed arguments
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 EXPORT_C TInt CArgs::ArgCount() const
       
    77     {
       
    78 
       
    79     return iArgCount;
       
    80 
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 //  CArgs::ArgType
       
    85 //  Returns type of argument (string/numeric)
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 EXPORT_C TArgType CArgs::ArgType( const TInt aArgIndex ) const
       
    89     {
       
    90 
       
    91     if ( aArgIndex < 1 || aArgIndex > iArgCount )
       
    92         {
       
    93         return EArgNotSpecified;
       
    94         }
       
    95     else
       
    96         {
       
    97         return iArgType[aArgIndex - 1];
       
    98         }
       
    99 
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 //  CArgs::NumArg
       
   104 //  Returns argument value as TInt
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 EXPORT_C TInt CArgs::NumArg( const TInt aArgIndex ) const
       
   108     {
       
   109 
       
   110     if ( aArgIndex < 1 ||
       
   111          aArgIndex > iArgCount ||
       
   112          iArgType[aArgIndex - 1] != EArgNum )
       
   113         {
       
   114         return 0;
       
   115         }
       
   116     else
       
   117         {
       
   118         return iNumArg[aArgIndex - 1];
       
   119         }
       
   120 
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 //  CArgs::StrArg
       
   125 //  Returns argument value as string
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C const TDesC& CArgs::StrArg( const TInt aArgIndex ) const
       
   129     {
       
   130 
       
   131     if ( aArgIndex < 1 || aArgIndex > iArgCount )
       
   132         {
       
   133         return KNullDesC;
       
   134         }
       
   135     else
       
   136         {
       
   137         return *iStrArg[aArgIndex - 1];
       
   138         }
       
   139 
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 //  CArgs::ParseLineL
       
   144 //  Parses arguments from a string
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 EXPORT_C TInt CArgs::ParseLineL(
       
   148     const TParseType aType,
       
   149     const TDes& aLine )
       
   150     {
       
   151 
       
   152     TBuf<1024> line;
       
   153     line.Append( aLine );
       
   154 
       
   155     ClearArgs();
       
   156     
       
   157     switch ( aType )
       
   158         {
       
   159         case EParseComment:
       
   160 
       
   161             line.Trim();
       
   162 
       
   163             // if line starts with "//"
       
   164             //
       
   165             if ( line.Length() >= 2 &&
       
   166                  line[0] == static_cast< TInt >( TChar( '/' ) ) &&
       
   167                  line[1] == static_cast< TInt >( TChar( '/' ) ) )
       
   168                 {
       
   169                 return KErrNone;
       
   170                 }
       
   171             break;
       
   172 
       
   173         case EParseFunction:
       
   174 
       
   175             TInt posDot = line.Locate( '.' );
       
   176             posDot = -1; // Messes stuff in case of ip addresses
       
   177             TInt posBraceOpen = line.Locate( '(' );
       
   178 
       
   179             if ( posBraceOpen == -1 )
       
   180                 {
       
   181                 return KErrArgument;
       
   182                 }
       
   183 
       
   184             iArgType[0] = EArgStr;
       
   185             iArgType[1] = EArgStr;
       
   186 
       
   187             // read classname and methodname: "class.method("
       
   188             //
       
   189             if ( posDot != -1 && posDot < posBraceOpen )
       
   190                 {
       
   191                 iStrArg[0] = HBufC::NewL( posDot );
       
   192                 iStrArg[0]->Des().Append( line.Ptr(), posDot );
       
   193                 iStrArg[0]->Des().Trim();
       
   194                 iStrArg[1] = HBufC::NewL( posBraceOpen - posDot );
       
   195                 iStrArg[1]->Des().Append( line.Ptr() + posDot + 1,
       
   196                                           posBraceOpen - ( posDot + 1 ) );
       
   197                 iStrArg[1]->Des().Trim();
       
   198                 }
       
   199             else // only methodname: "method("
       
   200                 {
       
   201                 iStrArg[0] = HBufC::NewL( 0 );
       
   202                 iStrArg[1] = HBufC::NewL( posBraceOpen );
       
   203                 iStrArg[1]->Des().Append( line.Ptr(), posBraceOpen );
       
   204                 iStrArg[1]->Des().Trim();
       
   205                 }
       
   206 
       
   207             iArgCount = 2;
       
   208 
       
   209             // remove "class.method(" or "method(" from line
       
   210             //
       
   211             line.Delete( 0, posBraceOpen + 1 );
       
   212             line.Trim();
       
   213 
       
   214             // parse arguments
       
   215             //
       
   216             while ( line.Length() > 0 )
       
   217                 {
       
   218                 line.Trim();
       
   219                 TInt pos = line.Locate( '"' );
       
   220 
       
   221                 // string
       
   222                 //
       
   223                 if ( pos == 0 )
       
   224                     {
       
   225                     line.Delete( 0, 1 );
       
   226                     pos = line.Locate( '"' );
       
   227                     if ( pos == -1 )
       
   228                         {
       
   229                         return KErrArgument;
       
   230                         }
       
   231 
       
   232                     iArgType[iArgCount] = EArgStr;
       
   233                     iStrArg[iArgCount] = HBufC::NewL( pos );
       
   234                     iStrArg[iArgCount]->Des().Append( line.Ptr(), pos );
       
   235                     iArgCount++;
       
   236                     line.Delete( 0, pos + 1 );
       
   237                     line.Trim();
       
   238 
       
   239                     // remove possible comma
       
   240                     //
       
   241                     if ( line.Locate( ',' ) == 0 )
       
   242                         {
       
   243                         line.Delete( 0, 1 );
       
   244                         line.Trim();
       
   245                         }
       
   246                     }
       
   247 
       
   248                 else // numeric? or enum value
       
   249                     {
       
   250                     pos = line.Locate( ',' );
       
   251                     if ( pos == -1 )
       
   252                         {
       
   253                         pos = line.Locate( ')' );
       
   254                         if ( pos == -1 )
       
   255                             {
       
   256                             return KErrArgument;
       
   257                             }
       
   258                         else if ( pos == 0 )
       
   259                             {
       
   260                             break;
       
   261                             }
       
   262                         }
       
   263 
       
   264                     iArgType[iArgCount] = EArgStr;
       
   265                     iStrArg[iArgCount] = HBufC::NewL( pos );
       
   266                     iStrArg[iArgCount]->Des().Append( line.Ptr(), pos );
       
   267                     iStrArg[iArgCount]->Des().Trim();
       
   268                     iArgCount++;
       
   269                     line.Delete( 0, pos + 1 );
       
   270                     line.Trim();
       
   271 
       
   272                     // len must be > 0
       
   273                     //
       
   274                     if ( iStrArg[iArgCount-1]->Length() == 0 )
       
   275                         {
       
   276                         return KErrArgument;
       
   277                         }
       
   278 
       
   279                     TBool isNum = ETrue;
       
   280                     TBool isNegative = EFalse;
       
   281                     TInt numVal = 0;
       
   282 
       
   283                     // check if argument is numeric -> [-]n(0-9)
       
   284                     //
       
   285                     for ( pos=0; pos < iStrArg[iArgCount - 1]->Length(); pos++ )
       
   286                         {
       
   287                         if ( pos == 0 &&
       
   288                              iStrArg[iArgCount - 1]->Locate( '-' ) == 0 )
       
   289                             {
       
   290                             isNegative = ETrue;
       
   291                             // if number is negative, len must be >= 2
       
   292                             //
       
   293                             if ( iStrArg[iArgCount - 1]->Length() < 2 )
       
   294                                 {
       
   295                                 isNum = EFalse;
       
   296                                 break;
       
   297                                 }
       
   298                             }
       
   299                         else
       
   300                             {
       
   301                             TInt number;
       
   302 
       
   303                             if      ( (*iStrArg[iArgCount - 1])[pos] ==
       
   304                                static_cast< TInt >( TChar( '0' ) ) ) number = 0;
       
   305                             else if ( (*iStrArg[iArgCount - 1])[pos] ==
       
   306                                static_cast< TInt >( TChar( '1' ) ) ) number = 1;
       
   307                             else if ( (*iStrArg[iArgCount - 1])[pos] ==
       
   308                                static_cast< TInt >( TChar( '2' ) ) ) number = 2;
       
   309                             else if ( (*iStrArg[iArgCount - 1])[pos] ==
       
   310                                static_cast< TInt >( TChar( '3' ) ) ) number = 3;
       
   311                             else if ( (*iStrArg[iArgCount - 1])[pos] ==
       
   312                                static_cast< TInt >( TChar( '4' ) ) ) number = 4;
       
   313                             else if ( (*iStrArg[iArgCount - 1])[pos] ==
       
   314                                static_cast< TInt >( TChar( '5' ) ) ) number = 5;
       
   315                             else if ( (*iStrArg[iArgCount - 1])[pos] ==
       
   316                                static_cast< TInt >( TChar( '6' ) ) ) number = 6;
       
   317                             else if ( (*iStrArg[iArgCount - 1])[pos] ==
       
   318                                static_cast< TInt >( TChar( '7' ) ) ) number = 7;
       
   319                             else if ( (*iStrArg[iArgCount - 1])[pos] ==
       
   320                                static_cast< TInt >( TChar( '8' ) ) ) number = 8;
       
   321                             else if ( (*iStrArg[iArgCount - 1])[pos] ==
       
   322                                static_cast< TInt >( TChar( '9' ) ) ) number = 9;
       
   323                             else
       
   324                                 {
       
   325                                 isNum = EFalse;
       
   326                                 break;
       
   327                                 }
       
   328                             numVal = (numVal * 10) + number;
       
   329                             }
       
   330                         }
       
   331                 
       
   332                     // argument is numeric
       
   333                     //
       
   334                     if ( isNum )
       
   335                         {
       
   336                         if ( isNegative )
       
   337                             {
       
   338                             numVal = -numVal;
       
   339                             }
       
   340 
       
   341                         iNumArg[iArgCount - 1] = numVal;
       
   342                         iArgType[iArgCount - 1] = EArgNum;
       
   343                         } 
       
   344 
       
   345                     } // else
       
   346 
       
   347                 } // while
       
   348 
       
   349             return KErrNone;
       
   350 
       
   351         } // switch
       
   352 
       
   353     return KErrArgument;
       
   354 
       
   355     }
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 //  CArgs::ClearArgs
       
   359 //  Clears all parsed arguments and frees allocated memory
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 void CArgs::ClearArgs()
       
   363     {
       
   364 
       
   365     for ( TInt i = 0; i < iArgCount; i++ )
       
   366         {
       
   367         delete iStrArg[i];
       
   368         }
       
   369 
       
   370     iArgCount = 0;
       
   371 
       
   372     }
       
   373 
       
   374 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   375 
       
   376 #if defined(__WINS__) && !defined(EKA2)
       
   377 
       
   378 // -----------------------------------------------------------------------------
       
   379 // E32Dll 
       
   380 // 
       
   381 // DLL entry point function. In a MARM implementation, the 
       
   382 // entry point is called when a thread is attached to or detached from the 
       
   383 // DLL.?description
       
   384 //
       
   385 // Returns: TInt:   For DLLs which do not set up thread-local storage, 
       
   386 //                  the function can ignore the TDllReason type 
       
   387 //                  parameter passed to it and simply return KErrNone;
       
   388 // -----------------------------------------------------------------------------
       
   389 //
       
   390 
       
   391 GLDEF_C TInt E32Dll( TDllReason /*aReason*/ )
       
   392     {
       
   393 
       
   394     return KErrNone;
       
   395     
       
   396     }
       
   397     
       
   398 #endif
       
   399 
       
   400 //  End of File