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