Merge to tip. Don't try to trim empty strings.
| 1 | /* |
| 2 | * The contents of this file are subject to the AOLserver Public License |
| 3 | * Version 1.1 (the "License"); you may not use this file except in |
| 4 | * compliance with the License. You may obtain a copy of the License at |
| 5 | * http://aolserver.com/. |
| 6 | * |
| 7 | * Software distributed under the License is distributed on an "AS IS" |
| 8 | * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 9 | * the License for the specific language governing rights and limitations |
| 10 | * under the License. |
| 11 | * |
| 12 | * The Original Code is AOLserver Code and related documentation |
| 13 | * distributed by AOL. |
| 14 | * |
| 15 | * The Initial Developer of the Original Code is America Online, |
| 16 | * Inc. Portions created by AOL are Copyright (C) 1999 America Online, |
| 17 | * Inc. All Rights Reserved. |
| 18 | * |
| 19 | * Alternatively, the contents of this file may be used under the terms |
| 20 | * of the GNU General Public License (the "GPL"), in which case the |
| 21 | * provisions of GPL are applicable instead of those above. If you wish |
| 22 | * to allow use of your version of this file only under the terms of the |
| 23 | * GPL and not to allow others to use your version of this file under the |
| 24 | * License, indicate your decision by deleting the provisions above and |
| 25 | * replace them with the notice and other provisions required by the GPL. |
| 26 | * If you do not delete the provisions above, a recipient may use your |
| 27 | * version of this file under either the License or the GPL. |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | * str.c -- |
| 33 | * |
| 34 | * Functions that deal with strings. |
| 35 | */ |
| 36 | |
| 37 | static const char *RCSID = "@(#) $Header: /cvsroot-fuse/aolserver/aolserver/nsd/str.c,v 1.6 2004/06/08 19:28:57 rcrittenden0569 Exp $, compiled: " __DATE__ " " __TIME__; |
| 38 | |
| 39 | #include "nsd.h" |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | *---------------------------------------------------------------------- |
| 44 | * |
| 45 | * <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrTrim">Ns_StrTrim</a> -- |
| 46 | * |
| 47 | * Trim leading and trailing white space from a string. |
| 48 | * |
| 49 | * Results: |
| 50 | * A pointer to the trimmed string, which will be in the original |
| 51 | * string. |
| 52 | * |
| 53 | * Side effects: |
| 54 | * May modify passed-in string. |
| 55 | * |
| 56 | *---------------------------------------------------------------------- |
| 57 | */ |
| 58 | |
| 59 | char * |
| 60 | <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrTrim">Ns_StrTrim</a>(char *string) |
| 61 | { |
| 62 | return <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrTrimLeft">Ns_StrTrimLeft</a>(<a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrTrimRight">Ns_StrTrimRight</a>(string)); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
| 67 | /* |
| 68 | *---------------------------------------------------------------------- |
| 69 | * |
| 70 | * <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrTrimLeft">Ns_StrTrimLeft</a> -- |
| 71 | * |
| 72 | * Trim leading white space from a string. |
| 73 | * |
| 74 | * Results: |
| 75 | * A pointer to the trimmed string, which will be in the |
| 76 | * original string. |
| 77 | * |
| 78 | * Side effects: |
| 79 | * None. |
| 80 | * |
| 81 | *---------------------------------------------------------------------- |
| 82 | */ |
| 83 | |
| 84 | char * |
| 85 | <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrTrimLeft">Ns_StrTrimLeft</a>(char *string) |
| 86 | { |
| 87 | if (string == NULL) { |
| 88 | return NULL; |
| 89 | } |
| 90 | while (isspace(UCHAR(*string))) { |
| 91 | ++string; |
| 92 | } |
| 93 | return string; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | /* |
| 99 | *---------------------------------------------------------------------- |
| 100 | * |
| 101 | * <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrTrimRight">Ns_StrTrimRight</a> -- |
| 102 | * |
| 103 | * Trim trailing white space from a string. |
| 104 | * |
| 105 | * Results: |
| 106 | * A pointer to the trimmed string, which will be in the |
| 107 | * original string. |
| 108 | * |
| 109 | * Side effects: |
| 110 | * The string will be modified. |
| 111 | * |
| 112 | *---------------------------------------------------------------------- |
| 113 | */ |
| 114 | |
| 115 | char * |
| 116 | <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrTrimRight">Ns_StrTrimRight</a>(char *string) |
| 117 | { |
| 118 | int len; |
| 119 | |
| 120 | if (string == NULL) { |
| 121 | return NULL; |
| 122 | } |
| 123 | len = strlen(string); |
| 124 | while ((--len >= 0) && |
| 125 | (isspace(UCHAR(string[len])) || |
| 126 | string[len] == '\n')) { |
| 127 | |
| 128 | string[len] = '\0'; |
| 129 | } |
| 130 | return string; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /* |
| 135 | *---------------------------------------------------------------------- |
| 136 | * |
| 137 | * <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrToLower">Ns_StrToLower</a> -- |
| 138 | * |
| 139 | * All alph. chars in a string will be made to be lowercase. |
| 140 | * |
| 141 | * Results: |
| 142 | * Same string as passed in. |
| 143 | * |
| 144 | * Side effects: |
| 145 | * Will modify string. |
| 146 | * |
| 147 | *---------------------------------------------------------------------- |
| 148 | */ |
| 149 | |
| 150 | char * |
| 151 | <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrToLower">Ns_StrToLower</a>(char *string) |
| 152 | { |
| 153 | char *s; |
| 154 | |
| 155 | s = string; |
| 156 | while (*s != '\0') { |
| 157 | if (isupper(UCHAR(*s))) { |
| 158 | *s = tolower(UCHAR(*s)); |
| 159 | } |
| 160 | ++s; |
| 161 | } |
| 162 | return string; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /* |
| 167 | *---------------------------------------------------------------------- |
| 168 | * |
| 169 | * <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrToUpper">Ns_StrToUpper</a> -- |
| 170 | * |
| 171 | * All alph. chars in a string will be made to be uppercase. |
| 172 | * |
| 173 | * Results: |
| 174 | * Same string as pssed in. |
| 175 | * |
| 176 | * Side effects: |
| 177 | * Will modify string. |
| 178 | * |
| 179 | *---------------------------------------------------------------------- |
| 180 | */ |
| 181 | |
| 182 | char * |
| 183 | <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrToUpper">Ns_StrToUpper</a>(char *string) |
| 184 | { |
| 185 | char *s; |
| 186 | |
| 187 | s = string; |
| 188 | while (*s != '\0') { |
| 189 | if (islower(UCHAR(*s))) { |
| 190 | *s = toupper(UCHAR(*s)); |
| 191 | } |
| 192 | ++s; |
| 193 | } |
| 194 | return string; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /* |
| 199 | *---------------------------------------------------------------------- |
| 200 | * |
| 201 | * <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_Match">Ns_Match</a> -- |
| 202 | * |
| 203 | * Compare the beginnings of two strings, case insensitively. |
| 204 | * The comparison stops when the end of the shorter string is |
| 205 | * reached. |
| 206 | * |
| 207 | * Results: |
| 208 | * NULL if no match, b if match. |
| 209 | * |
| 210 | * Side effects: |
| 211 | * None. |
| 212 | * |
| 213 | *---------------------------------------------------------------------- |
| 214 | */ |
| 215 | |
| 216 | char * |
| 217 | <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_Match">Ns_Match</a>(char *a, char *b) |
| 218 | { |
| 219 | if (a != NULL && b != NULL) { |
| 220 | while (*a != '\0' && *b != '\0') { |
| 221 | char c1, c2; |
| 222 | |
| 223 | c1 = islower(UCHAR(*a)) ? *a : tolower(UCHAR(*a)); |
| 224 | c2 = islower(UCHAR(*b)) ? *b : tolower(UCHAR(*b)); |
| 225 | if (c1 != c2) { |
| 226 | return NULL; |
| 227 | } |
| 228 | a++; |
| 229 | b++; |
| 230 | } |
| 231 | } |
| 232 | return (char *) b; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | /* |
| 237 | *---------------------------------------------------------------------- |
| 238 | * |
| 239 | * <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_NextWord">Ns_NextWord</a> -- |
| 240 | * |
| 241 | * Return a pointer to first character of the next word in a |
| 242 | * string; words are separated by white space. |
| 243 | * |
| 244 | * Results: |
| 245 | * A string pointer in the original string. |
| 246 | * |
| 247 | * Side effects: |
| 248 | * None. |
| 249 | * |
| 250 | *---------------------------------------------------------------------- |
| 251 | */ |
| 252 | |
| 253 | char * |
| 254 | <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_NextWord">Ns_NextWord</a>(char *line) |
| 255 | { |
| 256 | while (*line != '\0' && !isspace(UCHAR(*line))) { |
| 257 | ++line; |
| 258 | } |
| 259 | while (*line != '\0' && isspace(UCHAR(*line))) { |
| 260 | ++line; |
| 261 | } |
| 262 | return line; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /* |
| 267 | *---------------------------------------------------------------------- |
| 268 | * |
| 269 | * Ns_StrCaseStr -- |
| 270 | * |
| 271 | * Search for first substring within string, case insensitive. |
| 272 | * |
| 273 | * Results: |
| 274 | * A pointer to where substring starts or NULL. |
| 275 | * |
| 276 | * Side effects: |
| 277 | * None. |
| 278 | * |
| 279 | *---------------------------------------------------------------------- |
| 280 | */ |
| 281 | |
| 282 | char * |
| 283 | <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrNStr">Ns_StrNStr</a>(char *string, char *substring) |
| 284 | { |
| 285 | return <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrCaseFind">Ns_StrCaseFind</a>(string, substring); |
| 286 | } |
| 287 | |
| 288 | char * |
| 289 | <a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_StrCaseFind">Ns_StrCaseFind</a>(char *string, char *substring) |
| 290 | { |
| 291 | if (strlen(string) > strlen(substring)) { |
| 292 | while (*string != '\0') { |
| 293 | if (<a href="/cvs/aolserver/aolserver/nsd/str.c#A_Ns_Match">Ns_Match</a>(string, substring)) { |
| 294 | return string; |
| 295 | } |
| 296 | ++string; |
| 297 | } |
| 298 | } |
| 299 | return NULL; |
| 300 | } |
Copyright © 2010 Geeknet, Inc. All rights reserved. Terms of Use