xmlobj.cpp
author insilmaril
Tue, 18 Aug 2009 12:39:07 +0000
changeset 788 78ba80b54bc4
parent 721 12958f987bcf
child 791 f1006de05c54
permissions -rw-r--r--
Fix for segfault when deleting MCO (invalid QModelIndex needs to be returned in index(TreeItem*) )
     1 #include "xmlobj.h"
     2 
     3 #include <QRegExp>
     4 #include <QString>
     5 
     6 
     7 // returns masked "<" ">" "&"
     8 QString quotemeta(const QString &s)
     9 {
    10 	QString r=s;
    11     QRegExp  rx("&(?!amp;)");
    12     r.replace ( rx,"&amp;");
    13     rx.setPattern( ">");
    14     r.replace ( rx,"&gt;");
    15     rx.setPattern( "<");
    16     r.replace ( rx,"&lt;");
    17     rx.setPattern( "\"");
    18     r.replace ( rx,"&quot;");
    19     return r;
    20 }
    21 
    22 
    23 
    24 int XMLObj::actindent=0;		// make instance of actindent
    25 
    26 XMLObj::XMLObj()
    27 {
    28     indentwidth=4;
    29 }
    30 
    31 
    32 // returns <s at />
    33 QString XMLObj::singleElement(QString s, QString at)
    34 {
    35     return indent() + "<" + s +" " + at +" " + "/>\n";
    36 }
    37 
    38 // returns <s>
    39 QString XMLObj::beginElement(QString s)
    40 {
    41     return indent() + "<" + s + ">\n";
    42 }
    43 
    44 // returns <s at>
    45 QString XMLObj::beginElement(QString s, QString at)
    46 {
    47     return indent() + "<" + s + " " + at + ">\n";
    48 }
    49 
    50 // returns </s>
    51 QString XMLObj::endElement(QString s)
    52 {
    53     return indent() + "</" + s + ">\n";
    54 }
    55 
    56 // returns  at="val"
    57 QString XMLObj::attribut(QString at, QString val)
    58 {
    59     return " " + at + "=\"" + quotemeta (val) + "\""; 
    60 }
    61 
    62 // returns <s> val </s>
    63 QString XMLObj::valueElement(QString el, QString val)
    64 {
    65     return indent() + "<" + el + ">" + quotemeta(val) + "</" +el + ">\n";
    66 }
    67 
    68 // returns <s at> val </s>
    69 QString XMLObj::valueElement(QString el, QString val, QString at)
    70 {
    71     return indent() + "<" + el + " " + at + ">" + quotemeta(val) + "</" +el + ">\n";
    72 }
    73 
    74 void XMLObj::incIndent()
    75 {
    76     actindent++;
    77 }	
    78 
    79 void XMLObj::decIndent()
    80 {
    81     actindent--;
    82     if (actindent<0) actindent=0;
    83 }	
    84 
    85 QString XMLObj::indent()
    86 {
    87     QString s;
    88     int i;
    89     for (i=0; i<actindent*indentwidth; i++)
    90     {
    91 		s= s + " ";
    92     }
    93     return s;
    94 }	
    95