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