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