Home | History | Annotate | Download | only in utils
      1 /* Copyright (C) 2007-2008 The Android Open Source Project
      2 **
      3 ** This software is licensed under the terms of the GNU General Public
      4 ** License version 2, as published by the Free Software Foundation, and
      5 ** may be copied, distributed, and modified under those terms.
      6 **
      7 ** This program is distributed in the hope that it will be useful,
      8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 ** GNU General Public License for more details.
     11 */
     12 #ifndef _ANDROID_UTILS_MISC_H
     13 #define _ANDROID_UTILS_MISC_H
     14 
     15 #include <stdint.h>
     16 
     17 /** TABULAR OUTPUT
     18  **
     19  ** prints a list of strings in row/column format
     20  **
     21  **/
     22 
     23 extern void   print_tabular( const char** strings, int  count,
     24                              const char*  prefix,  int  width );
     25 
     26 /** CHARACTER TRANSLATION
     27  **
     28  ** converts one character into another in strings
     29  **/
     30 
     31 extern void   buffer_translate_char( char*        buff,
     32                                      unsigned     buffLen,
     33                                      const char*  src,
     34                                      char         fromChar,
     35                                      char         toChar );
     36 
     37 extern void   string_translate_char( char*  str, char from, char to );
     38 
     39 /** TEMP CHAR STRINGS
     40  **
     41  ** implement a circular ring of temporary string buffers
     42  **/
     43 
     44 extern char*  tempstr_get( int   size );
     45 extern char*  tempstr_format( const char*  fmt, ... );
     46 
     47 /** QUOTING
     48  **
     49  ** dumps a human-readable version of a string. this replaces
     50  ** newlines with \n, etc...
     51  **/
     52 
     53 extern const char*   quote_bytes( const char*  str, int  len );
     54 extern const char*   quote_str( const char*  str );
     55 
     56 /** DECIMAL AND HEXADECIMAL CHARACTER SEQUENCES
     57  **/
     58 
     59 /* decodes a sequence of 'len' hexadecimal chars from 'hex' into
     60  * an integer. returns -1 in case of error (i.e. badly formed chars)
     61  */
     62 extern int    hex2int( const uint8_t*  hex, int  len );
     63 
     64 /* encodes an integer 'val' into 'len' hexadecimal charaters into 'hex' */
     65 extern void   int2hex( uint8_t*  hex, int  len, int  val );
     66 
     67 /** STRING PARAMETER PARSING
     68  **/
     69 
     70 /* A strict 'int' version of the 'strtol'.
     71  * This routine is implemented on top of the standard 'strtol' for 32/64 bit
     72  * portability.
     73  */
     74 extern int strtoi(const char *nptr, char **endptr, int base);
     75 
     76 /* Gets a parameter value out of the parameter string.
     77  * Parameter format for this routine is as such:
     78  *      "<name1>=<value1> <name2>=<value2> ... <nameN>=<valueN>"
     79  * I.e.:
     80  *  - Every parameter must have a name, and a value.
     81  *  - Name and value must be separated with '='.
     82  *  - No spaces are allowed around '=' separating name and value.
     83  *  - Parameters must be separated with a single ' ' character.
     84  *  - No '=' character is allowed in name and in value.
     85  * Param:
     86  *  params - String, containing the parameters.
     87  *  name - Parameter name.
     88  *  value - Upon success contains value for the given parameter.
     89  *  val_size - Size of the 'value' string buffer.
     90  * Return:
     91  *  0 on success, -1 if requested parameter is not found, or (a positive) number
     92  *  of bytes, required to make a copy of the parameter's value if 'value' string
     93  *  was too small to contain it.
     94  */
     95 extern int get_token_value(const char* params, const char* name, char* value, int val_size);
     96 
     97 /* Gets a parameter value out of the parameter string.
     98  * This routine is similar to get_token_value, except it will always allocate
     99  * a string buffer for the value.
    100  * Param:
    101  *  params - String, containing the parameters.
    102  *  name - Parameter name.
    103  *  value - Upon success contains an allocated string containint the value for
    104  *      the given parameter. The caller is responsible for freeing the buffer
    105  *      returned in this parameter on success.
    106  * Return:
    107  *  0 on success, -1 if requested parameter is not found, or -2 on
    108  *  memory failure.
    109  */
    110 extern int get_token_value_alloc(const char* params, const char* name, char** value);
    111 
    112 /* Gets an integer parameter value out of the parameter string.
    113  * Param:
    114  *  params - String, containing the parameters. See comments to get_token_value
    115  *      routine on the parameters format.
    116  *  name - Parameter name. Parameter value must be a decimal number.
    117  *  value - Upon success contains integer value for the given parameter.
    118  * Return:
    119  *  0 on success, or -1 if requested parameter is not found, or -2 if parameter's
    120  *  format was bad (i.e. value was not a decimal number).
    121  */
    122 extern int get_token_value_int(const char* params, const char* name, int* value);
    123 
    124 #endif /* _ANDROID_UTILS_MISC_H */
    125