-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileDict.cpp
More file actions
399 lines (315 loc) · 8.38 KB
/
FileDict.cpp
File metadata and controls
399 lines (315 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// File association tables. Taken from the FOX library and slightly modified.
#include "config.h"
#include "i18n.h"
#include <fx.h>
#include <FXPNGIcon.h>
#include "xfedefs.h"
#include "icons.h"
#include "xfeutils.h"
#include "FileDict.h"
#define COMMANDLEN 256
#define EXTENSIONLEN 128
#define MIMETYPELEN 64
#define ICONNAMELEN 256
// Object implementation
FXIMPLEMENT(IconDict, FXDict, NULL, 0)
// Default icon path
const char IconDict::defaultIconPath[] = DEFAULTICONPATH;
// Build icon table
IconDict::IconDict(FXApp* a, const FXString& p) : path(p)
{
app = a;
source = new FXIconSource(a);
}
// Search for the icon name along the search path, and try to load it
void* IconDict::createData(const void* ptr)
{
return(source->loadIconFile(FXPath::search(path, (const char*)ptr)));
}
// Delete the icon
void IconDict::deleteData(void* ptr)
{
delete ((FXIcon*)ptr);
}
// Save data
void IconDict::save(FXStream& store) const
{
FXDict::save(store);
store << source;
store << path;
}
// Load data
void IconDict::load(FXStream& store)
{
FXDict::load(store);
store >> source;
store >> path;
}
// Destructor
IconDict::~IconDict()
{
delete source;
source = (FXIconSource*)-1L;
clear();
}
// These registry keys are used for default bindings.
const char FileDict::defaultExecBinding[] = "defaultexecbinding";
const char FileDict::defaultDirBinding[] = "defaultdirbinding";
const char FileDict::defaultFileBinding[] = "defaultfilebinding";
// Object implementation
FXIMPLEMENT(FileDict, FXDict, NULL, 0)
// Construct an file-extension association table
FileDict::FileDict(FXApp* a) : app(a), settings(&a->reg())
{
icons = new IconDict(a, settings->readStringEntry("SETTINGS", "iconpath", DEFAULTICONPATH));
}
// Construct an file-extension association table, and alternative settings database
FileDict::FileDict(FXApp* a, FXSettings* db) : app(a), settings(db)
{
icons = new IconDict(a, settings->readStringEntry("SETTINGS", "iconpath", DEFAULTICONPATH));
}
// Create new association from extension
void* FileDict::createData(const void* ptr)
{
register const char* p = (const char*)ptr;
register char* q;
char command[COMMANDLEN];
char extension[EXTENSIONLEN];
char mimetype[MIMETYPELEN];
char bigname[ICONNAMELEN];
char bignameopen[ICONNAMELEN];
char mininame[ICONNAMELEN];
char mininameopen[ICONNAMELEN];
FileAssoc* fileassoc;
// Make association record
fileassoc = new FileAssoc;
// Parse command
for (q = command; *p && *p != ';' && q < command+COMMANDLEN-1; *q++ = *p++)
{
}
*q = '\0';
// Skip section separator
if (*p == ';')
{
p++;
}
// Parse extension type
for (q = extension; *p && *p != ';' && q < extension+EXTENSIONLEN-1; *q++ = *p++)
{
}
*q = '\0';
// Skip section separator
if (*p == ';')
{
p++;
}
// Parse big icon name
for (q = bigname; *p && *p != ';' && *p != ':' && q < bigname+ICONNAMELEN-1; *q++ = *p++)
{
}
*q = '\0';
// Skip icon separator
if (*p == ':')
{
p++;
}
// Parse big open icon name
for (q = bignameopen; *p && *p != ';' && q < bignameopen+ICONNAMELEN-1; *q++ = *p++)
{
}
*q = '\0';
// Skip section separator
if (*p == ';')
{
p++;
}
// Parse mini icon name
for (q = mininame; *p && *p != ';' && *p != ':' && q < mininame+ICONNAMELEN-1; *q++ = *p++)
{
}
*q = '\0';
// Skip icon separator
if (*p == ':')
{
p++;
}
// Parse mini open icon name
for (q = mininameopen; *p && *p != ';' && q < mininameopen+ICONNAMELEN-1; *q++ = *p++)
{
}
*q = '\0';
// Skip section separator
if (*p == ';')
{
p++;
}
// Parse mime type
for (q = mimetype; *p && *p != ';' && q < mimetype+MIMETYPELEN-1; *q++ = *p++)
{
}
*q = '\0';
// Initialize association data
fileassoc->command = command;
fileassoc->extension = extension;
fileassoc->bigicon = NULL;
fileassoc->miniicon = NULL;
fileassoc->bigiconopen = NULL;
fileassoc->miniiconopen = NULL;
fileassoc->mimetype = mimetype;
fileassoc->dragtype = 0;
fileassoc->flags = 0;
// Insert icons into icon dictionary
if (bigname[0])
{
fileassoc->bigicon = fileassoc->bigiconopen = icons->insert(bigname);
}
if (mininame[0])
{
fileassoc->miniicon = fileassoc->miniiconopen = icons->insert(mininame);
}
// Add open icons also; we will fall back on the regular icons in needed
if (bignameopen[0])
{
fileassoc->bigiconopen = icons->insert(bignameopen);
}
if (mininameopen[0])
{
fileassoc->miniiconopen = icons->insert(mininameopen);
}
// Return the binding
return(fileassoc);
}
// Delete association
void FileDict::deleteData(void* ptr)
{
delete ((FileAssoc*)ptr);
}
// Set icon search path
void FileDict::setIconPath(const FXString& path)
{
// Replace iconpath setting in registry
settings->writeStringEntry("SETTINGS", "iconpath", path.text());
// Change it in icon dictionary
icons->setIconPath(path);
}
// Return current icon search path
FXString FileDict::getIconPath() const
{
return(icons->getIconPath());
}
// Replace or add file association
FileAssoc* FileDict::replace(const char* ext, const char* str)
{
// Replace entry in registry
settings->writeStringEntry("FILETYPES", ext, str);
// Replace record
return((FileAssoc*)FXDict::replace(ext, str));
}
// Remove file association
FileAssoc* FileDict::remove(const char* ext)
{
// Delete registry entry for this type
settings->deleteEntry("FILETYPES", ext);
// Remove record
FXDict::remove(ext);
return(NULL);
}
// Find file association using the lower case file extension
FileAssoc* FileDict::associate(const char* key)
{
register const char* association;
register FileAssoc* record;
register char lowkey[MAXPATHLEN];
// Convert the association key to lower case
// Uses these functions because they seem to be faster than FXString
strlcpy(lowkey, key, strlen(key)+1);
strlow(lowkey);
// See if we have an existing record already and stores the key extension
if ((record = find(lowkey)) != NULL)
{
record->key = lowkey;
return(record);
}
// See if this entry is known in FILETYPES
association = settings->readStringEntry("FILETYPES", lowkey, "");
// If not an empty string, make a record for it now and stores the key extension
if (association[0])
{
record = (FileAssoc*)FXDict::insert(lowkey, association);
record->key = lowkey;
return(record);
}
// Not a known file type
return(NULL);
}
// Find file association from registry
FileAssoc* FileDict::findFileBinding(const char* pathname)
{
register const char* filename = pathname;
register const char* p = pathname;
register FileAssoc* record;
while (*p)
{
if (ISPATHSEP(*p))
{
filename = p+1;
}
p++;
}
if (strlen(filename) == 0)
{
return(associate(defaultFileBinding));
}
record = associate(filename);
if (record)
{
return(record);
}
filename = strchr(filename, '.');
while (filename)
{
if (strlen(filename) > 1)
{
record = associate(filename+1);
}
if (record)
{
return(record);
}
filename = strchr(filename+1, '.');
}
return(associate(defaultFileBinding));
}
// Find directory association from registry
FileAssoc* FileDict::findDirBinding(const char* pathname)
{
register const char* path = pathname;
register FileAssoc* record;
while (*path)
{
record = associate(path);
if (record)
{
return(record);
}
path++;
while (*path && !ISPATHSEP(*path))
{
path++;
}
}
return(associate(defaultDirBinding));
}
// Find executable association from registry
FileAssoc* FileDict::findExecBinding(const char* pathname)
{
return(associate(defaultExecBinding));
}
// Destructor
FileDict::~FileDict()
{
delete icons;
clear();
app = (FXApp*)-1;
icons = (IconDict*)-1;
}