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 
     13 #include "android/utils/misc.h"
     14 #include "android/utils/stralloc.h"
     15 #include "android/utils/debug.h"
     16 #include <string.h>
     17 #include <stdio.h>
     18 #include <stdlib.h>
     19 
     20 extern void
     21 print_tabular( const char** strings, int  count,
     22                const char*  prefix,  int  width )
     23 {
     24     int  nrows, ncols, r, c, n, maxw = 0;
     25 
     26     for (n = 0; n < count; n++) {
     27         int  len = strlen(strings[n]);
     28         if (len > maxw)
     29             maxw = len;
     30     }
     31     maxw += 2;
     32     ncols = width/maxw;
     33     nrows = (count + ncols-1)/ncols;
     34 
     35     for (r = 0; r < nrows; r++) {
     36         printf( "%s", prefix );
     37         for (c = 0; c < ncols; c++) {
     38             int  index = c*nrows + r;
     39             if (index >= count) {
     40                 break;
     41             }
     42             printf( "%-*s", maxw, strings[index] );
     43         }
     44         printf( "\n" );
     45     }
     46 }
     47 
     48 extern void
     49 string_translate_char( char*  str, char from, char to )
     50 {
     51     char*  p = str;
     52     while (p != NULL && (p = strchr(p, from)) != NULL)
     53         *p++ = to;
     54 }
     55 
     56 extern void
     57 buffer_translate_char( char*        buff,
     58                        unsigned     buffLen,
     59                        const char*  src,
     60                        char         fromChar,
     61                        char         toChar )
     62 {
     63     int    len = strlen(src);
     64 
     65     if (len >= buffLen)
     66         len = buffLen-1;
     67 
     68     memcpy(buff, src, len);
     69     buff[len] = 0;
     70 
     71     string_translate_char( buff, fromChar, toChar );
     72 }
     73 
     74 
     75 /** TEMP CHAR STRINGS
     76  **
     77  ** implement a circular ring of temporary string buffers
     78  **/
     79 
     80 typedef struct Temptring {
     81     struct TempString*  next;
     82     char*               buffer;
     83     int                 size;
     84 } TempString;
     85 
     86 #define  MAX_TEMP_STRINGS   16
     87 
     88 static TempString  _temp_strings[ MAX_TEMP_STRINGS ];
     89 static int         _temp_string_n;
     90 
     91 extern char*
     92 tempstr_get( int  size )
     93 {
     94     TempString*  t = &_temp_strings[_temp_string_n];
     95 
     96     if ( ++_temp_string_n >= MAX_TEMP_STRINGS )
     97         _temp_string_n = 0;
     98 
     99     size += 1;  /* reserve 1 char for terminating zero */
    100 
    101     if (t->size < size) {
    102         t->buffer = realloc( t->buffer, size );
    103         if (t->buffer == NULL) {
    104             derror( "%s: could not allocate %d bytes",
    105                     __FUNCTION__, size );
    106             exit(1);
    107         }
    108         t->size   = size;
    109     }
    110     return  t->buffer;
    111 }
    112 
    113 extern char*
    114 tempstr_format( const char*  fmt, ... )
    115 {
    116     va_list  args;
    117     char*    result;
    118     STRALLOC_DEFINE(s);
    119     va_start(args, fmt);
    120     stralloc_formatv(s, fmt, args);
    121     va_end(args);
    122     result = stralloc_to_tempstr(s);
    123     stralloc_reset(s);
    124     return result;
    125 }
    126 
    127 /** QUOTING
    128  **
    129  ** dumps a human-readable version of a string. this replaces
    130  ** newlines with \n, etc...
    131  **/
    132 
    133 extern const char*
    134 quote_bytes( const char*  str, int  len )
    135 {
    136     STRALLOC_DEFINE(s);
    137     char*  q;
    138 
    139     stralloc_add_quote_bytes( s, str, len );
    140     q = stralloc_to_tempstr( s );
    141     stralloc_reset(s);
    142     return q;
    143 }
    144 
    145 extern const char*
    146 quote_str( const char*  str )
    147 {
    148     int  len = strlen(str);
    149     return quote_bytes( str, len );
    150 }
    151 
    152 /** HEXADECIMAL CHARACTER SEQUENCES
    153  **/
    154 
    155 static int
    156 hexdigit( int  c )
    157 {
    158     unsigned  d;
    159 
    160     d = (unsigned)(c - '0');
    161     if (d < 10) return d;
    162 
    163     d = (unsigned)(c - 'a');
    164     if (d < 6) return d+10;
    165 
    166     d = (unsigned)(c - 'A');
    167     if (d < 6) return d+10;
    168 
    169     return -1;
    170 }
    171 
    172 int
    173 hex2int( const uint8_t*  hex, int  len )
    174 {
    175     int  result = 0;
    176     while (len > 0) {
    177         int  c = hexdigit(*hex++);
    178         if (c < 0)
    179             return -1;
    180 
    181         result = (result << 4) | c;
    182         len --;
    183     }
    184     return result;
    185 }
    186 
    187 void
    188 int2hex( uint8_t*  hex, int  len, int  val )
    189 {
    190     static const uint8_t  hexchars[16] = "0123456789abcdef";
    191     while ( --len >= 0 )
    192         *hex++ = hexchars[(val >> (len*4)) & 15];
    193 }
    194