Upload packaging folder
[platform/upstream/iotjs.git] / tools / src / iotjs_string.c
1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17 #include "iotjs_def.h"
18 #include "iotjs_string.h"
19 #include "iotjs_util.h"
20
21 #include <string.h>
22
23
24 iotjs_string_t iotjs_string_create() {
25   iotjs_string_t str;
26   IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_string_t, &str);
27
28   _this->size = 0;
29   _this->data = NULL;
30
31   return str;
32 }
33
34
35 iotjs_string_t iotjs_string_create_with_size(const char* data, uint32_t size) {
36   iotjs_string_t str;
37   IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_string_t, &str);
38
39   _this->size = size;
40
41   if (size > 0) {
42     IOTJS_ASSERT(data != NULL);
43     _this->data = iotjs_buffer_allocate(size);
44     memcpy(_this->data, data, size);
45   } else {
46     _this->data = NULL;
47   }
48
49   return str;
50 }
51
52
53 iotjs_string_t iotjs_string_create_with_buffer(char* buffer, uint32_t size) {
54   iotjs_string_t str;
55   IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_string_t, &str);
56
57   _this->size = size;
58
59   if (size > 0) {
60     IOTJS_ASSERT(buffer != NULL);
61     _this->data = buffer;
62   } else {
63     _this->data = NULL;
64   }
65
66   return str;
67 }
68
69
70 void iotjs_string_destroy(iotjs_string_t* str) {
71   IOTJS_VALIDATED_STRUCT_DESTRUCTOR(iotjs_string_t, str);
72
73   if (_this->data != NULL) {
74     iotjs_buffer_release(_this->data);
75     _this->size = 0;
76   }
77 }
78
79
80 bool iotjs_string_is_empty(const iotjs_string_t* str) {
81   const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_string_t, str);
82
83   return _this->size == 0;
84 }
85
86
87 void iotjs_string_make_empty(iotjs_string_t* str) {
88   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_string_t, str);
89
90   if (_this->data != NULL) {
91     iotjs_buffer_release(_this->data);
92     _this->size = 0;
93     _this->data = NULL;
94   }
95 }
96
97
98 void iotjs_string_append(iotjs_string_t* str, const char* data, uint32_t size) {
99   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_string_t, str);
100
101   IOTJS_ASSERT(data != NULL);
102
103   if (size == 0) {
104     return;
105   }
106
107   if (_this->data != NULL) {
108     _this->data = iotjs_buffer_reallocate(_this->data, _this->size + size);
109   } else {
110     IOTJS_ASSERT(_this->size == 0);
111     _this->data = iotjs_buffer_allocate(size);
112   }
113
114   memcpy(_this->data + _this->size, data, size);
115   _this->size += size;
116 }
117
118
119 const char* iotjs_string_data(const iotjs_string_t* str) {
120   const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_string_t, str);
121   if (_this->data == NULL) {
122     return "";
123   }
124
125   return _this->data;
126 }
127
128
129 unsigned iotjs_string_size(const iotjs_string_t* str) {
130   const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_string_t, str);
131   return _this->size;
132 }