chiark / gitweb /
Lots of new source files.
[mLib] / str.c
CommitLineData
081e6815 1/* -*-c-*-
2 *
f3a542e8 3 * $Id: str.c,v 1.2 1999/05/26 20:52:57 mdw Exp $
081e6815 4 *
5 * Functions for hacking with strings
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: str.c,v $
f3a542e8 33 * Revision 1.2 1999/05/26 20:52:57 mdw
34 * Add new `rest' argument for `str_split'.
35 *
081e6815 36 * Revision 1.1 1999/05/17 20:37:01 mdw
37 * Some trivial string hacks.
38 *
39 */
40
41/*----- Header files ------------------------------------------------------*/
42
43#include <ctype.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47
48#include "str.h"
49
50/*----- Main code ---------------------------------------------------------*/
51
52/* --- @str_getword@ --- *
53 *
54 * Arguments: @char **pp@ = address of pointer into string
55 *
56 * Returns: Pointer to the next space-separated word from the string,
57 * or null.
58 *
59 * Use: Parses off space-separated words from a string.
60 */
61
62char *str_getword(char **pp)
63{
64 char *p = *pp, *q;
65
66 if (!p)
67 return (0);
68
69 while (isspace((unsigned char)*p))
70 p++;
71
72 for (q = p; *q; q++) {
73 if (isspace((unsigned char)*q)) {
74 *q = 0;
75 *pp = q + 1;
76 return (p);
77 }
78 }
79
80 *pp = 0;
81 return (p);
82}
83
84/* --- @str_split@ --- *
85 *
86 * Arguments: @char *p@ = pointer to string
87 * @char *v[]@ = pointer to array to fill in
88 * @size_t c@ = count of strings to fill in
f3a542e8 89 * @char **rest@ = where to store the remainder of the string
081e6815 90 *
91 * Returns: Number of strings filled in.
92 *
93 * Use: Fills an array with pointers to the individual words of a
94 * string. The string is modified in place to contain zero
95 * bytes at the word boundaries, and the words have leading
96 * and trailing space stripped off. No more than @c@ words
97 * are read; the actual number is returned as the value of the
98 * function. Unused slots in the array are populated with
f3a542e8 99 * null bytes. If there's any string left, the address of the
100 * remainder is stored in @rest@ (if it's non-null); otherwise
101 * @rest@ is set to a null pointer.
081e6815 102 */
103
f3a542e8 104size_t str_split(char *p, char *v[], size_t c, char **rest)
081e6815 105{
106 size_t n = 0;
107 char *q;
108
109 while (c && (q = str_getword(&p)) != 0) {
110 *v++ = q;
111 c--;
112 n++;
113 }
114
115 while (c) {
116 *v++ = 0;
117 c--;
118 }
f3a542e8 119
120 if (rest) {
121 if (!p)
122 *rest = 0;
123 else {
124 while (isspace((unsigned char)*p))
125 p++;
126 if (*p)
127 *rest = p;
128 else
129 *rest = 0;
130 }
131 }
081e6815 132 return (n);
133}
134
135/* --- @str_sanitize@ --- *
136 *
137 * Arguments: @char *d@ = destination buffer
138 * @const char *p@ = pointer to source string
139 * @size_t sz@ = size of destination buffer
140 *
141 * Returns: ---
142 *
143 * Use: Writes a string into a buffer, being careful not to overflow
144 * the buffer, to null terminate the result, and to prevent
145 * nasty nonprintable characters ending up in the buffer.
146 */
147
148void str_sanitize(char *d, const char *p, size_t sz)
149{
150 if (!sz)
151 return;
152 sz--;
153 while (*p && sz) {
154 int ch = *p++;
155 if (!isgraph((unsigned char)ch))
156 ch = '_';
157 *d++ = ch;
158 sz--;
159 }
160 *d++ = 0;
161}
162
163/*----- That's all, folks -------------------------------------------------*/