File: strsplit.3

package info (click to toggle)
publib 0.40-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: ansic: 6,708; sh: 395; makefile: 369
file content (113 lines) | stat: -rw-r--r-- 4,555 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
.\"   Part of publib.
.\"  
.\"   Copyright (c) 1994-2006 Lars Wirzenius.  All rights reserved.
.\"  
.\"   Redistribution and use in source and binary forms, with or without
.\"   modification, are permitted provided that the following conditions
.\"   are met:
.\"  
.\"   1. Redistributions of source code must retain the above copyright
.\"      notice, this list of conditions and the following disclaimer.
.\"  
.\"   2. Redistributions in binary form must reproduce the above
.\"      copyright notice, this list of conditions and the following
.\"      disclaimer in the documentation and/or other materials provided
.\"      with the distribution.
.\"  
.\"   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
.\"   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
.\"   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\"   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
.\"   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\"   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
.\"   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\"   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\"   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
.\"   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
.\"   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"  
.\" part of publib
.\" "@(#)publib-strutil:$Id: strsplit.3,v 1.2 1994/02/19 20:58:36 liw Exp $"
.\"
.TH STRSPLIT 3 "C Programmer's Manual" Publib "C Programmer's Manual"
.SH NAME
strsplit \- split string into words
.SH SYNOPSIS
.nf
#include <publib.h>
int \fBstrsplit\fR(char *\fIsrc\fR, char **\fIwords\fR, int \fImaxw\fR, const char *\fIsep\fR);
.SH DESCRIPTION
\fIstrsplit\fR splits the \fIsrc\fR string into words separated by one
or more of the characters in \fIsep\fR (or by whitespace characters, as
specified by \fIisspace\fR(3), if \fIsep\fR is the empty string).
Pointers to the words are stored in successive elements in the array
pointed to by \fIwords\fR.  No more than \fImaxw\fR pointers are stored.
The input string is modifed by replacing the separator character
following a word with '\\0'.  However, if there are more than \fImaxw\fR
words, only \fImaxw\fR-1 words will be returned, and the \fImaxw\fRth 
pointer in the array will point to the rest of the string.  If
\fImaxw\fR is 0, no modification is done.  This can be used for counting
how many words there are, e.g., so that space for the word pointer table
can be allocated dynamically.
.PP
strsplit splits the src string into words separated by one or more
of the characters in sep (or by whitespace characters, as defined by
isspace(3), if sep is the empty string).  The src string is modified
by replacing the separator character after each word with '\\0'.  A
pointer to each word is stored into successive elements of the
array words.  If there are more than maxw words, a '\\0' is stored
after the first maxw-1 words only, and the words[maxw-1] will contain
a pointer to the rest of the string after the word in words[maxw-2].

.SH "RETURN VALUE"
\fIstrsplit\fR returns the total number of words in the input string.
.SH EXAMPLE
Assuming that words are separated by white space, to count the number
of words on a line, one might say the following.
.sp 1
.nf
.in +5
n = strsplit(line, NULL, 0, "");
.in -5
.PP
To print out the fields of a colon-separated list (such as PATH, or a
line from /etc/passwd or /etc/group), one might do the following.
.sp 1
.nf
.in +5
char *fields[15];
int i, n;

n = strsplit(list, fields, 15, ":");
if (n > 15)
	n = 15;
for (i = 0; i < n; ++i)
	printf("field %d: %s\\n", i, fields[i]);
.in -5
.PP
In real life, one would of course prefer to not restrict the number of
fields, so one might either allocated the pointer table dynamically
(first counting the number of words using something like the first
example), or realize that since it is the original string that is
being modified, one can do the following:
.sp 1
.nf
.in +5
char *fields[15];
int i, n;

do {
	n = strsplit(list, fields, 15, ":");
	if (n > 15)
		n = 15;
	for (i = 0; i < n; ++i)
		printf("field %d: %s\\n", i, fields[i]);
	list = field[n-1] + strlen(field[n-1]);
} while (n == 15);
.in -5
.SH "SEE ALSO"
publib(3), strtok(3)
.SH AUTHOR
The idea for this function came from C-News source code by Henry Spencer
and Geoff Collyer.  Their function is very similar, but this
implementation is by Lars Wirzenius (lars.wirzenius@helsinki.fi)