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