FreeBSD isn't evil - just misguided
[cvsps-yd/commitid.git] / cbtcommon / text_util.c
blob052a94b4eef312e255c93d879938735bf69fb509
1 /*
2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
4 */
6 /**
7 * Copyright (c) 1998 Cobite, Inc. All Rights Reserved.
8 * @author Karl LaRocca
9 * @created Fri Nov 6 14:33:29 1998
10 * @version $Revision: 1.9 $$Date: 2001/10/25 18:36:11 $
12 #include <ctype.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
17 #include "text_util.h"
18 #include "rcsid.h"
20 RCSID("$Id: text_util.c,v 1.9 2001/10/25 18:36:11 adam Exp $");
22 char*
23 chop( char* src )
25 char* p = src + strlen(src) - 1;
27 while( p >= src )
29 if ( *p == '\n' || *p == '\r' )
31 *p-- = 0;
34 else
36 break;
40 return( src );
43 char*
44 digits( char* src )
46 char* start = src;
47 char* check = src;
49 while( *check )
51 if ( isdigit( *check ) )
53 *start++ = *check;
56 check++;
59 *start = 0;
61 return( src );
64 char*
65 lower_case( char* src )
67 char* p = src;
69 while( *p )
71 *p = tolower( *p );
72 p++;
75 return( src );
78 char*
79 reverse( char* src )
81 int i;
82 int len = strlen( src );
83 char tmp;
85 for( i = len / 2; --i >= 0; )
87 tmp = src[ i ];
88 src[ i ] = src[ len - i - 1 ];
89 src[ len - i - 1 ] = tmp;
92 return( src );
95 char*
96 trim( char* src )
98 char *p = src + strlen(src) - 1;
100 while( p >= src && isspace(*p) )
101 *p-- = '\0';
103 return src;
106 char*
107 upper_case( char* src )
109 char* p = src;
111 while( *p )
113 *p = toupper(*p);
114 p++;
117 return( src );
121 strrcmp( const char* haystack, const char* needle )
123 int hlen = strlen( haystack );
124 int nlen = strlen( needle );
125 if( hlen < nlen )
126 return( -1 );
127 else
128 return( strcmp( haystack + hlen - nlen, needle ) );
132 * Finding a - anywhere in the string makes it money negative.
133 * all characters other than digits, '-', and '.' are ignored, so:
134 * ab36-.g98 = -36.98
135 * This is fair, I think, if we don't want to reject anything as
136 * improperly formatted.
138 long
139 money2cents( const char* money )
141 long retval = 0;
142 int decimal_places = -1;
143 int neg = 0;
145 while( *money && decimal_places < 2 )
147 if ( isdigit( *money ) )
149 if ( decimal_places >= 0 )
150 decimal_places++;
152 retval *= 10;
153 retval += (*money) - '0';
156 else if ( *money == '.' )
157 decimal_places = 0;
159 else if ( *money == '-' )
160 neg = 1;
162 money++;
165 if ( decimal_places == 1 )
166 retval *= 10;
168 else if ( decimal_places <= 0 )
169 retval *= 100;
171 return( neg ? -retval : retval );
174 const char*
175 cents2money( long cents )
177 static char buff[ 64 ];
178 int idx = 0;
179 char* d = buff;
181 if ( cents == 0 )
183 strcpy( buff, "0.00" );
186 else if ( cents < 100 )
188 sprintf( buff, "0.%2.2ld", cents );
191 else
193 while( cents > 0 )
195 *d++ = '0' + ( cents % 10 );
196 cents = cents / 10;
198 if ( idx == 1 )
200 *d++ = '.';
203 else if ( cents > 0 && ( idx - 1 ) % 3 == 0 )
205 *d++ = ',';
208 idx++;
211 *d++ = 0;
213 reverse( buff );
216 return( buff );
219 void trim_zeros_after_decimal( char* src )
221 char * end = src + strlen( src ) - 1;
223 while( end != src )
225 if( *end == '0' )
226 *end = 0;
227 else if( *end == '.' )
229 *end = 0;
230 break;
232 else
233 break;
235 end--;
239 #ifdef linux
240 extern void *memfrob(void *, size_t);
241 #else
242 static void * memfrob(void * mem, size_t len)
244 size_t i;
245 char *c = (char *)mem;
247 for (i = 0; i < len; i++)
249 *c = *c ^ 42;
250 c++;
253 return mem;
255 #endif
257 // simple functions to obfuscate strings in a binary
258 char* frobstr( char* src )
260 char* retval = (char*)malloc( strlen(src) * 2 + 1 );
262 memfrob( src, strlen( src ) );
263 str2hex( retval, src, 0 );
264 memfrob( src, strlen( src ) );
266 return( retval );
269 char* unfrobstr( char* src )
271 int slen = strlen( src ) / 2;
272 char* retval = (char*)malloc( slen + 1 );
274 hex2str( retval, src, 0 );
275 memfrob( retval, slen );
277 return( retval );
280 void str2hex( char* dest, const char* src, int slen )
282 int i;
283 char* p = dest;
285 if( slen == 0 )
286 slen = strlen( src );
288 for ( i = 0; i < slen; i++ )
290 sprintf( p, "%02x", src[i] );
291 p += 2;
294 *p = 0;
297 void hex2str( char* dest, const char* src, int slen )
299 const char* p = src;
300 int i;
301 unsigned int v;
303 if( slen == 0 )
304 slen = strlen( src );
306 slen /= 2;
308 for( i = 0; i < slen; i++ )
310 sscanf( p, "%02x", &v );
311 dest[i] = (char)v;
312 p += 2;
315 dest[ slen ] = 0;