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