home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_TextToStr.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  4KB  |  123 lines

  1. /* $XConsortium: TextToStr.c,v 1.5 94/04/17 20:21:19 rws Exp $ */
  2. /*
  3.  
  4. Copyright (c) 1989  X Consortium
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  19. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  
  23. Except as contained in this notice, the name of the X Consortium shall not be
  24. used in advertising or otherwise to promote the sale, use or other dealings
  25. in this Software without prior written authorization from the X Consortium.
  26.  
  27. */
  28.  
  29. /* $XFree86: xc/lib/X11/TextToStr.c,v 1.1.1.1.12.2 1998/05/18 14:08:39 dawes Exp $ */
  30.  
  31. #include "Xlib_private.h"
  32. #include <X11/Xatom.h>
  33. #include <X11/Xutil.h>
  34.  
  35.  
  36. /*
  37.  * XTextPropertyToStringList - set list and count to contain data stored in
  38.  * null-separated STRING property.
  39.  */
  40.  
  41. Status XTextPropertyToStringList (tp, list_return, count_return)
  42.     XTextProperty *tp;
  43.     char ***list_return;
  44.     int *count_return;
  45. {
  46.     DBUG_ENTER("XTextPropertyToStringList")
  47.     char **list;            /* return value */
  48.     int nelements;            /* return value */
  49.     register char *cp;            /* temp variable */
  50.     char *start;            /* start of thing to copy */
  51.     int i, j;                /* iterator variables */
  52.     int datalen = (int) tp->nitems;    /* for convenience */
  53.  
  54.     /*
  55.      * make sure we understand how to do it
  56.      */
  57.     if (tp->encoding != XA_STRING ||  tp->format != 8) DBUG_RETURN(False);
  58.  
  59.     if (datalen == 0) {
  60.     *list_return = NULL;
  61.     *count_return = 0;
  62.     DBUG_RETURN(True);
  63.     }
  64.  
  65.     /*
  66.      * walk the list to figure out how many elements there are
  67.      */
  68.     nelements = 1;            /* since null-separated */
  69.     for (cp = (char *) tp->value, i = datalen; i > 0; cp++, i--) {
  70.     if (*cp == '\0') nelements++;
  71.     }
  72.  
  73.     /*
  74.      * allocate list and duplicate
  75.      */
  76.     list = (char **) Xmalloc (nelements * sizeof (char *));
  77.     if (!list) DBUG_RETURN(False);
  78.     
  79.     start = (char *) Xmalloc ((datalen + 1) * sizeof (char));    /* for <NUL> */
  80.     if (!start) {
  81.     Xfree ((char *) list);
  82.     DBUG_RETURN(False);
  83.     }
  84.  
  85.     /*
  86.      * copy data
  87.      */
  88.     memcpy (start, (char *) tp->value, tp->nitems);
  89.     start[datalen] = '\0';
  90.  
  91.     /*
  92.      * walk down list setting value
  93.      */
  94.     for (cp = start, i = datalen + 1, j = 0; i > 0; cp++, i--) {
  95.     if (*cp == '\0') {
  96.         list[j] = start;
  97.         start = (cp + 1);
  98.         j++;
  99.     }
  100.     }
  101.  
  102.     /*
  103.      * append final null pointer and then return data
  104.      */
  105.     *list_return = list;
  106.     *count_return = nelements;
  107.     DBUG_RETURN(True);
  108. }
  109.  
  110.  
  111. void XFreeStringList (list)
  112.     char **list;
  113. {
  114.     DBUG_ENTER("XFreeStringList")
  115.     if (list) {
  116.     if (list[0]) Xfree (list[0]);
  117.     Xfree ((char *) list);
  118.     list = NULL;
  119.     }
  120.     DBUG_VOID_RETURN;
  121. }
  122.  
  123.