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 #include <stdio.h>
7 /**
8  * IoTString wraps the underlying char string.
9  * @author Brian Demsky <bdemsky@uci.edu>
10  * @version 1.0
11  */
12
13 inline unsigned int hashCharArray(Array<char> *array) {
14         uint len = array->length();
15         unsigned int hash = 0;
16         for (uint i = 0; i < len; i++) {
17                 hash = 31 * hash + ((unsigned int) array->get(i));
18         }
19         return hash;
20 }
21
22 class IoTString {
23 private:
24         Array<char> *array;
25         IoTString() {}
26         unsigned int hashvalue;
27         /**
28          * Builds an IoTString object around the char array.  This
29          * constructor makes a copy, so the caller is free to modify the char array.
30          */
31
32 public:
33         IoTString(Array<char> *_array) :
34                 array(new Array<char>(_array)),
35                 hashvalue(hashCharArray(array)) {
36         }
37
38         IoTString(const char *_array) {
39                 int32_t len = strlen(_array);
40                 array = new Array<char>(len);
41                 memcpy(array->internalArray(), _array, len);
42                 hashvalue = hashCharArray(array);
43         }
44
45         IoTString(IoTString *string) :
46           array(new Array<char>(string->array)),
47                 hashvalue(string->hashvalue) {
48         }
49
50         ~IoTString() {
51                 delete array;
52         }
53
54         void print() {
55                 for(uint i=0; i < array->length(); i++)
56                         printf("%c", array->get(i));
57         };
58         
59         char get(uint i) {return array->get(i);}
60
61         /**
62          * Internal method to grab a reference to our char array.  Caller
63          * must not modify it.
64          */
65
66         Array<char> *internalBytes() { return array; }
67
68         /**
69          * Returns a copy of the underlying char string.
70          */
71
72         Array<char> *getBytes() { return new Array<char>(array); }
73
74         /**
75          * Returns the length in chars of the IoTString.
76          */
77
78         
79         bool equals(IoTString *str) {
80                 uint strlength = str->array->length();
81                 uint thislength = array->length();
82                 if (strlength != thislength)
83                         return false;
84
85                 int result = memcmp(str->array->internalArray(), array->internalArray(), strlength);
86                 return result == 0;
87         }
88
89         unsigned int hashValue() { return hashvalue;}
90         int length() { return array->length(); }
91         friend IoTString *IoTString_shallow(Array<char> *_array);
92 };
93
94 inline IoTString *IoTString_shallow(Array<char> *_array) {
95         IoTString *str = new IoTString();
96         str->array = _array;
97         str->hashvalue = hashCharArray(_array);
98         return str;
99 }
100
101 inline unsigned int hashString(IoTString *a) {
102         return a->hashValue();
103 }
104
105 inline bool StringEquals(IoTString *a, IoTString *b) {
106         return a->equals(b);
107 }
108 #endif