00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 static const char *rcsid = "$Header: /export/cvs-gridlab/wp-1/Codes/GATEngine/C-reference/src/GATLocation.c,v 1.17 2004/04/02 12:31:58 hartmutkaiser Exp $";
00021
00022
00023
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027
00028
00029 #include "GATLocation.h"
00030 #include "GATInternal.h"
00031 #include "GATErrors.h"
00032 #include "GATXdsWrapper.h"
00033 #include "GATUtil.h"
00034
00035
00036 GATOBJECT_DEFINE_VTABLE(GATLocation);
00037 GATSERIALISABLE_DEFINE_VTABLE(GATLocation);
00038
00039
00040 GATOBJECT_DEFINE_CONVERTERS(GATLocation);
00041
00042
00043
00044
00045
00046 struct GATLocation_S
00047 {
00048 GATLocation_vtable *GATObject__vtable;
00049 GATLocation_ISerialisable_vtable *GATSerialisable__vtable;
00050 char *uri;
00051 };
00052
00053
00054
00055 static char *GetSubString(const char *string,
00056 int start,
00057 int end);
00058
00059 static int
00060 GATLocation_DeSerialise_Create(GATContext context, GATObject stream,
00061 char const *uri, GATLocation *new_object);
00062
00063
00064 static GATLocation_vtable GATLocation__vtable = {
00065 GATLocation_GetType,
00066 GATLocation_Destroy,
00067 GATLocation_Equals,
00068 GATLocation_Clone,
00069 GATLocation_GetInterface,
00070 NULL
00071 };
00072
00073 static GATLocation_ISerialisable_vtable
00074 GATLocation_ISerialisable__vtable =
00075 {
00076 GATLocation_Serialise,
00077 GATLocation_DeSerialise,
00078 GATLocation_GetIsDirty,
00079 };
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 GATResult GATLocation_Register_GATSerialisable(void)
00090 {
00091 return GATObject_Register_GATSerialisable(GATType_GATLocation,
00092 &GATLocation_ISerialisable__vtable);
00093 }
00094
00095
00096
00097
00098
00099
00100
00101 GATLocation GATLocation_Create(const char *uri)
00102 {
00103 GATLocation this;
00104
00105 this = (GATLocation)malloc(sizeof(*this));
00106
00107 if(this)
00108 {
00109 this->GATObject__vtable = &GATLocation__vtable;
00110 this->GATSerialisable__vtable = &GATLocation_ISerialisable__vtable;
00111 this->uri = GetSubString(uri,0,strlen(uri));
00112
00113 if(!this->uri)
00114 {
00115 free(this);
00116 this = NULL;
00117 }
00118 }
00119
00120 return this;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129 void GATLocation_Destroy(GATLocation *this)
00130 {
00131 if(NULL != this && NULL != *this)
00132 {
00133 if((*this)->uri)
00134 {
00135 free((*this)->uri);
00136 }
00137 free(*this);
00138 *this = NULL;
00139 }
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 char *GATLocation_ToString(GATLocation_const this)
00152
00153 {
00154 char *retval;
00155
00156 retval = GetSubString(this->uri,0,strlen(this->uri));
00157
00158 return retval;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 GATType GATLocation_GetType(GATLocation_const location)
00171 {
00172 GAT_UNUSED_PARAMETER(location);
00173 return GATType_GATLocation;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 int GATLocation_Clone(GATLocation_const this, GATLocation *new_location)
00185 {
00186 int retval = GAT_INVALID_PARAMETER;
00187 GATLocation location = GATLocation_Create(this->uri);
00188
00189 if (NULL != location)
00190 {
00191 if (NULL != new_location)
00192 {
00193 *new_location = location;
00194 retval = GAT_SUCCESS;
00195 }
00196 }
00197 else
00198 {
00199 retval = GAT_MEMORYFAILURE;
00200 }
00201
00202 if (GAT_SUCCESS != retval)
00203 {
00204 GATLocation_Destroy(&location);
00205 }
00206 return retval;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 int GATLocation_Equals(GATLocation_const this, GATLocation_const that,
00218 GATBool *isequal)
00219 {
00220 char *this_str = GATLocation_ToString(this);
00221 char *that_str = GATLocation_ToString(that);
00222 int result = GAT_INVALID_PARAMETER;
00223
00224 if (NULL != this_str && NULL != that_str && NULL != isequal)
00225 {
00226 *isequal = strcmp(this_str, that_str) ? GATFalse : GATTrue;
00227 result = GAT_SUCCESS;
00228 }
00229
00230 free(this_str);
00231 free(that_str);
00232 return result;
00233 }
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 GATResult
00248 GATLocation_GetInterface(GATLocation_const object, GATInterface iftype,
00249 void const **ifp)
00250 {
00251 GATResult retval = GAT_INVALID_PARAMETER;
00252
00253 if (NULL != ifp)
00254 {
00255 *ifp = NULL;
00256 if (GATInterface_ISerialisable == iftype)
00257 {
00258 *ifp = (void const *) &object->GATSerialisable__vtable;
00259 retval = GAT_SUCCESS;
00260 }
00261 else
00262 {
00263 retval = GAT_NO_INTERFACE;
00264 }
00265 }
00266 return retval;
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 GATResult
00285 GATLocation_Serialise(GATLocation object, GATObject stream, GATBool clear_dirty)
00286 {
00287 GATResult retval = GAT_INVALID_HANDLE;
00288 if (NULL != object)
00289 {
00290 retval = GATXds_SerialiseObject(GATLocation_ToGATObject(object), stream,
00291 clear_dirty, 0, "uint32 string", GATLOCATION_VERSION1, object->uri);
00292 }
00293 return retval;
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 static GATBool
00311 GATLocation_VersionCallback(GATuint32 version)
00312 {
00313 GATBool retval = GATFalse;
00314 if ((version & ~GATLOCATION_MINOR_MASK) <= GATLOCATION_LASTVERSION)
00315 {
00316 retval = GATTrue;
00317 }
00318 return retval;
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 static GATResult
00345 GATLocation_DeSerialiseCallback(GATContext context, GATObject stream,
00346 GATObject *new_object, GATuint32 version, va_list args)
00347 {
00348
00349 char const **uri = va_arg(args, char const **);
00350 GATLocation object = NULL;
00351
00352
00353 GATResult retval = GATLocation_DeSerialise_Create(context, stream, *uri, &object);
00354
00355 GAT_UNUSED_PARAMETER(version);
00356
00357 if (GAT_SUCCESS == retval)
00358 {
00359 if (NULL != object)
00360 {
00361 *new_object = GATLocation_ToGATObject(object);
00362 }
00363 else
00364 {
00365 GATLocation_Destroy(&object);
00366 retval = GAT_INVALID_PARAMETER;
00367 }
00368 }
00369 return retval;
00370 }
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 GATLocation
00387 GATLocation_DeSerialise(GATContext context, GATObject stream, GATResult *result)
00388 {
00389 GAT_USES_STATUS(context, "GATLocation_DeSerialise");
00390
00391 GATObject object = NULL;
00392
00393
00394
00395 GATuint32 version = 0;
00396 char *uri = NULL;
00397
00398
00399 GAT_CREATE_STATUS(GATXds_DeSerialiseObject(context, stream,
00400 GATLocation_DeSerialiseCallback, GATLocation_VersionCallback, &object,
00401 "uint32 string", &version, &uri));
00402
00403 free(uri);
00404 if (NULL != result)
00405 {
00406 *result = GAT_CURRENT_STATUS();
00407 }
00408 else
00409 {
00410 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00411 }
00412 GAT_STORE_STATUS();
00413 return GATObject_ToGATLocation(object);
00414 }
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 GATResult GATLocation_GetIsDirty(GATLocation_const object, GATBool *isdirty)
00427 {
00428 GATResult retval = GAT_INVALID_HANDLE;
00429 if (NULL != object)
00430 {
00431 if (NULL != isdirty)
00432 {
00433 *isdirty = GATFalse;
00434 retval = GAT_SUCCESS;
00435 }
00436 else
00437 {
00438 retval = GAT_INVALID_PARAMETER;
00439 }
00440 }
00441 return retval;
00442 }
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 static GATResult
00463 GATLocation_DeSerialise_Create(GATContext context, GATObject stream,
00464 char const *uri, GATLocation *object)
00465 {
00466 GAT_USES_STATUS(context, "GATLocation_DeSerialise_Create");
00467 GATLocation new_object = (GATLocation) malloc(sizeof(struct GATLocation_S));
00468 GAT_UNUSED_PARAMETER(stream);
00469 if(NULL != new_object)
00470 {
00471 memset(new_object, 0, sizeof(struct GATLocation_S));
00472 new_object->GATObject__vtable = &GATLocation__vtable;
00473 new_object->GATSerialisable__vtable = &GATLocation_ISerialisable__vtable;
00474
00475 new_object->uri = GATUtil_strdup(uri);
00476 GAT_CREATE_STATUS_IF(NULL == new_object->uri, GAT_MEMORYFAILURE);
00477 }
00478 else
00479 {
00480 GAT_CREATE_STATUS(GAT_MEMORYFAILURE);
00481 }
00482
00483 if (GAT_SUCCEEDED(GAT_CURRENT_STATUS()))
00484 {
00485 if (NULL != new_object)
00486 {
00487 *object = new_object;
00488 }
00489 else
00490 {
00491 GATLocation_Destroy(&new_object);
00492 GAT_CREATE_STATUS(GAT_INVALID_PARAMETER);
00493 }
00494 }
00495 else
00496 {
00497 GATLocation_Destroy(&new_object);
00498 }
00499 return GAT_RETURN_STATUS();
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 static char *GetSubString(const char *string,
00513 int start,
00514 int end)
00515 {
00516 char *retval;
00517 int length;
00518
00519 length = end-start;
00520
00521 if(length > 0)
00522 {
00523 retval = (char *)malloc(length+1);
00524
00525 if(retval)
00526 {
00527 strncpy(retval, string+start, length);
00528
00529 retval[length] = 0;
00530 }
00531 }
00532 else
00533 {
00534 retval = NULL;
00535 }
00536
00537 return retval;
00538 }