Home | History | Annotate | Download | only in src
      1 /*---------------------------------------------------------------------------*
      2  *  CommandLine.c  *
      3  *                                                                           *
      4  *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
      5  *                                                                           *
      6  *  Licensed under the Apache License, Version 2.0 (the 'License');          *
      7  *  you may not use this file except in compliance with the License.         *
      8  *                                                                           *
      9  *  You may obtain a copy of the License at                                  *
     10  *      http://www.apache.org/licenses/LICENSE-2.0                           *
     11  *                                                                           *
     12  *  Unless required by applicable law or agreed to in writing, software      *
     13  *  distributed under the License is distributed on an 'AS IS' BASIS,        *
     14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
     15  *  See the License for the specific language governing permissions and      *
     16  *  limitations under the License.                                           *
     17  *                                                                           *
     18  *---------------------------------------------------------------------------*/
     19 
     20 #include "ESR_CommandLine.h"
     21 
     22 ESR_ReturnCode ESR_CommandLineGetValue(int argc, const LCHAR * argv[],
     23                                        LCHAR* key, LCHAR* value, size_t* len)
     24 {
     25   const LCHAR* lastKeyFound = NULL;
     26 
     27   while (--argc > 0 && **++argv)
     28   {
     29     if (**argv != '-')
     30     {
     31       /* got value */
     32       if (key == NULL)
     33       {
     34         /* but we don't have any key to associate it with */
     35         continue;
     36       }
     37       else
     38       {
     39         if (lastKeyFound != NULL && LSTRCMP(lastKeyFound, key) == 0)
     40         {
     41           if (LSTRLEN(*argv) + 1 > *len)
     42           {
     43             *len = LSTRLEN(*argv) + 1;
     44             return ESR_BUFFER_OVERFLOW;
     45           }
     46           *len = LSTRLEN(*argv) + 1;
     47           LSTRCPY(value, *argv);
     48           return ESR_SUCCESS;
     49         }
     50       }
     51     }
     52     else
     53     {
     54       /* got key */
     55       if (lastKeyFound != NULL && LSTRCMP(lastKeyFound, key) == 0)
     56       {
     57         *len = 1;
     58         LSTRCPY(value, L(""));
     59         return ESR_SUCCESS;
     60       }
     61       lastKeyFound = *argv + 1;
     62     }
     63   }
     64 
     65   /* Handle case where last argument is a key with no value */
     66   if (lastKeyFound != NULL && LSTRCMP(lastKeyFound, key) == 0)
     67   {
     68     *len = 1;
     69     LSTRCPY(value, L(""));
     70     return ESR_SUCCESS;
     71   }
     72   return ESR_NO_MATCH_ERROR;
     73 }
     74