Back to index

openldap  2.4.31
index.c
Go to the documentation of this file.
00001 /* index.c - index utilities */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 #include "portable.h"
00018 
00019 #include <stdio.h>
00020 #include <ac/string.h>
00021 #include <lutil.h>
00022 
00023 #include "slap.h"
00024 
00025 static slap_verbmasks idxstr[] = {
00026        { BER_BVC("pres"), SLAP_INDEX_PRESENT },
00027        { BER_BVC("eq"), SLAP_INDEX_EQUALITY },
00028        { BER_BVC("approx"), SLAP_INDEX_APPROX },
00029        { BER_BVC("subinitial"), SLAP_INDEX_SUBSTR_INITIAL },
00030        { BER_BVC("subany"), SLAP_INDEX_SUBSTR_ANY },
00031        { BER_BVC("subfinal"), SLAP_INDEX_SUBSTR_FINAL },
00032        { BER_BVC("sub"), SLAP_INDEX_SUBSTR_DEFAULT },
00033        { BER_BVC("substr"), 0 },
00034        { BER_BVC("notags"), SLAP_INDEX_NOTAGS },
00035        { BER_BVC("nolang"), 0 },   /* backwards compat */
00036        { BER_BVC("nosubtypes"), SLAP_INDEX_NOSUBTYPES },
00037        { BER_BVNULL, 0 }
00038 };
00039 
00040 
00041 int slap_str2index( const char *str, slap_mask_t *idx )
00042 {
00043        int i;
00044 
00045        i = verb_to_mask( str, idxstr );
00046        if ( BER_BVISNULL(&idxstr[i].word) ) return LDAP_OTHER;
00047        while ( !idxstr[i].mask ) i--;
00048        *idx = idxstr[i].mask;
00049 
00050 
00051        return LDAP_SUCCESS;
00052 }
00053 
00054 void slap_index2bvlen( slap_mask_t idx, struct berval *bv )
00055 {
00056        int i;
00057 
00058        bv->bv_len = 0;
00059 
00060        for ( i=0; !BER_BVISNULL( &idxstr[i].word ); i++ ) {
00061               if ( !idxstr[i].mask ) continue;
00062               if ( IS_SLAP_INDEX( idx, idxstr[i].mask )) {
00063                      if ( (idxstr[i].mask & SLAP_INDEX_SUBSTR) &&
00064                             ((idx & SLAP_INDEX_SUBSTR_DEFAULT) != idxstr[i].mask))
00065                             continue;
00066                      if ( bv->bv_len ) bv->bv_len++;
00067                      bv->bv_len += idxstr[i].word.bv_len;
00068               }
00069        }
00070 }
00071 
00072 /* caller must provide buffer space, after calling index2bvlen */
00073 void slap_index2bv( slap_mask_t idx, struct berval *bv )
00074 {
00075        int i;
00076        char *ptr;
00077 
00078        if ( !bv->bv_len ) return;
00079 
00080        ptr = bv->bv_val;
00081        for ( i=0; !BER_BVISNULL( &idxstr[i].word ); i++ ) {
00082               if ( !idxstr[i].mask ) continue;
00083               if ( IS_SLAP_INDEX( idx, idxstr[i].mask )) {
00084                      if ( (idxstr[i].mask & SLAP_INDEX_SUBSTR) &&
00085                             ((idx & SLAP_INDEX_SUBSTR_DEFAULT) != idxstr[i].mask))
00086                             continue;
00087                      if ( ptr != bv->bv_val ) *ptr++ = ',';
00088                      ptr = lutil_strcopy( ptr, idxstr[i].word.bv_val );
00089               }
00090        }
00091 }