ThingWorx C SDK
twBaseTypes.h
Go to the documentation of this file.
1 /***************************************
2  * Copyright 2017, PTC, Inc.
3  ***************************************/
4 
10 #ifndef BASETYPES_H
11 #define BASETYPES_H
12 
13 #include "twOSPort.h"
14 #include "twDefinitions.h"
15 #include "twDefaultSettings.h"
16 
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 struct twInfoTable;
23 struct cJSON;
24 
35 typedef struct twStream {
36  char * data;
37  char * ptr;
38  uint32_t length;
39  uint32_t maxlength;
40  char ownsData;
41  TW_FILE_HANDLE file ;
42 } twStream;
43 
53 
69 twStream * twStream_CreateFromCharArray(const char * data, uint32_t length);
70 
86 twStream * twStream_CreateFromCharArrayZeroCopy(const char * data, uint32_t length);
87 
96 void twStream_Delete(void* s);
97 
105 char * twStream_GetData(struct twStream * s);
106 
114 int32_t twStream_GetIndex(struct twStream * s);
115 
123 int32_t twStream_GetLength(struct twStream * s);
124 
135 int twStream_AddBytes(struct twStream * s, void * b, uint32_t count);
136 
148 int twStream_GetBytes(struct twStream * s, void * b, uint32_t count);
149 
159 int twStream_Reset(struct twStream * s);
160 
168 twStream * twStream_CreateFromFile(const char * fname);
169 
177 typedef struct twLocation {
178  double latitude;
179  double longitude;
180  double elevation;
181 } twLocation;
182 
183 /***************************************/
184 /* Helper functions that are */
185 /* typically not directly used */
186 /* by application developers */
187 /***************************************/
188 void swap2bytes(char * bytes);
189 void swap4bytes(char * bytes);
190 void swap8bytes(char * bytes);
191 int stringToStream(char * string, twStream * s);
192 char * streamToString(twStream * s);
193 enum BaseType baseTypeFromString(const char * s);
194 const char * baseTypeToString(enum BaseType b);
195 
204 typedef struct twPrimitive {
205  enum BaseType type;
207  uint32_t length;
208  union {
209  int32_t integer;
210  double number;
211  DATETIME datetime;
213  char boolean;
214  struct {
215  char * data;
216  int32_t len;
217  } bytes;
219  struct twPrimitive * variant;
220  } val;
221 } twPrimitive;
222 
223 
235 
251 
266 
285 
302 
311 void twPrimitive_Delete(void * p);
312 
326 
343 
354 
367 char twPrimitive_IsTrue(twPrimitive * p1) ;
368 
388 
404 
417 twPrimitive * twPrimitive_CreateFromNumber(const double value);
418 
431 twPrimitive * twPrimitive_CreateFromInteger(const int32_t value);
432 
445 twPrimitive * twPrimitive_CreateFromDatetime(const DATETIME value);
446 
458 
471 twPrimitive * twPrimitive_CreateFromBoolean(const char value);
472 
488 
504 
522 twPrimitive * twPrimitive_CreateFromString(const char * value, char duplicate);
523 
544 twPrimitive * twPrimitive_CreateFromBlob(const char * value, int32_t length, char isImage, char duplicate);
545 
567 twPrimitive * twPrimitive_CreateFromVariable(const void * value, enum BaseType type, char duplicateCharArray, uint32_t blobLength);
568 
595 struct cJSON * twPrimitive_ToJson(const char * name, twPrimitive * p, struct cJSON * parent);
596 
613 twPrimitive * twPrimitive_CreateFromJson(struct cJSON * j, char * name, enum BaseType type);
614 
615 #ifdef __cplusplus
616 }
617 #endif
618 
619 #endif
struct twPrimitive twPrimitive
BaseType primitive structure.
twPrimitive * twPrimitive_FullCopy(twPrimitive *p)
Creates a new twPrimitive structure and copies all data from an existing twPrimitive structure to the...
Definition: twBaseTypes.c:902
twPrimitive * twPrimitive_CreateFromJson(struct cJSON *j, char *name, enum BaseType type)
Helper function to convert a cJSON object to a twPrimitive.
Definition: twBaseTypes.c:1182
struct twLocation twLocation
Location primitive structure.
int32_t integer
Definition: twBaseTypes.h:209
struct twPrimitive * variant
Definition: twBaseTypes.h:219
twPrimitive * twPrimitive_CreateVariant(twPrimitive *input)
Helper function to create a twPrimitive of type TW_VARIANT from a twPrimitive structure.
Definition: twBaseTypes.c:1148
twLocation location
Definition: twBaseTypes.h:212
double number
Definition: twBaseTypes.h:210
struct twStream twStream
Dynamically allocated byte array. Automatically expands its length as needed.
double latitude
Definition: twBaseTypes.h:178
twStream * twStream_Create()
Creates a new twStream structure.
Definition: twBaseTypes.c:263
char * data
Definition: twBaseTypes.h:36
twPrimitive * twPrimitive_CreateFromNumber(const double value)
Helper function to create a twPrimitive of type TW_NUMBER from a double.
Definition: twBaseTypes.c:1108
uint32_t length
Definition: twBaseTypes.h:207
int twStream_GetBytes(struct twStream *s, void *b, uint32_t count)
Copies count bytes of data from the twStream::data of a twStream into buf.
Definition: twBaseTypes.c:416
twStream * twStream_CreateFromFile(const char *fname)
Creates a stream from a file.
Definition: twBaseTypes.c:283
twPrimitive * twPrimitive_CreateFromStreamTyped(twStream *s, enum BaseType type)
Allocates a new twPrimitive structure of a specified type and populates it with data from a twStream...
Definition: twBaseTypes.c:471
char * ptr
Definition: twBaseTypes.h:37
int twStream_Reset(struct twStream *s)
Resets the position pointer of a twStream (twStream::ptr) to the beginning of that twStream's data (t...
Definition: twBaseTypes.c:436
Dynamically allocated byte array. Automatically expands its length as needed.
Definition: twBaseTypes.h:35
twStream * twStream_CreateFromCharArrayZeroCopy(const char *data, uint32_t length)
Allocates a new twStream structure and points twStream::data to an existing char array.
Definition: twBaseTypes.c:331
char * twStream_GetData(struct twStream *s)
Gets a pointer to the twStream::data of a twStream.
Definition: twBaseTypes.c:361
Wrappers for OS-specific functionality.
struct cJSON * twPrimitive_ToJson(const char *name, twPrimitive *p, struct cJSON *parent)
Helper function to convert a twPrimitive to a CJSON object.
Definition: twBaseTypes.c:1332
double longitude
Definition: twBaseTypes.h:179
twPrimitive * twPrimitive_ZeroCopy(twPrimitive *p)
Creates a new twPrimitive structure which inherits all pointers of an existing twPrimitive structure ...
Definition: twBaseTypes.c:889
char boolean
Definition: twBaseTypes.h:213
int twPrimitive_ToStream(twPrimitive *p, twStream *s)
Copies the data from a twPrimitive structure to a twStream structure.
Definition: twBaseTypes.c:665
char twPrimitive_IsTrue(twPrimitive *p1)
Compares a twPrimitive to "true". For booleans true is if the value is "true", for numeric and dattim...
Definition: twBaseTypes.c:829
int twStream_AddBytes(struct twStream *s, void *b, uint32_t count)
Adds data bytes to a twStream.
Definition: twBaseTypes.c:376
twPrimitive * twPrimitive_CreateFromLocation(const twLocation *value)
Helper function to create a twPrimitive of type TW_LOCATION from a location structure.
Definition: twBaseTypes.c:1116
char * twPrimitive_DecoupleStringAndDelete(twPrimitive *p)
Helper function to return the value of a TW_STRING family type, remove it from the twPrimitive p...
Definition: twBaseTypes.c:1164
twPrimitive * twPrimitive_CreateFromString(const char *value, char duplicate)
Helper function to create a twPrimitive of type TW_STRING from a null-terminated char array...
Definition: twBaseTypes.c:1104
twPrimitive * twPrimitive_CreateFromCurrentTime()
Helper function to create a twPrimitive of type TW_DATETIME from the current time.
Definition: twBaseTypes.c:1135
int32_t twStream_GetIndex(struct twStream *s)
Gets the ::twStream#index of a twStream.
Definition: twBaseTypes.c:366
Default settings for ThingWorx C SDK.
int twPrimitive_Compare(twPrimitive *p1, twPrimitive *p2)
Compares two twPrimitive structures for equivalence.
Definition: twBaseTypes.c:754
enum BaseType typeFamily
Definition: twBaseTypes.h:206
DATETIME datetime
Definition: twBaseTypes.h:211
Common definitions for C SDK.
twPrimitive * twPrimitive_CreateFromBoolean(const char value)
Helper function to create a twPrimitive of type TW_BOOLEAN from a char type.
Definition: twBaseTypes.c:1140
void twPrimitive_Delete(void *p)
Frees all memory associated with a twPrimitive and all of its owned substructures.
Definition: twBaseTypes.c:638
struct twInfoTable * infotable
Definition: twBaseTypes.h:218
twPrimitive * twPrimitive_Create()
Creates a new twPrimitive structure.
Definition: twBaseTypes.c:450
twPrimitive * twPrimitive_CreateFromDatetime(const DATETIME value)
Helper function to create a twPrimitive of type TW_DATETIME from a DATETIME type. ...
Definition: twBaseTypes.c:1131
union twPrimitive::@0 val
twPrimitive * twPrimitive_CreateFromVariable(const void *value, enum BaseType type, char duplicateCharArray, uint32_t blobLength)
Helper function to create a twPrimitive of any type from a void *.
Definition: twBaseTypes.c:951
Info table base structure.
Definition: twInfoTable.h:418
double elevation
Definition: twBaseTypes.h:180
twPrimitive * twPrimitive_CreateFromStream(twStream *s)
Creates a new twPrimitive structure and populates it with data from a twStream.
Definition: twBaseTypes.c:460
twPrimitive * twPrimitive_CreateFromBlob(const char *value, int32_t length, char isImage, char duplicate)
Helper function to create a twPrimitive of type TW_BLOB or TW_IMAGE from a char array.
Definition: twBaseTypes.c:1126
uint32_t length
Definition: twBaseTypes.h:38
char ownsData
Definition: twBaseTypes.h:40
int32_t twStream_GetLength(struct twStream *s)
Gets the twStream::length of a twStream.
Definition: twBaseTypes.c:371
twPrimitive * twPrimitive_CreateFromLocationAndDelete(const twLocation *value)
Helper function to create a twPrimitive of type TW_LOCATION from a location structure. Deletes the TW_LOCATION after creating the primitive.
Definition: twBaseTypes.c:1120
twPrimitive * twPrimitive_CreateFromInfoTable(struct twInfoTable *it)
Helper function to create a twPrimitive of type TW_INFOTABLE from a twInfoTable structure.
Definition: twBaseTypes.c:1144
Location primitive structure.
Definition: twBaseTypes.h:177
twPrimitive * twPrimitive_CreateFromInteger(const int32_t value)
Helper function to create a twPrimitive of type TW_INTEGER from an integral type (int, short, char).
Definition: twBaseTypes.c:1112
enum BaseType type
Definition: twBaseTypes.h:205
int32_t len
Definition: twBaseTypes.h:216
Definition: cJSON.h:43
void twStream_Delete(void *s)
Frees all memory associated with a twStream and all of its owned substructures.
Definition: twBaseTypes.c:350
BaseType
Definition: twDefinitions.h:156
BaseType primitive structure.
Definition: twBaseTypes.h:204
char * data
Definition: twBaseTypes.h:215
uint32_t maxlength
Definition: twBaseTypes.h:39
twStream * twStream_CreateFromCharArray(const char *data, uint32_t length)
Creates a new twStream structure and copies the data from a char array to the new structure's twStrea...
Definition: twBaseTypes.c:307
Definition: gzappend.c:170