dir.c revision 23a0898a
1/*
2 * Id: dir.c,v 1.2 1999/11/02 06:16:47 keithp Exp $
3 *
4 * Copyright 1999 SuSE, Inc.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of SuSE not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission.  SuSE makes no representations about the
13 * suitability of this software for any purpose.  It is provided "as is"
14 * without express or implied warranty.
15 *
16 * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 *
23 * Author:  Keith Packard, SuSE, Inc.
24 */
25/* $XFree86: xc/lib/font/builtins/dir.c,v 1.3 1999/12/30 02:29:49 robin Exp $ */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include "builtin.h"
31
32static BuiltinDirPtr
33BuiltinDirsDup (const BuiltinDirPtr a_dirs,
34                int a_dirs_len)
35{
36    BuiltinDirPtr dirs=NULL ;
37    int i=0 ;
38
39    if (!a_dirs)
40        return NULL ;
41
42    dirs = xcalloc (a_dirs_len, sizeof (BuiltinDirRec)) ;
43    if (!dirs)
44        return NULL ;
45
46    for (i=0; i < a_dirs_len; i++) {
47        int len = strlen (a_dirs[i].file_name) ;
48        dirs[i].file_name = xcalloc (1, len) ;
49        memmove (dirs[i].file_name, a_dirs[i].file_name, len);
50        len = strlen (a_dirs[i].font_name) ;
51        dirs[i].font_name = xcalloc (1, len) ;
52        memmove (dirs[i].font_name, a_dirs[i].font_name, len);
53    }
54    return dirs ;
55}
56
57/**
58 * Copy a_save back into a_cur
59 * @param a_cur the instance of BuiltinDir to restore
60 * @param a_saved the saved instance of BuiltinDir to copy into a_cur
61 * @return 0 if went okay, 1 otherwise.
62 */
63static int
64BuiltinDirRestore (BuiltinDirPtr a_cur,
65                   const BuiltinDirPtr a_saved)
66{
67    if (!a_cur)
68        return 1 ;
69    if (!a_saved)
70        return 0 ;
71
72    if (a_saved->font_name)
73        memmove (a_cur->font_name, a_saved->font_name, strlen (a_saved->font_name)) ;
74    return 0 ;
75}
76
77
78static int
79BuiltinDirsRestore (BuiltinDirPtr a_cur_tab,
80                    const BuiltinDirPtr a_saved_tab,
81                    int a_tab_len)
82{
83    int i=0 ;
84
85    if (!a_cur_tab)
86        return 1 ;
87    if (!a_saved_tab)
88        return 0 ;
89
90    for (i=0 ; i < a_tab_len; i++) {
91        if (BuiltinDirRestore (&a_cur_tab[i], &a_saved_tab[i]))
92            return 1 ;
93    }
94    return 0 ;
95}
96
97static BuiltinAliasPtr
98BuiltinAliasesDup (const BuiltinAliasPtr a_aliases,
99                   int a_aliases_len)
100{
101    BuiltinAliasPtr aliases=NULL ;
102    int i=0 ;
103
104    if (!a_aliases)
105        return NULL ;
106
107    aliases = xcalloc (a_aliases_len, sizeof (BuiltinAliasRec)) ;
108    if (!aliases)
109        return NULL ;
110
111    for (i=0; i < a_aliases_len; i++) {
112        int len = strlen (a_aliases[i].font_name) ;
113        aliases[i].font_name = xcalloc (1, len) ;
114        memmove (aliases[i].font_name, a_aliases[i].font_name, len);
115    }
116    return aliases ;
117}
118
119/**
120 * Copy a_save back into a_cur
121 * @param a_cur the instance of BuiltinAlias to restore
122 * @param a_saved the saved instance of BuiltinAlias to copy into a_cur
123 * @return 0 if went okay, 1 otherwise.
124 */
125static int
126BuiltinAliasRestore (BuiltinAliasPtr a_cur,
127                     const BuiltinAliasPtr a_save)
128{
129    if (!a_cur)
130        return 1 ;
131    if (!a_save)
132        return 0 ;
133    if (a_save->alias_name)
134        memmove (a_cur->alias_name, a_save->alias_name, strlen (a_save->alias_name)) ;
135    if (a_save->font_name)
136        memmove (a_cur->font_name, a_save->font_name, strlen (a_save->font_name)) ;
137    return 0 ;
138}
139
140static int
141BuiltinAliasesRestore (BuiltinAliasPtr a_cur_tab,
142                       const BuiltinAliasPtr a_saved_tab,
143                       int a_tab_len)
144{
145    int i=0 ;
146
147    if (!a_cur_tab)
148        return 1 ;
149    if (!a_saved_tab)
150        return 0 ;
151
152    for (i=0 ; i < a_tab_len; i++) {
153        if (BuiltinAliasRestore (&a_cur_tab[i], &a_saved_tab[i]))
154            return 1 ;
155    }
156    return 0 ;
157}
158
159int
160BuiltinReadDirectory (char *directory, FontDirectoryPtr *pdir)
161{
162    FontDirectoryPtr	dir;
163    int			i;
164
165    static BuiltinDirPtr saved_builtin_dir;
166    static BuiltinAliasPtr saved_builtin_alias;
167
168    dir = FontFileMakeDir ("", builtin_dir_count);
169
170    if (saved_builtin_dir)
171    {
172        BuiltinDirsRestore ((BuiltinDirPtr) builtin_dir,
173                            saved_builtin_dir,
174                            builtin_dir_count) ;
175    }
176    else
177    {
178        saved_builtin_dir = BuiltinDirsDup ((const BuiltinDirPtr) builtin_dir,
179                                            builtin_dir_count) ;
180    }
181
182    if (saved_builtin_alias)
183    {
184        BuiltinAliasesRestore ((BuiltinAliasPtr) builtin_alias,
185                               saved_builtin_alias,
186                               builtin_alias_count) ;
187    }
188    else
189    {
190        saved_builtin_alias = BuiltinAliasesDup ((const BuiltinAliasPtr) builtin_alias,
191                                                 builtin_alias_count) ;
192    }
193
194    for (i = 0; i < builtin_dir_count; i++)
195    {
196	if (!FontFileAddFontFile (dir,
197				  (char *) builtin_dir[i].font_name,
198				  (char *) builtin_dir[i].file_name))
199	{
200	    FontFileFreeDir (dir);
201	    return BadFontPath;
202	}
203    }
204    for (i = 0; i < builtin_alias_count; i++)
205    {
206	if (!FontFileAddFontAlias (dir,
207				   (char *) builtin_alias[i].alias_name,
208				   (char *) builtin_alias[i].font_name))
209	{
210	    FontFileFreeDir (dir);
211	    return BadFontPath;
212	}
213    }
214    FontFileSortDir (dir);
215    *pdir = dir;
216    return Successful;
217}
218