fontnames.c revision 23a0898a
1/* $Xorg: fontnames.c,v 1.4 2001/02/09 02:04:04 xorgcvs Exp $ */
2
3/*
4
5Copyright 1991, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included
14in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
20OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22OTHER DEALINGS IN THE SOFTWARE.
23
24Except as contained in this notice, the name of The Open Group shall
25not be used in advertising or otherwise to promote the sale, use or
26other dealings in this Software without prior written authorization
27from The Open Group.
28
29*/
30/* $XFree86: xc/lib/font/util/fontnames.c,v 1.4 2001/01/17 19:43:33 dawes Exp $ */
31
32/*
33 * Author:  Keith Packard, MIT X Consortium
34 *
35 *	@(#)fontnames.c	3.1	91/04/10
36 */
37
38#ifdef HAVE_CONFIG_H
39#include <config.h>
40#endif
41#include	<X11/fonts/fontmisc.h>
42#include	<X11/fonts/fontstruct.h>
43
44void
45FreeFontNames(FontNamesPtr pFN)
46{
47    int         i;
48
49    if (!pFN)
50	return;
51    for (i = 0; i < pFN->nnames; i++) {
52	xfree(pFN->names[i]);
53    }
54    xfree(pFN->names);
55    xfree(pFN->length);
56    xfree(pFN);
57}
58
59FontNamesPtr
60MakeFontNamesRecord(unsigned int size)
61{
62    FontNamesPtr pFN;
63
64    pFN = (FontNamesPtr) xalloc(sizeof(FontNamesRec));
65    if (pFN) {
66	pFN->nnames = 0;
67	pFN->size = size;
68	if (size)
69	{
70	    pFN->length = (int *) xalloc(size * sizeof(int));
71	    pFN->names = (char **) xalloc(size * sizeof(char *));
72	    if (!pFN->length || !pFN->names) {
73	    	xfree(pFN->length);
74	    	xfree(pFN->names);
75	    	xfree(pFN);
76	    	pFN = (FontNamesPtr) 0;
77	    }
78	}
79	else
80	{
81	    pFN->length = 0;
82	    pFN->names = 0;
83	}
84    }
85    return pFN;
86}
87
88int
89AddFontNamesName(FontNamesPtr names, char *name, int length)
90{
91    int         index = names->nnames;
92    char       *nelt;
93
94    nelt = (char *) xalloc(length + 1);
95    if (!nelt)
96	return AllocError;
97    if (index >= names->size) {
98	int         size = names->size << 1;
99	int        *nlength;
100	char      **nnames;
101
102	if (size == 0)
103	    size = 8;
104	nlength = (int *) xrealloc(names->length, size * sizeof(int));
105	nnames = (char **) xrealloc(names->names, size * sizeof(char *));
106	if (nlength && nnames) {
107	    names->size = size;
108	    names->length = nlength;
109	    names->names = nnames;
110	} else {
111	    xfree(nelt);
112	    xfree(nlength);
113	    xfree(nnames);
114	    return AllocError;
115	}
116    }
117    names->length[index] = length;
118    names->names[index] = nelt;
119    strncpy(nelt, name, length);
120    nelt[length] = '\0';
121    names->nnames++;
122    return Successful;
123}
124