8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sgs / libconv / common / strproc.c
blob96830c75c8b7d04d32f3505d47308c58daeff441
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 * General purpse string manipulation routines
32 #include <stdio.h>
33 #include <_conv.h>
37 * Implementation of isspace() that does not require <ctype.h>
38 * or <sys/ctype.h>, appropriate for simple non-localized use.
40 int
41 conv_strproc_isspace(int c)
43 return ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n'));
47 * Remove leading and trailing whitespace from the given string.
49 * entry:
50 * str - String to be trimmed
52 * exit:
53 * The pointer to the trimmed string is returned.
55 * note:
56 * Leading whitespace is trimmed by advancing the given str pointer,
57 * and not by making a copy or allocing more memory. Hence, the caller
58 * should retain a copy of the original str pointer if they need to
59 * free the original memory or otherwise access it.
61 * Trailing whitespace is trimmed by inserting a NULL termination
62 * in the position at which the first trailing whitespce character
63 * lies. This routine can therefore modify the memory used by
64 * the input string.
66 char *
67 conv_strproc_trim(char *str)
69 char *tail;
71 /* Skip leading whitespace */
72 while (conv_strproc_isspace(*str))
73 str++;
75 /* Back up over trailing whitespace */
76 tail = str + strlen(str);
77 while ((tail > str) && conv_strproc_isspace(*(tail - 1)))
78 tail--;
79 *tail = '\0';
81 return (str);
85 * Given a debug token of the form:
87 * token=value
89 * extract and return a pointer to the value.
91 * entry:
92 * str - String to process
93 * token_len = Length of the token, not counting the '=' character,
94 * or any whitespace between the token and the '='.
95 * to_upper - True to convert the returned value to upper case.
96 * value - Address of pointer to receive the value string.
98 * exit:
99 * On success, *value is updated to point at the value string,
100 * and True (1) is returned. On failure, False (0) is returned.
102 * note:
103 * If CONV_SPEXV_F_UCASE is specified, this routine modifies
104 * the memory pointed at by str.
106 Boolean
107 conv_strproc_extract_value(char *str, size_t token_len, int flags,
108 const char **value)
110 int trim = (flags & CONV_SPEXV_F_NOTRIM) == 0;
112 /* Skip the token */
113 str += token_len;
116 * If TRIM, skip whitespace between token and '='
118 if (trim)
119 while (conv_strproc_isspace(*str))
120 str++;
122 /* If there's not a '=' here, this isn't the token we thought it was */
123 if (*str != '=')
124 return (FALSE);
126 str++; /* skip the '=' */
128 /* if TRIM, skip whitespace following the '=' */
129 if (trim)
130 while (conv_strproc_isspace(*str))
131 str++;
133 /* Null value and it's not OK? Make it an error. */
134 if (((flags & CONV_SPEXV_F_NULLOK) == 0) && (*str == '\0'))
135 return (FALSE);
137 *value = str;
139 /* Convert to uppercase on request */
140 if (flags & CONV_SPEXV_F_UCASE)
141 for (; *str; str++)
142 if ((*str >= 'a') && (*str <= 'z'))
143 *str = *str - ('a' - 'A');
145 return (TRUE);