| 1 |
|
|---|
| 2 | /*-------------------------------------------------------------------------*/
|
|---|
| 3 | /**
|
|---|
| 4 | @file strlib.c
|
|---|
| 5 | @author N. Devillard
|
|---|
| 6 | @date Jan 2001
|
|---|
| 7 | @version $Revision: 1.9 $
|
|---|
| 8 | @brief Various string handling routines to complement the C lib.
|
|---|
| 9 |
|
|---|
| 10 | This modules adds a few complementary string routines usually missing
|
|---|
| 11 | in the standard C library.
|
|---|
| 12 | */
|
|---|
| 13 | /*--------------------------------------------------------------------------*/
|
|---|
| 14 |
|
|---|
| 15 | /*
|
|---|
| 16 | $Id: strlib.c,v 1.9 2006-09-27 11:04:11 ndevilla Exp $
|
|---|
| 17 | $Author: ndevilla $
|
|---|
| 18 | $Date: 2006-09-27 11:04:11 $
|
|---|
| 19 | $Revision: 1.9 $
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | /*---------------------------------------------------------------------------
|
|---|
| 23 | Includes
|
|---|
| 24 | ---------------------------------------------------------------------------*/
|
|---|
| 25 |
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 | #include <ctype.h>
|
|---|
| 28 |
|
|---|
| 29 | #include "strlib.h"
|
|---|
| 30 |
|
|---|
| 31 | /*---------------------------------------------------------------------------
|
|---|
| 32 | Defines
|
|---|
| 33 | ---------------------------------------------------------------------------*/
|
|---|
| 34 | #define ASCIILINESZ 1024
|
|---|
| 35 |
|
|---|
| 36 | /*---------------------------------------------------------------------------
|
|---|
| 37 | Function codes
|
|---|
| 38 | ---------------------------------------------------------------------------*/
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | /*-------------------------------------------------------------------------*/
|
|---|
| 42 | /**
|
|---|
| 43 | @brief Convert a string to lowercase.
|
|---|
| 44 | @param s String to convert.
|
|---|
| 45 | @return ptr to statically allocated string.
|
|---|
| 46 |
|
|---|
| 47 | This function returns a pointer to a statically allocated string
|
|---|
| 48 | containing a lowercased version of the input string. Do not free
|
|---|
| 49 | or modify the returned string! Since the returned string is statically
|
|---|
| 50 | allocated, it will be modified at each function call (not re-entrant).
|
|---|
| 51 | */
|
|---|
| 52 | /*--------------------------------------------------------------------------*/
|
|---|
| 53 |
|
|---|
| 54 | char * strlwc(const char * s)
|
|---|
| 55 | {
|
|---|
| 56 | static char l[ASCIILINESZ+1];
|
|---|
| 57 | int i ;
|
|---|
| 58 |
|
|---|
| 59 | if (s==NULL) return NULL ;
|
|---|
| 60 | memset(l, 0, ASCIILINESZ+1);
|
|---|
| 61 | i=0 ;
|
|---|
| 62 | while (s[i] && i<ASCIILINESZ) {
|
|---|
| 63 | l[i] = (char)tolower((int)s[i]);
|
|---|
| 64 | i++ ;
|
|---|
| 65 | }
|
|---|
| 66 | l[ASCIILINESZ]=(char)0;
|
|---|
| 67 | return l ;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | /*-------------------------------------------------------------------------*/
|
|---|
| 73 | /**
|
|---|
| 74 | @brief Convert a string to uppercase.
|
|---|
| 75 | @param s String to convert.
|
|---|
| 76 | @return ptr to statically allocated string.
|
|---|
| 77 |
|
|---|
| 78 | This function returns a pointer to a statically allocated string
|
|---|
| 79 | containing an uppercased version of the input string. Do not free
|
|---|
| 80 | or modify the returned string! Since the returned string is statically
|
|---|
| 81 | allocated, it will be modified at each function call (not re-entrant).
|
|---|
| 82 | */
|
|---|
| 83 | /*--------------------------------------------------------------------------*/
|
|---|
| 84 |
|
|---|
| 85 | char * strupc(char * s)
|
|---|
| 86 | {
|
|---|
| 87 | static char l[ASCIILINESZ+1];
|
|---|
| 88 | int i ;
|
|---|
| 89 |
|
|---|
| 90 | if (s==NULL) return NULL ;
|
|---|
| 91 | memset(l, 0, ASCIILINESZ+1);
|
|---|
| 92 | i=0 ;
|
|---|
| 93 | while (s[i] && i<ASCIILINESZ) {
|
|---|
| 94 | l[i] = (char)toupper((int)s[i]);
|
|---|
| 95 | i++ ;
|
|---|
| 96 | }
|
|---|
| 97 | l[ASCIILINESZ]=(char)0;
|
|---|
| 98 | return l ;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | /*-------------------------------------------------------------------------*/
|
|---|
| 104 | /**
|
|---|
| 105 | @brief Skip blanks until the first non-blank character.
|
|---|
| 106 | @param s String to parse.
|
|---|
| 107 | @return Pointer to char inside given string.
|
|---|
| 108 |
|
|---|
| 109 | This function returns a pointer to the first non-blank character in the
|
|---|
| 110 | given string.
|
|---|
| 111 | */
|
|---|
| 112 | /*--------------------------------------------------------------------------*/
|
|---|
| 113 |
|
|---|
| 114 | char * strskp(char * s)
|
|---|
| 115 | {
|
|---|
| 116 | char * skip = s;
|
|---|
| 117 | if (s==NULL) return NULL ;
|
|---|
| 118 | while (isspace((int)*skip) && *skip) skip++;
|
|---|
| 119 | return skip ;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | /*-------------------------------------------------------------------------*/
|
|---|
| 125 | /**
|
|---|
| 126 | @brief Remove blanks at the end of a string.
|
|---|
| 127 | @param s String to parse.
|
|---|
| 128 | @return ptr to statically allocated string.
|
|---|
| 129 |
|
|---|
| 130 | This function returns a pointer to a statically allocated string,
|
|---|
| 131 | which is identical to the input string, except that all blank
|
|---|
| 132 | characters at the end of the string have been removed.
|
|---|
| 133 | Do not free or modify the returned string! Since the returned string
|
|---|
| 134 | is statically allocated, it will be modified at each function call
|
|---|
| 135 | (not re-entrant).
|
|---|
| 136 | */
|
|---|
| 137 | /*--------------------------------------------------------------------------*/
|
|---|
| 138 |
|
|---|
| 139 | char * strcrop(char * s)
|
|---|
| 140 | {
|
|---|
| 141 | static char l[ASCIILINESZ+1];
|
|---|
| 142 | char * last ;
|
|---|
| 143 |
|
|---|
| 144 | if (s==NULL) return NULL ;
|
|---|
| 145 | memset(l, 0, ASCIILINESZ+1);
|
|---|
| 146 | strcpy(l, s);
|
|---|
| 147 | last = l + strlen(l);
|
|---|
| 148 | while (last > l) {
|
|---|
| 149 | if (!isspace((int)*(last-1)))
|
|---|
| 150 | break ;
|
|---|
| 151 | last -- ;
|
|---|
| 152 | }
|
|---|
| 153 | *last = (char)0;
|
|---|
| 154 | return l ;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 | /*-------------------------------------------------------------------------*/
|
|---|
| 160 | /**
|
|---|
| 161 | @brief Remove blanks at the beginning and the end of a string.
|
|---|
| 162 | @param s String to parse.
|
|---|
| 163 | @return ptr to statically allocated string.
|
|---|
| 164 |
|
|---|
| 165 | This function returns a pointer to a statically allocated string,
|
|---|
| 166 | which is identical to the input string, except that all blank
|
|---|
| 167 | characters at the end and the beg. of the string have been removed.
|
|---|
| 168 | Do not free or modify the returned string! Since the returned string
|
|---|
| 169 | is statically allocated, it will be modified at each function call
|
|---|
| 170 | (not re-entrant).
|
|---|
| 171 | */
|
|---|
| 172 | /*--------------------------------------------------------------------------*/
|
|---|
| 173 | char * strstrip(char * s)
|
|---|
| 174 | {
|
|---|
| 175 | static char l[ASCIILINESZ+1];
|
|---|
| 176 | char * last ;
|
|---|
| 177 |
|
|---|
| 178 | if (s==NULL) return NULL ;
|
|---|
| 179 |
|
|---|
| 180 | while (isspace((int)*s) && *s) s++;
|
|---|
| 181 |
|
|---|
| 182 | memset(l, 0, ASCIILINESZ+1);
|
|---|
| 183 | strcpy(l, s);
|
|---|
| 184 | last = l + strlen(l);
|
|---|
| 185 | while (last > l) {
|
|---|
| 186 | if (!isspace((int)*(last-1)))
|
|---|
| 187 | break ;
|
|---|
| 188 | last -- ;
|
|---|
| 189 | }
|
|---|
| 190 | *last = (char)0;
|
|---|
| 191 |
|
|---|
| 192 | return (char*)l ;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /* Test code */
|
|---|
| 196 | #ifdef TEST
|
|---|
| 197 | int main(int argc, char * argv[])
|
|---|
| 198 | {
|
|---|
| 199 | char * str ;
|
|---|
| 200 |
|
|---|
| 201 | str = "\t\tI'm a lumberkack and I'm OK " ;
|
|---|
| 202 | printf("lowercase: [%s]\n", strlwc(str));
|
|---|
| 203 | printf("uppercase: [%s]\n", strupc(str));
|
|---|
| 204 | printf("skipped : [%s]\n", strskp(str));
|
|---|
| 205 | printf("cropped : [%s]\n", strcrop(str));
|
|---|
| 206 | printf("stripped : [%s]\n", strstrip(str));
|
|---|
| 207 |
|
|---|
| 208 | return 0 ;
|
|---|
| 209 | }
|
|---|
| 210 | #endif
|
|---|
| 211 | /* vim: set ts=4 et sw=4 tw=75 */
|
|---|