FontNames.c revision 61b2299d
1/* $Xorg: FontNames.c,v 1.4 2001/02/09 02:03:33 xorgcvs Exp $ */
2/*
3
4Copyright 1986, 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/* $XFree86: xc/lib/X11/FontNames.c,v 1.6 2001/12/14 19:54:00 dawes Exp $ */
29
30#define NEED_REPLIES
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34#include "Xlibint.h"
35
36char **
37XListFonts(
38register Display *dpy,
39_Xconst char *pattern,  /* null-terminated */
40int maxNames,
41int *actualCount)	/* RETURN */
42{
43    register long nbytes;
44    register unsigned i;
45    register int length;
46    char **flist;
47    char *ch;
48    xListFontsReply rep;
49    register xListFontsReq *req;
50    register long rlen;
51
52    LockDisplay(dpy);
53    GetReq(ListFonts, req);
54    req->maxNames = maxNames;
55    nbytes = req->nbytes = pattern ? strlen (pattern) : 0;
56    req->length += (nbytes + 3) >> 2;
57    _XSend (dpy, pattern, nbytes);
58    /* use _XSend instead of Data, since following _XReply will flush buffer */
59
60    if (!_XReply (dpy, (xReply *)&rep, 0, xFalse)) {
61	*actualCount = 0;
62	UnlockDisplay(dpy);
63	SyncHandle();
64	return (char **) NULL;
65    }
66
67    if (rep.nFonts) {
68	flist = (char **)Xmalloc ((unsigned)rep.nFonts * sizeof(char *));
69	rlen = rep.length << 2;
70	ch = (char *) Xmalloc((unsigned) (rlen + 1));
71	    /* +1 to leave room for last null-terminator */
72
73	if ((! flist) || (! ch)) {
74	    if (flist) Xfree((char *) flist);
75	    if (ch) Xfree(ch);
76	    _XEatData(dpy, (unsigned long) rlen);
77	    *actualCount = 0;
78	    UnlockDisplay(dpy);
79	    SyncHandle();
80	    return (char **) NULL;
81	}
82
83	_XReadPad (dpy, ch, rlen);
84	/*
85	 * unpack into null terminated strings.
86	 */
87	length = *(unsigned char *)ch;
88	*ch = 1; /* make sure it is non-zero for XFreeFontNames */
89	for (i = 0; i < rep.nFonts; i++) {
90	    flist[i] = ch + 1;  /* skip over length */
91	    ch += length + 1;  /* find next length ... */
92	    length = *(unsigned char *)ch;
93	    *ch = '\0';  /* and replace with null-termination */
94	}
95    }
96    else flist = (char **) NULL;
97    *actualCount = rep.nFonts;
98    UnlockDisplay(dpy);
99    SyncHandle();
100    return (flist);
101}
102
103int
104XFreeFontNames(char **list)
105{
106	if (list) {
107		if (!*(list[0]-1)) { /* from ListFontsWithInfo */
108			register char **names;
109			for (names = list+1; *names; names++)
110				Xfree (*names);
111		}
112		Xfree (list[0]-1);
113		Xfree ((char *)list);
114	}
115	return 1;
116}
117