ThingWorx C SDK
twBaseTypes.h
Go to the documentation of this file.
1 /***************************************
2  * Copyright (C) 2015 ThingWorx 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 #if (OFFLINE_MSG_STORE == 2)
42  TW_FILE_HANDLE file ;
43 #endif
44 } twStream;
45 
55 
71 twStream * twStream_CreateFromCharArray(const char * data, uint32_t length);
72 
88 twStream * twStream_CreateFromCharArrayZeroCopy(const char * data, uint32_t length);
89 
98 void twStream_Delete(void* s);
99 
107 char * twStream_GetData(struct twStream * s);
108 
116 int32_t twStream_GetIndex(struct twStream * s);
117 
125 int32_t twStream_GetLength(struct twStream * s);
126 
137 int twStream_AddBytes(struct twStream * s, void * b, uint32_t count);
138 
150 int twStream_GetBytes(struct twStream * s, void * b, uint32_t count);
151 
161 int twStream_Reset(struct twStream * s);
162 
163 #if (OFFLINE_MSG_STORE == 2)
164 
171 twStream * twStream_CreateFromFile(const char * fname);
172 #endif
173 
181 typedef struct twLocation {
182  double latitude;
183  double longitude;
184  double elevation;
185 } twLocation;
186 
187 /***************************************/
188 /* Helper functions that are */
189 /* typically not directly used */
190 /* by application developers */
191 /***************************************/
192 void swap4bytes(char * bytes);
193 void swap8bytes(char * bytes);
194 int stringToStream(char * string, twStream * s);
195 char * streamToString(twStream * s);
196 enum BaseType baseTypeFromString(const char * s);
197 const char * baseTypeToString(enum BaseType b);
198 
207 typedef struct twPrimitive {
208  enum BaseType type;
210  uint32_t length;
211  union {
212  int32_t integer;
213  double number;
214  DATETIME datetime;
216  char boolean;
217  struct {
218  char * data;
219  int32_t len;
220  } bytes;
222  struct twPrimitive * variant;
223  } val;
224 } twPrimitive;
225 
226 
238 
254 
269 
288 
305 
314 void twPrimitive_Delete(void * p);
315 
329 
346 
357 
370 char twPrimitive_IsTrue(twPrimitive * p1) ;
371 
391 
404 twPrimitive * twPrimitive_CreateFromNumber(const double value);
405 
418 twPrimitive * twPrimitive_CreateFromInteger(const int32_t value);
419 
432 twPrimitive * twPrimitive_CreateFromDatetime(const DATETIME value);
433 
445 
458 twPrimitive * twPrimitive_CreateFromBoolean(const char value);
459 
475 
491 
509 twPrimitive * twPrimitive_CreateFromString(const char * value, char duplicate);
510 
531 twPrimitive * twPrimitive_CreateFromBlob(const char * value, int32_t length, char isImage, char duplicate);
532 
554 twPrimitive * twPrimitive_CreateFromVariable(const void * value, enum BaseType type, char duplicateCharArray, uint32_t blobLength);
555 
582 struct cJSON * twPrimitive_ToJson(char * name, twPrimitive * p, struct cJSON * parent);
583 
600 twPrimitive * twPrimitive_CreateFromJson(struct cJSON * j, char * name, enum BaseType type);
601 
602 #ifdef __cplusplus
603 }
604 #endif
605 
606 #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:900
twPrimitive * twPrimitive_CreateFromJson(struct cJSON *j, char *name, enum BaseType type)
Helper function to convert a cJSON object to a twPrimitive.
Definition: twBaseTypes.c:1168
struct twLocation twLocation
Location primitive structure.
struct cJSON * twPrimitive_ToJson(char *name, twPrimitive *p, struct cJSON *parent)
Helper function to convert a twPrimitive to a CJSON object.
Definition: twBaseTypes.c:1319
int32_t integer
Definition: twBaseTypes.h:212
struct twPrimitive * variant
Definition: twBaseTypes.h:222
twPrimitive * twPrimitive_CreateVariant(twPrimitive *input)
Helper function to create a twPrimitive of type TW_VARIANT from a twPrimitive structure.
Definition: twBaseTypes.c:1138
twLocation location
Definition: twBaseTypes.h:215
double number
Definition: twBaseTypes.h:213
struct twStream twStream
Dynamically allocated byte array. Automatically expands its length as needed.
double latitude
Definition: twBaseTypes.h:182
twStream * twStream_Create()
Creates a new twStream structure.
Definition: twBaseTypes.c:259
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:1104
uint32_t length
Definition: twBaseTypes.h:210
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:410
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:469
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:432
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:321
char * twStream_GetData(struct twStream *s)
Gets a pointer to the twStream::data of a twStream.
Definition: twBaseTypes.c:353
Wrappers for OS-specific functionality.
double longitude
Definition: twBaseTypes.h:183
twPrimitive * twPrimitive_ZeroCopy(twPrimitive *p)
Creates a new twPrimitive structure which inherits all pointers of an existing twPrimitive structure ...
Definition: twBaseTypes.c:887
char boolean
Definition: twBaseTypes.h:216
int twPrimitive_ToStream(twPrimitive *p, twStream *s)
Copies the data from a twPrimitive structure to a twStream structure.
Definition: twBaseTypes.c:662
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:827
int twStream_AddBytes(struct twStream *s, void *b, uint32_t count)
Adds data bytes to a twStream.
Definition: twBaseTypes.c:368
twPrimitive * twPrimitive_CreateFromLocation(const twLocation *value)
Helper function to create a twPrimitive of type TW_LOCATION from a location structure.
Definition: twBaseTypes.c:1112
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:1150
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:1100
twPrimitive * twPrimitive_CreateFromCurrentTime()
Helper function to create a twPrimitive of type TW_DATETIME from the current time.
Definition: twBaseTypes.c:1125
int32_t twStream_GetIndex(struct twStream *s)
Gets the ::twStream#index of a twStream.
Definition: twBaseTypes.c:358
Default settings for ThingWorx C SDK.
int twPrimitive_Compare(twPrimitive *p1, twPrimitive *p2)
Compares two twPrimitive structures for equivalence.
Definition: twBaseTypes.c:752
enum BaseType typeFamily
Definition: twBaseTypes.h:209
DATETIME datetime
Definition: twBaseTypes.h:214
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:1130
void twPrimitive_Delete(void *p)
Frees all memory associated with a twPrimitive and all of its owned substructures.
Definition: twBaseTypes.c:636
struct twInfoTable * infotable
Definition: twBaseTypes.h:221
twPrimitive * twPrimitive_Create()
Creates a new twPrimitive structure.
Definition: twBaseTypes.c:448
twPrimitive * twPrimitive_CreateFromDatetime(const DATETIME value)
Helper function to create a twPrimitive of type TW_DATETIME from a DATETIME type. ...
Definition: twBaseTypes.c:1121
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:949
Info table base structure.
Definition: twInfoTable.h:418
double elevation
Definition: twBaseTypes.h:184
twPrimitive * twPrimitive_CreateFromStream(twStream *s)
Creates a new twPrimitive structure and populates it with data from a twStream.
Definition: twBaseTypes.c:458
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:1116
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:363
twPrimitive * twPrimitive_CreateFromInfoTable(struct twInfoTable *it)
Helper function to create a twPrimitive of type TW_INFOTABLE from a twInfoTable structure.
Definition: twBaseTypes.c:1134
Location primitive structure.
Definition: twBaseTypes.h:181
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:1108
enum BaseType type
Definition: twBaseTypes.h:208
int32_t len
Definition: twBaseTypes.h:219
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:340
BaseType
Definition: twDefinitions.h:155
BaseType primitive structure.
Definition: twBaseTypes.h:207
char * data
Definition: twBaseTypes.h:218
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:297