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