chiark / gitweb /
Interface change to make the `conn' selector useful for generic stream
[mLib] / str.c
CommitLineData
081e6815 1/* -*-c-*-
2 *
3 * $Id: str.c,v 1.1 1999/05/17 20:37:01 mdw Exp $
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 $
33 * Revision 1.1 1999/05/17 20:37:01 mdw
34 * Some trivial string hacks.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <ctype.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45#include "str.h"
46
47/*----- Main code ---------------------------------------------------------*/
48
49/* --- @str_getword@ --- *
50 *
51 * Arguments: @char **pp@ = address of pointer into string
52 *
53 * Returns: Pointer to the next space-separated word from the string,
54 * or null.
55 *
56 * Use: Parses off space-separated words from a string.
57 */
58
59char *str_getword(char **pp)
60{
61 char *p = *pp, *q;
62
63 if (!p)
64 return (0);
65
66 while (isspace((unsigned char)*p))
67 p++;
68
69 for (q = p; *q; q++) {
70 if (isspace((unsigned char)*q)) {
71 *q = 0;
72 *pp = q + 1;
73 return (p);
74 }
75 }
76
77 *pp = 0;
78 return (p);
79}
80
81/* --- @str_split@ --- *
82 *
83 * Arguments: @char *p@ = pointer to string
84 * @char *v[]@ = pointer to array to fill in
85 * @size_t c@ = count of strings to fill in
86 *
87 * Returns: Number of strings filled in.
88 *
89 * Use: Fills an array with pointers to the individual words of a
90 * string. The string is modified in place to contain zero
91 * bytes at the word boundaries, and the words have leading
92 * and trailing space stripped off. No more than @c@ words
93 * are read; the actual number is returned as the value of the
94 * function. Unused slots in the array are populated with
95 * null bytes.
96 */
97
98size_t str_split(char *p, char *v[], size_t c)
99{
100 size_t n = 0;
101 char *q;
102
103 while (c && (q = str_getword(&p)) != 0) {
104 *v++ = q;
105 c--;
106 n++;
107 }
108
109 while (c) {
110 *v++ = 0;
111 c--;
112 }
113 return (n);
114}
115
116/* --- @str_sanitize@ --- *
117 *
118 * Arguments: @char *d@ = destination buffer
119 * @const char *p@ = pointer to source string
120 * @size_t sz@ = size of destination buffer
121 *
122 * Returns: ---
123 *
124 * Use: Writes a string into a buffer, being careful not to overflow
125 * the buffer, to null terminate the result, and to prevent
126 * nasty nonprintable characters ending up in the buffer.
127 */
128
129void str_sanitize(char *d, const char *p, size_t sz)
130{
131 if (!sz)
132 return;
133 sz--;
134 while (*p && sz) {
135 int ch = *p++;
136 if (!isgraph((unsigned char)ch))
137 ch = '_';
138 *d++ = ch;
139 sz--;
140 }
141 *d++ = 0;
142}
143
144/*----- That's all, folks -------------------------------------------------*/