]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/ustring.cpp
* all: new skin text variable "$B" to get the stream bitrate
[vlc] / modules / gui / skins2 / utils / ustring.cpp
1 /*****************************************************************************
2  * ustring.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <string.h>
26 #include <sstream>
27 #include "ustring.hpp"
28
29
30 const uint32_t UString::npos = 0xffffffff;
31
32
33 UString::UString( const UString &rOther ): SkinObject( rOther.getIntf() )
34 {
35     m_length = rOther.m_length;
36     m_pString = new uint32_t[m_length + 1];
37     memcpy( m_pString, rOther.m_pString, 4 * m_length + 4);
38 }
39
40
41 UString::UString( intf_thread_t *pIntf, const char *pString ):
42     SkinObject( pIntf )
43 {
44     // First we compute the length of the string
45     const char *pCur = pString;
46     for( m_length = 0; pCur && *pCur; m_length++ )
47     {
48         if( (*pCur & 0xfc) == 0xfc )
49         {
50             pCur += 6;
51         }
52         else if ( (*pCur & 0xf8 ) == 0xf8 )
53         {
54             pCur += 5;
55         }
56         else if ( (*pCur & 0xf0 ) == 0xf0 )
57         {
58             pCur += 4;
59         }
60         else if ( (*pCur & 0xe0 ) == 0xe0 )
61         {
62             pCur += 3;
63         }
64         else if ( (*pCur & 0xc0 ) == 0xc0 )
65         {
66             pCur += 2;
67         }
68         else
69         {
70             pCur++;
71         }
72     }
73     if( !pCur || *pCur )
74     {
75         msg_Err( pIntf, "invalid UTF8 string: %s", pString );
76         m_length = 0;
77         m_pString = NULL;
78         return;
79     }
80
81     m_pString = new uint32_t[m_length + 1];
82
83     // Convert the UTF8 string into UNICODE
84     pCur = pString;
85     uint32_t aChar = 0;  // current unicode character
86     int remaining = 0;   // remaining bytes
87     for( uint32_t i = 0; i <= m_length; i++ )
88     {
89         if( (*pCur & 0xfc) == 0xfc )
90         {
91             aChar = *pCur & 1;
92             remaining = 5;
93         }
94         else if ( (*pCur & 0xf8 ) == 0xf8 )
95         {
96             aChar = *pCur & 3;
97             remaining = 4;
98         }
99         else if ( (*pCur & 0xf0 ) == 0xf0 )
100         {
101             aChar = *pCur & 7;
102             remaining = 3;
103         }
104         else if ( (*pCur & 0xe0 ) == 0xe0 )
105         {
106             aChar = *pCur & 15;
107             remaining = 2;
108         }
109         else if ( (*pCur & 0xc0 ) == 0xc0 )
110         {
111             aChar = *pCur & 31;
112             remaining = 1;
113         }
114         else
115         {
116             aChar = *pCur;
117             remaining = 0;
118         }
119         while( remaining )
120         {
121             pCur++;
122             remaining--;
123             aChar = ( aChar << 6 ) | ( *pCur & 0x3f );
124         }
125         m_pString[i] = aChar;
126         pCur++;
127     }
128     m_pString[m_length] = 0;
129 }
130
131
132 UString::~UString()
133 {
134     if( m_pString )
135     {
136         delete[] m_pString;
137     }
138 }
139
140
141 bool UString::operator ==( const UString &rOther ) const
142 {
143     if( size() != rOther.size() )
144     {
145         return false;
146     }
147
148     for( uint32_t i = 0; i < size(); i++ )
149     {
150         if( m_pString[i] != rOther.m_pString[i] )
151         {
152             return false;
153         }
154     }
155
156     return true;
157 }
158
159
160 bool UString::operator !=( const UString &rOther ) const
161 {
162     return !(*this == rOther);
163 }
164
165
166 bool UString::operator <( const UString &rOther ) const
167 {
168     const uint32_t *pOther = rOther.u_str();
169     uint32_t i;
170     for( i = 0; i < __MIN(m_length, rOther.length()); i++ )
171     {
172         if( m_pString[i] < pOther[i] )
173         {
174             return true;
175         }
176         else if( m_pString[i] > pOther[i] )
177         {
178             return false;
179         }
180     }
181     return( m_pString[i] < pOther[i] );
182 }
183
184
185 bool UString::operator <=( const UString &rOther ) const
186 {
187     return !( rOther < *this );
188 }
189
190
191 bool UString::operator >( const UString &rOther ) const
192 {
193     return ( rOther < *this );
194 }
195
196
197 bool UString::operator >=( const UString &rOther ) const
198 {
199     return !( *this < rOther );
200 }
201
202
203 void UString::operator =( const UString &rOther )
204 {
205     m_length = rOther.m_length;
206     delete[] m_pString;
207     m_pString = new uint32_t[size() + 1];
208     for( uint32_t i = 0; i <= size(); i++ )
209     {
210         m_pString[i] = rOther.m_pString[i];
211     }
212 }
213
214
215 void UString::operator +=( const UString &rOther )
216 {
217     int tempLength = this->length() + rOther.length();
218     uint32_t *pTempString = new uint32_t[tempLength + 1];
219     // Copy the first string
220     memcpy( pTempString, this->m_pString, 4 * this->size() );
221     // Append the second string
222 //     memcpy( pTempString + 4 * size(), rOther.m_pString,
223 //             4 * rOther.size() );
224     for( uint32_t i = 0; i < rOther.size(); i++ )
225     {
226         pTempString[this->size() + i] = rOther.m_pString[i];
227     }
228     pTempString[tempLength] = 0;
229
230     // Set the string internally
231     delete[] m_pString;
232     m_pString = pTempString;
233     m_length = tempLength;
234 }
235
236
237 const UString UString::operator +( const UString &rOther ) const
238 {
239     UString result( *this );
240     result += rOther;
241
242     return result;
243 }
244
245
246 const UString UString::operator +( const char *pString ) const
247 {
248     UString temp( getIntf(), pString );
249     return (*this + temp );
250 }
251
252
253 void UString::debug() const
254 {
255     char *s = new char[size() + 1];
256     for( uint32_t i = 0; i < size(); i++ )
257     {
258         s[i] = (char)m_pString[i];
259     }
260     s[size()] = '\0';
261     msg_Err( getIntf(), "%s", s );
262     delete[] s;
263 }
264
265
266 uint32_t UString::find( const UString &str, uint32_t position ) const
267 {
268     uint32_t pos;
269     for( pos = position; pos + str.size() <= size(); pos++ )
270     {
271         bool match = true;
272         for( uint32_t i = 0; i < str.size(); i++ )
273         {
274             if( m_pString[pos + i] != str.m_pString[i] )
275             {
276                 match = false;
277                 break;
278             }
279         }
280
281         // Found
282         if( match )
283         {
284             return pos;
285         }
286     }
287
288     // Not found
289     return npos;
290 }
291
292
293 uint32_t UString::find( const char *pString, uint32_t position ) const
294 {
295     UString tmp( getIntf(), pString );
296     return find( tmp, position );
297 }
298
299
300 void UString::replace( uint32_t position, uint32_t n1, const UString &str )
301 {
302     UString start( substr( 0, position ) );
303     UString end( substr( position + n1 ) );
304     *this = start + str + end;
305 }
306
307
308 void UString::replace( uint32_t position, uint32_t n1, const char *pString )
309 {
310     replace( position, n1, UString( getIntf(), pString ) );
311 }
312
313
314 UString UString::substr( uint32_t position, uint32_t n) const
315 {
316     UString tmp( getIntf(), "" );
317     if( position > size() )
318     {
319         msg_Err( getIntf(), "invalid position in UString::substr()" );
320         return tmp;
321     }
322     tmp.m_length = (n < size() - position) ? n : size() - position;
323     delete[] tmp.m_pString;
324     tmp.m_pString = new uint32_t[tmp.size() + 1];
325     for( uint32_t i = 0; i < tmp.size(); i++ )
326     {
327         tmp.m_pString[i] = m_pString[position + i];
328     }
329
330     return tmp;
331 }
332
333
334 UString UString::fromInt( intf_thread_t *pIntf, int number)
335 {
336     stringstream ss;
337     ss << number;
338     return UString( pIntf, ss.str().c_str() );
339 }
340