File: classbuffer.cc

package info (click to toggle)
ivtools 1.2.11a2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,364 kB
  • sloc: cpp: 174,988; ansic: 12,717; xml: 5,359; perl: 2,164; makefile: 831; sh: 326
file content (449 lines) | stat: -rw-r--r-- 11,430 bytes parent folder | download
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * Copyright (c) 1989 Stanford University
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Stanford not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  Stanford makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
 * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * ClassBuffer implementation.
 */

#include "classbuffer.h"
#include "classinfo.h"
#include "direct.h"
#include "globals.h"

#include <InterViews/regexp.h>
#include <InterViews/textbuffer.h>
#include <OS/types.h>
#include <OS/leave-scope.h>
#define boolean _lib_os(boolean)

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>

#ifndef S_ISDIR
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif

// No MAXPATHLEN on GNU Hurd, cobble something up.
#ifndef MAXPATHLEN
#define MAXPATHLEN 4096
#endif

/*****************************************************************************/

static const int MINTEXTSIZE = 10000;

/*****************************************************************************/

class Classes {
public:
    Classes();
    ~Classes();

    void Include(ClassInfo*);
    ClassInfo* Class(int index);
    ClassInfo* Find(const char* className);
    void Clear();
private:
    void Insert(ClassInfo*, int index);
    void Remove(int index);
    int Index(ClassInfo*);
    int Position(ClassInfo*);
private:
    ClassInfo** _buf;
    int _bufsize;
    int _count;
};

Classes::Classes () {
    const int defaultSize = 64;

    _bufsize = defaultSize;
    _buf = new ClassInfo*[_bufsize];
    _count = 0;
}

Classes::~Classes () {
    Clear();
    delete _buf;
}

void Classes::Include (ClassInfo* info) {
    Insert(info, Position(info));
}

ClassInfo* Classes::Class (int index) {
    return (0 <= index && index < _count) ? _buf[index] : nil;
}

ClassInfo* Classes::Find (const char* className) {
    for (int i = 0; i < _count; ++i) {
        if (strcmp(className, _buf[i]->Name()) == 0) {
            return _buf[i];
        }
    }
    return nil;
}

void Classes::Insert (ClassInfo* info, int index) {
    BufInsert(info, index, (const void**&) _buf, _bufsize, _count);
}

void Classes::Remove (int index) {
    if (0 <= index && index < _count) {
        BufRemove(index, (const void**) _buf, _count);
    }
}

int Classes::Index (ClassInfo* info) {
    for (int i = 0; i < _count; ++i) {
        if (info == _buf[i]) {
            return i;
        }
    }
    return -1;
}

int Classes::Position (ClassInfo* info) {
    for (int i = 0; i < _count; ++i) {
        if (strcmp(info->Name(), _buf[i]->Name()) < 0) {
            return i;
        }
    }
    return _count;
}

void Classes::Clear () {
    for (int i = 0; i < _count; ++i) {
        delete _buf[i];
    }
    _count = 0;
}

/*****************************************************************************/

ClassBuffer::ClassBuffer (
	boolean recursive, boolean verbose, boolean CPlusPlusFiles
    ) {
    _classes = new Classes;
    _recursive = recursive;
    _verbose = verbose;
    _CPlusPlusFiles = CPlusPlusFiles;
}

ClassBuffer::~ClassBuffer () {
    delete _classes;
}

const char* ClassBuffer::Class (int index) {
    ClassInfo* info = _classes->Class(index);
    return (info == nil) ? nil : info->Name();
}

const char* ClassBuffer::Parent (const char* className, int index) {
    ClassInfo* info = _classes->Find(className);
    ClassInfo* parentInfo = (info == nil) ? nil : info->Parent(index);
    return (parentInfo == nil) ? nil : parentInfo->Name();
}

const char* ClassBuffer::Child (const char* className, int index) {
    ClassInfo* info = _classes->Find(className);
    ClassInfo* childInfo = (info == nil) ? nil : info->Child(index);
    return (childInfo == nil) ? nil : childInfo->Name();
}

const char* ClassBuffer::Path (const char* className) {
    ClassInfo* info = _classes->Find(className);
    return (info == nil) ? nil : info->Path();
}

int ClassBuffer::LineNumber (const char* className) {
    ClassInfo* info = _classes->Find(className);
    return (info == nil) ? nil : info->LineNumber();
}

ClassInfo* ClassBuffer::Info (const char* className) {
    return _classes->Find(className);
}    

inline boolean DotOrDotDot (const char* dir) {
    return strcmp(dir, ".") == 0 || strcmp(dir, "..") == 0;
}

inline boolean HeaderFile (const char* file) {
    int length = strlen(file);

    return (file[length-1] == 'h' && file[length-2] == '.') ||
	(file[length-1] == 'h' && file[length-2] == 'h' && file[length-3] == '.');
}

inline boolean CPlusPlusFile (const char* file) {
    int length = strlen(file);

    return file[length-1] == 'C' && file[length-2] == '.';
}

static boolean IsADirectory (const char* path, struct stat& filestats) {
    stat(path, &filestats);
    return S_ISDIR(filestats.st_mode);
}

void ClassBuffer::Search (const char* path) {
    struct stat filestats;

    if (IsADirectory(path, filestats)) {
        if (_recursive) {
            SearchDirs(path);
        } else {
            SearchDir(path);
        }

    } else if (HeaderFile(path) || (_CPlusPlusFiles && CPlusPlusFile(path))) {
        SearchFile(path, filestats);
    }
}

void ClassBuffer::SearchDir (const char* path) {
    Directory dir(path);

    if (_verbose) {
	printf("searching directory %s\n", path);
    }

    for (int i = 0; i < dir.Count(); ++i) {
        const char* file = dir.File(i);

        if (HeaderFile(file) || (_CPlusPlusFiles && CPlusPlusFile(path))) {
            struct stat filestats;
            char filePath[MAXPATHLEN+1];
            strcpy(filePath, dir.Normalize(path));
            strcat(filePath, file);
        
            if (!IsADirectory(filePath, filestats)) {
                SearchFile(filePath, filestats);
            }
        }
    }            
}

void ClassBuffer::SearchDirs (const char* path) {
    Directory dir(path);

    if (_verbose) {
	printf("recursively searching directory %s\n", path);
    }

    for (int i = 0; i < dir.Count(); ++i) {
        const char* file = dir.File(i);

        if (!DotOrDotDot(file)) {
            char filePath[MAXPATHLEN+1];
            strcpy(filePath, dir.Normalize(path));
            strcat(filePath, file);

            Search(filePath);
        }
    }
}

#include <OS/file.h>
#include <OS/memory.h>
#include <OS/string.h>

void ClassBuffer::SearchFile (const char* path, struct stat&) {
    InputFile* f = InputFile::open(path);
    if (f == nil) {
	return;
    }
    const char* buf;
    int len = f->read(buf);
    if (len <= 0) {
	return;
    }
    if (_verbose) {
	printf("searching file %s (%d)\n", path, len);
    }

    /*
     * stupid regular expression requires writable strings to guarantee
     * null-termination
     */
    char* tbuf = new char[len + 1];
    Memory::copy(buf, tbuf, len);
    tbuf[len] = '\0';

    TextBuffer textbuf(tbuf, len, len);

    SearchTextBuffer(&textbuf, path);

    delete [] tbuf;
    f->close();
    delete f;
}

void ClassBuffer::SearchTextBuffer (TextBuffer* tb, const char* path) {
    int beg = 0;

    for (;;) {
        char* className = FindClassDecl(tb, beg);

        if (className == nil) {
            break;
        }
        ClassInfo* info = _classes->Find(className);
        
        if (info == nil) {
            info = new ClassInfo(className, path, tb->LineNumber(beg));
            _classes->Include(info);
        } else {
            info->Path(path);
            info->LineNumber(tb->LineNumber(beg));
        }

        for (;;) {
            char* parentName = ParentName(tb, beg);

            if (parentName == nil) {
                break;
            }
            ClassInfo* parentInfo = _classes->Find(parentName);

            if (parentInfo == nil) {
                parentInfo = new ClassInfo(parentName);
                _classes->Include(parentInfo);
            }
            info->IncludeParent(parentInfo);
            parentInfo->IncludeChild(info);
            delete parentName;
        }
        delete className;
    }
}

inline boolean KeyWord (const char* string) {
    return 
        strcmp(string, "public") == 0 ||
        strcmp(string, "protected") == 0 ||
        strcmp(string, "private") == 0 ||
        strcmp(string, "virtual") == 0;
}

char* ClassBuffer::ParentName (TextBuffer* tb, int& beg) {
    Regexp delimiter("{");
    int delim = tb->ForwardSearch(&delimiter, beg);
    if (delim < 0) {
	return nil;
    }

    for (;;) {
        char* string = Identifier(tb, beg);

        if (string == nil || beg >= delim) {
            delete string;
            beg = delim;
            return nil;

        } else if (KeyWord(string)) {
            delete string;
            string = nil;

        } else {
            return string;
        }
    }
}

char* ClassBuffer::FindClassDecl (TextBuffer* tb, int& beg) {
    Regexp classKeyWord("^class[ $]");
    Regexp delimiter("[:{;]");
    char* className = nil;
    int obeg = beg;

    for (;;) {
        beg = tb->ForwardSearch(&classKeyWord, beg);

        if (beg < 0) {
            break;
        }
        int tmp = beg;
        int delim = tb->ForwardSearch(&delimiter, tmp);
            
        if (delim >= 0 && *tb->Text(delim-1) != ';') {
            className = Identifier(tb, beg);
            break;
        }
    }
    if (className && (strcmp(className, "VTK_EXPORT")==0||strcmp(className, "ACE_Export")==0)) {
      // retry this time skipping the extra token
      Regexp classKeyWord(strcmp(className, "VTK_EXPORT")==0 ? 
			  "^class VTK_EXPORT[ $]" : "^class ACE_Export[ $]");
      Regexp delimiter("[:{;]");
      beg = obeg;

      for (;;) {
        beg = tb->ForwardSearch(&classKeyWord, beg);

        if (beg < 0) {
            break;
        }
        int tmp = beg;
        int delim = tb->ForwardSearch(&delimiter, tmp);
            
        if (delim >= 0 && *tb->Text(delim-1) != ';') {
            className = Identifier(tb, beg);
            break;
        }
      }
    }
    return className;
}

inline boolean IsValidChar (char c) {
    return isalpha(c) || c == '_' || c == '$';
}

char* ClassBuffer::Identifier (TextBuffer* tb, int& beg) {
    int i, j;
    const char* text = tb->Text();
    char* string = nil;

    for (i = beg; i < tb->Length(); ++i) {
        if (IsValidChar(text[i])) {
            break;
        }
    }
    for (j = i+1; j < tb->Length(); ++j) {
        char c = text[j];

        if (!IsValidChar(c) && !isdigit(c)) {
            break;
        }
    }
    if (j < tb->Length()) {
        string = strnnew(&text[i], j-i);
        beg = j;
    }
    return string;
}