to_xml.cpp 55.9 KB
Newer Older
1
2
3
4
5
6
7
8
9

#include "to_xml.h"
#include "dlib/dir_nav.h"
#include <vector>
#include <sstream>
#include <iostream>
#include <fstream>
#include <stack>
#include "dlib/cpp_tokenizer.h"
Davis King's avatar
Davis King committed
10
#include "dlib/string.h"
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

using namespace dlib;
using namespace std;

// ----------------------------------------------------------------------------------------

typedef cpp_tokenizer::kernel_1a_c tok_type;

// ----------------------------------------------------------------------------------------

class file_filter
{
public:

    file_filter( 
        const string& filter
    )
    {
        // pick out the filter strings
        istringstream sin(filter);
        string temp;
        sin >> temp;
        while (sin)
        {
            endings.push_back("." + temp);
            sin >> temp;
        }
    }

    bool operator() ( const file& f) const
    {
        // check if any of the endings match
        for (unsigned long i = 0; i < endings.size(); ++i)
        {
            // if the ending is bigger than f's name then it obviously doesn't match
            if (endings[i].size() > f.name().size())
                continue;

            // now check if the actual characters that make up the end of the file name 
            // matches what is in endings[i].
            if ( std::equal(endings[i].begin(), endings[i].end(), f.name().end()-endings[i].size()))
                return true;
        }

        return false;
    }

    std::vector<string> endings;
};

// ----------------------------------------------------------------------------------------

void obtain_list_of_files (
    const cmd_line_parser<char>::check_1a_c& parser, 
    const std::string& filter, 
    const unsigned long search_depth,
67
    std::vector<std::pair<string,string> >& files
68
69
70
71
)
{
    for (unsigned long i = 0; i < parser.option("i").count(); ++i)
    {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        const directory dir(parser.option("i").argument(0,i));

        const std::vector<file>& temp = get_files_in_directory_tree(dir, file_filter(filter), search_depth);

        // figure out how many characters need to be removed from the path of each file
        const string parent = dir.get_parent().full_name();
        unsigned long strip = parent.size();
        if (parent.size() > 0 && parent[parent.size()-1] != '\\' && parent[parent.size()-1] != '/')
            strip += 1;

        for (unsigned long i = 0; i < temp.size(); ++i)
        {
            files.push_back(make_pair(temp[i].full_name().substr(strip), temp[i].full_name()));
        }
86
87
88
89
    }

    for (unsigned long i = 0; i < parser.number_of_arguments(); ++i)
    {
90
        files.push_back(make_pair(parser[i], parser[i]));
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
    }

    std::sort(files.begin(), files.end());
}

// ----------------------------------------------------------------------------------------

struct tok_function_record
{
    std::vector<std::pair<int,string> > declaration;
    string scope;
    string file;
    string comment;
};

struct tok_method_record
{
    std::vector<std::pair<int,string> > declaration;
    string comment;
};

struct tok_variable_record
{
    std::vector<std::pair<int,string> > declaration;
};

struct tok_typedef_record
{
    std::vector<std::pair<int,string> > declaration;
};

struct tok_class_record 
{
    std::vector<std::pair<int,string> > declaration;
    string name;
    string scope;
    string file;
    string comment;

    std::vector<tok_method_record> public_methods;
131
    std::vector<tok_method_record> protected_methods;
132
133
    std::vector<tok_variable_record> public_variables;
    std::vector<tok_typedef_record> public_typedefs;
134
135
    std::vector<tok_variable_record> protected_variables;
    std::vector<tok_typedef_record> protected_typedefs;
Davis King's avatar
Davis King committed
136
    std::vector<tok_class_record> public_inner_classes;
137
    std::vector<tok_class_record> protected_inner_classes;
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
};

// ----------------------------------------------------------------------------------------

struct function_record
{
    string name;
    string scope;
    string declaration;
    string file;
    string comment;
};

struct method_record
{
    string name;
    string declaration;
    string comment;
};

struct variable_record
{
    string declaration;
};

struct typedef_record
{
    string declaration;
};

struct class_record 
{
    string name;
    string scope;
    string declaration;
    string file;
    string comment;

    std::vector<method_record> public_methods;
    std::vector<variable_record> public_variables;
    std::vector<typedef_record> public_typedefs;
179
180
181
182
183

    std::vector<method_record> protected_methods;
    std::vector<variable_record> protected_variables;
    std::vector<typedef_record> protected_typedefs;

Davis King's avatar
Davis King committed
184
    std::vector<class_record> public_inner_classes;
185
    std::vector<class_record> protected_inner_classes;
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
};

// ----------------------------------------------------------------------------------------

unsigned long count_newlines (
    const string& str
)
/*!
    ensures
        - returns the number of '\n' characters inside str
!*/
{
    unsigned long count = 0;
    for (unsigned long i = 0; i < str.size(); ++i)
    {
        if (str[i] == '\n')
            ++count;
    }
    return count;
}

// ----------------------------------------------------------------------------------------

bool contains_unescaped_newline (
    const string& str
)
/*!
    ensures
        - returns true if str contains a '\n' character that isn't preceded by a '\' 
          character.
!*/
{
    if (str.size() == 0)
        return false;

    if (str[0] == '\n')
        return true;

    for (unsigned long i = 1; i < str.size(); ++i)
    {
        if (str[i] == '\n' && str[i-1] != '\\')
            return true;
    }

    return false;
}

// ----------------------------------------------------------------------------------------

bool is_formal_comment (
    const string& str
)
{
    if (str.size() < 6)
        return false;

    if (str[0] == '/' &&
        str[1] == '*' &&
        str[2] == '!' &&
        str[3] != 'P' &&
        str[3] != 'p' &&
        str[str.size()-3] == '!' &&
        str[str.size()-2] == '*' &&
        str[str.size()-1] == '/' )
        return true;

    return false;
}

// ----------------------------------------------------------------------------------------

string make_scope_string (
    const std::vector<string>& namespaces,
    unsigned long exclude_last_num_scopes = 0 
)
{
    string temp;
    for (unsigned long i = 0; i + exclude_last_num_scopes < namespaces.size(); ++i)
    {
        if (namespaces[i].size() == 0)
            continue;

        if (temp.size() == 0)
            temp = namespaces[i];
        else
            temp += "::" + namespaces[i];
    }
    return temp;
}

// ----------------------------------------------------------------------------------------

bool looks_like_function_declaration (
    const std::vector<std::pair<int,string> >& declaration
)
{

    // Check if declaration contains IDENTIFIER ( ) somewhere in it.
    bool seen_first_part = false;
    bool seen_operator = false;
    int local_paren_count = 0;
    for (unsigned long i = 1; i < declaration.size(); ++i)
    {
        if (declaration[i].first == tok_type::KEYWORD &&
            declaration[i].second == "operator")
        {
            seen_operator = true;
        }

        if (declaration[i].first == tok_type::OTHER &&
            declaration[i].second == "(" &&
            (declaration[i-1].first == tok_type::IDENTIFIER || seen_operator))
        {
            seen_first_part = true;
        }

        if (declaration[i].first == tok_type::OTHER) 
        {
            if ( declaration[i].second == "(")
                ++local_paren_count;
            else if ( declaration[i].second == ")")
                --local_paren_count;
        }
    }

    if (seen_first_part && local_paren_count == 0)
        return true;
    else
        return false;
}

// ----------------------------------------------------------------------------------------

319
320
321
322
323
324
325
326
enum scope_type
{
    public_scope,
    protected_scope,
    private_scope
};


327
void process_file (
328
    istream& fin,
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
    const string& file,
    std::vector<tok_function_record>& functions,
    std::vector<tok_class_record>& classes
)
/*!
    ensures
        - scans the given file for global functions and appends any found into functions.
        - scans the given file for global classes and appends any found into classes.
!*/
{
    tok_type tok;
    tok.set_stream(fin);

    bool recently_seen_struct_keyword = false;
        // true if we have seen the struct keyword and
        // we have not seen any identifiers or { characters

    string last_struct_name;
        // the name of the last struct we have seen

    bool recently_seen_class_keyword = false;
        // true if we have seen the class keyword and
        // we have not seen any identifiers or { characters

    string last_class_name;
        // the name of the last class we have seen

    bool recently_seen_namespace_keyword = false;
        // true if we have seen the namespace keyword and
        // we have not seen any identifiers or { characters

    string last_namespace_name;
        // the name of the last namespace we have seen

    bool recently_seen_pound_define = false;
        // true if we have seen a #define and haven't seen an unescaped newline

366
367
368
    bool recently_seen_preprocessor = false;
        // true if we have seen a preprocessor statement and haven't seen an unescaped newline

369
370
371
372
373
374
375
    bool recently_seen_typedef = false;
        // true if we have seen a typedef keyword and haven't seen a ;

    bool recently_seen_paren_0 = false;
        // true if we have seen paren_count transition to zero but haven't yet seen a ; or { or 
        // a new line if recently_seen_pound_define is true.

376
377
378
379
    bool recently_seen_slots = false;
        // true if we have seen the identifier "slots" at a zero scope but haven't seen any
        // other identifiers or the ';' or ':' characters.

380
381
382
383
384
385
386
387
388
389
390
391
392
393
    bool recently_seen_closing_bracket = false;
        // true if we have seen a } and haven't yet seen an IDENTIFIER or ;

    bool recently_seen_new_scope = false;  
        // true if we have seen the keywords class, namespace, struct, or extern and
        // we have not seen the characters {, ), or ; since then

    bool at_top_of_new_scope = false;
        // true if we have seen the { that started a new scope but haven't seen anything yet but WHITE_SPACE

    std::vector<string> namespaces; 
        // a stack to hold the names of the scopes we have entered.  This is the classes, structs, and namespaces we enter.
    namespaces.push_back(""); // this is the global namespace

394
    std::stack<scope_type> scope_access;
395
        // If the stack isn't empty then we are inside a class or struct and the top value
396
        // in the stack tells if we are in a public, protected, or private region.
397
398

    std::stack<unsigned long> scopes; // a stack to hold current and old scope counts 
399
                             // the top of the stack counts the number of new scopes (i.e. unmatched { } we have entered 
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
                             // since we were at a scope where functions can be defined.
                             // We also maintain the invariant that scopes.size() == namespaces.size()
    scopes.push(0);

    std::stack<tok_class_record> class_stack;
        // This is a stack where class_stack.top() == the incomplete class record for the class declaration we are
        // currently in.

    unsigned long paren_count = 0; 
        // this is the number of ( we have seen minus the number of ) we have
        // seen.

    std::vector<std::pair<int,string> > token_accum;
        // Used to accumulate tokens for function and class declarations

    std::vector<std::pair<int,string> > last_full_declaration;
        // Once we determine that token_accum has a full declaration in it we copy it into last_full_declaration. 

    int type;
    string token;

    tok.get_token(type, token);

    while (type != tok_type::END_OF_FILE)
    {
        switch(type)
        {
            case tok_type::KEYWORD: // ------------------------------------------
                {
                    token_accum.push_back(make_pair(type,token));

431
432
433
                    if (token[0] == '#')
                        recently_seen_preprocessor = true;

434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
                    if (token == "class")
                    {
                        recently_seen_class_keyword = true;
                        recently_seen_new_scope = true;
                    }
                    else if (token == "struct")
                    {
                        recently_seen_struct_keyword = true;
                        recently_seen_new_scope = true;
                    }
                    else if (token == "namespace")
                    {
                        recently_seen_namespace_keyword = true;
                        recently_seen_new_scope = true;
                    }
                    else if (token == "extern")
                    {
                        recently_seen_new_scope = true;
                    }
                    else if (token == "#define")
                    {
                        recently_seen_pound_define = true;
                    }
                    else if (token == "typedef")
                    {
                        recently_seen_typedef = true;
                    }
                    else if (recently_seen_pound_define == false)
                    {
                        // eat white space
                        int temp_type;
                        string temp_token;
                        if (tok.peek_type() == tok_type::WHITE_SPACE)
                            tok.get_token(temp_type, temp_token);

                        const bool next_is_colon = (tok.peek_type() == tok_type::OTHER && tok.peek_token() == ":");
                        if (next_is_colon)
                        {
                            // eat the colon
                            tok.get_token(temp_type, temp_token);

475
                            if (scope_access.size() > 0 && token == "public")
476
                            {
477
                                scope_access.top() = public_scope;
478
479
480
                                token_accum.clear();
                                last_full_declaration.clear();
                            }
481
                            else if (scope_access.size() > 0 && token == "protected")
482
                            {
483
                                scope_access.top() = protected_scope;
484
485
486
                                token_accum.clear();
                                last_full_declaration.clear();
                            }
487
                            else if (scope_access.size() > 0 && token == "private")
488
                            {
489
                                scope_access.top() = private_scope;
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
                                token_accum.clear();
                                last_full_declaration.clear();
                            }
                        }
                    }

                    at_top_of_new_scope = false;

                }break;

            case tok_type::COMMENT: // ------------------------------------------
                {
                    if (scopes.top() == 0 && last_full_declaration.size() > 0 && is_formal_comment(token) &&
                        paren_count == 0)
                    {

                        // if we are inside a class or struct
507
                        if (scope_access.size() > 0)
508
509
510
511
512
513
514
515
516
517
518
519
520
                        {
                            // if we are looking at a comment at the top of a class
                            if (at_top_of_new_scope)
                            {
                                // push an entry for this class into the class_stack
                                tok_class_record temp;
                                temp.declaration = last_full_declaration;
                                temp.file = file;
                                temp.name = namespaces.back();
                                temp.scope = make_scope_string(namespaces,1);
                                temp.comment = token;
                                class_stack.push(temp);
                            }
521
                            else if (scope_access.top() == public_scope || scope_access.top() == protected_scope)
522
523
524
525
526
527
528
529
                            {
                                // This should be a member function.  
                                // Only do anything if the class that contains this member function is
                                // in the class_stack.
                                if (class_stack.size() > 0 && class_stack.top().name == namespaces.back() &&
                                    looks_like_function_declaration(last_full_declaration))
                                {
                                    tok_method_record temp;
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560

                                    // Check if there is an initialization list inside the declaration and if there is
                                    // then find out where the starting : is located so we can avoid including it in 
                                    // the output.
                                    unsigned long pos = last_full_declaration.size();
                                    long temp_paren_count = 0;
                                    for (unsigned long i = 0; i < last_full_declaration.size(); ++i)
                                    {
                                        if (last_full_declaration[i].first == tok_type::OTHER)
                                        {
                                            if (last_full_declaration[i].second == "(")
                                                ++temp_paren_count;
                                            else if (last_full_declaration[i].second == ")")
                                                --temp_paren_count;
                                            else if (temp_paren_count == 0 && last_full_declaration[i].second == ":")
                                            {
                                                // if this is a :: then ignore it
                                                if (i > 0 && last_full_declaration[i-1].second == ":")
                                                    continue;
                                                else if (i+1 < last_full_declaration.size() && last_full_declaration[i+1].second == ":")
                                                    continue;
                                                else 
                                                {
                                                    pos = i;
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    temp.declaration.assign(last_full_declaration.begin(), last_full_declaration.begin()+pos);
561
                                    temp.comment = token;
562
563
564
565
                                    if (scope_access.top() == public_scope)
                                        class_stack.top().public_methods.push_back(temp);
                                    else
                                        class_stack.top().protected_methods.push_back(temp);
566
567
568
569
570
571
572
573
574
                                }
                            }
                        }
                        else
                        {
                            // we should be looking at a global declaration of some kind.   
                            if (looks_like_function_declaration(last_full_declaration))
                            {
                                tok_function_record temp;
575

Davis King's avatar
Davis King committed
576
577
                                // make sure we never include anything beyond the first closing ) 
                                // if we are looking at a #defined function
578
                                unsigned long pos = last_full_declaration.size();
Davis King's avatar
Davis King committed
579
                                if (last_full_declaration[0].second == "#define")
580
                                {
Davis King's avatar
Davis King committed
581
582
                                    long temp_paren_count = 0;
                                    for (unsigned long i = 0; i < last_full_declaration.size(); ++i)
583
                                    {
Davis King's avatar
Davis King committed
584
                                        if (last_full_declaration[i].first == tok_type::OTHER)
585
                                        {
Davis King's avatar
Davis King committed
586
587
588
589
590
                                            if (last_full_declaration[i].second == "(")
                                            {
                                                ++temp_paren_count;
                                            }
                                            else if (last_full_declaration[i].second == ")")
591
                                            {
Davis King's avatar
Davis King committed
592
593
594
595
596
597
                                                --temp_paren_count;
                                                if (temp_paren_count == 0)
                                                {
                                                    pos = i+1;
                                                    break;
                                                }
598
599
600
601
602
603
                                            }
                                        }
                                    }
                                }

                                temp.declaration.assign(last_full_declaration.begin(), last_full_declaration.begin()+pos);
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
                                temp.file = file;
                                temp.scope = make_scope_string(namespaces);
                                temp.comment = token;
                                functions.push_back(temp);
                            }
                        }

                        token_accum.clear();
                        last_full_declaration.clear();
                    }

                    at_top_of_new_scope = false;
                }break;

            case tok_type::IDENTIFIER: // ------------------------------------------
                {
                    if (recently_seen_class_keyword)
                    {
                        last_class_name = token;
                        last_struct_name.clear();
                        last_namespace_name.clear();
                    }
                    else if (recently_seen_struct_keyword)
                    {
                        last_struct_name = token;
                        last_class_name.clear();
                        last_namespace_name.clear();
                    }
                    else if (recently_seen_namespace_keyword)
                    {
                        last_namespace_name = token;
                        last_class_name.clear();
                        last_struct_name.clear();
                    }

639
640
641
642
643
                    if (scopes.top() == 0 && token == "slots")
                        recently_seen_slots = true;
                    else
                        recently_seen_slots = false;

644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
                    recently_seen_class_keyword = false;
                    recently_seen_struct_keyword = false;
                    recently_seen_namespace_keyword = false;
                    recently_seen_closing_bracket = false;
                    at_top_of_new_scope = false;

                    token_accum.push_back(make_pair(type,token));
                }break;

            case tok_type::OTHER: // ------------------------------------------
                {
                    switch(token[0])
                    {
                        case '{':
                            // if we are entering a new scope
                            if (recently_seen_new_scope)
                            {
                                scopes.push(0);
                                at_top_of_new_scope = true;

                                // if we are entering a class 
                                if (last_class_name.size() > 0)
                                {
667
                                    scope_access.push(private_scope);
668
669
670
671
                                    namespaces.push_back(last_class_name);
                                }
                                else if (last_struct_name.size() > 0)
                                {
672
                                    scope_access.push(public_scope);
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
                                    namespaces.push_back(last_struct_name);
                                }
                                else if (last_namespace_name.size() > 0)
                                {
                                    namespaces.push_back(last_namespace_name);
                                }
                                else
                                {
                                    namespaces.push_back("");
                                }
                            }
                            else
                            {
                                scopes.top() += 1;
                            }
                            recently_seen_new_scope = false;
                            recently_seen_class_keyword = false;
                            recently_seen_struct_keyword = false;
                            recently_seen_namespace_keyword = false;
                            recently_seen_paren_0 = false;

                            // a { at function scope is an end of a potential declaration
                            if (scopes.top() == 0)
                            {
                                // put token_accum into last_full_declaration
                                token_accum.swap(last_full_declaration);
                            }
                            token_accum.clear();
                            break;

                        case '}':
                            if (scopes.top() > 0)
                            {
                                scopes.top() -= 1;
                            }
                            else if (scopes.size() > 1)
                            {
                                scopes.pop();

712
713
                                if (scope_access.size() > 0)
                                    scope_access.pop();
714

715
716
717
                                // If the scope we are leaving is the top class on the class_stack
                                // then we need to either pop it into its containing class or put it
                                // into the classes output vector.
718
                                if (class_stack.size() > 0 && namespaces.back() == class_stack.top().name)
719
                                {
720
721
                                    // If this class is a inner_class of another then push it into the
                                    // public_inner_classes or protected_inner_classes field of it's containing class.
722
723
724
725
                                    if (class_stack.size() > 1)
                                    {
                                        tok_class_record temp = class_stack.top();
                                        class_stack.pop();
726
727
728
729
730
731
732
                                        if (scope_access.size() > 0)
                                        {
                                            if (scope_access.top() == public_scope)
                                                class_stack.top().public_inner_classes.push_back(temp);
                                            else if (scope_access.top() == protected_scope)
                                                class_stack.top().protected_inner_classes.push_back(temp);
                                        }
733
734
735
736
737
738
                                    }
                                    else if (class_stack.size() > 0)
                                    {
                                        classes.push_back(class_stack.top());
                                        class_stack.pop();
                                    }
739
                                }
740
741
742

                                namespaces.pop_back();
                                last_full_declaration.clear();
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
                            }

                            token_accum.clear();
                            recently_seen_closing_bracket = true;
                            at_top_of_new_scope = false;
                            break;

                        case ';':
                            // a ; at function scope is an end of a potential declaration
                            if (scopes.top() == 0)
                            {
                                // put token_accum into last_full_declaration
                                token_accum.swap(last_full_declaration);
                            }
                            token_accum.clear();

                            // if we are inside the public area of a class and this ; might be the end
                            // of a typedef or variable declaration
761
762
                            if (scopes.top() == 0 && scope_access.size() > 0 && 
                                (scope_access.top() == public_scope || scope_access.top() == protected_scope) &&
763
764
765
766
767
768
769
770
771
772
                                recently_seen_closing_bracket == false)
                            {
                                if (recently_seen_typedef)
                                {
                                    // This should be a typedef inside the public area of a class or struct:
                                    // Only do anything if the class that contains this typedef is in the class_stack.
                                    if (class_stack.size() > 0 && class_stack.top().name == namespaces.back())
                                    {
                                        tok_typedef_record temp;
                                        temp.declaration = last_full_declaration;
773
774
775
776
                                        if (scope_access.top() == public_scope)
                                            class_stack.top().public_typedefs.push_back(temp);
                                        else
                                            class_stack.top().protected_typedefs.push_back(temp);
777
778
779
780
781
782
783
784
785
786
787
                                    }

                                }
                                else if (recently_seen_paren_0 == false && recently_seen_new_scope == false)
                                {
                                    // This should be some kind of public variable declaration inside a class or struct:
                                    // Only do anything if the class that contains this member variable is in the class_stack.
                                    if (class_stack.size() > 0 && class_stack.top().name == namespaces.back())
                                    {
                                        tok_variable_record temp;
                                        temp.declaration = last_full_declaration;
788
789
790
791
                                        if (scope_access.top() == public_scope)
                                            class_stack.top().public_variables.push_back(temp);
                                        else
                                            class_stack.top().protected_variables.push_back(temp);
792
793
794
795
796
797
798
799
800
                                    }

                                }
                            }

                            recently_seen_new_scope = false;
                            recently_seen_typedef = false;
                            recently_seen_paren_0 = false;
                            recently_seen_closing_bracket = false;
801
                            recently_seen_slots = false;
802
803
804
                            at_top_of_new_scope = false;
                            break;

805
                        case ':':
806
                            token_accum.push_back(make_pair(type,token));
807
808
809
810
811
812
813
814
                            if (recently_seen_slots)
                            {
                                token_accum.clear();
                                last_full_declaration.clear();
                                recently_seen_slots = false;
                            }
                            break;

815
816
817
818
819
820
821
822
823
824
825
826
827
                        case '(':
                            ++paren_count;
                            token_accum.push_back(make_pair(type,token));
                            at_top_of_new_scope = false;
                            break;

                        case ')':
                            token_accum.push_back(make_pair(type,token));

                            --paren_count;
                            if (paren_count == 0)
                            {
                                recently_seen_paren_0 = true;
Davis King's avatar
Davis King committed
828
829
830
831
                                if (scopes.top() == 0)
                                {
                                    last_full_declaration = token_accum;
                                }
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
                            }

                            recently_seen_new_scope = false;
                            at_top_of_new_scope = false;
                            break;

                        default:
                            token_accum.push_back(make_pair(type,token));
                            at_top_of_new_scope = false;
                            break;
                    }
                }break;


            case tok_type::WHITE_SPACE: // ------------------------------------------
                {
                    if (recently_seen_pound_define)
                    {
                        if (contains_unescaped_newline(token))
                        {
                            recently_seen_pound_define = false;
                            recently_seen_paren_0 = false;
854
                            recently_seen_preprocessor = false;
855
856
857
858
859
860

                            // this is an end of a potential declaration
                            token_accum.swap(last_full_declaration);
                            token_accum.clear();
                        }
                    }
861
862
863
864
865
866
867
868
869
870
871

                    if (recently_seen_preprocessor)
                    {
                        if (contains_unescaped_newline(token))
                        {
                            recently_seen_preprocessor = false;

                            last_full_declaration.clear();
                            token_accum.clear();
                        }
                    }
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
                }break;

            default: // ------------------------------------------
                {
                    token_accum.push_back(make_pair(type,token));
                    at_top_of_new_scope = false;
                }break;
        }


        tok.get_token(type, token);
    }
}

// ----------------------------------------------------------------------------------------

string get_function_name (
    const std::vector<std::pair<int,string> >& declaration
)
{
    string name;

Davis King's avatar
Davis King committed
894
895
    bool contains_operator = false;
    unsigned long operator_pos = 0;
896
897
    for (unsigned long i = 0; i < declaration.size(); ++i)
    {
Davis King's avatar
Davis King committed
898
899
        if (declaration[i].first == tok_type::KEYWORD &&
            declaration[i].second == "operator")
900
        {
Davis King's avatar
Davis King committed
901
902
            contains_operator = true;
            operator_pos = i;
903
904
            break;
        }
Davis King's avatar
Davis King committed
905
    }
906

Davis King's avatar
Davis King committed
907
908
909
910
911
912
913
914

    // find the opening ( for the function
    unsigned long paren_pos = 0;
    long paren_count = 0;
    for (long i = declaration.size()-1; i >= 0; --i)
    {
        if (declaration[i].first == tok_type::OTHER &&
            declaration[i].second == ")")
915
        {
Davis King's avatar
Davis King committed
916
            ++paren_count;
917
        }
Davis King's avatar
Davis King committed
918
919
        else if (declaration[i].first == tok_type::OTHER &&
                 declaration[i].second == "(")
920
        {
Davis King's avatar
Davis King committed
921
922
923
924
925
926
            --paren_count;
            if (paren_count == 0)
            {
                paren_pos = i;
                break;
            }
927
        }
Davis King's avatar
Davis King committed
928
    }
929

Davis King's avatar
Davis King committed
930
931
932
933
934

    if (contains_operator)
    {
        name = declaration[operator_pos].second;
        for (unsigned long i = operator_pos+1; i < paren_pos; ++i)
935
        {
Davis King's avatar
Davis King committed
936
            if (declaration[i].first == tok_type::IDENTIFIER || declaration[i].first == tok_type::KEYWORD) 
937
938
939
940
941
942
            {
                name += " ";
            }

            name += declaration[i].second;
        }
Davis King's avatar
Davis King committed
943
944
945
946
947
948
    }
    else
    {
        // if this is a destructor then include the ~
        if (paren_pos > 1 && declaration[paren_pos-2].second == "~")
            name = "~" + declaration[paren_pos-1].second;
949
        else if (paren_pos > 0)
Davis King's avatar
Davis King committed
950
951
            name = declaration[paren_pos-1].second;

952
953
954
955
956
957
958
959

    }

    return name;
}

// ----------------------------------------------------------------------------------------

Davis King's avatar
Davis King committed
960
961
962
963
964
string pretty_print_declaration (
    const std::vector<std::pair<int,string> >& decl
)
{
    string temp;
965
966
    long angle_count = 0;
    long paren_count = 0;
Davis King's avatar
Davis King committed
967
968
969

    if (decl.size() == 0)
        return temp;
970
971
972
973
974
975
976
977
978
979
980

    temp = decl[0].second;


    bool just_closed_template = false;
    bool in_template = false;
    bool last_was_scope_res = false;
    bool seen_operator = false;

    if (temp == "operator")
        seen_operator = true;
Davis King's avatar
Davis King committed
981
982
983

    for (unsigned long i = 1; i < decl.size(); ++i)
    {
984
985
986
987
        bool last_was_less_than = false;
        if (decl[i-1].first == tok_type::OTHER && decl[i-1].second == "<")
            last_was_less_than = true;

Davis King's avatar
Davis King committed
988

Davis King's avatar
Davis King committed
989
        if (decl[i].first == tok_type::OTHER && decl[i].second == "<" && 
990
            (decl[i-1].second != "operator" && ((i>1 && decl[i-2].second != "operator") || decl[i-1].second != "<") ))
991
992
993
994
            ++angle_count;

        if (decl[i-1].first == tok_type::KEYWORD && decl[i-1].second == "template" && 
            decl[i].first == tok_type::OTHER && decl[i].second == "<")
Davis King's avatar
Davis King committed
995
        {
996
997
            in_template = true;
            temp += " <\n    ";
Davis King's avatar
Davis King committed
998
        }
999
        else if (decl[i].first == tok_type::OTHER && decl[i].second == ">")
Davis King's avatar
Davis King committed
1000
        {
Davis King's avatar
Davis King committed
1001
            // don't count angle brackets when they are part of an operator 
1002
            if (decl[i-1].second != "operator" && ((i>1 && decl[i-2].second != "operator") || decl[i-1].second != ">"))
Davis King's avatar
Davis King committed
1003
1004
                --angle_count;

1005
            if (angle_count == 0 && in_template)
Davis King's avatar
Davis King committed
1006
            {
Davis King's avatar
Davis King committed
1007
                temp += "\n    >\n";
1008
1009
                just_closed_template = true;
                in_template = false;
Davis King's avatar
Davis King committed
1010
            }
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
            else
            {
                temp += ">";
            }
        }
        else if (decl[i].first == tok_type::OTHER && decl[i].second == "<")
        {
            temp += "<";
        }
        else if (decl[i].first == tok_type::OTHER && decl[i].second == ",")
        {
1022
            if (in_template || (paren_count == 1 && angle_count == 0))
1023
1024
1025
1026
1027
1028
1029
                temp += ",\n   ";
            else
                temp += ",";
        }
        else if (decl[i].first == tok_type::OTHER && decl[i].second == "&")
        {
            temp += "&";
Davis King's avatar
Davis King committed
1030
        }
Davis King's avatar
Davis King committed
1031
1032
1033
1034
        else if (decl[i].first == tok_type::OTHER && decl[i].second == ".")
        {
            temp += ".";
        }
Davis King's avatar
Davis King committed
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
        else if (decl[i].first == tok_type::SINGLE_QUOTED_TEXT)
        {
            temp += decl[i].second;
        }
        else if (decl[i].first == tok_type::DOUBLE_QUOTED_TEXT)
        {
            temp += decl[i].second;
        }
        else if (decl[i-1].first == tok_type::SINGLE_QUOTED_TEXT && decl[i].second == "'")
        {
            temp += decl[i].second;
        }
        else if (decl[i-1].first == tok_type::DOUBLE_QUOTED_TEXT && decl[i].second == "\"")
        {
            temp += decl[i].second;
        }
Davis King's avatar
Davis King committed
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
        else if (decl[i].first == tok_type::OTHER && decl[i].second == "[")
        {
            temp += "[";
        }
        else if (decl[i].first == tok_type::OTHER && decl[i].second == "]")
        {
            temp += "]";
        }
        else if (decl[i].first == tok_type::OTHER && decl[i].second == "-")
        {
            temp += "-";
        }
        else if (decl[i].first == tok_type::NUMBER)
        {
            if (decl[i-1].second == "=")
                temp += " " + decl[i].second;
            else
                temp += decl[i].second;
        }
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
        else if (decl[i].first == tok_type::OTHER && decl[i].second == "*")
        {
            temp += "*";
        }
        else if (decl[i].first == tok_type::KEYWORD && decl[i].second == "operator")
        {
            temp += "\noperator";
            seen_operator = true;
        }
        else if (decl[i].first == tok_type::OTHER && decl[i].second == ":" &&
                 (decl[i-1].second == ":" || (i+1<decl.size() && decl[i+1].second == ":") ) )
        {
            temp += ":";
            last_was_scope_res = true;
        }
        else if (decl[i].first == tok_type::OTHER && decl[i].second == "(")
        {
            const bool next_is_paren = (i+1 < decl.size() && decl[i+1].first == tok_type::OTHER && decl[i+1].second == ")");

Davis King's avatar
Davis King committed
1089
1090
            if (paren_count == 0 && next_is_paren == false && in_template == false)
                temp += " (\n    ";
1091
1092
1093
1094
1095
1096
1097
1098
            else
                temp += "(";

            ++paren_count;
        }
        else if (decl[i].first == tok_type::OTHER && decl[i].second == ")")
        {
            --paren_count;
Davis King's avatar
Davis King committed
1099
            if (paren_count == 0 && decl[i-1].second != "(" && in_template == false)
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
                temp += "\n)";
            else
                temp += ")";
        }
        else if (decl[i].first == tok_type::IDENTIFIER && i+1 < decl.size() &&
                 decl[i+1].first == tok_type::OTHER && decl[i+1].second == "(")
        {
            if (just_closed_template || paren_count != 0 || decl[i-1].second == "~")
                temp += decl[i].second;
            else if (seen_operator)
                temp += " " + decl[i].second;
            else
                temp += "\n" + decl[i].second;

            just_closed_template = false;
            last_was_scope_res = false;
        }
        else
        {
            if (just_closed_template || last_was_scope_res || last_was_less_than || 
Davis King's avatar
Davis King committed
1120
1121
                (seen_operator && paren_count == 0 && decl[i].first == tok_type::OTHER ) ||
                ((decl[i].first == tok_type::KEYWORD || decl[i].first == tok_type::IDENTIFIER) && i>0 && decl[i-1].second == "("))
1122
1123
1124
1125
1126
1127
1128
1129
                temp += decl[i].second;
            else
                temp += " " + decl[i].second;

            just_closed_template = false;
            last_was_scope_res = false;
        }

Davis King's avatar
Davis King committed
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139


    }

    return temp;
}

// ----------------------------------------------------------------------------------------

string format_comment (
1140
1141
    const string& comment,
    const unsigned long expand_tabs
Davis King's avatar
Davis King committed
1142
1143
1144
1145
1146
1147
1148
)
{
    if (comment.size() <= 6)
        return "";

    string temp = trim(trim(comment.substr(3,comment.size()-6), " \t"), "\n\r");

1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178

    // if we should expand tabs to spaces
    if (expand_tabs != 0)
    {
        unsigned long column = 0;
        string str;
        for (unsigned long i = 0; i < temp.size(); ++i)
        {
            if (temp[i] == '\t')
            {
                const unsigned long num_spaces = expand_tabs - column%expand_tabs;
                column += num_spaces;
                str.insert(str.end(), num_spaces, ' ');
            }
            else if (temp[i] == '\n' || temp[i] == '\r')
            {
                str += temp[i];
                column = 0;
            }
            else
            {
                str += temp[i];
                ++column;
            }
        }

        // put str into temp
        str.swap(temp);
    }

Davis King's avatar
Davis King committed
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
    // now figure out what the smallest amount of leading white space is and remove it from each line.
    unsigned long num_whitespace = 100000;
    
    string::size_type pos1 = 0, pos2 = 0;

    while (pos1 != string::npos)
    {
        // find start of non-white-space
        pos2 = temp.find_first_not_of(" \t",pos1);

        // if this is a line of just white space then ignore it
        if (pos2 != string::npos && temp[pos2] != '\n' && temp[pos2] != '\r')
        {
            if (pos2-pos1 < num_whitespace)
                num_whitespace = pos2-pos1;
        }

        // find end-of-line
        pos1 = temp.find_first_of("\n\r", pos2);
        // find start of next line
        pos2 = temp.find_first_not_of("\n\r", pos1);
        pos1 = pos2;
    }

    // now remove the leading white space
    string temp2;
    unsigned long counter = 0;
    for (unsigned long i = 0; i < temp.size(); ++i)
    {
        // if we are looking at a new line
        if (temp[i] == '\n' || temp[i] == '\r')
        {
            counter = 0;
        }
        else if (counter < num_whitespace)
        {
            ++counter;
            continue;
        }

        temp2 += temp[i];
    }

    return temp2;
}

// ----------------------------------------------------------------------------------------

1227
1228
1229
1230
1231
typedef_record convert_tok_typedef_record (
    const tok_typedef_record& rec
)
{
    typedef_record temp;
Davis King's avatar
Davis King committed
1232
    temp.declaration = pretty_print_declaration(rec.declaration);
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
    return temp;
}

// ----------------------------------------------------------------------------------------

variable_record convert_tok_variable_record (
    const tok_variable_record& rec
)
{
    variable_record temp;
Davis King's avatar
Davis King committed
1243
    temp.declaration = pretty_print_declaration(rec.declaration);
1244
1245
1246
1247
1248
1249
    return temp;
}

// ----------------------------------------------------------------------------------------

method_record convert_tok_method_record (
1250
1251
    const tok_method_record& rec,
    const unsigned long expand_tabs
1252
1253
1254
1255
)
{
    method_record temp;

1256
    temp.comment = format_comment(rec.comment, expand_tabs);
1257
    temp.name = get_function_name(rec.declaration);
Davis King's avatar
Davis King committed
1258
    temp.declaration = pretty_print_declaration(rec.declaration);
1259
1260
1261
1262
1263
1264
    return temp;
}

// ----------------------------------------------------------------------------------------

class_record convert_tok_class_record (
1265
1266
    const tok_class_record& rec,
    const unsigned long expand_tabs
1267
1268
1269
1270
1271
1272
1273
)
{
    class_record crec;


    crec.scope = rec.scope;
    crec.file = rec.file;
1274
    crec.comment = format_comment(rec.comment, expand_tabs);
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290

    crec.name.clear();

    // find the first class token
    for (unsigned long i = 0; i+1 < rec.declaration.size(); ++i)
    {
        if (rec.declaration[i].first == tok_type::KEYWORD &&
            (rec.declaration[i].second == "class" ||
            rec.declaration[i].second == "struct" )
            )
        {
            crec.name = rec.declaration[i+1].second;
            break;
        }
    }

Davis King's avatar
Davis King committed
1291
    crec.declaration = pretty_print_declaration(rec.declaration);
1292
1293
1294
1295
1296
1297
1298

    for (unsigned long i = 0; i < rec.public_typedefs.size(); ++i)
        crec.public_typedefs.push_back(convert_tok_typedef_record(rec.public_typedefs[i]));

    for (unsigned long i = 0; i < rec.public_variables.size(); ++i)
        crec.public_variables.push_back(convert_tok_variable_record(rec.public_variables[i]));

1299
1300
1301
1302
1303
1304
    for (unsigned long i = 0; i < rec.protected_typedefs.size(); ++i)
        crec.protected_typedefs.push_back(convert_tok_typedef_record(rec.protected_typedefs[i]));

    for (unsigned long i = 0; i < rec.protected_variables.size(); ++i)
        crec.protected_variables.push_back(convert_tok_variable_record(rec.protected_variables[i]));

1305
    for (unsigned long i = 0; i < rec.public_methods.size(); ++i)
1306
        crec.public_methods.push_back(convert_tok_method_record(rec.public_methods[i], expand_tabs));
1307

1308
1309
1310
    for (unsigned long i = 0; i < rec.protected_methods.size(); ++i)
        crec.protected_methods.push_back(convert_tok_method_record(rec.protected_methods[i], expand_tabs));

Davis King's avatar
Davis King committed
1311
    for (unsigned long i = 0; i < rec.public_inner_classes.size(); ++i)
1312
        crec.public_inner_classes.push_back(convert_tok_class_record(rec.public_inner_classes[i], expand_tabs));
1313

1314
1315
1316
    for (unsigned long i = 0; i < rec.protected_inner_classes.size(); ++i)
        crec.protected_inner_classes.push_back(convert_tok_class_record(rec.protected_inner_classes[i], expand_tabs));

1317
1318
1319
1320
1321
1322
1323

    return crec;
}

// ----------------------------------------------------------------------------------------

function_record convert_tok_function_record (
1324
1325
    const tok_function_record& rec,
    const unsigned long expand_tabs
1326
1327
1328
1329
1330
1331
)
{
    function_record temp;

    temp.scope = rec.scope;
    temp.file = rec.file;
1332
    temp.comment = format_comment(rec.comment, expand_tabs);
1333
    temp.name = get_function_name(rec.declaration);
Davis King's avatar
Davis King committed
1334
    temp.declaration = pretty_print_declaration(rec.declaration);
1335
1336
1337
1338
1339
1340
1341
1342
1343

    return temp;
}

// ----------------------------------------------------------------------------------------

void convert_to_normal_records (
    const std::vector<tok_function_record>& tok_functions,
    const std::vector<tok_class_record>& tok_classes,
1344
    const unsigned long expand_tabs,
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
    std::vector<function_record>& functions,
    std::vector<class_record>& classes
)
{
    functions.clear();
    classes.clear();


    for (unsigned long i = 0; i < tok_functions.size(); ++i)
    {
1355
        functions.push_back(convert_tok_function_record(tok_functions[i], expand_tabs));
1356
1357
1358
1359
1360
    }


    for (unsigned long i = 0; i < tok_classes.size(); ++i)
    {
1361
        classes.push_back(convert_tok_class_record(tok_classes[i], expand_tabs));
1362
1363
1364
1365
1366
1367
1368
    }


}

// ----------------------------------------------------------------------------------------

Davis King's avatar
Davis King committed
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
string add_entity_ref (const string& str)
{
    string temp;
    for (unsigned long i = 0; i < str.size(); ++i)
    {
        if (str[i] == '&')
            temp += "&amp;";
        else if (str[i] == '<')
            temp += "&lt;";
        else if (str[i] == '>')
            temp += "&gt;";
        else
            temp += str[i];
    }
    return temp;
}

// ----------------------------------------------------------------------------------------

1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
string flip_slashes (string str)
{
    for (unsigned long i = 0; i < str.size(); ++i)
    {
        if (str[i] == '\\')
            str[i] = '/';
    }
    return str;
}

// ----------------------------------------------------------------------------------------

1400
1401
1402
1403
1404
1405
void write_as_xml (
    const function_record& rec,
    ostream& fout
)
{
    fout << "    <function>\n";
1406
1407
1408
1409
1410
    fout << "      <name>"        << add_entity_ref(rec.name)               << "</name>\n";
    fout << "      <scope>"       << add_entity_ref(rec.scope)              << "</scope>\n";
    fout << "      <declaration>" << add_entity_ref(rec.declaration)        << "</declaration>\n";
    fout << "      <file>"        << flip_slashes(add_entity_ref(rec.file)) << "</file>\n";
    fout << "      <comment>"     << add_entity_ref(rec.comment)            << "</comment>\n";
1411
1412
1413
1414
1415
1416
1417
    fout << "    </function>\n";
}

// ----------------------------------------------------------------------------------------

void write_as_xml (
    const class_record& rec,
Davis King's avatar
Davis King committed
1418
1419
    ostream& fout,
    unsigned long indent 
1420
1421
)
{
Davis King's avatar
Davis King committed
1422
    const string pad(indent, ' ');
1423

Davis King's avatar
Davis King committed
1424
    fout << pad << "<class>\n";
1425
1426
1427
1428
1429
    fout << pad << "  <name>"        << add_entity_ref(rec.name)               << "</name>\n";
    fout << pad << "  <scope>"       << add_entity_ref(rec.scope)              << "</scope>\n";
    fout << pad << "  <declaration>" << add_entity_ref(rec.declaration)        << "</declaration>\n";
    fout << pad << "  <file>"        << flip_slashes(add_entity_ref(rec.file)) << "</file>\n";
    fout << pad << "  <comment>"     << add_entity_ref(rec.comment)            << "</comment>\n";
1430

Davis King's avatar
Davis King committed
1431
1432

    if (rec.public_typedefs.size() > 0)
1433
    {
Davis King's avatar
Davis King committed
1434
1435
1436
1437
1438
1439
        fout << pad << "  <public_typedefs>\n";
        for (unsigned long i = 0; i < rec.public_typedefs.size(); ++i)
        {
            fout << pad << "    <typedef>"        << add_entity_ref(rec.public_typedefs[i].declaration)        << "</typedef>\n";
        }
        fout << pad << "  </public_typedefs>\n";
1440
1441
1442
    }


Davis King's avatar
Davis King committed
1443
    if (rec.public_variables.size() > 0)
1444
    {
Davis King's avatar
Davis King committed
1445
1446
1447
1448
1449
1450
        fout << pad << "  <public_variables>\n";
        for (unsigned long i = 0; i < rec.public_variables.size(); ++i)
        {
            fout << pad << "    <variable>"        << add_entity_ref(rec.public_variables[i].declaration)        << "</variable>\n";
        }
        fout << pad << "  </public_variables>\n";
1451
1452
    }

1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
    if (rec.protected_typedefs.size() > 0)
    {
        fout << pad << "  <protected_typedefs>\n";
        for (unsigned long i = 0; i < rec.protected_typedefs.size(); ++i)
        {
            fout << pad << "    <typedef>"        << add_entity_ref(rec.protected_typedefs[i].declaration)        << "</typedef>\n";
        }
        fout << pad << "  </protected_typedefs>\n";
    }


    if (rec.protected_variables.size() > 0)
    {
        fout << pad << "  <protected_variables>\n";
        for (unsigned long i = 0; i < rec.protected_variables.size(); ++i)
        {
            fout << pad << "    <variable>"        << add_entity_ref(rec.protected_variables[i].declaration)        << "</variable>\n";
        }
        fout << pad << "  </protected_variables>\n";
    }

1474

Davis King's avatar
Davis King committed
1475
    if (rec.public_methods.size() > 0)
1476
    {
Davis King's avatar
Davis King committed
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
        fout << pad << "  <public_methods>\n";
        for (unsigned long i = 0; i < rec.public_methods.size(); ++i)
        {
            fout << pad << "    <method>\n";
            fout << pad << "      <name>"        << add_entity_ref(rec.public_methods[i].name)        << "</name>\n";
            fout << pad << "      <declaration>" << add_entity_ref(rec.public_methods[i].declaration) << "</declaration>\n";
            fout << pad << "      <comment>"     << add_entity_ref(rec.public_methods[i].comment)     << "</comment>\n";
            fout << pad << "    </method>\n";
        }
        fout << pad << "  </public_methods>\n";
1487
1488
1489
    }


1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
    if (rec.protected_methods.size() > 0)
    {
        fout << pad << "  <protected_methods>\n";
        for (unsigned long i = 0; i < rec.protected_methods.size(); ++i)
        {
            fout << pad << "    <method>\n";
            fout << pad << "      <name>"        << add_entity_ref(rec.protected_methods[i].name)        << "</name>\n";
            fout << pad << "      <declaration>" << add_entity_ref(rec.protected_methods[i].declaration) << "</declaration>\n";
            fout << pad << "      <comment>"     << add_entity_ref(rec.protected_methods[i].comment)     << "</comment>\n";
            fout << pad << "    </method>\n";
        }
        fout << pad << "  </protected_methods>\n";
    }


Davis King's avatar
Davis King committed
1505
    if (rec.public_inner_classes.size() > 0)
1506
    {
Davis King's avatar
Davis King committed
1507
1508
        fout << pad << "  <public_inner_classes>\n";
        for (unsigned long i = 0; i < rec.public_inner_classes.size(); ++i)
Davis King's avatar
Davis King committed
1509
        {
Davis King's avatar
Davis King committed
1510
            write_as_xml(rec.public_inner_classes[i], fout, indent+4);
Davis King's avatar
Davis King committed
1511
        }
Davis King's avatar
Davis King committed
1512
        fout << pad << "  </public_inner_classes>\n";
1513
1514
    }

1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
    if (rec.protected_inner_classes.size() > 0)
    {
        fout << pad << "  <protected_inner_classes>\n";
        for (unsigned long i = 0; i < rec.protected_inner_classes.size(); ++i)
        {
            write_as_xml(rec.protected_inner_classes[i], fout, indent+4);
        }
        fout << pad << "  </protected_inner_classes>\n";
    }

1525

Davis King's avatar
Davis King committed
1526
    fout << pad << "</class>\n";
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
}

// ----------------------------------------------------------------------------------------

void save_to_xml_file (
    const std::vector<function_record>& functions,
    const std::vector<class_record>& classes
)
{
    ofstream fout("output.xml");

Davis King's avatar
Davis King committed
1538
    fout << "<!-- This XML file was generated using the htmlify tool available from http://dlib.net. -->" << endl;
1539
1540
1541
1542
1543
    fout << "<code>" << endl;

    fout << "  <classes>" << endl;
    for (unsigned long i = 0; i < classes.size(); ++i)
    {
Davis King's avatar
Davis King committed
1544
        write_as_xml(classes[i], fout, 4);
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
        fout << "\n";
    }
    fout << "  </classes>\n\n" << endl;


    fout << "  <global_functions>" << endl;
    for (unsigned long i = 0; i < functions.size(); ++i)
    {
        write_as_xml(functions[i], fout);
        fout << "\n";
    }
    fout << "  </global_functions>" << endl;

    fout << "</code>" << endl;
}

// ----------------------------------------------------------------------------------------

void generate_xml_markup(
    const cmd_line_parser<char>::check_1a_c& parser, 
    const std::string& filter, 
1566
1567
    const unsigned long search_depth,
    const unsigned long expand_tabs
1568
1569
1570
1571
)
{

    // first figure out which files should be processed
1572
    std::vector<std::pair<string,string> > files; 
1573
1574
1575
1576
1577
1578
1579
1580
    obtain_list_of_files(parser, filter, search_depth, files);


    std::vector<tok_function_record> tok_functions;
    std::vector<tok_class_record> tok_classes;

    for (unsigned long i = 0; i < files.size(); ++i)
    {
1581
1582
1583
        ifstream fin(files[i].second.c_str());
        if (!fin)
        {
Davis King's avatar
Davis King committed
1584
            cerr << "Error opening file: " << files[i].second << endl;
1585
1586
1587
            return;
        }
        process_file(fin, files[i].first, tok_functions, tok_classes); 
1588
1589
1590
1591
1592
    }

    std::vector<function_record> functions;
    std::vector<class_record> classes;

1593
    convert_to_normal_records(tok_functions, tok_classes, expand_tabs, functions, classes);
1594
1595
1596
1597
1598
1599

    save_to_xml_file(functions, classes);
}

// ----------------------------------------------------------------------------------------