UniMRCP  1.7.0
apt_string.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2015 Arsen Chaloyan
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef APT_STRING_H
18 #define APT_STRING_H
19 
20 /**
21  * @file apt_string.h
22  * @brief String Representation
23  */
24 
25 #include "apt.h"
26 
28 
29 /** Empty string */
30 #define APT_EMPTY_STRING ""
31 
32 /** String declaration */
33 typedef struct apt_str_t apt_str_t;
34 
35 /** String representation */
36 struct apt_str_t {
37  /** String buffer (might be not NULL terminated) */
38  char *buf;
39  /** Length of the string (not counting terminating NULL character if exists) */
40  apr_size_t length;
41 };
42 
43 /** Reset string. */
44 static APR_INLINE void apt_string_reset(apt_str_t *str)
45 {
46  str->buf = NULL;
47  str->length = 0;
48 }
49 
50 /** Get string buffer. */
51 static APR_INLINE const char* apt_string_buffer_get(const apt_str_t *str)
52 {
53  if(str->buf) {
54  return str->buf;
55  }
56  return APT_EMPTY_STRING;
57 }
58 
59 /** Get string length. */
60 static APR_INLINE apr_size_t apt_string_length_get(const apt_str_t *str)
61 {
62  return str->length;
63 }
64 
65 /** Check whether string is empty. */
66 static APR_INLINE apr_size_t apt_string_is_empty(const apt_str_t *str)
67 {
68  return str->length ? FALSE : TRUE;
69 }
70 
71 /**
72  * Set NULL terminated string.
73  * @param str the destination string
74  * @param src the NULL terminated string to set
75  */
76 static APR_INLINE void apt_string_set(apt_str_t *str, const char *src)
77 {
78  str->buf = (char*)src;
79  str->length = src ? strlen(src) : 0;
80 }
81 
82 /**
83  * Assign (copy) NULL terminated string.
84  * @param str the destination string
85  * @param src the NULL terminated string to copy
86  * @param pool the pool to allocate memory from
87  */
88 static APR_INLINE void apt_string_assign(apt_str_t *str, const char *src, apr_pool_t *pool)
89 {
90  str->buf = NULL;
91  str->length = src ? strlen(src) : 0;
92  if(str->length) {
93  str->buf = apr_pstrmemdup(pool,src,str->length);
94  }
95 }
96 
97 /**
98  * Assign (copy) n characters from the src string.
99  * @param str the destination string
100  * @param src the NULL terminated string to copy
101  * @param pool the pool to allocate memory from
102  */
103 static APR_INLINE void apt_string_assign_n(apt_str_t *str, const char *src, apr_size_t length, apr_pool_t *pool)
104 {
105  str->buf = NULL;
106  str->length = length;
107  if(str->length) {
108  str->buf = apr_pstrmemdup(pool,src,str->length);
109  }
110 }
111 
112 /**
113  * Copy string.
114  * @param dest_str the destination string
115  * @param src_str the source string
116  * @param pool the pool to allocate memory from
117  */
118 static APR_INLINE void apt_string_copy(apt_str_t *str, const apt_str_t *src_str, apr_pool_t *pool)
119 {
120  str->buf = NULL;
121  str->length = src_str->length;
122  if(str->length) {
123  str->buf = apr_pstrmemdup(pool,src_str->buf,src_str->length);
124  }
125 }
126 
127 /**
128  * Compare two strings (case insensitive).
129  * @param str1 the string to compare
130  * @param str2 the string to compare
131  * @return TRUE if equal, FALSE otherwise
132  */
133 static APR_INLINE apt_bool_t apt_string_compare(const apt_str_t *str1, const apt_str_t *str2)
134 {
135  if(str1->length != str2->length || !str1->length) {
136  return FALSE;
137  }
138  return (strncasecmp(str1->buf,str2->buf,str1->length) == 0) ? TRUE : FALSE;
139 }
140 
141 /**
142  * Compare two strings (case insensitive).-
143  * @param str1 the string to compare
144  * @param str2 the string to compare
145  * @return TRUE if equal (including empty strings), FALSE otherwise
146  */
147 static APR_INLINE apt_bool_t apt_strings_compare(const apt_str_t *str1, const apt_str_t *str2)
148 {
149  if(str1->length != str2->length) {
150  return FALSE;
151  }
152  if(!str1->length) {
153  return TRUE;
154  }
155  return (strncasecmp(str1->buf,str2->buf,str1->length) == 0) ? TRUE : FALSE;
156 }
157 
158 /**
159  * Represent string as iovec.
160  * @param str the string to represent
161  * @param vec the iovec to set
162  */
163 static APR_INLINE void apt_string_to_iovec(const apt_str_t *str, struct iovec *vec)
164 {
165  vec->iov_base = str->buf;
166  vec->iov_len = str->length;
167 }
168 
170 
171 #endif /* APT_STRING_H */
#define APT_END_EXTERN_C
Definition: apt.h:38
int apt_bool_t
Definition: apt.h:57
apr_size_t length
Definition: apt_string.h:40
#define APT_BEGIN_EXTERN_C
Definition: apt.h:36
APR Toolkit Definitions.
#define APT_EMPTY_STRING
Definition: apt_string.h:30
Definition: apt_string.h:36
char * buf
Definition: apt_string.h:38