1 /*
2 * Copyright 2008-2014 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 * $Id: apt_string.h 2136 2014-07-04 06:33:36Z achaloyan@gmail.com $
17 */
18
19 #ifndef APT_STRING_H
20 #define APT_STRING_H
21
22 /**
23 * @file apt_string.h
24 * @brief String Representation
25 */
26
27 #include "apt.h"
28
29 APT_BEGIN_EXTERN_C
30
31 /** Empty string */
32 #define APT_EMPTY_STRING ""
33
34 /** String declaration */
35 typedef struct apt_str_t apt_str_t;
36
37 /** String representation */
38 struct apt_str_t {
39 /** String buffer (might be not NULL terminated) */
40 char *buf;
41 /** Length of the string (not counting terminating NULL character if exists) */
42 apr_size_t length;
43 };
44
45 /** Reset string. */
apt_string_reset(apt_str_t * str)46 static APR_INLINE void apt_string_reset(apt_str_t *str)
47 {
48 str->buf = NULL;
49 str->length = 0;
50 }
51
52 /** Get string buffer. */
apt_string_buffer_get(const apt_str_t * str)53 static APR_INLINE const char* apt_string_buffer_get(const apt_str_t *str)
54 {
55 if(str->buf) {
56 return str->buf;
57 }
58 return APT_EMPTY_STRING;
59 }
60
61 /** Get string length. */
apt_string_length_get(const apt_str_t * str)62 static APR_INLINE apr_size_t apt_string_length_get(const apt_str_t *str)
63 {
64 return str->length;
65 }
66
67 /** Check whether string is empty. */
apt_string_is_empty(const apt_str_t * str)68 static APR_INLINE apr_size_t apt_string_is_empty(const apt_str_t *str)
69 {
70 return str->length ? FALSE : TRUE;
71 }
72
73 /**
74 * Set NULL terminated string.
75 * @param str the destination string
76 * @param src the NULL terminated string to set
77 */
apt_string_set(apt_str_t * str,const char * src)78 static APR_INLINE void apt_string_set(apt_str_t *str, const char *src)
79 {
80 str->buf = (char*)src;
81 str->length = src ? strlen(src) : 0;
82 }
83
84 /**
85 * Assign (copy) NULL terminated string.
86 * @param str the destination string
87 * @param src the NULL terminated string to copy
88 * @param pool the pool to allocate memory from
89 */
apt_string_assign(apt_str_t * str,const char * src,apr_pool_t * pool)90 static APR_INLINE void apt_string_assign(apt_str_t *str, const char *src, apr_pool_t *pool)
91 {
92 str->buf = NULL;
93 str->length = src ? strlen(src) : 0;
94 if(str->length) {
95 str->buf = apr_pstrmemdup(pool,src,str->length);
96 }
97 }
98
99 /**
100 * Assign (copy) n characters from the src string.
101 * @param str the destination string
102 * @param src the NULL terminated string to copy
103 * @param pool the pool to allocate memory from
104 */
apt_string_assign_n(apt_str_t * str,const char * src,apr_size_t length,apr_pool_t * pool)105 static APR_INLINE void apt_string_assign_n(apt_str_t *str, const char *src, apr_size_t length, apr_pool_t *pool)
106 {
107 str->buf = NULL;
108 str->length = length;
109 if(str->length) {
110 str->buf = apr_pstrmemdup(pool,src,str->length);
111 }
112 }
113
114 /**
115 * Copy string.
116 * @param dest_str the destination string
117 * @param src_str the source string
118 * @param pool the pool to allocate memory from
119 */
apt_string_copy(apt_str_t * str,const apt_str_t * src_str,apr_pool_t * pool)120 static APR_INLINE void apt_string_copy(apt_str_t *str, const apt_str_t *src_str, apr_pool_t *pool)
121 {
122 str->buf = NULL;
123 str->length = src_str->length;
124 if(str->length) {
125 str->buf = apr_pstrmemdup(pool,src_str->buf,src_str->length);
126 }
127 }
128
129 /**
130 * Compare two strings (case insensitive).
131 * @param str1 the string to compare
132 * @param str2 the string to compare
133 * @return TRUE if equal, FALSE otherwise
134 */
apt_string_compare(const apt_str_t * str1,const apt_str_t * str2)135 static APR_INLINE apt_bool_t apt_string_compare(const apt_str_t *str1, const apt_str_t *str2)
136 {
137 if(str1->length != str2->length || !str1->length) {
138 return FALSE;
139 }
140 return (strncasecmp(str1->buf,str2->buf,str1->length) == 0) ? TRUE : FALSE;
141 }
142
143 /**
144 * Represent string as iovec.
145 * @param str the string to represent
146 * @param vec the iovec to set
147 */
apt_string_to_iovec(const apt_str_t * str,struct iovec * vec)148 static APR_INLINE void apt_string_to_iovec(const apt_str_t *str, struct iovec *vec)
149 {
150 vec->iov_base = str->buf;
151 vec->iov_len = str->length;
152 }
153
154 APT_END_EXTERN_C
155
156 #endif /* APT_STRING_H */
157