DS3 C SDK  4.1.0
Provides access to the Spectra S3 API with C
ds3_string.c
Go to the documentation of this file.
1 /*
2  * ******************************************************************************
3  * Copyright 2014-2017 Spectra Logic Corporation. All Rights Reserved.
4  * Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5  * this file except in compliance with the License. A copy of the License is located at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * or in the "license" file accompanying this file.
10  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  * ****************************************************************************
14  */
15 
16 #include "ds3_string.h"
17 #include <glib.h>
18 #include <string.h>
19 
20 ds3_str* ds3_str_init(const char* string) {
21  size_t size = strlen(string);
22  return ds3_str_init_with_size(string, size);
23 }
24 
25 ds3_str* ds3_str_init_with_size(const char* string, size_t size) {
26  ds3_str* str = g_new0(ds3_str, 1);
27  str->value = g_strndup(string, size);
28  str->size = size;
29  return str;
30 }
31 
32 ds3_str* ds3_str_dup(const ds3_str* string) {
33  if (string == NULL) {
34  return NULL;
35  }
36  ds3_str* str = g_new0(ds3_str, 1);
37  str->value = g_strndup(string->value, string->size);
38  str->size = string->size;
39  return str;
40 }
41 
42 char* ds3_str_value(const ds3_str* string) {
43  return string->value;
44 }
45 
46 size_t ds3_str_size(const ds3_str* string) {
47  return string->size;
48 }
49 
50 void ds3_str_free(ds3_str* string) {
51  if (string == NULL) return;
52 
53  if (string->value != NULL) {
54  g_free(string->value);
55  }
56  g_free(string);
57 }
58 
size_t size
Definition: ds3_string.h:38
size_t ds3_str_size(const ds3_str *string)
Definition: ds3_string.c:46
void ds3_str_free(ds3_str *string)
Definition: ds3_string.c:50
ds3_str * ds3_str_dup(const ds3_str *string)
Definition: ds3_string.c:32
ds3_str * ds3_str_init_with_size(const char *string, size_t size)
Definition: ds3_string.c:25
ds3_str * ds3_str_init(const char *string)
Definition: ds3_string.c:20
char * value
Definition: ds3_string.h:37
char * ds3_str_value(const ds3_str *string)
Definition: ds3_string.c:42