forked from qunying/rhide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ideutil.cc
409 lines (373 loc) · 8.54 KB
/
ideutil.cc
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
400
401
402
403
404
405
406
407
/* Copyright (C) 1996-2000 Robert H”hne, see COPYING.RH for details */
/* This file is part of RHIDE. */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define Uses_TNSSortedCollection
#define Uses_ifpstream
#define Uses_fpstream
#define Uses_TDirList
#define Uses_tvutilFunctions
#include <libtvuti.h>
#include <rhutils.h>
#define Uses_TProject
#define Uses_TOptions
#define Uses_ideFunctions
#define Uses_TDepCollection
#include <libide.h>
#include "rhide.h"
int debug_dependencies = 0;
int debug_commands = 0;
int debug_tempfiles = 0;
int debug_files = 0;
int c_words_changed = 1;
int gpc_words_changed = 1;
int fpc_words_changed = 1;
int user_words_changed = 1;
char *RHIDE_DIR = NULL;
char *RHIDE_NAME = NULL;
char *RHIDE_EXT = NULL;
int
FindFile(char *name, char *&full_name)
{
return (Boolean) FindFile((const char *) name, full_name) == True ? 1 : 0;
}
Boolean
FindFile(const char *name, char *&full_name)
{
FILE_TYPE t;
Boolean retval = False;
if (!name || !*name)
{
string_dup(full_name, "");
return False;
}
if (debug_files)
{
fprintf(stderr, _("Search for file '%s'\n"), name);
}
if (strchr(name, '/') != NULL && __file_exists(name))
{
string_dup(full_name, name);
FExpand(full_name);
retval = True;
}
else
{
t = get_file_type(name);
switch (t)
{
case FILE_C_SOURCE:
case FILE_C_SOURCE_I:
case FILE_CC_SOURCE:
case FILE_CC_SOURCE_II:
case FILE_ASM_SOURCE:
case FILE_FLEX_SOURCE:
case FILE_BISON_SOURCE:
case FILE_OBJC_SOURCE:
case FILE_PASCAL_SOURCE:
case FILE_FPC_SOURCE:
case FILE_FORTRAN_SOURCE:
case FILE_FORTRAN_SOURCE_PRE:
case FILE_ADA_SOURCE:
case FILE_ADA_SPEC:
retval = FindFile(name, Options.SrcDirs, full_name, False);
break;
case FILE_LIBRARY:
case FILE_DLL:
retval = FindFile(name, Options.library_path, full_name, False);
break;
case FILE_OBJECT:
retval = FindFile(name, Options.ObjDirs, full_name);
break;
case FILE_HEADER:
retval = FindFile(name, Options.include_path, full_name, False);
break;
case FILE_EXE:
/*
EXE files are searched only in the current directory.
Maybe I add a separate option for that in the future.
*/
string_dup(full_name, name);
FExpand(full_name);
retval = __file_exists(full_name);
break;
default:
/* What is with files, which have no known source suffixes?
I think this is the better way to find it. As an example
I have in the doc directory a project doc.gpr where I have
inserted the file rhide.tx which should be searched also
in the source directories. May be I will add sometimes a
path for so called 'other files'.
*/
retval = FindFile(name, Options.SrcDirs, full_name, False);
break;
}
}
if (debug_files)
{
if (retval == True)
fprintf(stderr, _("found '%s'\n"), full_name);
else
fprintf(stderr, _("not found: take '%s'\n"), full_name);
}
return retval;
}
class FindCacheCollection:public TNSSortedCollection
{
public:
FindCacheCollection(ccIndex alimit,
ccIndex adelta):TNSSortedCollection(alimit, adelta)
{
}
private:
virtual int compare(void *, void *);
virtual void freeItem(void *);
};
struct FileFind
{
char *name;
char *full_name;
FileFind(const char *_name, const char *_full_name)
{
string_dup(name, _name);
if (_full_name)
string_dup(full_name, _full_name);
else
full_name = NULL;
}
~FileFind()
{
string_free(name);
string_free(full_name);
}
};
int Fcompcount;
int
FindCacheCollection::compare(void *key1, void *key2)
{
return strcmp(((FileFind *) key1)->name, ((FileFind *) key2)->name);
}
void
FindCacheCollection::freeItem(void *item)
{
delete((FileFind *) item);
}
static FindCacheCollection *FindCache = NULL;
void
ClearFindCache()
{
if (debug_files)
{
fprintf(stderr, _("clearing the FindCache\n"));
}
destroy(FindCache);
FindCache = NULL;
}
Boolean
FindFile(const char *name, char *&rel_name, TDirList * list)
{
rel_name = NULL;
for (int i = 0; i < list->getCount(); i++)
{
string_dup(rel_name, name);
char *dir = expand_rhide_spec((const char *) list->at(i));
FExpand(dir);
Boolean found = AbsToRelPath(dir, rel_name, NULL, 0);
string_free(dir);
if (found)
return True;
string_free(rel_name);
}
return False;
}
Boolean
FindFile(const char *name, TDirList * list, char *&full_name, Boolean uselist)
{
int i;
FileFind *ff = NULL;
ccIndex findIndex;
char bname[PATH_MAX];
if (!name)
{
string_dup(full_name, "");
return False;
}
strcpy(bname, name);
// That's now a try for allowing directories in 'name'
if (!NoFileCache)
{
ff = new FileFind(bname, NULL);
if (!FindCache)
FindCache = new FindCacheCollection(100, 100);
if (FindCache->search(ff, findIndex) == True)
{
string_dup(full_name,
((FileFind *) FindCache->at(findIndex))->full_name);
delete(ff);
if (debug_files)
{
fprintf(stderr, _("found '%s' in FindCache\n"), name);
}
return True;
}
}
if (__file_exists(bname))
{
string_dup(full_name, bname);
FExpand(full_name);
if (debug_files)
{
fprintf(stderr, _("found '%s' in current directory\n"), name);
}
if (!NoFileCache)
{
string_dup(ff->full_name, full_name);
FindCache->atInsert(findIndex, ff);
}
return True;
}
for (i = 0; i < list->getCount(); i++)
{
full_name = expand_rhide_spec((const char *) list->at(i));
string_cat(full_name, "/");
string_cat(full_name, bname);
if (__file_exists(full_name))
{
if (debug_files)
{
fprintf(stderr, _("found '%s' using the search directory '%s'\n"),
name, (char *) list->at(i));
}
FExpand(full_name);
if (!NoFileCache)
{
string_dup(ff->full_name, full_name);
FindCache->atInsert(findIndex, ff);
}
return True;
}
string_free(full_name);
}
{
FILE_TYPE t;
if ((t = get_file_type(name)) == FILE_ADA_SOURCE || t == FILE_ADA_SPEC)
{
full_name = expand_rhide_spec("$(ADA_INCLUDE_PATH)");
if (*full_name)
{
string_cat(full_name, "/", bname, NULL);
if (__file_exists(full_name))
{
if (debug_files)
{
fprintf(stderr, _("found '%s' using the search directory '%s'\n"),
name, "$(ADA_INCLUDE_PATH)");
}
FExpand(full_name);
if (!NoFileCache)
{
string_dup(ff->full_name, full_name);
FindCache->atInsert(findIndex, ff);
}
return True;
}
}
string_free(full_name);
}
}
if (uselist == True && list && list->getCount() > 0)
{
full_name = expand_rhide_spec((const char *) list->at(0));
string_cat(full_name, "/");
string_cat(full_name, bname);
}
else
{
string_dup(full_name, name);
}
FExpand(full_name);
if (!NoFileCache)
delete(ff);
return False;
}
#ifdef FILEFINDDEBUG
static __attribute__ ((__destructor__))
void print_findfiles()
{
if (!FindCache)
return;
int i, count = FindCache->getCount();
fprintf(stderr, "FindFiles(%d)\n", count);
for (i = 0; i < count; i++)
{
FileFind *ff = (FileFind *) FindCache->at(i);
fprintf(stderr, "%s[%s]\n", ff->full_name, ff->name);
}
}
#endif
//#define USE_OPEN
ifpstream *
open_ifpstream(const char *name)
{
#ifdef USE_OPEN
int handle;
#ifdef O_BINARY
handle = open(name, O_RDONLY | O_BINARY);
#else
handle = open(name, O_RDONLY);
#endif
if (handle < 0)
{
return NULL;
}
return new ifpstream(handle);
#else
ifpstream *ret = new ifpstream();
ret->open(name);
return ret;
#endif
}
void
close_ifpstream(ifpstream * is)
{
#ifdef USE_OPEN
is->close();
#endif
delete is;
}
static void
ConvertName(TFileName * &_name)
{
if (!_name)
return;
char *name = expand_rhide_spec(FName(_name));
delete _name;
InitFName(_name, name);
string_free(name);
}
static void
ConvertDep(TDependency * dep)
{
ConvertName(dep->source_name);
ConvertName(dep->dest_name);
int i;
if (dep->dependencies)
{
for (i = 0; i < dep->dependencies->getCount(); i++)
{
ConvertDep((TDependency *) dep->dependencies->at(i));
}
}
}
void
ExpandFileNames(TProject * prj)
{
TProject *old = project;
project = prj;
ConvertDep(project);
project = old;
}