edits
[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
25         IoTString(const char *_array) {
26                 int32_t len = strlen(_array);
27                 array = new Array<char>(len);
28                 strcpy(array->internalArray(), _array);
29         }
30
31         IoTString(IoTString *string) : array(new Array<char>(string->array)) {
32         }
33
34         ~IoTString() {
35                 delete array;
36         }
37
38         /**
39          * Internal method to grab a reference to our char array.  Caller
40          * must not modify it.
41          */
42
43         Array<char> *internalBytes() { return array; }
44
45         /**
46          * Returns a copy of the underlying char string.
47          */
48
49         Array<char> *getBytes() { return new Array<char>(array); }
50
51         /**
52          * Returns the length in chars of the IoTString.
53          */
54
55         bool equals(IoTString *str) {
56                 uint strlength = str->array->length();
57                 uint thislength = array->length();
58                 if (strlength != thislength)
59                         return false;
60
61                 int result = memcmp(str->array->internalArray(), array->internalArray(), strlength);
62                 return result == 0;
63         }
64
65         int length() { return array->length(); }
66         friend IoTString *IoTString_shallow(Array<char> *_array);
67 };
68
69 IoTString *IoTString_shallow(Array<char> *_array) {
70         IoTString *str = new IoTString();
71         str->array = _array;
72         return str;
73 }
74 #endif