Switch to unified view

a/src/conftree.cpp b/src/conftree.cpp
...
...
50
#define LOGDEB(X) fprintf X
50
#define LOGDEB(X) fprintf X
51
#else
51
#else
52
#define LOGDEB(X)
52
#define LOGDEB(X)
53
#endif
53
#endif
54
54
55
static const SimpleRegexp varcomment_rx("[ \t]*#[ \t]*([a-zA-Z0-9]+)[ \t]*=",
56
                                        0, 1);
57
55
void ConfSimple::parseinput(istream& input)
58
void ConfSimple::parseinput(istream& input)
56
{
59
{
57
    string submapkey;
60
    string submapkey;
58
    string cline;
61
    string cline;
59
    bool appending = false;
62
    bool appending = false;
...
...
98
        trimstring(line);
101
        trimstring(line);
99
        if (line.empty() || line.at(0) == '#') {
102
        if (line.empty() || line.at(0) == '#') {
100
            if (eof) {
103
            if (eof) {
101
                break;
104
                break;
102
            }
105
            }
106
            if (varcomment_rx.simpleMatch(line)) {
107
                m_order.push_back(ConfLine(ConfLine::CFL_VARCOMMENT, line,
108
                                           varcomment_rx.getMatch(line, 1)));
109
            } else {
103
            m_order.push_back(ConfLine(ConfLine::CFL_COMMENT, line));
110
                m_order.push_back(ConfLine(ConfLine::CFL_COMMENT, line));
111
            }
104
            continue;
112
            continue;
105
        }
113
        }
106
        if (line[line.length() - 1] == '\\') {
114
        if (line[line.length() - 1] == '\\') {
107
            line.erase(line.length() - 1);
115
            line.erase(line.length() - 1);
108
            appending = true;
116
            appending = true;
...
...
116
                submapkey = path_tildexpand(line);
124
                submapkey = path_tildexpand(line);
117
            } else {
125
            } else {
118
                submapkey = line;
126
                submapkey = line;
119
            }
127
            }
120
            m_subkeys_unsorted.push_back(submapkey);
128
            m_subkeys_unsorted.push_back(submapkey);
121
129
            m_order.push_back(ConfLine(ConfLine::CFL_SK, submapkey));
122
            // No need for adding sk to order, will be done with first
123
            // variable insert. Also means that empty section are
124
            // expandable (won't be output when rewriting)
125
            // Another option would be to add the subsec to m_order here
126
            // and not do it inside i_set() if init is true
127
            continue;
130
            continue;
128
        }
131
        }
129
132
130
        // Look for first equal sign
133
        // Look for first equal sign
131
        string::size_type eqpos = line.find("=");
134
        string::size_type eqpos = line.find("=");
...
...
369
        LOGDEB((stderr, "ConfSimple::i_set: new submap\n"));
372
        LOGDEB((stderr, "ConfSimple::i_set: new submap\n"));
370
        map<string, string> submap;
373
        map<string, string> submap;
371
        submap[nm] = value;
374
        submap[nm] = value;
372
        m_submaps[sk] = submap;
375
        m_submaps[sk] = submap;
373
376
374
        // Maybe add sk entry to m_order data:
377
        // Maybe add sk entry to m_order data, if not already there.
375
        if (!sk.empty()) {
378
        if (!sk.empty()) {
376
            ConfLine nl(ConfLine::CFL_SK, sk);
379
            ConfLine nl(ConfLine::CFL_SK, sk);
377
            // Append SK entry only if it's not already there (erase
380
            // Append SK entry only if it's not already there (erase
378
            // does not remove entries from the order data, adn it may
381
            // does not remove entries from the order data, and it may
379
            // be being recreated after deletion)
382
            // be being recreated after deletion)
380
            if (find(m_order.begin(), m_order.end(), nl) == m_order.end()) {
383
            if (find(m_order.begin(), m_order.end(), nl) == m_order.end()) {
381
                m_order.push_back(nl);
384
                m_order.push_back(nl);
382
            }
385
            }
383
        }
386
        }
...
...
443
    }
446
    }
444
447
445
    // It may happen that the order entry already exists because erase doesnt
448
    // It may happen that the order entry already exists because erase doesnt
446
    // update m_order
449
    // update m_order
447
    if (find(start, fin, ConfLine(ConfLine::CFL_VAR, nm)) == fin) {
450
    if (find(start, fin, ConfLine(ConfLine::CFL_VAR, nm)) == fin) {
451
        // Look for a varcomment line, insert the value right after if
452
        // it's there.
453
        bool inserted(false);
454
        vector<ConfLine>::iterator it;
455
        for (it = start; it != fin; it++) {
456
            if (it->m_kind == ConfLine::CFL_VARCOMMENT && it->m_aux == nm) {
457
                it++;
458
                m_order.insert(it, ConfLine(ConfLine::CFL_VAR, nm));
459
                inserted = true;
460
                break;
461
            }
462
        }
463
        if (!inserted) {
448
        m_order.insert(fin, ConfLine(ConfLine::CFL_VAR, nm));
464
            m_order.insert(fin, ConfLine(ConfLine::CFL_VAR, nm));
449
    }
465
        }
466
    }
467
450
    return 1;
468
    return 1;
451
}
469
}
452
470
453
int ConfSimple::erase(const string& nm, const string& sk)
471
int ConfSimple::erase(const string& nm, const string& sk)
454
{
472
{
...
...
544
    string sk;
562
    string sk;
545
    for (vector<ConfLine>::const_iterator it = m_order.begin();
563
    for (vector<ConfLine>::const_iterator it = m_order.begin();
546
            it != m_order.end(); it++) {
564
            it != m_order.end(); it++) {
547
        switch (it->m_kind) {
565
        switch (it->m_kind) {
548
        case ConfLine::CFL_COMMENT:
566
        case ConfLine::CFL_COMMENT:
567
        case ConfLine::CFL_VARCOMMENT:
549
            out << it->m_data << endl;
568
            out << it->m_data << endl;
550
            if (!out.good()) {
569
            if (!out.good()) {
551
                return false;
570
                return false;
552
            }
571
            }
553
            break;
572
            break;
...
...
642
        }
661
        }
643
    }
662
    }
644
    return false;
663
    return false;
645
}
664
}
646
665
666
bool ConfSimple::commentsAsXML(ostream& out)
667
{
668
    const vector<ConfLine>& lines = getlines();
669
670
    out << "<confcomments>\n";
671
    
672
    string sk;
673
    for (vector<ConfLine>::const_iterator it = lines.begin();
674
         it != lines.end(); it++) {
675
        switch (it->m_kind) {
676
        case ConfLine::CFL_COMMENT:
677
        case ConfLine::CFL_VARCOMMENT:
678
        {
679
            string::size_type pos = it->m_data.find_first_not_of("# ");
680
            if (pos != string::npos) {
681
                out << it->m_data.substr(pos) << endl;
682
            }
683
            break;
684
        }
685
        default:
686
            break;
687
        }
688
    }
689
    out << "</confcomments>\n";
690
    
691
    return true;
692
}
693
694
647
// //////////////////////////////////////////////////////////////////////////
695
// //////////////////////////////////////////////////////////////////////////
648
// ConfTree Methods: conftree interpret keys like a hierarchical file tree
696
// ConfTree Methods: conftree interpret keys like a hierarchical file tree
649
// //////////////////////////////////////////////////////////////////////////
697
// //////////////////////////////////////////////////////////////////////////
650
698
651
int ConfTree::get(const std::string& name, string& value, const string& sk)
699
int ConfTree::get(const std::string& name, string& value, const string& sk)