]> git.sur5r.net Git - openldap/blob - libraries/libldap/string.c
Implement ldap_dn_normalize and friends. Should be used by clients
[openldap] / libraries / libldap / string.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6 #include "portable.h"
7
8 #include <ac/stdlib.h>
9 #include <ac/string.h>
10 #include <ac/time.h>
11 #include <ac/ctype.h>
12
13 #include "ldap-int.h"
14
15
16 #if defined ( HAVE_STRSPN )
17 #define int_strspn strspn
18 #else
19 static int int_strspn( const char *str, const char *delim )
20 {
21         int pos;
22         const char *p=delim;
23
24         for( pos=0; (*str) ; pos++,str++) {
25                 if (*str!=*p) {
26                         for( p=delim; (*p) ; p++ ) {
27                                 if (*str==*p) {
28                                         break;
29                                 }
30                         }
31                 }
32
33                 if (*p=='\0') {
34                         return pos;
35                 }
36         }
37         return pos;
38 }
39 #endif
40
41 #if defined( HAVE_STRPBRK )
42 #define int_strpbrk strpbrk
43 #else
44 static char *(int_strpbrk)( const char *str, const char *accept )
45 {
46         const char *p;
47
48         for( ; (*str) ; str++ ) {
49                 for( p=accept; (*p) ; p++) {
50                         if (*str==*p) {
51                                 return str;
52                         }
53                 }
54         }
55
56         return NULL;
57 }
58 #endif
59
60 char *(ldap_pvt_strtok)( char *str, const char *delim, char **pos )
61 {
62         char *p;
63
64         if (pos==NULL) {
65                 return NULL;
66         }
67
68         if (str==NULL) {
69                 if (*pos==NULL) {
70                         return NULL;
71                 }
72
73                 str=*pos;
74         }
75
76         /* skip any initial delimiters */
77         str += int_strspn( str, delim );
78         if (*str == '\0') {
79                 return NULL;
80         }
81
82         p = int_strpbrk( str, delim );
83         if (p==NULL) {
84                 *pos = NULL;
85
86         } else {
87                 *p ='\0';
88                 *pos = p+1;
89         }
90
91         return str;
92 }
93
94 char *
95 (ldap_pvt_strdup)( const char *s )
96 {
97         char    *p;
98         size_t  len = strlen( s ) + 1;
99
100         if ( (p = (char *) malloc( len )) == NULL ) {
101                 return( NULL );
102         }
103
104         memcpy( p, s, len );
105         return( p );
106 }
107
108 char *
109 ldap_pvt_str2upper( char *str )
110 {
111         char    *s;
112
113         /* to upper */
114         for ( s = str; *s; s++ ) {
115                 *s = TOUPPER( (unsigned char) *s );
116         }
117
118         return( str );
119 }
120
121 char *
122 ldap_pvt_str2lower( char *str )
123 {
124         char    *s;
125
126         /* to lower */
127         for ( s = str; *s; s++ ) {
128                 *s = TOLOWER( (unsigned char) *s );
129         }
130
131         return( str );
132 }