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