CUBRID Engine  latest
log_top_string.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * log_top_string.c -
22  */
23 
24 #ident "$Id$"
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "cas_common.h"
31 #include "log_top_string.h"
32 
33 #define STR_ALLOC_SIZE(X) (((X) + 1023) / 1024 * 1024)
34 
35 T_STRING *
36 t_string_make (int init_size)
37 {
38  T_STRING *t_str;
39 
40  t_str = (T_STRING *) MALLOC (sizeof (T_STRING));
41  if (t_str == NULL)
42  return NULL;
43 
44  if (init_size <= 0)
45  init_size = 1;
46  init_size = STR_ALLOC_SIZE (init_size);
47 
48  t_str->data = (char *) MALLOC (init_size);
49  if (t_str->data == NULL)
50  {
51  FREE_MEM (t_str);
52  return NULL;
53  }
54  t_str->alloc_size = init_size;
55  t_string_clear (t_str);
56  return t_str;
57 }
58 
59 void
61 {
62  t_str->data[0] = '\0';
63  t_str->data_len = 0;
64  t_str->bind_len = 0;
65 }
66 
67 int
68 t_string_add (T_STRING * t_str, char *str, int str_len)
69 {
70  return t_bind_string_add (t_str, str, str_len, 0);
71 }
72 
73 int
74 t_bind_string_add (T_STRING * t_str, char *str, int str_len, int bind_len)
75 {
76  if (t_str->alloc_size < t_str->data_len + str_len + 1)
77  {
78  int new_alloc_size = STR_ALLOC_SIZE (t_str->data_len + str_len + 1);
79  t_str->data = (char *) REALLOC (t_str->data, new_alloc_size);
80  if (t_str->data == NULL)
81  return -1;
82  t_str->alloc_size = new_alloc_size;
83  }
84  memcpy (t_str->data + t_str->data_len, str, str_len);
85  t_str->data_len += str_len;
86  t_str->data[t_str->data_len] = '\0';
87  t_str->bind_len = bind_len;
88  return 0;
89 }
90 
91 void
93 {
94  if (t_str)
95  {
96  FREE_MEM (t_str->data);
97  FREE_MEM (t_str);
98  }
99 }
100 
101 char *
103 {
104  return t_str->data;
105 }
106 
107 int
109 {
110  return t_str->data_len;
111 }
112 
113 int
115 {
116  return t_str->bind_len;
117 }
int t_string_add(T_STRING *t_str, char *str, int str_len)
void t_string_free(T_STRING *t_str)
int t_string_len(T_STRING *t_str)
char * data
char * t_string_str(T_STRING *t_str)
int alloc_size
#define NULL
Definition: freelistheap.h:34
#define MALLOC(SIZE)
Definition: cas_common.h:53
int t_string_bind_len(T_STRING *t_str)
#define FREE_MEM(PTR)
Definition: cas_common.h:58
#define STR_ALLOC_SIZE(X)
T_STRING * t_string_make(int init_size)
int t_bind_string_add(T_STRING *t_str, char *str, int str_len, int bind_len)
void t_string_clear(T_STRING *t_str)
#define REALLOC(PTR, SIZE)
Definition: cas_common.h:54