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 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 sal_Char* cpystr( sal_Char* dst, const sal_Char* src ) 79 { 80 const sal_Char* psrc = src; 81 sal_Char* pdst = dst; 82 83 while( *pdst++ = *psrc++ ); 84 return ( dst ); 85 } 86 87 sal_Char* cpynstr( sal_Char* dst, const sal_Char* src, sal_uInt32 cnt ) 88 { 89 90 const sal_Char* psrc = src; 91 sal_Char* pdst = dst; 92 sal_uInt32 len = cnt; 93 sal_uInt32 i; 94 95 if ( len >= AStringLen(src) ) 96 { 97 return( cpystr( dst, src ) ); 98 } 99 100 // copy string by char 101 for( i = 0; i < len; i++ ) 102 *pdst++ = *psrc++; 103 *pdst = '\0'; 104 105 return ( dst ); 106 } 107 108 //------------------------------------------------------------------------ 109 sal_Bool cmpstr( const sal_Char* str1, const sal_Char* str2, sal_uInt32 len ) 110 { 111 const sal_Char* pBuf1 = str1; 112 const sal_Char* pBuf2 = str2; 113 sal_uInt32 i = 0; 114 115 while ( (*pBuf1 == *pBuf2) && i < len ) 116 { 117 (pBuf1)++; 118 (pBuf2)++; 119 i++; 120 } 121 return( i == len ); 122 } 123 //------------------------------------------------------------------------ 124 sal_Bool cmpustr( const sal_Unicode* str1, const sal_Unicode* str2, sal_uInt32 len ) 125 { 126 const sal_Unicode* pBuf1 = str1; 127 const sal_Unicode* pBuf2 = str2; 128 sal_uInt32 i = 0; 129 130 while ( (*pBuf1 == *pBuf2) && i < len ) 131 { 132 (pBuf1)++; 133 (pBuf2)++; 134 i++; 135 } 136 return( i == len ); 137 } 138 139 //----------------------------------------------------------------------- 140 sal_Bool cmpustr( const sal_Unicode* str1, const sal_Unicode* str2 ) 141 { 142 const sal_Unicode* pBuf1 = str1; 143 const sal_Unicode* pBuf2 = str2; 144 sal_Bool res = sal_True; 145 146 while ( (*pBuf1 == *pBuf2) && *pBuf1 !='\0' && *pBuf2 != '\0') 147 { 148 (pBuf1)++; 149 (pBuf2)++; 150 } 151 if (*pBuf1 == '\0' && *pBuf2 == '\0') 152 res = sal_True; 153 else 154 res = sal_False; 155 return (res); 156 } 157 158 sal_Char* createName( sal_Char* dst, const sal_Char* meth, sal_uInt32 cnt ) 159 { 160 sal_Char* pdst = dst; 161 sal_Char nstr[16]; 162 sal_Char* pstr = nstr; 163 rtl_str_valueOfInt32( pstr, cnt, 10 ); 164 165 cpystr( pdst, meth ); 166 cpystr( pdst+ AStringLen(meth), "_" ); 167 168 if ( cnt < 100 ) 169 { 170 cpystr(pdst + AStringLen(pdst), "0" ); 171 } 172 if ( cnt < 10 ) 173 { 174 cpystr(pdst + AStringLen(pdst), "0" ); 175 } 176 177 cpystr( pdst + AStringLen(pdst), nstr ); 178 return( pdst ); 179 } 180 181 //------------------------------------------------------------------------ 182 // testing the method compareTo( const OString & aStr ) 183 //------------------------------------------------------------------------ 184 void makeComment( char *com, const char *str1, const char *str2, 185 sal_Int32 sgn ) 186 { 187 cpystr(com, str1); 188 int str1Length = AStringLen( str1 ); 189 const char *sign = (sgn == 0) ? " == " : (sgn > 0) ? " > " : " < " ; 190 cpystr(com + str1Length, sign); 191 int signLength = AStringLen(sign); 192 cpystr(com + str1Length + signLength, str2); 193 com[str1Length + signLength + AStringLen(str2)] = 0; 194 } 195 196 197 //------------------------------------------------------------------------ 198 199 sal_Bool AStringToFloatCompare ( const sal_Char *pStr, 200 const float nX, 201 const float nEPS 202 ) 203 { 204 sal_Bool cmp = sal_False; 205 206 if ( pStr != NULL ) 207 { 208 ::rtl::OString aStr(pStr); 209 210 float actNum = 0; 211 float expNum = nX; 212 float eps = nEPS; 213 214 actNum = aStr.toFloat(); 215 216 if ( abs( (int)(actNum - expNum) ) <= eps ) 217 { 218 cmp = sal_True; 219 } // if 220 } // if 221 222 return cmp; 223 } // AStringToFloatCompare 224 225 //------------------------------------------------------------------------ 226 227 sal_Bool AStringToDoubleCompare ( const sal_Char *pStr, 228 const double nX, 229 const double nEPS 230 ) 231 { 232 sal_Bool cmp = sal_False; 233 234 if ( pStr != NULL ) 235 { 236 ::rtl::OString aStr(pStr); 237 238 double actNum = 0; 239 double expNum = nX; 240 double eps = nEPS; 241 242 actNum = aStr.toDouble(); 243 244 if ( abs( (int)(actNum - expNum) ) <= eps ) 245 { 246 cmp = sal_True; 247 } // if 248 } // if 249 250 return cmp; 251 } // AStringToDoubleCompare 252 253 //------------------------------------------------------------------------ 254 255 256 //------------------------------------------------------------------------ 257 258 sal_uInt32 UStringLen( const sal_Unicode *pUStr ) 259 { 260 sal_uInt32 nUStrLen = 0; 261 262 if ( pUStr != NULL ) 263 { 264 const sal_Unicode *pTempUStr = pUStr; 265 266 while( *pTempUStr ) 267 { 268 pTempUStr++; 269 } // while 270 271 nUStrLen = (sal_uInt32)( pTempUStr - pUStr ); 272 } // if 273 274 return nUStrLen; 275 } // UStringLen 276 277 //------------------------------------------------------------------------ 278 279 sal_Bool AStringIsValid( const sal_Char *pAStr ) 280 { 281 if ( pAStr != NULL ) 282 { 283 sal_uInt32 nLen = AStringLen( pAStr ); 284 sal_uChar uChar = 0; 285 286 while ( ( nLen >= 0 ) && ( *pAStr ) ) 287 { 288 uChar = (unsigned char)*pAStr; 289 290 if ( uChar > 127 ) 291 { 292 return sal_False; 293 } // if 294 295 pAStr++; 296 297 // Since we are dealing with unsigned integers 298 // we want to make sure that the last number is 299 // indeed zero. 300 301 if ( nLen > 0 ) 302 { 303 nLen--; 304 } // if 305 else 306 { 307 break; 308 } // else 309 } // while 310 } // if 311 312 return sal_True; 313 } // AStringIsValid 314 315 //------------------------------------------------------------------------ 316 317 sal_Bool AStringNIsValid( const sal_Char *pAStr, 318 const sal_uInt32 nStrLen 319 ) 320 { 321 sal_uInt32 nLen = nStrLen; 322 sal_uChar uChar = 0; 323 324 while ( ( nLen >= 0 ) && ( *pAStr ) ) 325 { 326 uChar = (unsigned char)*pAStr; 327 328 if ( uChar > 127 ) 329 { 330 return sal_False; 331 } // if 332 333 pAStr++; 334 335 // Since we are dealing with unsigned integers 336 // we want to make sure that the last number is 337 // indeed zero. 338 339 if ( nLen > 0 ) 340 { 341 nLen--; 342 } // if 343 else 344 { 345 break; 346 } // else 347 } // while 348 349 return sal_True; 350 } // AStringNIsValid 351 352 //------------------------------------------------------------------------ 353 354 static inline sal_Int32 ACharToUCharCompare( const sal_Unicode *pUStr, 355 const sal_Char *pAStr 356 ) 357 { 358 sal_Int32 nCmp = 0; 359 sal_Int32 nUChar = (sal_Int32)*pUStr; 360 sal_Int32 nChar = (sal_Int32)((unsigned char)*pAStr); 361 362 nCmp = nUChar - nChar; 363 364 return nCmp; 365 } // ACharToUCharCompare 366 367 //------------------------------------------------------------------------ 368 369 sal_Int32 AStringToUStringCompare( const sal_Unicode *pUStr, 370 const sal_Char *pAStr 371 ) 372 { 373 sal_Int32 nCmp = kErrCompareAStringToUString; 374 375 if ( ( pUStr != NULL ) && ( pAStr != NULL ) ) 376 { 377 nCmp = ACharToUCharCompare( pUStr, pAStr ); 378 379 while ( ( nCmp == 0 ) && ( *pAStr ) ) 380 { 381 pUStr++; 382 pAStr++; 383 384 nCmp = ACharToUCharCompare( pUStr, pAStr ); 385 } // while 386 } // if 387 388 return nCmp; 389 } // AStringToUStringCompare 390 391 //------------------------------------------------------------------------ 392 393 sal_Int32 AStringToUStringNCompare( const sal_Unicode *pUStr, 394 const sal_Char *pAStr, 395 const sal_uInt32 nAStrCount 396 ) 397 { 398 sal_Int32 nCmp = kErrCompareNAStringToUString; 399 400 if ( ( pUStr != NULL ) && ( pAStr != NULL ) ) 401 { 402 sal_uInt32 nCount = nAStrCount; 403 404 nCmp = ACharToUCharCompare( pUStr, pAStr ); 405 406 while ( ( nCmp == 0 ) && ( *pAStr ) && ( nCount ) ) 407 { 408 pUStr++; 409 pAStr++; 410 411 nCmp = ACharToUCharCompare( pUStr, pAStr ); 412 413 // Since we are dealing with unsigned integers 414 // we want to make sure that the last number is 415 // indeed zero. 416 417 if ( nCount > 0 ) 418 { 419 nCount--; 420 } // if 421 else 422 { 423 break; 424 } // else 425 } // while 426 } // if 427 428 return nCmp; 429 } // AStringToUStringNCompare 430 431 //------------------------------------------------------------------------ 432 433 sal_Int32 AStringToRTLUStringCompare( const rtl_uString *pRTLUStr, 434 const sal_Char *pAStr 435 ) 436 { 437 sal_Int32 nCmp = kErrCompareAStringToRTLUString; 438 439 if ( ( pRTLUStr != NULL ) && ( pAStr != NULL ) ) 440 { 441 rtl_uString *pRTLUStrCopy = NULL; 442 443 rtl_uString_newFromString( &pRTLUStrCopy, pRTLUStr ); 444 445 if ( pRTLUStrCopy != NULL ) 446 { 447 const sal_Unicode *pUStr = rtl_uString_getStr( pRTLUStrCopy ); 448 449 if ( pUStr != NULL ) 450 { 451 nCmp = AStringToUStringCompare( pUStr, pAStr ); 452 } // if 453 454 rtl_uString_release( pRTLUStrCopy ); 455 456 pRTLUStrCopy = NULL; 457 } // if 458 } // if 459 460 return nCmp; 461 } // AStringToRTLUStringCompare 462 463 //------------------------------------------------------------------------ 464 465 sal_Int32 AStringToRTLUStringNCompare( const rtl_uString *pRTLUStr, 466 const sal_Char *pAStr, 467 const sal_uInt32 nAStrCount 468 ) 469 { 470 sal_Int32 nCmp = kErrCompareNAStringToRTLUString; 471 472 if ( ( pRTLUStr != NULL ) && ( pAStr != NULL ) ) 473 { 474 rtl_uString *pRTLUStrCopy = NULL; 475 476 rtl_uString_newFromString( &pRTLUStrCopy, pRTLUStr ); 477 478 if ( pRTLUStrCopy != NULL ) 479 { 480 const sal_Unicode *pUStr = rtl_uString_getStr( pRTLUStrCopy ); 481 482 if ( pUStr != NULL ) 483 { 484 nCmp = AStringToUStringNCompare( pUStr, pAStr, nAStrCount ); 485 } // if 486 487 rtl_uString_release( pRTLUStrCopy ); 488 489 pRTLUStrCopy = NULL; 490 } // if 491 } // if 492 493 return nCmp; 494 } // AStringToRTLUStringNCompare 495 496 //------------------------------------------------------------------------ 497 498 sal_Bool AStringToUStringCopy( sal_Unicode *pDest, 499 const sal_Char *pSrc 500 ) 501 { 502 sal_Bool bCopied = sal_False; 503 sal_uInt32 nCount = AStringLen( pSrc ); 504 sal_uInt32 nLen = nCount; 505 506 if ( ( pDest != NULL ) 507 && ( pSrc != NULL ) 508 && ( AStringNIsValid( pSrc, nLen ) ) 509 ) 510 { 511 while ( nCount >= 0 ) 512 { 513 *pDest = (unsigned char)*pSrc; 514 515 pDest++; 516 pSrc++; 517 518 // Since we are dealing with unsigned integers 519 // we want to make sure that the last number is 520 // indeed zero. 521 522 if ( nCount > 0 ) 523 { 524 nCount--; 525 } // if 526 else 527 { 528 break; 529 } // else 530 } // while 531 532 if ( nCount == 0 ) 533 { 534 bCopied = sal_True; 535 } // if 536 } // if 537 538 return bCopied; 539 } // AStringToUStringCopy 540 541 //------------------------------------------------------------------------ 542 543 sal_Bool AStringToUStringNCopy( sal_Unicode *pDest, 544 const sal_Char *pSrc, 545 const sal_uInt32 nSrcLen 546 ) 547 { 548 sal_Bool bCopied = sal_False; 549 sal_uInt32 nCount = nSrcLen; 550 sal_uInt32 nLen = nSrcLen; 551 552 if ( ( pDest != NULL ) 553 && ( pSrc != NULL ) 554 && ( AStringNIsValid( pSrc, nLen ) ) 555 ) 556 { 557 while ( nCount >= 0 ) 558 { 559 *pDest = (unsigned char)*pSrc; 560 561 pDest++; 562 pSrc++; 563 564 // Since we are dealing with unsigned integers 565 // we want to make sure that the last number is 566 // indeed zero. 567 568 if ( nCount > 0 ) 569 { 570 nCount--; 571 } // if 572 else 573 { 574 break; 575 } // else 576 } // while 577 578 if ( nCount == 0 ) 579 { 580 bCopied = sal_True; 581 } // if 582 } // if 583 584 return bCopied; 585 } // AStringToUStringNCopy 586 587 //------------------------------------------------------------------------ 588 //------------------------------------------------------------------------ 589 590