Debian Codegraph @

pTk/mTk/generic/tkFileFilter.c

raw symbols SCIP
1 /*
2 * tkFileFilter.c --
3 *
4 * Process the -filetypes option for the file dialogs on Windows and the
5 * Mac.
6 *
7 * Copyright (c) 1996 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkFileFilter.c,v 1.5 2002/01/27 11:10:50 das Exp $
13 */
14
15 #include "tkInt.h"
16 #include "tkFileFilter.h"
17
18 static int AddClause _ANSI_ARGS_((
19 Tcl_Interp * interp, FileFilter * filterPtr,
20 Tcl_Obj * patternsStr, Tcl_Obj * ostypesStr,
21 int isWindows));
22 static void FreeClauses _ANSI_ARGS_((FileFilter * filterPtr));
23 static void FreeGlobPatterns _ANSI_ARGS_((
24 FileFilterClause * clausePtr));
25 static void FreeMacFileTypes _ANSI_ARGS_((
26 FileFilterClause * clausePtr));
27 static FileFilter * GetFilter _ANSI_ARGS_((FileFilterList * flistPtr,
28 CONST char * name));
29
30 /*
31 *----------------------------------------------------------------------
32 *
33 * TkInitFileFilters --
34 *
35 * Initializes a FileFilterList data structure. A FileFilterList
36 * must be initialized EXACTLY ONCE before any calls to
37 * TkGetFileFilters() is made. The usual flow of control is:
38 * TkInitFileFilters(&flist);
39 * TkGetFileFilters(&flist, ...);
40 * TkGetFileFilters(&flist, ...);
41 * ...
42 * TkFreeFileFilters(&flist);
43 *
44 * Results:
45 * None.
46 *
47 * Side effects:
48 * The fields in flistPtr are initialized.
49 *----------------------------------------------------------------------
50 */
51
52 void
53 TkInitFileFilters(flistPtr)
54 FileFilterList * flistPtr; /* The structure to be initialized. */
55 {
56 flistPtr->filters = NULL;
57 flistPtr->filtersTail = NULL;
58 flistPtr->numFilters = 0;
59 }
60
61 /*
62 *----------------------------------------------------------------------
63 *
64 * TkGetFileFilters --
65 *
66 * This function is called by the Mac and Windows implementation
67 * of tk_getOpenFile and tk_getSaveFile to translate the string
68 * value of the -filetypes option of into an easy-to-parse C
69 * structure (flistPtr). The caller of this function will then use
70 * flistPtr to perform filetype matching in a platform specific way.
71 *
72 * flistPtr must be initialized (See comments in TkInitFileFilters).
73 *
74 * Results:
75 * A standard TCL return value.
76 *
77 * Side effects:
78 * The fields in flistPtr are changed according to string.
79 *----------------------------------------------------------------------
80 */
81 int
82 TkGetFileFilters(interp, flistPtr, arg, isWindows)
83 Tcl_Interp *interp; /* Interpreter to use for error reporting. */
84 FileFilterList * flistPtr; /* Stores the list of file filters. */
85 Tcl_Obj * arg; /* Value of the -filetypes option. */
86 int isWindows; /* True if we are running on Windows. */
87 {
88 int listArgc;
89 Tcl_Obj * * listArgv = NULL;
90 Tcl_Obj * * typeInfo = NULL;
91 int typeCount;
92 int code = TCL_OK;
93 int i;
94
95 if (arg == NULL) {
96 goto done;
97 }
98
99 if (Tcl_ListObjGetElements(interp, arg, &listArgc, &listArgv) != TCL_OK) {
100 return TCL_ERROR;
101 }
102 if (listArgc == 0) {
103 goto done;
104 }
105
106 /*
107 * Free the filter information that have been allocated the previous
108 * time -- the -filefilters option may have been used more than once in
109 * the command line.
110 */
111 TkFreeFileFilters(flistPtr);
112
113 for (i = 0; i<listArgc; i++) {
114 /*
115 * Each file type should have two or three elements: the first one
116 * is the name of the type and the second is the filter of the type.
117 * The third is the Mac OSType ID, but we don't care about them here.
118 */
119 FileFilter * filterPtr;
120
121 if (Tcl_ListObjGetElements(interp, listArgv[i], &typeCount, &typeInfo) != TCL_OK) {
122 code = TCL_ERROR;
123 goto done;
124 }
125
126 if (typeCount != 2 && typeCount != 3) {
127 Tcl_AppendResult(interp, "bad file type \"", Tcl_GetString(listArgv[i]), "\", ",
128 "should be \"typeName {extension ?extensions ...?} ",
129 "?{macType ?macTypes ...?}?\"", NULL);
130 code = TCL_ERROR;
131 goto done;
132 }
133
134 filterPtr = GetFilter(flistPtr, Tcl_GetString(typeInfo[0]));
135
136 if (typeCount == 2) {
137 code = AddClause(interp, filterPtr, typeInfo[1], NULL,
138 isWindows);
139 } else {
140 code = AddClause(interp, filterPtr, typeInfo[1], typeInfo[2],
141 isWindows);
142 }
143 if (code != TCL_OK) {
144 goto done;
145 }
146
147 typeInfo = NULL;
148 }
149
150 done:
151 return code;
152 }
153
154 /*
155 *----------------------------------------------------------------------
156 *
157 * TkFreeFileFilters --
158 *
159 * Frees the malloc'ed file filter information.
160 *
161 * Results:
162 * None.
163 *
164 * Side effects:
165 * The fields allocated by TkGetFileFilters() are freed.
166 *----------------------------------------------------------------------
167 */
168
169 void
170 TkFreeFileFilters(flistPtr)
171 FileFilterList * flistPtr; /* List of file filters to free */
172 {
173 FileFilter * filterPtr, *toFree;
174
175 filterPtr=flistPtr->filters;
176 while (filterPtr) {
177 toFree = filterPtr;
178 filterPtr=filterPtr->next;
179 FreeClauses(toFree);
180 ckfree((char*)toFree->name);
181 ckfree((char*)toFree);
182 }
183 flistPtr->filters = NULL;
184 }
185
186 /*
187 *----------------------------------------------------------------------
188 *
189 * AddClause --
190 *
191 * Add one FileFilterClause to filterPtr.
192 *
193 * Results:
194 * A standard TCL result.
195 *
196 * Side effects:
197 * The list of filter clauses are updated in filterPtr.
198 *----------------------------------------------------------------------
199 */
200
201 static int AddClause(interp, filterPtr, patternsStr, ostypesStr, isWindows)
202 Tcl_Interp * interp; /* Interpreter to use for error reporting. */
203 FileFilter * filterPtr; /* Stores the new filter clause */
204 Tcl_Obj * patternsStr; /* A TCL list of glob patterns. */
205 Tcl_Obj * ostypesStr; /* A TCL list of Mac OSType strings. */
206 int isWindows; /* True if we are running on Windows; False
207 * if we are running on the Mac; Glob
208 * patterns need to be processed differently
209 * on these two platforms */
210 {
211 Tcl_Obj **globList = NULL;
212 int globCount;
213 Tcl_Obj **ostypeList = NULL;
214 int ostypeCount;
215 FileFilterClause * clausePtr;
216 int i;
217 int code = TCL_OK;
218
219 if (Tcl_ListObjGetElements(interp, patternsStr, &globCount, &globList)!= TCL_OK) {
220 code = TCL_ERROR;
221 goto done;
222 }
223 if (ostypesStr != NULL) {
224 if (Tcl_ListObjGetElements(interp, ostypesStr, &ostypeCount, &ostypeList)
225 != TCL_OK) {
226 code = TCL_ERROR;
227 goto done;
228 }
229 for (i=0; i<ostypeCount; i++) {
230 if (strlen(Tcl_GetStringFromObj(ostypeList[i],NULL)) != 4) {
231 Tcl_AppendResult(interp, "bad Macintosh file type \"",
232 ostypeList[i], "\"", NULL);
233 code = TCL_ERROR;
234 goto done;
235 }
236 }
237 }
238
239 /*
240 * Add the clause into the list of clauses
241 */
242
243 clausePtr = (FileFilterClause*)ckalloc(sizeof(FileFilterClause));
244 clausePtr->patterns = NULL;
245 clausePtr->patternsTail = NULL;
246 clausePtr->macTypes = NULL;
247 clausePtr->macTypesTail = NULL;
248
249 if (filterPtr->clauses == NULL) {
250 filterPtr->clauses = filterPtr->clausesTail = clausePtr;
251 } else {
252 filterPtr->clausesTail->next = clausePtr;
253 filterPtr->clausesTail = clausePtr;
254 }
255 clausePtr->next = NULL;
256
257 if (globCount > 0 && globList != NULL) {
258 for (i=0; i<globCount; i++) {
259 GlobPattern * globPtr = (GlobPattern*)ckalloc(sizeof(GlobPattern));
260 int len;
261 char *globi = Tcl_GetStringFromObj(globList[i],NULL);
262
263 len = (strlen(globi) + 1) * sizeof(char);
264
265 if (globi[0] && globi[0] != '*') {
266 /*
267 * Prepend a "*" to patterns that do not have a leading "*"
268 */
269 globPtr->pattern = (char*)ckalloc((unsigned int) len+1);
270 globPtr->pattern[0] = '*';
271 strcpy(globPtr->pattern+1, globi);
272 }
273 else if (isWindows) {
274 if (strcmp(globi, "*") == 0) {
275 globPtr->pattern = (char*)ckalloc(4*sizeof(char));
276 strcpy(globPtr->pattern, "*.*");
277 }
278 else if (strcmp(globi, "") == 0) {
279 /*
280 * An empty string means "match all files with no
281 * extensions"
282 * BUG: "*." actually matches with all files on Win95
283 */
284 globPtr->pattern = (char*)ckalloc(3*sizeof(char));
285 strcpy(globPtr->pattern, "*.");
286 }
287 else {
288 globPtr->pattern = (char*)ckalloc((unsigned int) len);
289 strcpy(globPtr->pattern, globi);
290 }
291 } else {
292 globPtr->pattern = (char*)ckalloc((unsigned int) len);
293 strcpy(globPtr->pattern, globi);
294 }
295
296 /*
297 * Add the glob pattern into the list of patterns.
298 */
299
300 if (clausePtr->patterns == NULL) {
301 clausePtr->patterns = clausePtr->patternsTail = globPtr;
302 } else {
303 clausePtr->patternsTail->next = globPtr;
304 clausePtr->patternsTail = globPtr;
305 }
306 globPtr->next = NULL;
307 }
308 }
309 if (ostypeCount > 0 && ostypeList != NULL) {
310 for (i=0; i<ostypeCount; i++) {
311 MacFileType * mfPtr = (MacFileType*)ckalloc(sizeof(MacFileType));
312
313 memcpy(&mfPtr->type, Tcl_GetStringFromObj(ostypeList[i],NULL), sizeof(OSType));
314
315 /*
316 * Add the Mac type pattern into the list of Mac types
317 */
318 if (clausePtr->macTypes == NULL) {
319 clausePtr->macTypes = clausePtr->macTypesTail = mfPtr;
320 } else {
321 clausePtr->macTypesTail->next = mfPtr;
322 clausePtr->macTypesTail = mfPtr;
323 }
324 mfPtr->next = NULL;
325 }
326 }
327
328 done:
329
330 return code;
331 }
332
333 /*
334 *----------------------------------------------------------------------
335 *
336 * GetFilter --
337 *
338 * Add one FileFilter to flistPtr.
339 *
340 * Results:
341 * A standard TCL result.
342 *
343 * Side effects:
344 * The list of filters are updated in flistPtr.
345 *----------------------------------------------------------------------
346 */
347
348 static FileFilter * GetFilter(flistPtr, name)
349 FileFilterList * flistPtr; /* The FileFilterList that contains the
350 * newly created filter */
351 CONST char * name; /* Name of the filter. It is usually displayed
352 * in the "File Types" listbox in the file
353 * dialogs. */
354 {
355 FileFilter * filterPtr;
356
357 for (filterPtr=flistPtr->filters; filterPtr; filterPtr=filterPtr->next) {
358 if (strcmp(filterPtr->name, name)==0) {
359 return filterPtr;
360 }
361 }
362
363 filterPtr = (FileFilter*)ckalloc(sizeof(FileFilter));
364 filterPtr->clauses = NULL;
365 filterPtr->clausesTail = NULL;
366 filterPtr->name = (char*)ckalloc((strlen(name)+1) * sizeof(char));
367 strcpy(filterPtr->name, name);
368
369 if (flistPtr->filters == NULL) {
370 flistPtr->filters = flistPtr->filtersTail = filterPtr;
371 } else {
372 flistPtr->filtersTail->next = filterPtr;
373 flistPtr->filtersTail = filterPtr;
374 }
375 filterPtr->next = NULL;
376
377 ++flistPtr->numFilters;
378 return filterPtr;
379 }
380
381 /*
382 *----------------------------------------------------------------------
383 *
384 * FreeClauses --
385 *
386 * Frees the malloc'ed file type clause
387 *
388 * Results:
389 * None.
390 *
391 * Side effects:
392 * The list of clauses in filterPtr->clauses are freed.
393 *----------------------------------------------------------------------
394 */
395
396 static void
397 FreeClauses(filterPtr)
398 FileFilter * filterPtr; /* FileFilter whose clauses are to be freed */
399 {
400 FileFilterClause * clausePtr, * toFree;
401
402 clausePtr = filterPtr->clauses;
403 while (clausePtr) {
404 toFree = clausePtr;
405 clausePtr=clausePtr->next;
406 FreeGlobPatterns(toFree);
407 FreeMacFileTypes(toFree);
408 ckfree((char*)toFree);
409 }
410 filterPtr->clauses = NULL;
411 filterPtr->clausesTail = NULL;
412 }
413
414 /*
415 *----------------------------------------------------------------------
416 *
417 * FreeGlobPatterns --
418 *
419 * Frees the malloc'ed glob patterns in a clause
420 *
421 * Results:
422 * None.
423 *
424 * Side effects:
425 * The list of glob patterns in clausePtr->patterns are freed.
426 *----------------------------------------------------------------------
427 */
428
429 static void
430 FreeGlobPatterns(clausePtr)
431 FileFilterClause * clausePtr;/* The clause whose patterns are to be freed*/
432 {
433 GlobPattern * globPtr, * toFree;
434
435 globPtr = clausePtr->patterns;
436 while (globPtr) {
437 toFree = globPtr;
438 globPtr=globPtr->next;
439
440 ckfree((char*)toFree->pattern);
441 ckfree((char*)toFree);
442 }
443 clausePtr->patterns = NULL;
444 }
445
446 /*
447 *----------------------------------------------------------------------
448 *
449 * FreeMacFileTypes --
450 *
451 * Frees the malloc'ed Mac file types in a clause
452 *
453 * Results:
454 * None.
455 *
456 * Side effects:
457 * The list of Mac file types in clausePtr->macTypes are freed.
458 *----------------------------------------------------------------------
459 */
460
461 static void
462 FreeMacFileTypes(clausePtr)
463 FileFilterClause * clausePtr; /* The clause whose mac types are to be
464 * freed */
465 {
466 MacFileType * mfPtr, * toFree;
467
468 mfPtr = clausePtr->macTypes;
469 while (mfPtr) {
470 toFree = mfPtr;
471 mfPtr=mfPtr->next;
472 ckfree((char*)toFree);
473 }
474 clausePtr->macTypes = NULL;
475 }
476