summaryrefslogtreecommitdiff
path: root/noncore/apps/tableviewer/xmlencodeattr.cpp
blob: de264f7947896caa8cad2f74a9e89ef8be6d7834 (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
/*
 * xmlencodeattr.h
 *
 * copyright   : (c) 2003 by Joseph Wenninger
 *               except for a small modification it's identical to qdom.cpp:encodeAttr
 * email       : jowenn@handhelds.org
 *
 */
/***************************************************************************
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "xmlencodeattr.h"
QString encodeAttr( const QString& str )
{
    QString tmp( str );
    uint len = tmp.length();
    uint i = 0;
    while ( i < len ) {
        if ( tmp[(int)i] == '<' ) {
            tmp.replace( i, 1, "&lt;" );
            len += 3;
            i += 4;
        } else if ( tmp[(int)i] == '"' ) {
            tmp.replace( i, 1, "&quot;" );
            len += 5;
            i += 6;
        } else if ( tmp[(int)i] == '&' ) {
            tmp.replace( i, 1, "&amp;" );
            len += 4;
            i += 5;
        } else if ( tmp[(int)i] == '>'  ) {
            tmp.replace( i, 1, "&gt;" );
            len += 3;
            i += 4;
        } else {
            ++i;
        }
    }

    return tmp;
}