11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1989, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included in
121ab64890Smrgall copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
171ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201ab64890Smrg
211ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
221ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
231ab64890Smrgin this Software without prior written authorization from The Open Group.
241ab64890Smrg
251ab64890Smrg*/
261ab64890Smrg
271ab64890Smrg
281ab64890Smrg#ifdef HAVE_CONFIG_H
291ab64890Smrg#include <config.h>
301ab64890Smrg#endif
311ab64890Smrg#include <X11/Xlibint.h>
321ab64890Smrg#include <X11/Xatom.h>
331ab64890Smrg#include <X11/Xutil.h>
34258a0ebeSmrg#include "reallocarray.h"
351ab64890Smrg
361ab64890Smrg/*
371ab64890Smrg * XTextPropertyToStringList - set list and count to contain data stored in
381ab64890Smrg * null-separated STRING property.
391ab64890Smrg */
401ab64890Smrg
411ab64890SmrgStatus XTextPropertyToStringList (
421ab64890Smrg    XTextProperty *tp,
431ab64890Smrg    char ***list_return,
441ab64890Smrg    int *count_return)
451ab64890Smrg{
461ab64890Smrg    char **list;			/* return value */
471ab64890Smrg    int nelements;			/* return value */
481ab64890Smrg    register char *cp;			/* temp variable */
491ab64890Smrg    char *start;			/* start of thing to copy */
501ab64890Smrg    int i, j;				/* iterator variables */
511ab64890Smrg    int datalen = (int) tp->nitems;	/* for convenience */
521ab64890Smrg
531ab64890Smrg    /*
541ab64890Smrg     * make sure we understand how to do it
551ab64890Smrg     */
561ab64890Smrg    if (tp->encoding != XA_STRING ||  tp->format != 8) return False;
571ab64890Smrg
581ab64890Smrg    if (datalen == 0) {
591ab64890Smrg	*list_return = NULL;
601ab64890Smrg	*count_return = 0;
611ab64890Smrg	return True;
621ab64890Smrg    }
631ab64890Smrg
641ab64890Smrg    /*
651ab64890Smrg     * walk the list to figure out how many elements there are
661ab64890Smrg     */
671ab64890Smrg    nelements = 1;			/* since null-separated */
681ab64890Smrg    for (cp = (char *) tp->value, i = datalen; i > 0; cp++, i--) {
691ab64890Smrg	if (*cp == '\0') nelements++;
701ab64890Smrg    }
711ab64890Smrg
721ab64890Smrg    /*
731ab64890Smrg     * allocate list and duplicate
741ab64890Smrg     */
75258a0ebeSmrg    list = Xmallocarray (nelements, sizeof (char *));
761ab64890Smrg    if (!list) return False;
7761b2299dSmrg
78eb411b4bSmrg    start = Xmalloc ((datalen + 1) * sizeof (char));	/* for <NUL> */
791ab64890Smrg    if (!start) {
80818534a1Smrg	Xfree (list);
811ab64890Smrg	return False;
821ab64890Smrg    }
831ab64890Smrg
841ab64890Smrg    /*
851ab64890Smrg     * copy data
861ab64890Smrg     */
871ab64890Smrg    memcpy (start, (char *) tp->value, tp->nitems);
881ab64890Smrg    start[datalen] = '\0';
891ab64890Smrg
901ab64890Smrg    /*
911ab64890Smrg     * walk down list setting value
921ab64890Smrg     */
931ab64890Smrg    for (cp = start, i = datalen + 1, j = 0; i > 0; cp++, i--) {
941ab64890Smrg	if (*cp == '\0') {
951ab64890Smrg	    list[j] = start;
961ab64890Smrg	    start = (cp + 1);
971ab64890Smrg	    j++;
981ab64890Smrg	}
991ab64890Smrg    }
1001ab64890Smrg
1011ab64890Smrg    /*
1021ab64890Smrg     * append final null pointer and then return data
1031ab64890Smrg     */
1041ab64890Smrg    *list_return = list;
1051ab64890Smrg    *count_return = nelements;
1061ab64890Smrg    return True;
1071ab64890Smrg}
1081ab64890Smrg
1091ab64890Smrg
1101ab64890Smrgvoid XFreeStringList (char **list)
1111ab64890Smrg{
1121ab64890Smrg    if (list) {
1131ab64890Smrg	if (list[0]) Xfree (list[0]);
114818534a1Smrg	Xfree (list);
1151ab64890Smrg	list = NULL;
1161ab64890Smrg    }
1171ab64890Smrg}
1181ab64890Smrg
119