1 /* Copyright (C) 2005-2012 by George Williams */
2 /*
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5 
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer.
8 
9  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12 
13  * The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <fontforge-config.h>
29 
30 #include "fontforgevw.h"
31 #include "groups.h"
32 #include "ustring.h"
33 #include "utype.h"
34 
35 #include <unistd.h>
36 
37 Group *group_root = NULL;
38 
GroupFree(Group * g)39 void GroupFree(Group *g) {
40     int i;
41 
42     if ( g==NULL )
43 return;
44 
45     free(g->name);
46     free(g->glyphs);
47     for ( i=0; i<g->kid_cnt; ++i )
48 	GroupFree(g->kids[i]);
49     free(g->kids);
50     chunkfree(g,sizeof(Group));
51 }
52 
GroupCopy(Group * g)53 Group *GroupCopy(Group *g) {
54     int i;
55     Group *gp;
56 
57     if ( g==NULL )
58 return( NULL );
59 
60     gp = chunkalloc(sizeof(Group));
61     gp->name = copy(g->name);
62     gp->glyphs = copy(g->glyphs);
63     if ( g->kid_cnt!=0 ) {
64 	gp->kids = malloc((gp->kid_cnt=g->kid_cnt)*sizeof(Group *));
65 	for ( i=0; i<g->kid_cnt; ++i ) {
66 	    gp->kids[i] = GroupCopy(g->kids[i]);
67 	    gp->kids[i]->parent = gp;
68 	}
69     }
70 return( gp );
71 }
72 
73 /******************************************************************************/
74 /***************************** File IO for Groups *****************************/
75 /******************************************************************************/
76 
77 /*  Returns the same string on each call, only allocating a new string
78     when called the first time.
79     May return NULL if user's config dir cannot be determined.
80 */
81 
getPfaEditGroups(void)82 static char *getPfaEditGroups(void) {
83     static char *groupname=NULL;
84     char buffer[1025];
85     char *userConfigDir;
86 
87     if ( groupname==NULL ) {
88         userConfigDir = getFontForgeUserDir(Config);
89         if ( userConfigDir!=NULL ) {
90             sprintf(buffer,"%s/groups", userConfigDir);
91             groupname = copy(buffer);
92             free(userConfigDir);
93         }
94     }
95     return( groupname );
96 }
97 
_SaveGroupList(FILE * file,Group * g,int indent)98 static void _SaveGroupList(FILE *file, Group *g, int indent) {
99     int i;
100 
101     for ( i=0; i<indent; ++i )
102 	putc(' ',file);
103     fprintf(file,"\"%s\": %d", g->name, g->unique );
104     if ( g->glyphs!=NULL && g->kid_cnt==0 )
105 	fprintf(file, " \"%s\"\n", g->glyphs );
106     else {
107 	putc('\n',file);
108 	for ( i=0; i<g->kid_cnt; ++i )
109 	    _SaveGroupList(file,g->kids[i], indent+1);
110     }
111 }
112 
SaveGroupList(void)113 void SaveGroupList(void) {
114     char *groupfilename;
115     FILE *groups;
116 
117     groupfilename = getPfaEditGroups();
118     if ( groupfilename==NULL )
119 return;
120     if ( group_root==NULL || (group_root->kid_cnt==0 && group_root->glyphs==NULL )) {
121 	unlink(groupfilename);
122 return;
123     }
124     groups = fopen(groupfilename,"w");
125     if ( groups==NULL )
126 return;
127     _SaveGroupList(groups,group_root,0);
128     fclose(groups);
129 }
130 
131 struct gcontext {
132     int found_indent;
133     int bmax;
134     char *buffer;
135     int lineno;
136 };
137 
countIndent(FILE * file)138 static int countIndent(FILE *file) {
139     int ch, cnt=0;
140 
141     while ( (ch=getc(file))==' ' )
142 	++cnt;
143     if ( cnt==0 && ch==EOF )
144 return( -1 );
145     ungetc(ch,file);
146 return( cnt );
147 }
148 
lineCountIndent(FILE * file,struct gcontext * gc)149 static int lineCountIndent(FILE *file, struct gcontext *gc) {
150     int ch;
151 
152     while ( (ch=getc(file))!=EOF && ch!='\n' && ch!='\r' );
153     if ( ch!=EOF )
154 	++gc->lineno;
155     if ( ch=='\r' ) {
156 	ch = getc(file);
157 	if ( ch!='\n' )
158 	    ungetc(ch,file);
159     }
160 return( gc->found_indent = countIndent(file));
161 }
162 
loadString(FILE * file,struct gcontext * gc)163 static char *loadString(FILE *file, struct gcontext *gc) {
164     int i, ch;
165 
166     ch = getc(file);
167     if ( ch!='"' ) {
168 	ungetc(ch,file);
169 return( NULL );
170     }
171     for ( i=0 ; (ch=getc(file))!=EOF && ch!='"' ; ++i ) {
172 	if ( i+1>=gc->bmax ) {
173 	    gc->bmax += 100;
174 	    gc->buffer = realloc(gc->buffer,gc->bmax);
175 	}
176 	gc->buffer[i] = ch;
177     }
178     if ( ch==EOF )
179 return( NULL );
180 
181     if ( i==0 )
182 return( copy(""));
183     gc->buffer[i] = '\0';
184 return( copy( gc->buffer ));
185 }
186 
_LoadGroupList(FILE * file,Group * parent,int expected_indent,struct gcontext * gc)187 static Group *_LoadGroupList(FILE *file, Group *parent, int expected_indent,
188 	struct gcontext *gc) {
189     Group *g;
190     char *n;
191     int i, ch;
192     Group **glist=NULL;
193     int gmax = 0;
194 
195     if ( expected_indent!=gc->found_indent )
196 return( NULL );
197 
198     n = loadString(file,gc);
199     if ( n==NULL )
200 return( NULL );
201     g = chunkalloc(sizeof(Group));
202     g->parent = parent;
203     g->name = n;
204     if ( (ch = getc(file))==':' )
205 	ch = getc(file);
206     while ( ch==' ' )
207 	ch = getc(file);
208     if ( ch=='1' )
209 	g->unique = true;
210     else if ( ch!='0' ) {
211 	GroupFree(g);
212 return( NULL );
213     }
214     while ( (ch = getc(file))==' ' );
215     if ( ch=='"' ) {
216 	ungetc(ch,file);
217 	g->glyphs = loadString(file,gc);
218 	if ( g->glyphs==NULL ) {
219 	    GroupFree(g);
220 return( NULL );
221 	}
222 	lineCountIndent(file,gc);
223     } else if ( ch=='\n' || ch=='\r' ) {
224 	ungetc(ch,file);
225 	lineCountIndent(file,gc);
226 	for ( i=0 ;; ++i ) {
227 	    if ( i>=gmax ) {
228 		gmax += 10;
229 		glist = realloc(glist,gmax*sizeof(Group *));
230 	    }
231 	    glist[i] = _LoadGroupList(file, g, expected_indent+1, gc);
232 	    if ( glist[i]==NULL )
233 	break;
234 	}
235 	g->kid_cnt = i;
236 	if ( i!=0 ) {
237 	    g->kids = malloc(i*sizeof(Group *));
238 	    memcpy(g->kids,glist,i*sizeof(Group *));
239 	    free(glist);
240 	}
241     }
242 return( g );
243 }
244 
LoadGroupList(void)245 void LoadGroupList(void) {
246     char *groupfilename;
247     FILE *groups;
248     struct gcontext gc;
249 
250     groupfilename = getPfaEditGroups();
251     if ( groupfilename==NULL )
252 return;
253     groups = fopen(groupfilename,"r");
254     if ( groups==NULL )
255 return;
256     GroupFree(group_root);
257     memset(&gc,0,sizeof(gc));
258     gc.found_indent = countIndent(groups);
259     group_root = _LoadGroupList(groups,NULL,0,&gc);
260     if ( !feof(groups))
261 	LogError( _("Unparsed characters found after end of groups file (last line parsed was %d).\n"), gc.lineno );
262     fclose(groups);
263 
264     free(gc.buffer);
265 }
266