00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATString.c,v 1.22 2004/04/02 12:31:58 hartmutkaiser Exp $";
00020
00021
00022
00023 #include <math.h>
00024 #include <stdio.h>
00025 #include <limits.h>
00026 #include <string.h>
00027 #include <stdlib.h>
00028 #include <errno.h>
00029
00030
00031
00032 #include "GATType.h"
00033 #include "GATString.h"
00034 #include "GATErrors.h"
00035 #include "GATInternal.h"
00036 #include "GATXdsWrapper.h"
00037
00038
00039
00040 #include <iconv.h>
00041
00042
00043
00044
00045 #define GATSTRING_BUFFER_LENGTH 1024
00046
00047
00048 #define GATSTRING_MAX_ENCODING_LENGTH 41
00049
00050
00051
00052
00053 GATOBJECT_DEFINE_VTABLE(GATString);
00054 GATSERIALISABLE_DEFINE_VTABLE(GATString);
00055
00056
00057 GATOBJECT_DEFINE_CONVERTERS(GATString);
00058
00059 struct GATString_S
00060 {
00061 GATString_vtable *GATObject__vtable;
00062 GATString_ISerialisable_vtable *GATSerialisable__vtable;
00063
00064 char *byteArray;
00065 GATuint32 lengthInBytes;
00066 char encoding[GATSTRING_MAX_ENCODING_LENGTH];
00067 };
00068
00069
00070
00071
00072 static GATString_vtable GATString__vtable = {
00073 GATString_GetType,
00074 GATString_Destroy,
00075 GATString_Equals,
00076 GATString_Clone,
00077 GATString_GetInterface,
00078 NULL
00079 };
00080
00081 static GATString_ISerialisable_vtable GATString_ISerialisable__vtable = {
00082 GATString_Serialise,
00083 GATString_DeSerialise,
00084 GATString_GetIsDirty
00085 };
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 GATResult GATString_Register_GATSerialisable()
00096 {
00097 return GATObject_Register_GATSerialisable(GATType_GATString,
00098 &GATString_ISerialisable__vtable);
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 GATString GATString_Create(const char *byteArray, GATuint32 lengthInBytes, const char *encoding)
00329 {
00330 GATString this;
00331
00332 this = NULL;
00333
00334 if( (NULL != byteArray) && (0 < lengthInBytes) && (NULL != encoding) )
00335 {
00336 this = (GATString) malloc( sizeof(*this) );
00337 if(NULL != this)
00338 {
00339 float charPerByte;
00340 char *tempByteArray;
00341 size_t lengthInChars;
00342
00343 this->byteArray = NULL;
00344 this->GATObject__vtable = &GATString__vtable;
00345 this->GATSerialisable__vtable = &GATString_ISerialisable__vtable;
00346 this->lengthInBytes = lengthInBytes;
00347
00348 strncpy( this->encoding, encoding, GATSTRING_MAX_ENCODING_LENGTH );
00349 (this->encoding)[GATSTRING_MAX_ENCODING_LENGTH - 1] = '\0';
00350
00351 charPerByte = ( 8 / CHAR_BIT );
00352 lengthInChars = (size_t) ceil( lengthInBytes * charPerByte );
00353 tempByteArray = (char *) malloc( lengthInChars );
00354 if(NULL == tempByteArray)
00355 {
00356 GATString_Destroy( &this);
00357 }
00358 else
00359 {
00360 memcpy( (void *) tempByteArray, (const void *) byteArray, lengthInChars);
00361 this->byteArray = tempByteArray;
00362 }
00363 }
00364 }
00365
00366 return this;
00367 }
00368
00369
00370
00371
00372
00373
00374
00375
00376 GATType GATString_GetType(GATString_const string)
00377 {
00378 GAT_UNUSED_PARAMETER(string);
00379 return GATType_GATString;
00380 }
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 int GATString_Equals(GATString_const this, GATString_const that, GATBool *isequal)
00402 {
00403 int retval;
00404 GATuint32 count;
00405 GATString thisString;
00406 GATString thatString;
00407 GATuint32 thisArrayLength;
00408 GATuint32 thatArrayLength;
00409
00410 retval = GAT_INVALID_PARAMETER;
00411 if( (NULL != this) && (NULL != that) && (NULL != isequal) )
00412 {
00413 (*isequal) = GATFalse;
00414
00415 if( GAT_SUCCESS == (retval = GATString_Translate(this, "UTF-32BE", &thisString)) )
00416 {
00417 if( GAT_SUCCESS == (retval = GATString_Translate(that, "UTF-32BE", &thatString)) )
00418 {
00419 thisArrayLength = (GATuint32) (GATString_GetLengthInBytes(thisString) / 4);
00420 thatArrayLength = (GATuint32) (GATString_GetLengthInBytes(thatString) / 4);
00421
00422 if( thisArrayLength == thatArrayLength )
00423 {
00424 GATBool equalFlag;
00425 const GATuint32 *thisArray;
00426 const GATuint32 *thatArray;
00427
00428 equalFlag = GATTrue;
00429 thisArray = (const GATuint32 *) GATString_GetBuffer(thisString);
00430 thatArray = (const GATuint32 *) GATString_GetBuffer(thatString);
00431
00432 for(count = 0; (count < thisArrayLength) && (GATTrue == equalFlag); count++)
00433 {
00434 if(thisArray[count] != thatArray[count])
00435 {
00436 equalFlag = GATFalse;
00437 }
00438 }
00439
00440 if( GATTrue == equalFlag )
00441 {
00442 (*isequal) = GATTrue;
00443 }
00444 }
00445 GATString_Destroy(&thatString);
00446 }
00447 GATString_Destroy(&thisString);
00448 }
00449 }
00450 return retval;
00451 }
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 int GATString_Clone(GATString_const string, GATString *stringClone)
00463 {
00464 int retval;
00465
00466 retval = GAT_INVALID_PARAMETER;
00467 if( (NULL != string) && (NULL != stringClone) )
00468 {
00469 GATuint32 length;
00470 const char *buffer;
00471 const char *encoding;
00472
00473 (*stringClone) = NULL;
00474
00475 retval = GAT_MEMORYFAILURE;
00476
00477 buffer = GATString_GetBuffer(string);
00478 encoding = GATString_GetEncoding(string);
00479 length = GATString_GetLengthInBytes(string);
00480
00481 (*stringClone) = GATString_Create(buffer, length, encoding);
00482
00483 if( NULL != (*stringClone) )
00484 {
00485 retval = GAT_SUCCESS;
00486 }
00487 }
00488
00489 return retval;
00490 }
00491
00492
00493
00494
00495
00496
00497 void GATString_Destroy(GATString *string)
00498 {
00499 if( (NULL != string) && (NULL != (*string)) )
00500 {
00501 free( (*string)->byteArray );
00502
00503 free( (*string) );
00504 (*string) = NULL;
00505 }
00506 }
00507
00508
00509
00510
00511
00512
00513
00514
00515 GATuint32 GATString_GetLengthInBytes(GATString_const string)
00516 {
00517 GATuint32 lengthInBytes;
00518
00519 lengthInBytes = -1;
00520
00521 if(NULL != string)
00522 {
00523 lengthInBytes = string->lengthInBytes;
00524 }
00525
00526 return lengthInBytes;
00527 }
00528
00529
00530
00531
00532
00533
00534
00535 const char * GATString_GetBuffer(GATString_const string)
00536 {
00537 const char *buffer;
00538
00539 buffer = NULL;
00540
00541 if(NULL != string)
00542 {
00543 buffer = string->byteArray;
00544 }
00545
00546 return buffer;
00547 }
00548
00549
00550
00551
00552
00553
00554
00555 const char * GATString_GetEncoding(GATString_const string)
00556 {
00557 const char *encoding;
00558
00559 encoding = NULL;
00560
00561 if(NULL != string)
00562 {
00563 encoding = string->encoding;
00564 }
00565
00566 return encoding;
00567 }
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591 int GATString_Translate(GATString_const string, const char *newEncoding, GATString *result)
00592 {
00593 int retval;
00594
00595 retval = GAT_INVALID_PARAMETER;
00596
00597 if( (NULL != string) && (NULL != newEncoding) && (NULL != result) )
00598 {
00599 const char *oldEncoding;
00600 iconv_t conversionDescriptor;
00601
00602 (*result) = NULL;
00603 retval = GAT_INVALID_ENCODING;
00604 oldEncoding = GATString_GetEncoding( string );
00605 conversionDescriptor = iconv_open(newEncoding, oldEncoding);
00606 if( (iconv_t)(-1) != conversionDescriptor)
00607 {
00608 GATBool isDone;
00609 char *newBuffer;
00610 char *tempNewBuffer;
00611 const char *oldBuffer;
00612 const char *tempOldBuffer;
00613 GATuint32 oldBufferLengthInBytes;
00614 GATuint32 newBufferLengthInBytes;
00615 GATuint32 tempOldBufferLengthInBytes;
00616 GATuint32 tempNewBufferLengthInBytes;
00617
00618 isDone = GATFalse;
00619
00620 retval = GAT_MEMORYFAILURE;
00621
00622 oldBuffer = GATString_GetBuffer(string);
00623 oldBufferLengthInBytes = GATString_GetLengthInBytes(string);
00624
00625 tempOldBuffer = oldBuffer;
00626 tempOldBufferLengthInBytes = oldBufferLengthInBytes;
00627
00628 tempNewBuffer = NULL;
00629 tempNewBufferLengthInBytes = 0;
00630
00631 newBufferLengthInBytes = (GATuint32) GATSTRING_BUFFER_LENGTH * (CHAR_BIT / 8);
00632 newBuffer = (char *) malloc( newBufferLengthInBytes * (8 / CHAR_BIT) );
00633
00634 if(NULL != newBuffer)
00635 {
00636 size_t iconvRetVal;
00637 tempNewBuffer = newBuffer;
00638 tempNewBufferLengthInBytes = newBufferLengthInBytes;
00639
00640 while(GATFalse == isDone)
00641 {
00642 newBuffer = tempNewBuffer;
00643 oldBuffer = tempOldBuffer;
00644 newBufferLengthInBytes = tempNewBufferLengthInBytes;
00645 oldBufferLengthInBytes = tempOldBufferLengthInBytes;
00646
00647 iconvRetVal = (int) iconv(conversionDescriptor, &oldBuffer, (size_t *) &oldBufferLengthInBytes, &newBuffer, (size_t *) &newBufferLengthInBytes);
00648
00649 if( ((size_t)(-1) == iconvRetVal) && (EILSEQ == errno) )
00650 {
00651 isDone = GATTrue;
00652 retval = GAT_INVALID_ENCODING;
00653 } else if( (size_t)(-1) != iconvRetVal )
00654 {
00655 isDone = GATTrue;
00656 retval = GAT_MEMORYFAILURE;
00657 (*result) = GATString_Create( tempNewBuffer, (tempNewBufferLengthInBytes - newBufferLengthInBytes), newEncoding );
00658 if( NULL != (*result) )
00659 {
00660 retval = GAT_SUCCESS;
00661 }
00662 } else if( ((size_t)(-1) == iconvRetVal) && (EINVAL == errno) )
00663 {
00664 isDone = GATTrue;
00665 retval = GAT_INCOMPLETE_ENCODING;
00666 } else if( ((size_t)(-1) == iconvRetVal) && (E2BIG == errno) )
00667 {
00668 tempNewBufferLengthInBytes = 2 * tempNewBufferLengthInBytes;
00669 if( NULL == (tempNewBuffer = (char *)realloc(tempNewBuffer, tempNewBufferLengthInBytes * (8 / CHAR_BIT))) )
00670 {
00671 isDone = GATTrue;
00672 retval = GAT_MEMORYFAILURE;
00673 }
00674 }
00675 }
00676 }
00677
00678 free( tempNewBuffer );
00679 }
00680
00681 iconv_close( conversionDescriptor );
00682 }
00683
00684 return retval;
00685 }
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724 int GATString_CompareTo(GATString_const this, GATString_const that, int *comparrison)
00725 {
00726 int retval;
00727
00728 retval = GAT_INVALID_PARAMETER;
00729 if( (NULL != this) && (NULL != that) && (NULL != comparrison) )
00730 {
00731 GATString thisString;
00732 GATString thatString;
00733
00734 (*comparrison) = 0;
00735 if( GAT_SUCCESS == (retval = GATString_Translate(this, "UTF-32BE", &thisString)) )
00736 {
00737 if( GAT_SUCCESS == (retval = GATString_Translate(that, "UTF-32BE", &thatString)) )
00738 {
00739 GATuint32 thisLengthInBytes;
00740 GATuint32 thatLengthInBytes;
00741
00742 retval = GAT_SUCCESS;
00743 thisLengthInBytes = GATString_GetLengthInBytes(thisString);
00744 thatLengthInBytes = GATString_GetLengthInBytes(thatString);
00745 if( thisLengthInBytes == thatLengthInBytes )
00746 {
00747 GATBool isDone;
00748 GATuint32 count;
00749 GATuint32 countMax;
00750 const GATuint32 *thisArray;
00751 const GATuint32 *thatArray;
00752
00753 isDone = GATFalse;
00754 (*comparrison) = 0;
00755 thisArray = (const GATuint32 *) GATString_GetBuffer(thisString);
00756 thatArray = (const GATuint32 *) GATString_GetBuffer(thatString);
00757 countMax = (GATuint32) (thisLengthInBytes / 4);
00758 for(count = 0; (count < countMax) && (GATFalse == isDone); count++)
00759 {
00760 if(thisArray[count] != thatArray[count])
00761 {
00762 isDone = GATTrue;
00763 (*comparrison) = (int) (thisArray[count] - thatArray[count]);
00764 }
00765 }
00766 }
00767 else
00768 {
00769 (*comparrison) = (int) (thisLengthInBytes - thatLengthInBytes);
00770 }
00771
00772 GATString_Destroy( &thisString );
00773 GATString_Destroy( &thatString );
00774 }
00775 else
00776 {
00777 GATString_Destroy( &thisString );
00778 }
00779 }
00780 }
00781
00782 return retval;
00783 }
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802 int GATString_EndsWith(GATString_const string, GATString_const query, GATBool *result)
00803 {
00804 int retval;
00805
00806 retval = GAT_INVALID_PARAMETER;
00807
00808 if( (NULL != string) && (NULL != query) && (NULL != result) )
00809 {
00810 GATString queryString;
00811 GATString stringString;
00812
00813 (*result) = GATFalse;
00814 if( GAT_SUCCESS == (retval =GATString_Translate(string, "UTF-32BE", &stringString)) )
00815 {
00816 if( GAT_SUCCESS == (retval =GATString_Translate(query, "UTF-32BE", &queryString)) )
00817 {
00818 GATuint32 queryLength;
00819 GATuint32 stringLength;
00820
00821 retval = GAT_SUCCESS;
00822 queryLength = (GATuint32) (GATString_GetLengthInBytes(queryString) / 4);
00823 stringLength = (GATuint32) (GATString_GetLengthInBytes(stringString) / 4);
00824
00825 if( queryLength <= stringLength )
00826 {
00827 size_t charactersToCompare;
00828 const GATuint32 *queryArray;
00829 const GATuint32 *stringArray;
00830 const void *queryComparrisonStart;
00831 const void *stringComparrisonStart;
00832
00833 queryArray = (const GATuint32 *) GATString_GetBuffer(queryString);
00834 stringArray = (const GATuint32 *) GATString_GetBuffer(stringString);
00835
00836 queryComparrisonStart = (const void *) queryArray;
00837 charactersToCompare = (size_t) ( (queryLength * 4) * (8 / CHAR_BIT) );
00838 stringComparrisonStart = (const void *) &( stringArray[stringLength - queryLength] );
00839
00840 if( 0 == memcmp(stringComparrisonStart, queryComparrisonStart, charactersToCompare) )
00841 {
00842 (*result) = GATTrue;
00843 }
00844 }
00845
00846 GATString_Destroy( &queryString );
00847 GATString_Destroy( &stringString );
00848 }
00849 else
00850 {
00851 GATString_Destroy( &stringString );
00852 }
00853 }
00854 }
00855
00856 return retval;
00857 }
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876 int GATString_StartsWith(GATString_const string, GATString_const query, GATBool *result)
00877 {
00878 int retval;
00879
00880 retval = GAT_INVALID_PARAMETER;
00881
00882 if( (NULL != string) && (NULL != query) && (NULL != result) )
00883 {
00884 GATString queryString;
00885 GATString stringString;
00886
00887 (*result) = GATFalse;
00888 if( GAT_SUCCESS == (retval = GATString_Translate(string, "UTF-32BE", &stringString)) )
00889 {
00890 if( GAT_SUCCESS == (retval = GATString_Translate(query, "UTF-32BE", &queryString)) )
00891 {
00892 GATuint32 queryLength;
00893 GATuint32 stringLength;
00894
00895 retval = GAT_SUCCESS;
00896 queryLength = (GATuint32) (GATString_GetLengthInBytes(queryString) / 4);
00897 stringLength = (GATuint32) (GATString_GetLengthInBytes(stringString) / 4);
00898
00899 if( queryLength <= stringLength )
00900 {
00901 size_t charactersToCompare;
00902 const GATuint32 *queryArray;
00903 const GATuint32 *stringArray;
00904 const void *queryComparrisonStart;
00905 const void *stringComparrisonStart;
00906
00907 queryArray = (const GATuint32 *) GATString_GetBuffer(queryString);
00908 stringArray = (const GATuint32 *) GATString_GetBuffer(stringString);
00909
00910 queryComparrisonStart = (const void *) queryArray;
00911 stringComparrisonStart = (const void *) stringArray;
00912 charactersToCompare = (size_t) ( ((queryLength - 1) * 4) * (8 / CHAR_BIT) );
00913
00914 if( 0 == memcmp(stringComparrisonStart, queryComparrisonStart, charactersToCompare) )
00915 {
00916 (*result) = GATTrue;
00917 }
00918 }
00919
00920 GATString_Destroy( &queryString );
00921 GATString_Destroy( &stringString );
00922 }
00923 else
00924 {
00925 GATString_Destroy( &stringString );
00926 }
00927 }
00928 }
00929
00930 return retval;
00931 }
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949 int GATString_Concatenate(GATString_const this, GATString_const that, GATString *result)
00950 {
00951 int retval ;
00952
00953 retval = GAT_INVALID_PARAMETER;
00954
00955 if( (NULL != this) && (NULL != that) && (NULL != result) )
00956 {
00957 GATString thisString;
00958 GATString thatString;
00959
00960 (*result) = NULL;
00961 if( GAT_SUCCESS == (retval =GATString_Translate(this, "UTF-32BE", &thisString)) )
00962 {
00963 if( GAT_SUCCESS == (retval =GATString_Translate(that, "UTF-32BE", &thatString)) )
00964 {
00965 GATuint32 *resultBuffer;
00966 GATuint32 thisLengthInBytes;
00967 GATuint32 thatLengthInBytes;
00968
00969 retval = GAT_MEMORYFAILURE;
00970 thisLengthInBytes = GATString_GetLengthInBytes( thisString );
00971 thatLengthInBytes = GATString_GetLengthInBytes( thatString );
00972
00973 resultBuffer = (GATuint32 *) malloc( (thisLengthInBytes + thatLengthInBytes - 4) * (8 / CHAR_BIT) );
00974
00975 if(NULL != resultBuffer)
00976 {
00977 const GATuint32 *thisArray;
00978 const GATuint32 *thatArray;
00979
00980 thisArray = (const GATuint32 *) GATString_GetBuffer( thisString );
00981 thatArray = (const GATuint32 *) GATString_GetBuffer( thatString );
00982
00983 memcpy( (void *) resultBuffer, (const void *) thisArray, (size_t) (thisLengthInBytes - 4) * (8 / CHAR_BIT) );
00984 memcpy( (void *) &(resultBuffer[(thisLengthInBytes - 4) / 4]), (const void *) thatArray, (size_t) (thatLengthInBytes) * (8 / CHAR_BIT) );
00985
00986 (*result) = GATString_Create( (const char *) resultBuffer, (thisLengthInBytes + thatLengthInBytes - 4), "UTF-32BE" );
00987
00988 if(NULL != (*result) )
00989 {
00990 retval = GAT_SUCCESS;
00991 }
00992 }
00993
00994 free(resultBuffer);
00995 GATString_Destroy( &thisString );
00996 GATString_Destroy( &thatString );
00997 }
00998 else
00999 {
01000 GATString_Destroy( &thisString );
01001 }
01002 }
01003 }
01004
01005 return retval;
01006 }
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025 int GATString_LastIndexOf(GATString_const string, GATString_const query, GATuint32 *queryIndex)
01026 {
01027 int retval;
01028
01029 retval = GAT_INVALID_PARAMETER;
01030
01031 if( (NULL != string) && (NULL != query) && (NULL != queryIndex) )
01032 {
01033 GATString queryString;
01034 GATString stringString;
01035
01036 (*queryIndex) = -1;
01037 if( GAT_SUCCESS == (retval =GATString_Translate(string, "UTF-32BE", &stringString)) )
01038 {
01039 if( GAT_SUCCESS == (retval =GATString_Translate(query, "UTF-32BE", &queryString)) )
01040 {
01041 GATuint32 queryLengthInBytes;
01042 GATuint32 stringLengthInBytes;
01043
01044 retval = GAT_INVALID_PARAMETER;
01045 queryLengthInBytes = GATString_GetLengthInBytes( queryString );
01046 stringLengthInBytes = GATString_GetLengthInBytes( stringString );
01047
01048 if( queryLengthInBytes <= stringLengthInBytes )
01049 {
01050 GATBool isDone;
01051 GATuint32 count;
01052 GATuint32 countMaximum;
01053 size_t charsToCompare;
01054 const char *queryArray;
01055 const char *stringArray;
01056
01057 retval = GAT_SUCCESS;
01058 queryArray = GATString_GetBuffer(queryString);
01059 stringArray = GATString_GetBuffer(stringString);
01060
01061 isDone = GATFalse;
01062 charsToCompare = (size_t) ( (queryLengthInBytes - 4) * (8 / CHAR_BIT) );
01063 countMaximum = (GATuint32) ( ((stringLengthInBytes - queryLengthInBytes) / 4) + 1 );
01064
01065 count = countMaximum;
01066 while( GATFalse == isDone )
01067 {
01068 if( 0 == memcmp( (const void *) ( stringArray + (4 * count) - 4), (const void *) queryArray, charsToCompare ) )
01069 {
01070 isDone = GATTrue;
01071 (*queryIndex) = (count - 1);
01072 }
01073
01074 if( 1 == count )
01075 {
01076 isDone = GATTrue;
01077 }
01078 else
01079 {
01080 count = count - 1;
01081 }
01082 }
01083 }
01084
01085 GATString_Destroy( &queryString );
01086 GATString_Destroy( &stringString );
01087 }
01088 else
01089 {
01090 GATString_Destroy( &stringString );
01091 }
01092 }
01093 }
01094
01095 return retval;
01096 }
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116 int GATString_FirstIndexOf(GATString_const string, GATString_const query, GATuint32 *queryIndex)
01117 {
01118 int retval;
01119
01120 retval = GAT_INVALID_PARAMETER;
01121
01122 if( (NULL != string) && (NULL != query) && (NULL != queryIndex) )
01123 {
01124 GATString queryString;
01125 GATString stringString;
01126
01127 (*queryIndex) = -1;
01128 if( GAT_SUCCESS == (retval =GATString_Translate(string, "UTF-32BE", &stringString)) )
01129 {
01130 if( GAT_SUCCESS == (retval =GATString_Translate(query, "UTF-32BE", &queryString)) )
01131 {
01132 GATuint32 queryLengthInBytes;
01133 GATuint32 stringLengthInBytes;
01134
01135 retval = GAT_INVALID_PARAMETER;
01136 queryLengthInBytes = GATString_GetLengthInBytes( queryString );
01137 stringLengthInBytes = GATString_GetLengthInBytes( stringString );
01138
01139 if( queryLengthInBytes <= stringLengthInBytes )
01140 {
01141 GATBool isDone;
01142 GATuint32 count;
01143 GATuint32 countMaximum;
01144 size_t charsToCompare;
01145 const char *queryArray;
01146 const char *stringArray;
01147
01148 retval = GAT_SUCCESS;
01149 queryArray = GATString_GetBuffer(queryString);
01150 stringArray = GATString_GetBuffer(stringString);
01151
01152 isDone = GATFalse;
01153 charsToCompare = (size_t) ( (queryLengthInBytes - 4) * (8 / CHAR_BIT) );
01154 countMaximum = (GATuint32) ( ((stringLengthInBytes - queryLengthInBytes) / 4) + 1 );
01155
01156 count = 1;
01157 while( GATFalse == isDone )
01158 {
01159 if( 0 == memcmp( (const void *) ( stringArray + (4 * count) - 4), (const void *) queryArray, charsToCompare ) )
01160 {
01161 isDone = GATTrue;
01162 (*queryIndex) = (count - 1);
01163 }
01164
01165 if( countMaximum == count)
01166 {
01167 isDone = GATTrue;
01168 }
01169 else
01170 {
01171 count = count + 1;
01172 }
01173 }
01174 }
01175
01176 GATString_Destroy( &queryString );
01177 GATString_Destroy( &stringString );
01178 }
01179 else
01180 {
01181 GATString_Destroy( &stringString );
01182 }
01183 }
01184 }
01185
01186 return retval;
01187 }
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207 int GATString_GetSubString(GATString_const string, GATuint32 start, GATuint32 end, GATString *substring)
01208 {
01209 int retval;
01210
01211 retval = GAT_INVALID_PARAMETER;
01212
01213 if( (NULL != string) && (start < end) && (NULL != substring) )
01214 {
01215 GATString stringString;
01216
01217 (*substring) = NULL;
01218 if( GAT_SUCCESS == (retval =GATString_Translate(string, "UTF-32BE", &stringString)) )
01219 {
01220 GATuint32 stringLengthInChars;
01221 GATuint32 stringLengthInBytes;
01222
01223 retval = GAT_INVALID_PARAMETER;
01224 stringLengthInBytes = GATString_GetLengthInBytes(stringString);
01225 stringLengthInChars = (GATuint32) (stringLengthInBytes / 4);
01226
01227 if( (start <= stringLengthInChars) && (end <= (stringLengthInChars + 1)) )
01228 {
01229 const char *stringArray;
01230 GATuint32 *substringBuffer;
01231 size_t substringLengthInChar;
01232 GATuint32 substringLengthInBytes;
01233 size_t substringLengthInCharacters;
01234
01235 retval = GAT_MEMORYFAILURE;
01236 substringLengthInCharacters = (end - start);
01237 substringLengthInBytes = (size_t) (substringLengthInCharacters * 4);
01238 stringArray = (const char *) GATString_GetBuffer(stringString);
01239 substringLengthInChar = (size_t) ( substringLengthInBytes * (8 / CHAR_BIT) );
01240
01241 substringBuffer = (GATuint32 *) malloc( substringLengthInChar );
01242 if( NULL != substringBuffer )
01243 {
01244 memcpy( (void *) substringBuffer, (const void *) ( stringArray + (start * 4) ), substringLengthInChar );
01245
01246 (*substring) = GATString_Create( (const char *) substringBuffer, substringLengthInBytes, "UTF-32BE");
01247 if( NULL != (*substring) )
01248 {
01249 retval = GAT_SUCCESS;
01250 }
01251 }
01252 free(substringBuffer);
01253 }
01254 GATString_Destroy( &stringString );
01255 }
01256 }
01257
01258 return retval;
01259 }
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273 int GATString_GetInterface(GATString_const string, GATInterface iftype, void const **ifp)
01274 {
01275 int retval = GAT_INVALID_PARAMETER;
01276
01277 if (NULL != ifp)
01278 {
01279 *ifp = NULL;
01280 if (GATInterface_ISerialisable == iftype ||
01281 GATInterface_IAdvertisable == iftype)
01282 {
01283 *ifp = (void const *) &string->GATSerialisable__vtable;
01284 retval = GAT_SUCCESS;
01285 }
01286 else
01287 {
01288 retval = GAT_NO_INTERFACE;
01289 }
01290 }
01291 return retval;
01292 }
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309 int
01310 GATString_Serialise(GATString string, GATObject stream, GATBool clear_dirty)
01311 {
01312 int retval = GAT_INVALID_HANDLE;
01313 if (NULL != string)
01314 {
01315 retval = GATXds_SerialiseObject(GATString_ToGATObject(string), stream,
01316 clear_dirty, 0, "uint32 octet string",
01317 GATSTRING_VERSION1, string->byteArray, string->lengthInBytes,
01318 string->encoding);
01319 }
01320 return retval;
01321 }
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338 static GATBool
01339 GATString_VersionCallback(GATuint32 version)
01340 {
01341 GATBool retval = GATFalse;
01342 if ((version & ~GATSTRING_MINOR_MASK) <= GATSTRING_LASTVERSION)
01343 {
01344 retval = GATTrue;
01345 }
01346 return retval;
01347 }
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371 static int
01372 GATString_DeSerialiseCallback(GATContext context, GATObject stream,
01373 GATObject *object, GATuint32 version, va_list args)
01374 {
01375 int retval = GAT_FAIL;
01376
01377
01378 char **byte_array = va_arg(args, char **);
01379 GATuint32 *byte_array_size = va_arg(args, GATuint32 *);
01380 char const **encoding = va_arg(args, char const **);
01381
01382
01383 GATString string = (GATString) malloc(sizeof(struct GATString_S));
01384 if (NULL == string)
01385 {
01386 retval = GAT_MEMORYFAILURE;
01387 }
01388 else
01389 {
01390 string->GATObject__vtable = &GATString__vtable;
01391 string->GATSerialisable__vtable = &GATString_ISerialisable__vtable;
01392 string->byteArray = *byte_array;
01393 string->lengthInBytes = *byte_array_size;
01394
01395 strncpy(string->encoding, *encoding, GATSTRING_MAX_ENCODING_LENGTH);
01396
01397 (string->encoding)[GATSTRING_MAX_ENCODING_LENGTH - 1] = '\0';
01398
01399 retval = GAT_SUCCESS;
01400 }
01401
01402 if (GAT_SUCCESS == retval)
01403 {
01404 if (NULL != object)
01405 {
01406 *object = GATString_ToGATObject(string);
01407 }
01408 else
01409 {
01410 GATString_Destroy(&string);
01411 retval = GAT_INVALID_PARAMETER;
01412 }
01413 }
01414 return retval;
01415 }
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431 GATString
01432 GATString_DeSerialise(GATContext context, GATObject stream, GATResult *result)
01433 {
01434 GATObject string = NULL;
01435
01436
01437
01438 GATuint32 version = 0;
01439 char *byte_array = NULL;
01440 GATuint32 byte_array_size = 0;
01441 char *encoding = NULL;
01442
01443
01444 int retval = GATXds_DeSerialiseObject(context, stream,
01445 GATString_DeSerialiseCallback, GATString_VersionCallback, &string,
01446 "uint32 octet string", &version, &byte_array, &byte_array_size, &encoding);
01447
01448
01449
01450 if (GAT_SUCCESS != retval)
01451 {
01452 free(byte_array);
01453 }
01454 free(encoding);
01455
01456
01457 return GATObject_ToGATString(string);
01458 }
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470 int GATString_GetIsDirty(GATString_const string, GATBool *isdirty)
01471 {
01472 int retval = GAT_INVALID_HANDLE;
01473 if (NULL != string)
01474 {
01475 if (NULL != isdirty)
01476 {
01477 *isdirty = GATFalse;
01478 retval = GAT_SUCCESS;
01479 }
01480 else
01481 {
01482 retval = GAT_INVALID_PARAMETER;
01483 }
01484 }
01485 return retval;
01486 }