a7343db5bf0b564d5c1124642f5fa6aef3bd7092
[iotcloud.git] / version2 / src / C / IoTString.h
1 #ifndef IOTSTRING_H
2 #define IOTSTRING_H
3
4 #include "array.h"
5 #include <string.h>
6 /**
7  * IoTString wraps the underlying char string.
8  * @author Brian Demsky <bdemsky@uci.edu>
9  * @version 1.0
10  */
11
12 class IoTString {
13 private:
14         Array<char> *array;
15         IoTString() {}
16
17         /**
18          * Builds an IoTString object around the char array.  This
19          * constructor makes a copy, so the caller is free to modify the char array.
20          */
21
22 public:
23         IoTString(Array<char> *_array) : array(new Array<char>(_array)) {}
24         IoTString(const char *_array) {
25                 int32_t len = strlen(_array);
26                 array = new Array<char>(len);
27                 strcpy(array->internalArray(), _array);
28         }
29
30         ~IoTString() {
31                 delete array;
32         }
33
34         /**
35          * Internal method to grab a reference to our char array.  Caller
36          * must not modify it.
37          */
38
39         Array<char> *internalBytes() { return array; }
40
41         /**
42          * Returns a copy of the underlying char string.
43          */
44
45         Array<char> *getBytes() { return new Array<char>(array); }
46
47         /**
48          * Returns the length in chars of the IoTString.
49          */
50
51         bool equals(IoTString *str) {
52                 uint strlength = str->array->length();
53                 uint thislength = array->length();
54                 if (strlength != thislength)
55                         return false;
56
57                 int result = memcmp(str->array->internalArray(), array->internalArray(), strlength);
58                 return result == 0;
59         }
60
61         int length() { return array->length(); }
62         friend IoTString *IoTString_shallow(Array<char> *_array);
63 };
64
65 IoTString *IoTString_shallow(Array<char> *_array) {
66         IoTString *str = new IoTString();
67         str->array = _array;
68         return str;
69 }
70 #endif