Home | History | Annotate | Line # | Download | only in util
      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 "libxfontint.h"
     38 #include "src/util/replace.h"
     39 #include	<X11/fonts/fontmisc.h>
     40 #include	<X11/fonts/fontstruct.h>
     41 
     42 void
     43 xfont2_free_font_names(FontNamesPtr pFN)
     44 {
     45     int         i;
     46 
     47     if (!pFN)
     48 	return;
     49     for (i = 0; i < pFN->nnames; i++) {
     50 	free(pFN->names[i]);
     51     }
     52     free(pFN->names);
     53     free(pFN->length);
     54     free(pFN);
     55 }
     56 
     57 FontNamesPtr
     58 xfont2_make_font_names_record(unsigned size)
     59 {
     60     FontNamesPtr pFN;
     61 
     62     pFN = malloc(sizeof(FontNamesRec));
     63     if (pFN) {
     64 	pFN->nnames = 0;
     65 	pFN->size = size;
     66 	if (size)
     67 	{
     68 	    pFN->length = mallocarray(size, sizeof(int));
     69 	    pFN->names = mallocarray(size, sizeof(char *));
     70 	    if (!pFN->length || !pFN->names) {
     71 	    	free(pFN->length);
     72 	    	free(pFN->names);
     73 	    	free(pFN);
     74 	    	pFN = (FontNamesPtr) 0;
     75 	    }
     76 	}
     77 	else
     78 	{
     79 	    pFN->length = 0;
     80 	    pFN->names = 0;
     81 	}
     82     }
     83     return pFN;
     84 }
     85 
     86 int
     87 xfont2_add_font_names_name(FontNamesPtr names,
     88 			   char *name,
     89 			   int length)
     90 {
     91     int         index = names->nnames;
     92     char       *nelt;
     93 
     94     nelt = malloc(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 = reallocarray(names->length, size, sizeof(int));
    105 	nnames = reallocarray(names->names, size, sizeof(char *));
    106 	if (nlength && nnames) {
    107 	    names->size = size;
    108 	    names->length = nlength;
    109 	    names->names = nnames;
    110 	} else {
    111 	    free(nelt);
    112 	    free(nlength);
    113 	    free(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