2 /*-------------------------------------------------------------------------*/
7 @version $Revision: 1.8 $
8 @brief Various string handling routines to complement the C lib.
10 This modules adds a few complementary string routines usually missing
11 in the standard C library.
13 /*--------------------------------------------------------------------------*/
16 $Id: strlib.c,v 1.8 2002/12/12 10:29:16 ndevilla Exp $
18 $Date: 2002/12/12 10:29:16 $
22 /*---------------------------------------------------------------------------
24 ---------------------------------------------------------------------------*/
30 #include "inistrlib.h"
32 /*---------------------------------------------------------------------------
34 ---------------------------------------------------------------------------*/
35 #define ASCIILINESZ 1024
37 /*---------------------------------------------------------------------------
39 ---------------------------------------------------------------------------*/
42 /*-------------------------------------------------------------------------*/
44 @brief Convert a string to lowercase.
45 @param s String to convert.
46 @return ptr to statically allocated string.
48 This function returns a pointer to a statically allocated string
49 containing a lowercased version of the input string. Do not free
50 or modify the returned string! Since the returned string is statically
51 allocated, it will be modified at each function call (not re-entrant).
53 /*--------------------------------------------------------------------------*/
55 char * inistrlwc(char * s
)
57 static char l
[ASCIILINESZ
+1];
60 if (s
==NULL
) return NULL
;
61 memset(l
, 0, ASCIILINESZ
+1);
63 while (s
[i
] && i
<ASCIILINESZ
) {
64 l
[i
] = (char)tolower((int)s
[i
]);
67 l
[ASCIILINESZ
]=(char)0;
73 /*-------------------------------------------------------------------------*/
75 @brief Convert a string to uppercase.
76 @param s String to convert.
77 @return ptr to statically allocated string.
79 This function returns a pointer to a statically allocated string
80 containing an uppercased version of the input string. Do not free
81 or modify the returned string! Since the returned string is statically
82 allocated, it will be modified at each function call (not re-entrant).
84 /*--------------------------------------------------------------------------*/
86 char * inistrupc(char * s
)
88 static char l
[ASCIILINESZ
+1];
91 if (s
==NULL
) return NULL
;
92 memset(l
, 0, ASCIILINESZ
+1);
94 while (s
[i
] && i
<ASCIILINESZ
) {
95 l
[i
] = (char)toupper((int)s
[i
]);
98 l
[ASCIILINESZ
]=(char)0;
104 /*-------------------------------------------------------------------------*/
106 @brief Skip blanks until the first non-blank character.
107 @param s String to parse.
108 @return Pointer to char inside given string.
110 This function returns a pointer to the first non-blank character in the
113 /*--------------------------------------------------------------------------*/
115 char * inistrskp(char * s
)
118 if (s
==NULL
) return NULL
;
119 while (isspace((int)*skip
) && *skip
) skip
++;
125 /*-------------------------------------------------------------------------*/
127 @brief Remove blanks at the end of a string.
128 @param s String to parse.
129 @return ptr to statically allocated string.
131 This function returns a pointer to a statically allocated string,
132 which is identical to the input string, except that all blank
133 characters at the end of the string have been removed.
134 Do not free or modify the returned string! Since the returned string
135 is statically allocated, it will be modified at each function call
138 /*--------------------------------------------------------------------------*/
140 char * inistrcrop(char * s
)
142 static char l
[ASCIILINESZ
+1];
145 if (s
==NULL
) return NULL
;
146 memset(l
, 0, ASCIILINESZ
+1);
148 last
= l
+ strlen(l
);
150 if (!isspace((int)*(last
-1)))
160 /*-------------------------------------------------------------------------*/
162 @brief Remove blanks at the beginning and the end of a string.
163 @param s String to parse.
164 @return ptr to statically allocated string.
166 This function returns a pointer to a statically allocated string,
167 which is identical to the input string, except that all blank
168 characters at the end and the beg. of the string have been removed.
169 Do not free or modify the returned string! Since the returned string
170 is statically allocated, it will be modified at each function call
173 /*--------------------------------------------------------------------------*/
174 char * inistrstrip(char * s
)
176 static char l
[ASCIILINESZ
+1];
179 if (s
==NULL
) return NULL
;
181 while (isspace((int)*s
) && *s
) s
++;
183 memset(l
, 0, ASCIILINESZ
+1);
185 last
= l
+ strlen(l
);
187 if (!isspace((int)*(last
-1)))
196 /*-------------------------------------------------------------------------*/
198 @brief Duplicate a string by allocating memory and copy the contents.
199 @param s String to duplicate.
200 @return ptr to dynamically allocated string.
202 This function returns a pointer to a dynamically allocated string,
203 which is identical to the input string.
204 Free the returned string when it is no longer used.
206 /*--------------------------------------------------------------------------*/
207 char *inistrdup(const char *s
) {
212 l
= (int)strlen(s
) + 1;