Added toggle buttons for switching speak engine, update PO files
[mstardict] / stardict-plugins / stardict-wiki-parsedata-plugin / global.cpp
1 #include "global.h"
2
3 // *****************************************************************************
4 // *****************************************************************************
5 //
6 // global string functions
7 //
8 // *****************************************************************************
9 // *****************************************************************************
10
11 // The following functions should be language specific
12 bool is_text_char ( chart ch )
13         {
14     if ( ch >= 'a' && ch <= 'z' ) return true ;
15     if ( ch >= 'A' && ch <= 'Z' ) return true ;
16     return false ;
17         }    
18
19
20 // These are not :
21         
22 string left ( string &s , size_t num )
23         {
24         if ( num <= 0 ) return "" ;
25         if ( num >= s.length() ) return s ;
26         return s.substr ( 0 , num ) ;
27         }
28      
29 string right ( string &s , int num )
30         {
31         if ( num <= 0 ) return "" ;
32     int from = s.length() - num ;
33     string ret ;
34     if ( from <= 0 ) ret = s ;
35     else ret = s.substr ( from , s.length() ) ;
36     return ret ;
37         }    
38
39 string upper ( string s ) // For internal purposes, will do...
40         {
41         size_t a ;
42         for ( a = 0 ; a < s.length() ; a++ )
43                 {
44         if ( s[a] >= 'a' && s[a] <= 'z' ) s[a] = s[a] - 'a' + 'A' ;
45                 }    
46     return s ;
47         }    
48
49 void explode ( chart ch , string &l , vector <string> &parts )
50         {
51     parts.clear () ;
52     size_t a , b ;
53     for ( a = b = 0 ; a < l.length() ; a++ )
54         {
55             if ( l[a] == ch )
56                {
57            parts.push_back ( l.substr ( b , a - b ) ) ;
58            b = a+1 ;
59                }    
60         }    
61         parts.push_back ( l.substr ( b , a - b ) ) ;
62
63         if ( debug ) cout << "Explode : " << l << endl ;
64         for ( a = 0 ; a < parts.size() ; a++ )
65                 if ( debug ) cout << a << " " << parts[a] << endl ;
66     if ( debug ) cout << endl ;         
67         }    
68         
69 string implode ( string mid , vector <string> &parts )
70         {
71     if ( parts.size() == 0 ) return "" ;
72     if ( parts.size() == 1 ) return parts[0] ;
73     string ret = parts[0] ;
74     for ( size_t a = 1 ; a < parts.size() ; a++ )
75         ret += mid + parts[a] ;
76         return ret ;
77         }    
78
79 string unquote ( chart quote , string &s )
80         {
81         size_t a ;
82         for ( a = 0 ; a < s.length() ; a++ )
83                 {
84                 if ( s[a] == quote && ( a == 0 || ( a > 0 && s[a-1] != '\\' ) ) )
85                    {
86                    s.insert ( a , "\\" ) ;
87                    a++ ;
88                    }    
89                 }    
90     return s ;
91         }    
92         
93 bool submatch ( string &main , string &sub , int from )
94         {
95         if ( from + sub.length() > main.length() ) return false ;
96         size_t a ;
97         for ( a = 0 ; a < sub.length() ; a++ )
98                 {
99                 if ( sub[a] != main[a+from] ) return false ;
100                 }    
101         return true ;
102         }
103      
104 int find_first ( chart c , string &s )
105         {
106         size_t a ;
107         for ( a = 0 ; a < s.length() && s[a] != c ; a++ ) ;
108         if ( a == s.length() ) return -1 ;
109     return a ;
110         }    
111      
112 int find_last ( chart c , string &s )
113         {
114         size_t a;
115         int b = -1 ;
116         for ( a = 0 ; a < s.length() ; a++ )
117                 {
118                 if ( s[a] == c ) b = a ;
119                 }    
120         return b ;
121         }    
122      
123 string before_first ( chart c , string s )
124         {
125         int pos = find_first ( c , s ) ;
126         if ( pos == -1 ) return s ;
127         return s.substr ( 0 , pos ) ;
128         }
129
130 string before_last ( chart c , string s )
131         {
132         int pos = find_last ( c , s ) ;
133         if ( pos == -1 ) return "" ;
134         return s.substr ( 0 , pos ) ;
135         }
136
137 string after_first ( chart c , string s )
138         {
139         int pos = find_first ( c , s ) ;
140         if ( pos == -1 ) return "" ;
141         return s.substr ( pos+1 , s.length() ) ;
142         }
143
144 string after_last ( chart c , string s )
145         {
146         int pos = find_last ( c , s ) ;
147         if ( pos == -1 ) return s ;
148         return s.substr ( pos+1 , s.length() ) ;
149         }
150      
151 string trim ( string &s )
152         {
153         if ( s.length() == 0 ) return s ;
154         if ( s[0] != ' ' && s[s.length()-1] != ' ' ) return s ;
155         size_t a;
156         int b ;
157         for ( a = 0 ; a < s.length() && s[a] == ' ' ; a++ ) ;
158         for ( b = s.length()-1 ; b >= 0 && s[b] == ' ' ; b-- ) ;
159         return s.substr ( a , b - a + 1 ) ;
160         }
161
162 int find_next_unquoted ( chart c , string &s , int start )
163         {
164         size_t a ;
165         chart lastquote = ' ' ;
166         for ( a = start ; a < s.length() ; a++ )
167                 {
168                 if ( s[a] == c && lastquote == ' ' ) return a ; // Success!
169                 if ( s[a] != SINGLE_QUOTE && s[a] != DOUBLE_QUOTE ) continue ; // No quotes, next
170                 if ( a > 0 && s[a-1] == '\\' ) continue ; // Ignore \' and \"
171                 if ( lastquote == ' ' ) lastquote = s[a] ; // Remember opening quote, text now quoted
172                 else if ( lastquote == s[a] ) lastquote = ' ' ; // Close quote, not quoted anymore
173                 }
174         return -1 ;
175         }
176     
177 string val ( int a )
178     {
179     char t[20] ;
180     sprintf ( t , "%d" , a ) ;
181     return string ( t ) ;
182     }
183
184 string xml_embed ( string inside , string tag , string param )
185     {
186     string ret ;
187     ret = "<" + tag ;
188     if ( param != "" ) ret += " " + param ;
189     if ( inside == "" ) return ret + "/>" ;
190     return ret + ">" + trim ( inside ) + "</" + tag + ">" ;
191     }
192
193 string xml_params ( string l ) // Yes, this function is thin...
194         {
195         string ret ;
196         vector <string> params ;
197         while ( l != "" )
198                 {
199                 int p = find_next_unquoted ( ' ' , l ) ;
200                 string first ;
201                 if ( p == -1 )
202                         {
203                         first = l ;
204                         l = "" ;
205                         }
206                 else
207                         {
208                     first = left ( l , p ) ;
209                     l = l.substr ( p , l.length() - p ) ;
210                         }        
211                 first = trim ( first ) ;
212                 l = trim ( l ) ;
213                 if ( first == "" ) continue ;
214                 
215                 p = find_next_unquoted ( '=' , first ) ;
216                 if ( p == -1 ) first = xml_embed ( first , "value" ) ;
217                 else
218                         {
219                         first = xml_embed ( left ( first , p ) , "key" ) +
220                                         xml_embed ( first.substr ( p + 1 , first.length() - p ) , "value" ) ;
221                         }    
222                 first = xml_embed ( first , "wikiparameter" ) ;
223                 ret += first ;
224                 }    
225         return ret ;
226         }
227