Back to index

openldap  2.4.31
string.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 /*
00017  * Locale-specific 1-byte character versions
00018  * See utf-8.c for UTF-8 versions
00019  */
00020 
00021 #include "portable.h"
00022 
00023 #include <ac/stdlib.h>
00024 #include <ac/string.h>
00025 #include <ac/time.h>
00026 #include <ac/ctype.h>
00027 
00028 #include "ldap-int.h"
00029 
00030 
00031 #if defined ( HAVE_STRSPN )
00032 #define int_strspn strspn
00033 #else
00034 static int int_strspn( const char *str, const char *delim )
00035 {
00036        int pos;
00037        const char *p=delim;
00038 
00039        for( pos=0; (*str) ; pos++,str++) {
00040               if (*str!=*p) {
00041                      for( p=delim; (*p) ; p++ ) {
00042                             if (*str==*p) {
00043                                    break;
00044                             }
00045                      }
00046               }
00047 
00048               if (*p=='\0') {
00049                      return pos;
00050               }
00051        }
00052        return pos;
00053 }
00054 #endif
00055 
00056 #if defined( HAVE_STRPBRK )
00057 #define int_strpbrk strpbrk
00058 #else
00059 static char *(int_strpbrk)( const char *str, const char *accept )
00060 {
00061        const char *p;
00062 
00063        for( ; (*str) ; str++ ) {
00064               for( p=accept; (*p) ; p++) {
00065                      if (*str==*p) {
00066                             return str;
00067                      }
00068               }
00069        }
00070 
00071        return NULL;
00072 }
00073 #endif
00074 
00075 char *(ldap_pvt_strtok)( char *str, const char *delim, char **pos )
00076 {
00077        char *p;
00078 
00079        if (pos==NULL) {
00080               return NULL;
00081        }
00082 
00083        if (str==NULL) {
00084               if (*pos==NULL) {
00085                      return NULL;
00086               }
00087 
00088               str=*pos;
00089        }
00090 
00091        /* skip any initial delimiters */
00092        str += int_strspn( str, delim );
00093        if (*str == '\0') {
00094               return NULL;
00095        }
00096 
00097        p = int_strpbrk( str, delim );
00098        if (p==NULL) {
00099               *pos = NULL;
00100 
00101        } else {
00102               *p ='\0';
00103               *pos = p+1;
00104        }
00105 
00106        return str;
00107 }
00108 
00109 char *
00110 ldap_pvt_str2upper( char *str )
00111 {
00112        char    *s;
00113 
00114        /* to upper */
00115        if ( str ) {
00116               for ( s = str; *s; s++ ) {
00117                      *s = TOUPPER( (unsigned char) *s );
00118               }
00119        }
00120 
00121        return( str );
00122 }
00123 
00124 struct berval *
00125 ldap_pvt_str2upperbv( char *str, struct berval *bv )
00126 {
00127        char    *s = NULL;
00128 
00129        assert( bv != NULL );
00130 
00131        /* to upper */
00132        if ( str ) {
00133               for ( s = str; *s; s++ ) {
00134                      *s = TOUPPER( (unsigned char) *s );
00135               }
00136        }
00137 
00138        bv->bv_val = str;
00139        bv->bv_len = (ber_len_t)(s - str);
00140        
00141        return( bv );
00142 }
00143 
00144 char *
00145 ldap_pvt_str2lower( char *str )
00146 {
00147        char    *s;
00148 
00149        /* to lower */
00150        if ( str ) {
00151               for ( s = str; *s; s++ ) {
00152                      *s = TOLOWER( (unsigned char) *s );
00153               }
00154        }
00155 
00156        return( str );
00157 }
00158 
00159 struct berval *
00160 ldap_pvt_str2lowerbv( char *str, struct berval *bv )
00161 {
00162        char    *s = NULL;
00163 
00164        assert( bv != NULL );
00165 
00166        /* to lower */
00167        if ( str ) {
00168               for ( s = str; *s; s++ ) {
00169                      *s = TOLOWER( (unsigned char) *s );
00170               }
00171        }
00172 
00173        bv->bv_val = str;
00174        bv->bv_len = (ber_len_t)(s - str);
00175 
00176        return( bv );
00177 }