NCBI C++ ToolKit
tdsstring.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  * Copyright (C) 2004-2015 Frediano Ziglio
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 #include <config.h>
21 
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <assert.h>
25 
26 #if HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif /* HAVE_STDLIB_H */
29 
30 #if HAVE_STRING_H
31 #include <string.h>
32 #endif /* HAVE_STRING_H */
33 
34 #include <freetds/tds.h>
35 #include <freetds/string.h>
36 
37 
38 /**
39  * \ingroup libtds
40  * \defgroup dstring Dynamic string functions
41  * Handle dynamic string. In this string are always valid
42  * (you don't have NULL pointer, only empty strings)
43  */
44 
45 /* This is in a separate module because we use the pointer to discriminate allocated and not allocated */
46 const struct tds_dstr tds_str_empty = { 0, "" };
47 #define EMPTY ((struct tds_dstr*) &tds_str_empty)
48 
49 /**
50  * \addtogroup dstring
51  * @{
52  */
53 
54 /** clear all string filling with zeroes (mainly for security reason) */
55 void
57 {
58  memset((*s)->dstr_s, 0, (*s)->dstr_size);
59 }
60 
61 /** free string */
62 void
64 {
65  if (*s != EMPTY)
66  free(*s);
67  *s = EMPTY;
68 }
69 
70 /**
71  * Set string to a given buffer of characters
72  * @param s dynamic string
73  * @param src source buffer
74  * @param length length of source buffer
75  * @return string copied or NULL on memory error
76  */
77 DSTR*
78 tds_dstr_copyn(DSTR * s, const char *src, size_t length)
79 {
80  if (!length) {
81  if (*s != EMPTY) {
82  free(*s);
83  *s = EMPTY;
84  }
85  } else {
86  struct tds_dstr *p = (struct tds_dstr *) malloc(length + TDS_OFFSET(struct tds_dstr, dstr_s) + 1);
87  if (TDS_UNLIKELY(!p))
88  return NULL;
89  memcpy(p->dstr_s, src, length);
90  p->dstr_s[length] = 0;
91  p->dstr_size = length;
92  if (*s != EMPTY)
93  free(*s);
94  *s = p;
95  }
96  return s;
97 }
98 
99 /**
100  * set a string from another buffer.
101  * The string will use the supplied buffer (it not copy the string),
102  * so it should be a pointer returned by malloc.
103  * @param s dynamic string
104  * @param src source buffer
105  * @return string copied or NULL on memory error
106  */
107 DSTR*
108 tds_dstr_set(DSTR * s, char *src)
109 {
110  DSTR *res = tds_dstr_copy(s, src);
111  if (TDS_LIKELY(res != NULL))
112  free(src);
113  return res;
114 }
115 
116 /**
117  * copy a string from another
118  * @param s dynamic string
119  * @param src source buffer
120  * @return string copied or NULL on memory error
121  */
122 DSTR*
123 tds_dstr_copy(DSTR * s, const char *src)
124 {
125  return tds_dstr_copyn(s, src, strlen(src));
126 }
127 
128 /**
129  * Duplicate a string from another dynamic string
130  * @param s output string
131  * @param src source string to copy
132  * @return string copied or NULL on memory error
133  */
134 DSTR*
135 tds_dstr_dup(DSTR * s, const DSTR * src)
136 {
137  return tds_dstr_copyn(s, (*src)->dstr_s, (*src)->dstr_size);
138 }
139 
140 /**
141  * limit length of string, MUST be <= current length
142  * @param s dynamic string
143  * @param length new length
144  */
145 DSTR*
146 tds_dstr_setlen(DSTR *s, size_t length)
147 {
148 #if ENABLE_EXTRA_CHECKS
149  assert((*s)->dstr_size >= length);
150 #endif
151  /* test required for empty strings */
152  if ((*s)->dstr_size >= length && *s != EMPTY) {
153  (*s)->dstr_size = length;
154  (*s)->dstr_s[length] = 0;
155  }
156  return s;
157 }
158 
159 /**
160  * allocate space for length char
161  * @param s dynamic string
162  * @param length new length
163  * @return string allocated or NULL on memory error
164  */
165 DSTR*
166 tds_dstr_alloc(DSTR *s, size_t length)
167 {
168  struct tds_dstr *p = (struct tds_dstr *) malloc(length + TDS_OFFSET(struct tds_dstr, dstr_s) + 1);
169  if (TDS_UNLIKELY(!p))
170  return NULL;
171 
172  if (*s != EMPTY)
173  free(*s);
174  p->dstr_s[0] = 0;
175  p->dstr_size = length;
176  *s = p;
177  return s;
178 }
179 
180 /** @} */
#define NULL
Definition: ncbistd.hpp:225
DSTR * tds_dstr_setlen(DSTR *s, size_t length)
limit length of string, MUST be <= current length
Definition: tdsstring.c:146
void tds_dstr_zero(DSTR *s)
clear all string filling with zeroes (mainly for security reason)
Definition: tdsstring.c:56
DSTR * tds_dstr_dup(DSTR *s, const DSTR *src) TDS_WUR
Duplicate a string from another dynamic string.
Definition: tdsstring.c:135
DSTR * tds_dstr_copyn(DSTR *s, const char *src, size_t length) TDS_WUR
Set string to a given buffer of characters.
Definition: tdsstring.c:78
const struct tds_dstr tds_str_empty
Internal representation for an empty string.
Definition: tdsstring.c:46
DSTR * tds_dstr_alloc(DSTR *s, size_t length) TDS_WUR
allocate space for length char
Definition: tdsstring.c:166
DSTR * tds_dstr_set(DSTR *s, char *src) TDS_WUR
set a string from another buffer.
Definition: tdsstring.c:108
void tds_dstr_free(DSTR *s)
free string
Definition: tdsstring.c:63
DSTR * tds_dstr_copy(DSTR *s, const char *src) TDS_WUR
copy a string from another
Definition: tdsstring.c:123
#define assert(x)
Definition: srv_diag.hpp:58
Structure to hold a string.
Definition: tds.h:116
char dstr_s[1]
Definition: tds.h:118
size_t dstr_size
Definition: tds.h:117
Main include file for libtds.
#define TDS_OFFSET(str, field)
Definition: tds.h:364
#define TDS_LIKELY(x)
Definition: tds.h:371
#define TDS_UNLIKELY(x)
Definition: tds.h:372
#define EMPTY
Definition: tdsstring.c:47
void free(voidpf ptr)
voidp malloc(uInt size)
Modified on Fri Jan 05 07:25:07 2024 by modify_doxy.py rev. 669887