summaryrefslogtreecommitdiffstats
path: root/kttsd/libkttsd/utils.cpp
blob: 57bfe18164633cbb38a5dec34cba28bd56ea01d5 (plain)
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
/***************************************************************************
  Class of utility functions.
  -------------------
  Copyright : (C) 2004 Paul Giannaros
  -------------------
  Original author: Paul Giannaros <ceruleanblaze@gmail.com>
  Current Maintainer: Paul Giannaros <ceruleanblaze@gmail.com>
 ****************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; version 2 of the License.               *
 *                                                                         *
 ***************************************************************************/

#include <tqstring.h>
#include <kdebug.h>
#include <tqcombobox.h>

#include "utils.h"

KttsUtils::KttsUtils() {
}


KttsUtils::~KttsUtils() {
}

/** 
 * Check if an XML document has a certain root element.
 * @param xmldoc             The document to check for the element.
 * @param elementName        The element to check for in the document.
 * @returns                  True if the root element exists in the document, false otherwise.
*/
bool KttsUtils::hasRootElement(const TQString &xmldoc, const TQString &elementName) {
    // Strip all whitespace and go from there.
    TQString doc = xmldoc.simplifyWhiteSpace();
    // Take off the <?xml...?> if it exists
    if(doc.startsWith("<?xml")) {
        // Look for ?> and strip everything off from there to the start - effectively removing
        // <?xml...?>
        int xmlStatementEnd = doc.find("?>");
        if(xmlStatementEnd == -1) {
            kdDebug() << "KttsUtils::hasRootElement: Bad XML file syntax\n";
            return false;
        }
        xmlStatementEnd += 2;  // len '?>' == 2
        doc = doc.right(doc.length() - xmlStatementEnd);
    }
    // Take off leading comments, if they exist.
    while(doc.startsWith("<!--") || doc.startsWith(" <!--")) {
        int commentStatementEnd = doc.find("-->");
        if(commentStatementEnd == -1) {
            kdDebug() << "KttsUtils::hasRootElement: Bad XML file syntax\n";
            return false;
        }
        commentStatementEnd += 3; // len '>' == 2
        doc = doc.right(doc.length() - commentStatementEnd);
    }
    // Take off the doctype statement if it exists.
    while(doc.startsWith("<!DOCTYPE") || doc.startsWith(" <!DOCTYPE")) {
        int doctypeStatementEnd = doc.find(">");
        if(doctypeStatementEnd == -1) {
            kdDebug() << "KttsUtils::hasRootElement: Bad XML file syntax\n";
            return false;
        }
        doctypeStatementEnd += 1; // len '>' == 2
        doc = doc.right(doc.length() - doctypeStatementEnd);
    }
    // We should (hopefully) be left with the root element.
    return (doc.startsWith("<" + elementName) || doc.startsWith(" <" + elementName));
}

/** 
 * Check if an XML document has a certain DOCTYPE.
 * @param xmldoc             The document to check for the doctype.
 * @param name               The doctype name to check for. Pass TQString() to not check the name.
 * @param publicId           The public ID to check for. Pass TQString() to not check the ID.
 * @param systemId           The system ID to check for. Pass TQString() to not check the ID.
 * @returns                  True if the parameters match the doctype, false otherwise.
*/
bool KttsUtils::hasDoctype(const TQString &xmldoc, const TQString &name/*, const TQString &publicId, const TQString &systemId*/) {
    // Strip all whitespace and go from there.
    TQString doc = xmldoc.stripWhiteSpace();
    // Take off the <?xml...?> if it exists
    if(doc.startsWith("<?xml")) {
        // Look for ?> and strip everything off from there to the start - effectively removing
        // <?xml...?>
        int xmlStatementEnd = doc.find("?>");
        if(xmlStatementEnd == -1) {
            kdDebug() << "KttsUtils::hasDoctype: Bad XML file syntax\n";
            return false;
        }
        xmlStatementEnd += 2;  // len '?>' == 2
        doc = doc.right(doc.length() - xmlStatementEnd);
        doc = doc.stripWhiteSpace();
    }
    // Take off leading comments, if they exist.
    while(doc.startsWith("<!--")) {
        int commentStatementEnd = doc.find("-->");
        if(commentStatementEnd == -1) {
            kdDebug() << "KttsUtils::hasDoctype: Bad XML file syntax\n";
            return false;
        }
        commentStatementEnd += 3; // len '>' == 2
        doc = doc.right(doc.length() - commentStatementEnd);
        doc = doc.stripWhiteSpace();
    }
    // Match the doctype statement if it exists.
    // kdDebug() << "KttsUtils::hasDoctype: searching " << doc.left(20) << "... for " << "<!DOCTYPE " << name << endl;
    return (doc.startsWith("<!DOCTYPE " + name));
}

/**
 * Sets the current item in the given combobox to the item with the given text.
 * If item with the text not found, does nothing.
 */
/*static*/ void KttsUtils::setCbItemFromText(TQComboBox* cb, const TQString& text)
{
    const int itemCount = cb->count();
    for (int ndx = 0; ndx < itemCount; ++ndx)
    {
        if (cb->text(ndx) == text)
        {
            cb->setCurrentItem(ndx);
            return;
        }
    }
}