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 "libxfontint.h"
28#include "builtin.h"
29
30static BuiltinDirPtr
31BuiltinDirsDup (const BuiltinDirPtr a_dirs,
32                int a_dirs_len)
33{
34    BuiltinDirPtr dirs=NULL ;
35    int i=0 ;
36
37    if (!a_dirs)
38        return NULL ;
39
40    dirs = calloc (a_dirs_len, sizeof (BuiltinDirRec)) ;
41    if (!dirs)
42        return NULL ;
43
44    for (i=0; i < a_dirs_len; i++) {
45	dirs[i].file_name = strdup(a_dirs[i].file_name);
46	dirs[i].font_name = strdup(a_dirs[i].font_name);
47    }
48    return dirs ;
49}
50
51/**
52 * Copy a_save back into a_cur
53 * @param a_cur the instance of BuiltinDir to restore
54 * @param a_saved the saved instance of BuiltinDir to copy into a_cur
55 * @return 0 if went okay, 1 otherwise.
56 */
57static int
58BuiltinDirRestore (BuiltinDirPtr a_cur,
59                   const BuiltinDirPtr a_saved)
60{
61    if (!a_cur)
62        return 1 ;
63    if (!a_saved)
64        return 0 ;
65
66    if (a_saved->font_name)
67        memmove (a_cur->font_name, a_saved->font_name, strlen (a_saved->font_name)) ;
68    return 0 ;
69}
70
71
72static int
73BuiltinDirsRestore (BuiltinDirPtr a_cur_tab,
74                    const BuiltinDirPtr a_saved_tab,
75                    int a_tab_len)
76{
77    int i=0 ;
78
79    if (!a_cur_tab)
80        return 1 ;
81    if (!a_saved_tab)
82        return 0 ;
83
84    for (i=0 ; i < a_tab_len; i++) {
85        if (BuiltinDirRestore (&a_cur_tab[i], &a_saved_tab[i]))
86            return 1 ;
87    }
88    return 0 ;
89}
90
91static BuiltinAliasPtr
92BuiltinAliasesDup (const BuiltinAliasPtr a_aliases,
93                   int a_aliases_len)
94{
95    BuiltinAliasPtr aliases=NULL ;
96    int i=0 ;
97
98    if (!a_aliases)
99        return NULL ;
100
101    aliases = calloc (a_aliases_len, sizeof (BuiltinAliasRec)) ;
102    if (!aliases)
103        return NULL ;
104
105    for (i=0; i < a_aliases_len; i++) {
106	aliases[i].font_name = strdup(a_aliases[i].font_name);
107    }
108    return aliases ;
109}
110
111/**
112 * Copy a_save back into a_cur
113 * @param a_cur the instance of BuiltinAlias to restore
114 * @param a_saved the saved instance of BuiltinAlias to copy into a_cur
115 * @return 0 if went okay, 1 otherwise.
116 */
117static int
118BuiltinAliasRestore (BuiltinAliasPtr a_cur,
119                     const BuiltinAliasPtr a_save)
120{
121    if (!a_cur)
122        return 1 ;
123    if (!a_save)
124        return 0 ;
125    if (a_save->alias_name)
126        memmove (a_cur->alias_name, a_save->alias_name, strlen (a_save->alias_name)) ;
127    if (a_save->font_name)
128        memmove (a_cur->font_name, a_save->font_name, strlen (a_save->font_name)) ;
129    return 0 ;
130}
131
132static int
133BuiltinAliasesRestore (BuiltinAliasPtr a_cur_tab,
134                       const BuiltinAliasPtr a_saved_tab,
135                       int a_tab_len)
136{
137    int i=0 ;
138
139    if (!a_cur_tab)
140        return 1 ;
141    if (!a_saved_tab)
142        return 0 ;
143
144    for (i=0 ; i < a_tab_len; i++) {
145        if (BuiltinAliasRestore (&a_cur_tab[i], &a_saved_tab[i]))
146            return 1 ;
147    }
148    return 0 ;
149}
150
151int
152BuiltinReadDirectory (const char *directory, FontDirectoryPtr *pdir)
153{
154    FontDirectoryPtr	dir;
155    int			i;
156
157    static BuiltinDirPtr saved_builtin_dir;
158    static BuiltinAliasPtr saved_builtin_alias;
159
160    dir = FontFileMakeDir ("", builtin_dir_count);
161
162    if (saved_builtin_dir)
163    {
164        BuiltinDirsRestore ((BuiltinDirPtr) builtin_dir,
165                            saved_builtin_dir,
166                            builtin_dir_count) ;
167    }
168    else
169    {
170        saved_builtin_dir = BuiltinDirsDup ((const BuiltinDirPtr) builtin_dir,
171                                            builtin_dir_count) ;
172    }
173
174    if (saved_builtin_alias)
175    {
176        BuiltinAliasesRestore ((BuiltinAliasPtr) builtin_alias,
177                               saved_builtin_alias,
178                               builtin_alias_count) ;
179    }
180    else
181    {
182        saved_builtin_alias = BuiltinAliasesDup ((const BuiltinAliasPtr) builtin_alias,
183                                                 builtin_alias_count) ;
184    }
185
186    for (i = 0; i < builtin_dir_count; i++)
187    {
188	if (!FontFileAddFontFile (dir,
189				  (char *) builtin_dir[i].font_name,
190				  (char *) builtin_dir[i].file_name))
191	{
192	    FontFileFreeDir (dir);
193	    return BadFontPath;
194	}
195    }
196    for (i = 0; i < builtin_alias_count; i++)
197    {
198	if (!FontFileAddFontAlias (dir,
199				   (char *) builtin_alias[i].alias_name,
200				   (char *) builtin_alias[i].font_name))
201	{
202	    FontFileFreeDir (dir);
203	    return BadFontPath;
204	}
205    }
206    FontFileSortDir (dir);
207    *pdir = dir;
208    return Successful;
209}
210