1 /* $OpenLDAP: pkg/ldap/libraries/librewrite/var.c,v 1.13.2.3 2008/02/11 23:26:43 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2008 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Pierangelo Masarati for
17 * inclusion in OpenLDAP Software.
22 #include "rewrite-int.h"
33 const struct rewrite_var
*v1
, *v2
;
35 v1
= ( const struct rewrite_var
* )c1
;
36 v2
= ( const struct rewrite_var
* )c2
;
40 assert( v1
->lv_name
!= NULL
);
41 assert( v2
->lv_name
!= NULL
);
43 return strcasecmp( v1
->lv_name
, v2
->lv_name
);
55 struct rewrite_var
*v1
, *v2
;
57 v1
= ( struct rewrite_var
* )c1
;
58 v2
= ( struct rewrite_var
* )c2
;
62 assert( v1
->lv_name
!= NULL
);
63 assert( v2
->lv_name
!= NULL
);
65 return ( strcasecmp( v1
->lv_name
, v2
->lv_name
) == 0 ? -1 : 0 );
76 struct rewrite_var
*var
= v_var
;
77 assert( var
!= NULL
);
79 assert( var
->lv_name
!= NULL
);
80 assert( var
->lv_value
.bv_val
!= NULL
);
82 if ( var
->lv_flags
& REWRITE_VAR_COPY_NAME
)
84 if ( var
->lv_flags
& REWRITE_VAR_COPY_VALUE
)
85 free( var
->lv_value
.bv_val
);
97 avl_free( tree
, rewrite_var_free
);
98 return REWRITE_SUCCESS
;
110 struct rewrite_var var
;
112 assert( name
!= NULL
);
114 var
.lv_name
= ( char * )name
;
115 return ( struct rewrite_var
* )avl_find( tree
,
116 ( caddr_t
)&var
, rewrite_var_cmp
);
121 struct rewrite_var
*var
,
126 ber_len_t len
= strlen( value
);
128 if ( var
->lv_flags
& REWRITE_VAR_COPY_VALUE
) {
129 if ( flags
& REWRITE_VAR_COPY_VALUE
) {
130 if ( len
<= var
->lv_value
.bv_len
) {
131 AC_MEMCPY(var
->lv_value
.bv_val
, value
, len
+ 1);
134 free( var
->lv_value
.bv_val
);
135 var
->lv_value
.bv_val
= strdup( value
);
139 free( var
->lv_value
.bv_val
);
140 var
->lv_value
.bv_val
= (char *)value
;
141 var
->lv_flags
&= ~REWRITE_VAR_COPY_VALUE
;
145 if ( flags
& REWRITE_VAR_COPY_VALUE
) {
146 var
->lv_value
.bv_val
= strdup( value
);
147 var
->lv_flags
|= REWRITE_VAR_COPY_VALUE
;
150 var
->lv_value
.bv_val
= (char *)value
;
154 var
->lv_value
.bv_len
= len
;
160 * Inserts a newly created var
163 rewrite_var_insert_f(
170 struct rewrite_var
*var
;
173 assert( tree
!= NULL
);
174 assert( name
!= NULL
);
175 assert( value
!= NULL
);
177 var
= rewrite_var_find( *tree
, name
);
179 if ( flags
& REWRITE_VAR_UPDATE
) {
180 (void)rewrite_var_replace( var
, value
, flags
);
187 var
= calloc( sizeof( struct rewrite_var
), 1 );
192 memset( var
, 0, sizeof( struct rewrite_var
) );
194 if ( flags
& REWRITE_VAR_COPY_NAME
) {
195 var
->lv_name
= strdup( name
);
196 if ( var
->lv_name
== NULL
) {
200 var
->lv_flags
|= REWRITE_VAR_COPY_NAME
;
203 var
->lv_name
= (char *)name
;
206 if ( flags
& REWRITE_VAR_COPY_VALUE
) {
207 var
->lv_value
.bv_val
= strdup( value
);
208 if ( var
->lv_value
.bv_val
== NULL
) {
212 var
->lv_flags
|= REWRITE_VAR_COPY_VALUE
;
215 var
->lv_value
.bv_val
= (char *)value
;
217 var
->lv_value
.bv_len
= strlen( value
);
218 rc
= avl_insert( tree
, ( caddr_t
)var
,
219 rewrite_var_cmp
, rewrite_var_dup
);
222 if ( rc
!= 0 && var
) {
223 avl_delete( tree
, ( caddr_t
)var
, rewrite_var_cmp
);
224 rewrite_var_free( var
);
242 struct rewrite_var
*var
;
244 assert( tree
!= NULL
);
245 assert( name
!= NULL
);
246 assert( value
!= NULL
);
248 var
= rewrite_var_find( *tree
, name
);
250 if ( flags
& REWRITE_VAR_INSERT
) {
251 return rewrite_var_insert_f( tree
, name
, value
, flags
);
258 assert( var
->lv_value
.bv_val
!= NULL
);
260 (void)rewrite_var_replace( var
, value
, flags
);