Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 #ifndef OSCL_STRING_UTILS_H_INCLUDED
     19 #include "oscl_string_utils.h"
     20 #endif
     21 
     22 #include "oscl_stdstring.h"
     23 
     24 /* ======================================================================== */
     25 /*  Function : PV_atoi(char *buf,char new_format)                           */
     26 /*  Date     : 02/22/2001                                                   */
     27 /*  Purpose  : Extracts an integer from the input string.                   */
     28 /*  In/out   :                                                              */
     29 /*  Return   :                                                              */
     30 /*  Modified :                                                              */
     31 /* ======================================================================== */
     32 OSCL_EXPORT_REF bool PV_atoi(const char *buf, const char new_format, uint32& value)
     33 {
     34     return PV_atoi(buf, new_format, oscl_strlen(buf), value);
     35 }
     36 
     37 
     38 OSCL_EXPORT_REF bool PV_atoi(const char *buf, const char new_format, int length, uint32& value)
     39 {
     40     uint32 old, temp;
     41     const char *ptr = buf;
     42     value = 0;
     43 
     44     if (buf == NULL) return false;
     45 
     46     switch (new_format)
     47     {
     48         case 'x':
     49         {
     50             while (ptr - buf < length)
     51             {
     52                 if ((*ptr == 'a') || (*ptr == 'A'))
     53                     temp = 10;
     54                 else if ((*ptr == 'b') || (*ptr == 'B'))
     55                     temp = 11;
     56                 else if ((*ptr == 'c') || (*ptr == 'C'))
     57                     temp = 12;
     58                 else if ((*ptr == 'd') || (*ptr == 'D'))
     59                     temp = 13;
     60                 else if ((*ptr == 'e') || (*ptr == 'E'))
     61                     temp = 14;
     62                 else if ((*ptr == 'f') || (*ptr == 'F'))
     63                     temp = 15;
     64                 else if ((*ptr >= 48) && (*ptr <= 57))
     65                     temp = (int)(*ptr - 48);
     66                 else return false;
     67                 ++ptr;
     68                 old = value;
     69                 value = value * 16 + temp;
     70                 if (old > value)
     71                 {
     72                     // overflow
     73                     return false;
     74                 }
     75             }
     76         }
     77         break;
     78         case 'd':
     79         {
     80             while (ptr - buf < length)
     81             {
     82                 if ((*ptr >= 48) && (*ptr <= 57))
     83                 {
     84                     temp = (int)(*ptr - 48);
     85                     ++ptr;
     86                     old = value;
     87                     value = value * 10 + temp;
     88                     if (old > value)
     89                     {
     90                         // overflow
     91                         return false;
     92                     }
     93                 }
     94                 else
     95                 {
     96                     return false;
     97                 }
     98 
     99             }
    100         }
    101         break;
    102         default :
    103         {
    104             while (ptr - buf < length)
    105             {
    106                 if ((*ptr >= 48) && (*ptr <= 57))
    107                 {
    108                     temp = (int)(*ptr - 48);
    109                     ++ptr;
    110                     old = value;
    111                     value = value * 10 + temp;
    112                     if (old > value)
    113                     {
    114                         // overflow
    115                         return false;
    116                     }
    117                 }
    118                 else
    119                 {
    120                     return false;
    121                 }
    122             }
    123         }
    124         break;
    125     }
    126 
    127     return true;
    128 }
    129 
    130 OSCL_EXPORT_REF bool PV_atoi(const char *buf, const char new_format, int length, uint64& value)
    131 {
    132     uint64 old, temp;
    133     const char *ptr = buf;
    134     value = 0;
    135 
    136     if (buf == NULL) return false;
    137 
    138     switch (new_format)
    139     {
    140         case 'x':
    141         {
    142             while (ptr - buf < length)
    143             {
    144                 if ((*ptr == 'a') || (*ptr == 'A'))
    145                     temp = 10;
    146                 else if ((*ptr == 'b') || (*ptr == 'B'))
    147                     temp = 11;
    148                 else if ((*ptr == 'c') || (*ptr == 'C'))
    149                     temp = 12;
    150                 else if ((*ptr == 'd') || (*ptr == 'D'))
    151                     temp = 13;
    152                 else if ((*ptr == 'e') || (*ptr == 'E'))
    153                     temp = 14;
    154                 else if ((*ptr == 'f') || (*ptr == 'F'))
    155                     temp = 15;
    156                 else if ((*ptr >= 48) && (*ptr <= 57))
    157                     temp = (int)(*ptr - 48);
    158                 else return false;
    159                 ++ptr;
    160                 old = value;
    161                 value = value * (uint64)16 + temp;
    162                 if (old > value)
    163                 {
    164                     // overflow
    165                     return false;
    166                 }
    167             }
    168         }
    169         break;
    170         case 'd':
    171         {
    172             while (ptr - buf < length)
    173             {
    174                 if ((*ptr >= 48) && (*ptr <= 57))
    175                 {
    176                     temp = (int)(*ptr - 48);
    177                     ++ptr;
    178                     old = value;
    179                     value = value * (uint64)10 + temp;
    180                     if (old > value)
    181                     {
    182                         // overflow
    183                         return false;
    184                     }
    185                 }
    186                 else
    187                 {
    188                     return false;
    189                 }
    190 
    191             }
    192         }
    193         break;
    194         default :
    195         {
    196             while (ptr - buf < length)
    197             {
    198                 if ((*ptr >= 48) && (*ptr <= 57))
    199                 {
    200                     temp = (int)(*ptr - 48);
    201                     ++ptr;
    202                     old = value;
    203                     value = value * (uint64)10 + temp;
    204                     if (old > value)
    205                     {
    206                         // overflow
    207                         return false;
    208                     }
    209                 }
    210                 else
    211                 {
    212                     return false;
    213                 }
    214             }
    215         }
    216         break;
    217     }
    218 
    219     return true;
    220 }
    221 
    222 
    223 
    224 
    225 /* ======================================================================== */
    226 /*  Function : skip_whitespace                                          */
    227 /*  Date     : 1/1/2000                                 */
    228 /*  Purpose  : Skips over any leading whitespace (i.e., a space or      */
    229 /*                 horizontal tab character) in the input string and        */
    230 /*                 returns the pointer to the first non-whitespace          */
    231 /*                 character.                                               */
    232 /*  In/out   :                                  */
    233 /*  Return   : pointer to first non-whitespace character                */
    234 /*  Modified :                              */
    235 /* ======================================================================== */
    236 
    237 OSCL_EXPORT_REF const char* skip_whitespace(const char *ptr)
    238 {
    239     while (ptr && *ptr)
    240     {
    241         if (*ptr != ' ' && *ptr != '\t')
    242         {
    243             break;
    244         }
    245 
    246         ++ptr;
    247     }
    248 
    249     return ptr;
    250 }
    251 
    252 OSCL_EXPORT_REF char * skip_whitespace(char * ptr)
    253 {
    254     while (ptr && *ptr)
    255     {
    256         if (*ptr != ' ' && *ptr != '\t')
    257         {
    258             break;
    259         }
    260 
    261         ++ptr;
    262     }
    263 
    264     return ptr;
    265 }
    266 
    267 
    268 
    269 /* ======================================================================== */
    270 /*  Function : skip_whitespace                                          */
    271 /*  Date     : 1/1/2000                                 */
    272 /*  Purpose  : Skips over any leading whitespace (i.e., a space or      */
    273 /*                 horizontal tab character) in the input string and        */
    274 /*                 returns the pointer to the first non-whitespace          */
    275 /*                 character.  The input string is represented by           */
    276 /*                 starting and ending pointers and does not need to be     */
    277 /*                 NULL terminated.                                         */
    278 /*  In/out   :                                  */
    279 /*  Return   : pointer to first non-whitespace character                */
    280 /*  Modified :                              */
    281 /* ======================================================================== */
    282 
    283 OSCL_EXPORT_REF const char* skip_whitespace(const char *start, const char *end)
    284 {
    285     while (start && (start < end))
    286     {
    287         if (*start != ' ' && *start != '\t')
    288         {
    289             break;
    290         }
    291 
    292         ++start;
    293     }
    294 
    295     return start;
    296 }
    297 
    298 
    299 /* ======================================================================== */
    300 /*  Function : skip_to_whitespace                                       */
    301 /*  Date     : 1/1/2001                                 */
    302 /*  Purpose  : Skips to the first whitespace character (i.e., a space   */
    303 /*                 or horizontal tab character or line terminator) in the   */
    304 /*                 input string and returns the pointer to the first        */
    305 /*                 non-whitespace character.                                */
    306 /*                 The input string is represented by                       */
    307 /*                 starting and ending pointers and does not need to be     */
    308 /*                 NULL terminated.                                         */
    309 /*  In/out   :                                  */
    310 /*  Return   : pointer to first non-whitespace character                */
    311 /*  Modified :                              */
    312 /* ======================================================================== */
    313 
    314 OSCL_EXPORT_REF const char* skip_to_whitespace(const char *start, const char *end)
    315 {
    316     while (start && (start < end))
    317     {
    318         if (*start == ' ' || *start == '\t' ||
    319                 *start == '\r' || *start == '\n')
    320         {
    321             break;
    322         }
    323 
    324         ++start;
    325     }
    326 
    327     return start;
    328 }
    329 
    330 /* ======================================================================== */
    331 /*  Function : skip_to_line_term                                        */
    332 /*  Date     : 9/15/2001                                    */
    333 /*  Purpose  : Skips over any characters to the next line terminator    */
    334 /*                 (i.e., \r and \n) and                                    */
    335 /*                 returns the pointer to the line term character.          */
    336 /*                 The input string is represented by                       */
    337 /*                 starting and ending pointers and does not need to be     */
    338 /*                 NULL terminated.                                         */
    339 /*  In/out   :                                  */
    340 /*  Return   : pointer to line terminator character                     */
    341 /*  Modified :                              */
    342 /* ======================================================================== */
    343 
    344 OSCL_EXPORT_REF const char * skip_to_line_term(const char *start, const char *end)
    345 {
    346     while (start && (start < end))
    347     {
    348         if (*start == '\r' || *start == '\n')
    349         {
    350             return start;
    351         }
    352         ++start;
    353     }
    354 
    355     return start;
    356 }
    357 
    358 
    359 /* ======================================================================== */
    360 /*  Function : skip_whitespace_and_line_term                            */
    361 /*  Date     : 9/15/2001                                    */
    362 /*  Purpose  : Skips over any leading whitespace (i.e., a space or      */
    363 /*                 horizontal tab character) or line terminator (i.e., \r   */
    364 /*                 and \n) and                                              */
    365 /*                 returns the pointer to the first non-whitespace          */
    366 /*                 character.  The input string is represented by           */
    367 /*                 starting and ending pointers and does not need to be     */
    368 /*                 NULL terminated.                                         */
    369 /*  In/out   :                                  */
    370 /*  Return   : pointer to first non-whitespace character                */
    371 /*  Modified :                              */
    372 /* ======================================================================== */
    373 
    374 OSCL_EXPORT_REF const char* skip_whitespace_and_line_term(const char *start, const char *end)
    375 {
    376     while (start && (start < end))
    377     {
    378         if (*start != ' ' && *start != '\t' &&
    379                 *start != '\r' && *start != '\n')
    380         {
    381             break;
    382         }
    383 
    384         ++start;
    385     }
    386 
    387     return start;
    388 }
    389 
    390 
    391 
    392 
    393 /* ======================================================================== */
    394 /*  Function : extract_string                                           */
    395 /*  Date     : 1/1/2000                                 */
    396 /*  Purpose  : Extracts string of a maximum size after skipping any     */
    397 /*                 leading whitespace.  The input string is represented by  */
    398 /*                 starting and ending pointers and does not need to be     */
    399 /*                 NULL terminated.                                         */
    400 /*  In/out   :                                  */
    401 /*  Return   : length of the extracted string                   */
    402 /*  Modified :                              */
    403 /* ======================================================================== */
    404 
    405 
    406 OSCL_EXPORT_REF int extract_string(const char * start, const char *end, char *outstring, int maxsize)
    407 {
    408     int len = 0;
    409 
    410     if (! outstring)
    411     {
    412         return 0;
    413     }
    414 
    415     start = skip_whitespace(start, end);
    416 
    417     for (; start && (start < end) ; ++start)
    418     {
    419 
    420         if (*start == ' ' || *start == '\t' || *start == '\n' || *start == '\r')
    421         {
    422             // whitespace so stop copying
    423             break;
    424         }
    425 
    426         if (len < maxsize)
    427         {
    428             *outstring++ = *start;
    429         }
    430         else if (len == maxsize)
    431         {
    432             // too long so just terminate the string
    433             *(outstring - 1) = '\0';
    434         }
    435         ++len;
    436 
    437     }
    438 
    439     if (len < maxsize)
    440     {
    441         // terminate the string
    442         *outstring = '\0';
    443     }
    444 
    445     return len;
    446 }
    447 
    448 /* ======================================================================== */
    449 /*  Function : extract_string                                           */
    450 /*  Date     : 1/1/2000                                 */
    451 /*  Purpose  : Extracts string of a maximum size after skipping any     */
    452 /*                 leading whitespace.                                      */
    453 /*  In/out   :                                  */
    454 /*  Return   : length of the extracted string                   */
    455 /*  Modified :                              */
    456 /* ======================================================================== */
    457 
    458 OSCL_EXPORT_REF int extract_string(const char * in_ptr, char *outstring, int maxsize)
    459 {
    460     int len = 0;
    461 
    462     if (! outstring)
    463     {
    464         return 0;
    465     }
    466 
    467     in_ptr = skip_whitespace(in_ptr, in_ptr + oscl_strlen(in_ptr));
    468 
    469     for (; in_ptr && *in_ptr ; ++in_ptr)
    470     {
    471 
    472         if (*in_ptr == ' ' || *in_ptr == '\t' || *in_ptr == '\n' || *in_ptr == '\r')
    473         {
    474             // whitespace so stop copying
    475             break;
    476         }
    477 
    478         if (len < maxsize)
    479         {
    480             *outstring++ = *in_ptr;
    481         }
    482         else if (len == maxsize)
    483         {
    484             // too long so just terminate the string
    485             *(outstring - 1) = '\0';
    486         }
    487         ++len;
    488 
    489     }
    490 
    491     if (len < maxsize)
    492     {
    493         // terminate the string
    494         *outstring = '\0';
    495     }
    496 
    497     return len;
    498 }
    499 
    500 /* ======================================================================== */
    501 /*  Function : PV_atof(char *buf, OsclFloat& value)                         */
    502 /*  Purpose  : Converts a character string to a float. The string           */
    503 /*             argument has the following form:                             */
    504 /*             [whitespace] [sign] [digits] [.digits]                       */
    505 /*  In/out   :                                                              */
    506 /*  Return   :                                                              */
    507 /*  Modified :                                                              */
    508 /* ======================================================================== */
    509 OSCL_EXPORT_REF bool PV_atof(const char *buf, OsclFloat& value)
    510 {
    511     return PV_atof(buf, oscl_strlen(buf), value);
    512 }
    513 
    514 OSCL_EXPORT_REF bool PV_atof(const char *buf, int length, OsclFloat& value)
    515 {
    516     int i, decimal_point_index = -1;
    517     OsclFloat old;
    518     bool minus_sign = false;
    519     bool digit_found = false;
    520     value = 0.0;
    521 
    522     for (i = 0; i < length; i++)
    523     {
    524         if (buf[i] == ' ' || buf[i] == '\t' || buf[i] == '\r' || buf[i] == '\n')
    525         {
    526             if (digit_found)
    527                 break; // stop the conversion if the numeric value is ended with whitespace, tab, CR or LF (ie, "1.23  ")
    528             else
    529                 continue; // skip leading whitespace or tab
    530         }
    531         else if (buf[i] == '-')
    532         {
    533             if (digit_found)
    534                 break; // stop the conversion if the numeric value is ended with a minus sign (ie, "1.23-")
    535             else
    536             {
    537                 minus_sign = true;
    538                 continue;
    539             }
    540         }
    541         else if (buf[i] < '0' || buf[i] > '9')
    542         {
    543             if (buf[i] == '.')
    544             {
    545                 if (decimal_point_index > -1)
    546                     break; // found another decimal point so stopping
    547                 else // continue the conversion if this is the first decimal point
    548                 {
    549                     decimal_point_index = i;
    550                     continue;
    551                 }
    552             }
    553             else
    554                 return false;   // Non-numeric char so stopping
    555         }
    556         else
    557         {
    558             old = value;
    559             value = value * 10 + buf[i] - '0';
    560             if (old > value)
    561             {
    562                 // overflow
    563                 return false;
    564             }
    565             if (!digit_found)
    566                 digit_found = true;
    567         }
    568     }
    569 
    570     if (value > 0.0)
    571     {
    572         if (decimal_point_index != -1)
    573         {
    574             i = i - 1 - decimal_point_index;
    575             if (i > 0)
    576             {
    577                 while (i--)
    578                     value /= 10;
    579             }
    580         }
    581         if (minus_sign)
    582             value = (OsclFloat)(0.0 - value);
    583     }
    584     return true;
    585 }
    586 
    587 OSCL_EXPORT_REF int oscl_abs(int aVal)
    588 {
    589     return(aVal < 0 ? -aVal : aVal);
    590 }
    591 
    592