StrToText.c revision 61b2299d
1/* $Xorg: StrToText.c,v 1.4 2001/02/09 02:03:37 xorgcvs Exp $ */
2/*
3
4Copyright 1989, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26*/
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <X11/Xlibint.h>
32#include <X11/Xatom.h>
33#include <X11/Xutil.h>
34
35/*
36 * XStringListToTextProperty - fill in TextProperty structure with
37 * concatenated list of null-separated strings.  Return True if successful
38 * else False.  Allocate room on end for trailing NULL, but don't include in
39 * count.
40 */
41
42Status XStringListToTextProperty (
43    char **argv,
44    int argc,
45    XTextProperty *textprop)
46{
47    register int i;
48    register unsigned int nbytes;
49    XTextProperty proto;
50
51    /* figure out how much space we'll need for this list */
52    for (i = 0, nbytes = 0; i < argc; i++) {
53	nbytes += (unsigned) ((argv[i] ? strlen (argv[i]) : 0) + 1);
54    }
55
56    /* fill in a prototype containing results so far */
57    proto.encoding = XA_STRING;
58    proto.format = 8;
59    if (nbytes)
60	proto.nitems = nbytes - 1;	/* subtract one for trailing <NUL> */
61    else
62	proto.nitems = 0;
63    proto.value = NULL;
64
65    /* build concatenated list of strings */
66    if (nbytes > 0) {
67	register char *buf = Xmalloc (nbytes);
68	if (!buf) return False;
69
70	proto.value = (unsigned char *) buf;
71	for (i = 0; i < argc; i++) {
72	    char *arg = argv[i];
73
74	    if (arg) {
75		(void) strcpy (buf, arg);
76		buf += (strlen (arg) + 1);
77	    } else {
78		*buf++ = '\0';
79	    }
80	}
81    } else {
82	proto.value = (unsigned char *) Xmalloc (1);	/* easier for client */
83	if (!proto.value) return False;
84
85	proto.value[0] = '\0';
86    }
87
88    /* we were successful, so set return value */
89    *textprop = proto;
90    return True;
91}
92