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