Imported version 0.4-1
[mstardict] / stardict-plugins / stardict-wiki-parsedata-plugin / TXML.cpp
1 #include "TXML.h"
2
3 // *****************************************************************************
4 // *****************************************************************************
5 //
6 // TXML
7 //
8 // *****************************************************************************
9 // *****************************************************************************
10
11 TXML::TXML ( int f , int t , string &s , bool fix_comments )
12         {
13         from = f ;
14         to = t ;
15         name = s.substr ( from + 1 , to - (from+1) ) ;
16         name = trim ( name ) ;
17         name = before_first ( ' ' , name ) ;
18         closing = selfclosing = false ;
19         if ( left ( name , 1 ) == "/" )
20            {
21            closing = true ;
22            name = name.substr ( 1 , name.length()-1 ) ;
23            }
24         if ( right ( name , 1 ) == "/" )
25            {
26            selfclosing = true ;
27            name = name.substr ( 0 , name.length()-1 ) ;
28            }
29     name = trim ( name ) ;
30         
31         // This will replace < and > within a comment with the appropriate HTML entities
32         if ( fix_comments && left ( name , 1 ) == "!" )
33                 {
34                 int a ;
35                 for ( a = from+1 ; a < to ; a++ )
36                         {
37                         if ( s[a] != '>' && s[a] != '<' ) continue ;
38                         to += 3 ;
39                         if ( s[a] == '>' ) s.insert ( a , "&gt" ) ;
40                         if ( s[a] == '<' ) s.insert ( a , "&lt" ) ;
41                         s[a+3] = ';' ;
42                         }    
43                 }    
44         }    
45
46 void TXML::remove_at ( int pos )
47         {
48         if ( pos < from ) from-- ;
49         if ( pos < to ) to-- ;
50         }    
51
52 void TXML::insert_at ( int pos )
53         {
54         if ( pos < from ) from++ ;
55         if ( pos < to ) to++ ;
56         }    
57
58 void TXML::add_key_value ( string k , string v )
59         {
60     key.push_back ( trim ( k ) ) ;
61     value.push_back ( trim ( v ) ) ;
62         }    
63         
64 string TXML::get_string ()
65         {
66     string ret ;
67     ret = "<" + name ;
68     for ( size_t a = 0 ; a < key.size() ; a++ )
69         {
70             for ( size_t b = 0 ; b < key[a].length() ; b++ )
71                 {
72                 if ( key[a][b] == ' ' ) key[a][b] = '_' ;
73                 }               
74             ret += " " + key[a] ;
75             if ( value[a] != "" ) ret += "=\"" + unquote ( SINGLE_QUOTE , value[a] ) + "\"" ;
76         }    
77         if ( text == "" ) ret += "/>" ;
78         else ret += ">" + text + "</" + name + ">" ;
79         return ret ;
80         }
81  
82