1 /*************************************************************************
2 #
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org 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 Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26 #*************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
30 
31 //------------------------------------------------------------------------
32 //------------------------------------------------------------------------
33 
34 #include <math.h>
35 #include <stdlib.h>
36 
37 //------------------------------------------------------------------------
38 //------------------------------------------------------------------------
39 
40 #ifndef _SAL_TYPES_H_
41 	#include <sal/types.h>
42 #endif
43 
44 #ifndef _RTL_USTRING_H_
45 	#include <rtl/ustring.h>
46 #endif
47 
48 #ifndef _RTL_STRING_HXX_
49 	#include <rtl/string.hxx>
50 #endif
51 
52 //------------------------------------------------------------------------
53 //------------------------------------------------------------------------
54 
55 #ifndef _RTL_STRING_UTILS_CONST_H_
56 	#include <rtl_String_Utils_Const.h>
57 #endif
58 
59 //------------------------------------------------------------------------
60 //------------------------------------------------------------------------
61 
62 using namespace rtl;
63 
64 sal_uInt32 AStringLen( const sal_Char *pAStr )
65 {
66 	sal_uInt32  nStrLen = 0;
67 
68 	if ( pAStr != NULL )
69 	{
70 		const sal_Char *pTempStr = pAStr;
71 
72 		while( *pTempStr )
73 		{
74 			pTempStr++;
75 		} // while
76 
77 		nStrLen = (sal_uInt32)( pTempStr - pAStr );
78 	} // if
79 
80 	return nStrLen;
81 } // AStringLen
82 sal_Char* cpystr( sal_Char* dst, const sal_Char* src )
83 {
84     const sal_Char* psrc = src;
85     sal_Char* pdst = dst;
86 
87     while( *pdst++ = *psrc++ );
88     return ( dst );
89 }
90 
91 sal_Char* cpynstr( sal_Char* dst, const sal_Char* src, sal_uInt32 cnt )
92 {
93 
94     const sal_Char* psrc = src;
95     sal_Char* pdst = dst;
96     sal_uInt32 len = cnt;
97     sal_uInt32 i;
98 
99     if ( len >= AStringLen(src) )
100     {
101         return( cpystr( dst, src ) );
102     }
103 
104     // copy string by char
105     for( i = 0; i < len; i++ )
106         *pdst++ = *psrc++;
107     *pdst = '\0';
108 
109     return ( dst );
110 }
111 
112 //------------------------------------------------------------------------
113 sal_Bool cmpstr( const sal_Char* str1, const sal_Char* str2, sal_uInt32 len )
114 {
115     const sal_Char* pBuf1 = str1;
116     const sal_Char* pBuf2 = str2;
117     sal_uInt32 i = 0;
118 
119     while ( (*pBuf1 == *pBuf2) && i < len )
120     {
121         (pBuf1)++;
122         (pBuf2)++;
123         i++;
124     }
125     return( i == len );
126 }
127 //------------------------------------------------------------------------
128 sal_Bool cmpustr( const sal_Unicode* str1, const sal_Unicode* str2, sal_uInt32 len )
129 {
130     const sal_Unicode* pBuf1 = str1;
131     const sal_Unicode* pBuf2 = str2;
132     sal_uInt32 i = 0;
133 
134     while ( (*pBuf1 == *pBuf2) && i < len )
135     {
136         (pBuf1)++;
137         (pBuf2)++;
138         i++;
139     }
140     return( i == len );
141 }
142 
143 //-----------------------------------------------------------------------
144 sal_Bool cmpustr( const sal_Unicode* str1, const sal_Unicode* str2 )
145 {
146     const sal_Unicode* pBuf1 = str1;
147     const sal_Unicode* pBuf2 = str2;
148     sal_Bool res = sal_True;
149 
150     while ( (*pBuf1 == *pBuf2) && *pBuf1 !='\0' && *pBuf2 != '\0')
151     {
152         (pBuf1)++;
153         (pBuf2)++;
154     }
155     if (*pBuf1 == '\0' && *pBuf2 == '\0')
156         res = sal_True;
157     else
158         res = sal_False;
159     return (res);
160 }
161 
162 sal_Char* createName( sal_Char* dst, const sal_Char* meth, sal_uInt32 cnt )
163 {
164     sal_Char* pdst = dst;
165     sal_Char nstr[16];
166     sal_Char* pstr = nstr;
167     rtl_str_valueOfInt32( pstr, cnt, 10 );
168 
169     cpystr( pdst, meth );
170     cpystr( pdst+ AStringLen(meth), "_" );
171 
172     if ( cnt < 100 )
173     {
174         cpystr(pdst + AStringLen(pdst), "0" );
175     }
176     if ( cnt < 10 )
177     {
178         cpystr(pdst + AStringLen(pdst), "0" );
179     }
180 
181     cpystr( pdst + AStringLen(pdst), nstr );
182     return( pdst );
183 }
184 
185 //------------------------------------------------------------------------
186 //  testing the method compareTo( const OString & aStr )
187 //------------------------------------------------------------------------
188 void makeComment( char *com, const char *str1, const char *str2,
189                                                             sal_Int32 sgn )
190 {
191     cpystr(com, str1);
192     int str1Length = AStringLen( str1 );
193     const char *sign = (sgn == 0) ? " == " : (sgn > 0) ? " > " : " < " ;
194     cpystr(com + str1Length, sign);
195     int signLength = AStringLen(sign);
196     cpystr(com + str1Length + signLength, str2);
197     com[str1Length + signLength + AStringLen(str2)] = 0;
198 }
199 
200 
201 //------------------------------------------------------------------------
202 
203 sal_Bool AStringToFloatCompare ( const sal_Char  *pStr,
204                                  const float      nX,
205                                  const float      nEPS
206                                 )
207 {
208 	sal_Bool cmp = sal_False;
209 
210 	if ( pStr != NULL )
211 	{
212 		::rtl::OString aStr(pStr);
213 
214 		float actNum = 0;
215 		float expNum = nX;
216 		float eps    = nEPS;
217 
218 		actNum = aStr.toFloat();
219 
220         if ( abs( (int)(actNum - expNum) ) <= eps )
221 		{
222 			cmp = sal_True;
223 		} // if
224 	} // if
225 
226 	return cmp;
227 } // AStringToFloatCompare
228 
229 //------------------------------------------------------------------------
230 
231 sal_Bool AStringToDoubleCompare ( const sal_Char  *pStr,
232                                   const double     nX,
233                                   const double     nEPS
234                                 )
235 {
236 	sal_Bool cmp = sal_False;
237 
238 	if ( pStr != NULL )
239 	{
240 		::rtl::OString aStr(pStr);
241 
242 		double actNum = 0;
243 		double expNum = nX;
244 		double eps    = nEPS;
245 
246 		actNum = aStr.toDouble();
247 
248         if ( abs( (int)(actNum - expNum) ) <= eps )
249 		{
250 			cmp = sal_True;
251 		} // if
252 	} // if
253 
254 	return cmp;
255 } // AStringToDoubleCompare
256 
257 //------------------------------------------------------------------------
258 
259 
260 //------------------------------------------------------------------------
261 
262 sal_uInt32 UStringLen( const sal_Unicode *pUStr )
263 {
264 	sal_uInt32 nUStrLen = 0;
265 
266 	if ( pUStr != NULL )
267 	{
268 		const sal_Unicode *pTempUStr = pUStr;
269 
270 		while( *pTempUStr )
271 		{
272 			pTempUStr++;
273 		} // while
274 
275 		nUStrLen = (sal_uInt32)( pTempUStr - pUStr );
276 	} // if
277 
278 	return nUStrLen;
279 } // UStringLen
280 
281 //------------------------------------------------------------------------
282 
283 sal_Bool AStringIsValid( const sal_Char  *pAStr )
284 {
285 	if ( pAStr != NULL )
286 	{
287 		sal_uInt32 nLen  = AStringLen( pAStr );
288 		sal_uChar  uChar = 0;
289 
290 		while ( ( nLen >= 0 ) && ( *pAStr ) )
291 		{
292 			uChar = (unsigned char)*pAStr;
293 
294 			if ( uChar > 127 )
295 			{
296 				return sal_False;
297 			} // if
298 
299 			pAStr++;
300 
301 			// Since we are dealing with unsigned integers
302 			// we want to make sure that the last number is
303 			// indeed zero.
304 
305 			if ( nLen > 0 )
306 			{
307 				nLen--;
308 			} // if
309 			else
310 			{
311 				break;
312 			} // else
313 		} // while
314 	} // if
315 
316 	return sal_True;
317 } // AStringIsValid
318 
319 //------------------------------------------------------------------------
320 
321 sal_Bool AStringNIsValid( const sal_Char   *pAStr,
322                           const sal_uInt32  nStrLen
323                         )
324 {
325 	sal_uInt32 nLen  = nStrLen;
326 	sal_uChar  uChar = 0;
327 
328 	while ( ( nLen >= 0 ) && ( *pAStr ) )
329 	{
330 		uChar = (unsigned char)*pAStr;
331 
332 		if ( uChar > 127 )
333 		{
334 			return sal_False;
335 		} // if
336 
337 		pAStr++;
338 
339 		// Since we are dealing with unsigned integers
340 		// we want to make sure that the last number is
341 		// indeed zero.
342 
343 		if ( nLen > 0 )
344 		{
345 			nLen--;
346 		} // if
347 		else
348 		{
349 			break;
350 		} // else
351 	} // while
352 
353 	return sal_True;
354 } // AStringNIsValid
355 
356 //------------------------------------------------------------------------
357 
358 static inline sal_Int32 ACharToUCharCompare( const sal_Unicode *pUStr,
359                                              const sal_Char    *pAStr
360                                            )
361 {
362 	sal_Int32  nCmp   = 0;
363 	sal_Int32  nUChar = (sal_Int32)*pUStr;
364 	sal_Int32  nChar  = (sal_Int32)((unsigned char)*pAStr);
365 
366 	nCmp = nUChar - nChar;
367 
368 	return  nCmp;
369 } // ACharToUCharCompare
370 
371 //------------------------------------------------------------------------
372 
373 sal_Int32 AStringToUStringCompare( const sal_Unicode *pUStr,
374                                    const sal_Char    *pAStr
375                                  )
376 {
377  	sal_Int32 nCmp = kErrCompareAStringToUString;
378 
379 	if ( ( pUStr != NULL ) && ( pAStr != NULL ) )
380 	{
381 		nCmp = ACharToUCharCompare( pUStr, pAStr );
382 
383 		while ( ( nCmp == 0 ) && ( *pAStr ) )
384 		{
385 			pUStr++;
386 			pAStr++;
387 
388 			nCmp = ACharToUCharCompare( pUStr, pAStr );
389 		} // while
390 	} // if
391 
392 	return nCmp;
393 } // AStringToUStringCompare
394 
395 //------------------------------------------------------------------------
396 
397 sal_Int32 AStringToUStringNCompare( const sal_Unicode  *pUStr,
398                                     const sal_Char     *pAStr,
399                                     const sal_uInt32    nAStrCount
400                                    )
401 {
402 	sal_Int32 nCmp = kErrCompareNAStringToUString;
403 
404 	if ( ( pUStr != NULL ) && ( pAStr != NULL ) )
405 	{
406 		sal_uInt32 nCount = nAStrCount;
407 
408 		nCmp = ACharToUCharCompare( pUStr, pAStr );
409 
410 		while ( ( nCmp == 0 ) && ( *pAStr ) && ( nCount ) )
411 		{
412 			pUStr++;
413 			pAStr++;
414 
415 			nCmp = ACharToUCharCompare( pUStr, pAStr );
416 
417 			// Since we are dealing with unsigned integers
418 			// we want to make sure that the last number is
419 			// indeed zero.
420 
421 			if ( nCount > 0 )
422 			{
423 				nCount--;
424 			} // if
425 			else
426 			{
427 				break;
428 			} // else
429 		} // while
430 	} // if
431 
432 	return nCmp;
433 } // AStringToUStringNCompare
434 
435 //------------------------------------------------------------------------
436 
437 sal_Int32 AStringToRTLUStringCompare( const rtl_uString  *pRTLUStr,
438                                       const sal_Char     *pAStr
439                                     )
440 {
441 	sal_Int32 nCmp = kErrCompareAStringToRTLUString;
442 
443 	if ( ( pRTLUStr != NULL ) && ( pAStr != NULL ) )
444 	{
445 		rtl_uString *pRTLUStrCopy = NULL;
446 
447 		rtl_uString_newFromString( &pRTLUStrCopy, pRTLUStr );
448 
449 		if ( pRTLUStrCopy != NULL )
450 		{
451 			const sal_Unicode *pUStr = rtl_uString_getStr( pRTLUStrCopy );
452 
453 			if ( pUStr != NULL )
454 			{
455 				nCmp = AStringToUStringCompare( pUStr, pAStr );
456 			} // if
457 
458 			rtl_uString_release( pRTLUStrCopy );
459 
460 			pRTLUStrCopy = NULL;
461 		} // if
462 	} // if
463 
464 	return nCmp;
465 } // AStringToRTLUStringCompare
466 
467 //------------------------------------------------------------------------
468 
469 sal_Int32 AStringToRTLUStringNCompare( const rtl_uString  *pRTLUStr,
470                                        const sal_Char     *pAStr,
471                                        const sal_uInt32    nAStrCount
472                                      )
473 {
474 	sal_Int32 nCmp = kErrCompareNAStringToRTLUString;
475 
476 	if ( ( pRTLUStr != NULL ) && ( pAStr != NULL ) )
477 	{
478 		rtl_uString *pRTLUStrCopy = NULL;
479 
480 		rtl_uString_newFromString( &pRTLUStrCopy, pRTLUStr );
481 
482 		if ( pRTLUStrCopy != NULL )
483 		{
484 			const sal_Unicode  *pUStr = rtl_uString_getStr( pRTLUStrCopy );
485 
486 			if ( pUStr != NULL )
487 			{
488 				nCmp = AStringToUStringNCompare( pUStr, pAStr, nAStrCount );
489 			} // if
490 
491 			rtl_uString_release( pRTLUStrCopy );
492 
493 			pRTLUStrCopy = NULL;
494 		} // if
495 	} // if
496 
497 	return nCmp;
498 } // AStringToRTLUStringNCompare
499 
500 //------------------------------------------------------------------------
501 
502 sal_Bool AStringToUStringCopy( sal_Unicode     *pDest,
503                                const sal_Char  *pSrc
504                              )
505 {
506 	sal_Bool    bCopied = sal_False;
507 	sal_uInt32  nCount  = AStringLen( pSrc );
508 	sal_uInt32  nLen    = nCount;
509 
510 	if (    ( pDest != NULL )
511 	     && ( pSrc  != NULL )
512 	     && ( AStringNIsValid( pSrc, nLen ) )
513 	   )
514 	{
515 		while ( nCount >= 0 )
516 		{
517 			*pDest = (unsigned char)*pSrc;
518 
519 			pDest++;
520 			pSrc++;
521 
522 			// Since we are dealing with unsigned integers
523 			// we want to make sure that the last number is
524 			// indeed zero.
525 
526 			if ( nCount > 0 )
527 			{
528 				nCount--;
529 			} // if
530 			else
531 			{
532 				break;
533 			} // else
534 		} // while
535 
536 		if ( nCount == 0 )
537 		{
538 			bCopied = sal_True;
539 		} // if
540 	} // if
541 
542 	return  bCopied;
543 } // AStringToUStringCopy
544 
545 //------------------------------------------------------------------------
546 
547 sal_Bool AStringToUStringNCopy( sal_Unicode       *pDest,
548                                 const sal_Char    *pSrc,
549                                 const sal_uInt32   nSrcLen
550                               )
551 {
552 	sal_Bool    bCopied = sal_False;
553 	sal_uInt32  nCount  = nSrcLen;
554 	sal_uInt32  nLen    = nSrcLen;
555 
556 	if (    ( pDest != NULL )
557 	     && ( pSrc  != NULL )
558 	     && ( AStringNIsValid( pSrc, nLen ) )
559 	   )
560 	{
561 		while ( nCount >= 0 )
562 		{
563 			*pDest = (unsigned char)*pSrc;
564 
565 			pDest++;
566 			pSrc++;
567 
568 			// Since we are dealing with unsigned integers
569 			// we want to make sure that the last number is
570 			// indeed zero.
571 
572 			if ( nCount > 0 )
573 			{
574 				nCount--;
575 			} // if
576 			else
577 			{
578 				break;
579 			} // else
580 		} // while
581 
582 		if ( nCount == 0 )
583 		{
584 			bCopied = sal_True;
585 		} // if
586 	} // if
587 
588 	return  bCopied;
589 } // AStringToUStringNCopy
590 
591 //------------------------------------------------------------------------
592 //------------------------------------------------------------------------
593 
594