coan 4.2.4
directives_tally.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007-2011 Mike Kinghan, imk@strudl.org                  *
00003  *   All rights reserved.                                                  *
00004  *                                                                         *
00005  *   Contributed originally by Mike Kinghan, imk@strudl.org                *
00006  *                                                                         *
00007  *   Redistribution and use in source and binary forms, with or without    *
00008  *   modification, are permitted provided that the following conditions    *
00009  *   are met:                                                              *
00010  *                                                                         *
00011  *   Redistributions of source code must retain the above copyright        *
00012  *   notice, this list of conditions and the following disclaimer.         *
00013  *                                                                         *
00014  *   Redistributions in binary form must reproduce the above copyright     *
00015  *   notice, this list of conditions and the following disclaimer in the   *
00016  *   documentation and/or other materials provided with the distribution.  *
00017  *                                                                         *
00018  *   Neither the name of Symbian Software Ltd. nor the names of its        *
00019  *   contributors may be used to endorse or promote products derived from  *
00020  *   this software without specific prior written permission.              *
00021  *                                                                         *
00022  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   *
00023  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     *
00024  *   LIMITED TO, THE IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS    *
00025  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE        *
00026  *   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,   *
00027  *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  *
00028  *   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
00029  *   OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    *
00030  *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
00031  *   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
00032  *   THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH  *
00033  *   DAMAGE.                                                               *
00034  *                                                                         *
00035  **************************************************************************/
00036 #include "state_utils.h"
00037 #include "ptr_set.h"
00038 #include "report.h"
00039 #include "args.h"
00040 #include "lexicon.h"
00041 
00051 STATE_DEF(directives_tally)
00052 {
00053     ptr_vector_h directives_tab;
00056 }
00057 STATE_T(directives_tally);
00062 NO_PUBLIC_STATE(directives_tally);
00063 IMPLEMENT(directives_tally,USER_INITABLE);
00064 
00065 DEFINE_USER_INIT(directives_tally)(STATE_T(directives_tally) * st_state)
00066 {
00067     st_state->directives_tab =
00068         ptr_vector_new((dtor_t)ptr_set_dispose,(cloner_t)ptr_set_copy);
00069 }
00070 
00071 DEFINE_USER_FINIS(directives_tally)(STATE_T(directives_tally) * st_state)
00072 {
00073     ptr_vector_dispose(st_state->directives_tab);
00074 }
00077 /* Helpers */
00078 
00082 
00084 typedef struct directive {
00085     directive_type_t type;      
00086     char const *keyword;        
00087 } directive_t;
00088 
00089 
00091 static directive_t const directives_dict[] = {
00092     {   HASH_IF,                IF },
00093     {   HASH_IFDEF,             IFDEF },
00094     {   HASH_IFNDEF,    IFNDEF },
00095     {   HASH_ELSE,              ELSE },
00096     {   HASH_ELIF,              ELIF },
00097     {   HASH_ENDIF,             ENDIF },
00098     {   HASH_DEFINE,    DEFINE },
00099     {   HASH_UNDEF,             UNDEF },
00100     {   HASH_INCLUDE,   INCLUDE },
00101     {   HASH_PRAGMA,    PRAGMA },
00102     {   HASH_ERROR,             ERROR },
00103     {   HASH_UNKNOWN, NULL }
00104 };
00105 
00106 /* Helpers */
00107 
00116 static canonical_string_const_h
00117 directive_text_lookup(canonical_string_const_h cl, directive_type_t directive_type)
00118 {
00119     canonical_string_h found = NULL;
00120     char const *text = canonical_string_text(cl);
00121     ptr_vector_h directives_tab = GET_STATE(directives_tally,directives_tab);
00122     size_t ntypes = ptr_vector_count(directives_tab);
00123     ptr_set_h directive_tab = NULL;
00124     for (       ; ntypes <= (size_t)directive_type; ++ntypes) {
00125         ptr_vector_append(directives_tab,
00126                           ptr_set_new((comparator_t)canonical_string_compare,
00127                                       (dtor_t)canonical_string_dispose,
00128                                       (cloner_t)canonical_string_copy));
00129     }
00130 
00131     directive_tab = ptr_vector_at(directives_tab,directive_type);
00132     found = ptr_set_search(directive_tab,text,strlen(text));
00133     return found;
00134 }
00135 
00142 static void
00143 directive_text_add(canonical_string_const_h cl, directive_type_t directive_type)
00144 {
00145     ptr_vector_h directives_tab = GET_STATE(directives_tally,directives_tab);
00146     ptr_set_h directive_tab = ptr_vector_at(directives_tab,directive_type);
00147     ptr_set_insert(directive_tab,cl);
00148 }
00149 
00152 /* API *********************************************************************/
00153 
00154 directive_type_t
00155 get_directive_type(char const **keyword)
00156 {
00157     directive_t const * directive = directives_dict;
00158     for (       ; directive->type != HASH_UNKNOWN; ++directive) {
00159         if (match_keyword(keyword,directive->keyword)) {
00160             break;
00161         }
00162     }
00163     return directive->type;
00164 }
00165 
00166 char const *
00167 get_directive_keyword(directive_type_t directive_type)
00168 {
00169     directive_t const * directive = directives_dict;
00170     for (       ; directive->type != HASH_UNKNOWN; ++directive) {
00171         if (directive->type == directive_type) {
00172             break;
00173         }
00174     }
00175     return directive->keyword;
00176 }
00177 
00178 void
00179 directive_tally(canonical_string_const_h cl, directive_type_t directive_type)
00180 {
00181     canonical_string_const_h found = directive_text_lookup(cl,directive_type);
00182     if (!found) {
00183         directive_text_add(cl,directive_type);
00184         report_directive(cl,directive_type);
00185     } else if (!GET_PUBLIC(args,list_only_once)) {
00186         report_directive(found,directive_type);
00187     }
00188 }
00189 
00190 /* EOF*/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines