1
/* stringhelp.c - standard string helper functions
2
* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26
#include "libutil-config.h"
27
#include "stringhelp.h"
31
* look for the substring SUB in buffer and return a pointer to that
32
* substring in BUF or NULL if not found.
33
* Comparison is case-insensitive.
36
memistr( const char *buf, size_t buflen, const char *sub )
41
for( t=buf, n=buflen, s=sub ; n ; t++, n-- )
42
if( toupper(*t) == toupper(*s) ) {
43
for( buf=t++, buflen = n--, s++;
44
n && toupper(*t) == toupper(*s); t++, s++, n-- )
48
t = buf; n = buflen; s = sub ;
55
* Wie strncpy(), aber es werden maximal n-1 zeichen kopiert und ein
56
* '\0' angeh�ngt. Ist n = 0, so geschieht nichts, ist Destination
57
* gleich NULL, so wird via libutil_malloc Speicher besorgt, ist dann nicht
58
* gen�gend Speicher vorhanden, so bricht die funktion ab.
61
mem2str( char *dest , const void *src , size_t n )
68
dest = libutil_xmalloc( n ) ;
71
for(n--; n && *s; n-- )
81
* remove leading and trailing white spaces
84
trim_spaces( char *str )
86
char *string, *p, *mark;
89
/* find first non space character */
90
for( p=string; *p && isspace( *(byte*)p ) ; p++ )
93
for( (mark = NULL); (*string = *p); string++, p++ )
94
if( isspace( *(byte*)p ) ) {
101
*mark = '\0' ; /* remove trailing spaces */
107
* remove trailing white spaces
110
trim_trailing_spaces( char *string )
114
for( mark = NULL, p = string; *p; p++ ) {
115
if( isspace( *(byte*)p ) ) {
131
trim_trailing_chars( byte *line, unsigned len, const char *trimchars )
136
for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
137
if( strchr(trimchars, *p ) ) {
153
* remove trailing white spaces and return the length of the buffer
156
trim_trailing_ws( byte *line, unsigned len )
158
return trim_trailing_chars( line, len, " \t\r\n" );
163
/*********************************************
164
********** missing string functions *********
165
*********************************************/
169
stpcpy(char *a,const char *b)
191
#ifndef HAVE_STRCASECMP
193
strcasecmp( const char *a, const char *b )
195
for( ; *a && *b; a++, b++ ) {
196
if( *a != *b && toupper(*a) != toupper(*b) )
199
return *(const byte*)a - *(const byte*)b;
205
* mingw32/cpd has a memicmp()
209
memicmp( const char *a, const char *b, size_t n )
211
for( ; n; n--, a++, b++ )
212
if( *a != *b && toupper(*(const byte*)a) != toupper(*(const byte*)b) )
213
return *(const byte *)a - *(const byte*)b;