The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

modules.c

Go to the documentation of this file.
00001 /*  (C) Copyright David Abrahams 2001. Permission to copy, use, modify, sell and
00002  *  distribute this software is granted provided this copyright notice appears
00003  *  in all copies. This software is provided "as is" without express or implied
00004  *  warranty, and with no claim as to its suitability for any purpose.
00005  */
00006 #include "modules.h"
00007 #include "jam.h"
00008 #include "string.h"
00009 #include "hash.h"
00010 #include "newstr.h"
00011 #include "lists.h"
00012 #include "parse.h"
00013 #include "rules.h"
00014 #include "variable.h"
00015 #include "strings.h"
00016 #include <assert.h>
00017 
00018 static struct hash* module_hash = 0;
00019 
00020 static char* new_module_str( module_t* m, char* suffix )
00021 {
00022     char* result;
00023     string s;
00024     string_copy( &s, m->name );
00025     string_append( &s, suffix );
00026     result = newstr( s.value );
00027     string_free( &s );
00028     return result;
00029 }
00030 
00031 module_t* bindmodule( char* name )
00032 {
00033     string s;
00034     module_t m_, *m = &m_;
00035 
00036     if( !module_hash )
00037         module_hash = hashinit( sizeof( module_t ), "modules" );
00038 
00039     string_new( &s );
00040     if (name)
00041     {
00042         string_append( &s, name );
00043         string_push_back( &s, '.' );
00044     }
00045         
00046     m->name = s.value;
00047     
00048     if ( hashenter( module_hash, (HASHDATA **)&m ) )
00049     {
00050         m->name = newstr( m->name );
00051         m->variables = 0;
00052         m->rules = 0;
00053         m->imported_modules = 0;
00054         m->class_module = 0;
00055     }
00056     string_free( &s );
00057     return m;
00058 }
00059 
00060 /*
00061  * demand_rules() - Get the module's "rules" hash on demand
00062  */
00063 struct hash* demand_rules( module_t* m )
00064 {
00065     if ( !m->rules )
00066         m->rules = hashinit( sizeof( RULE ), new_module_str( m, "rules" ) );
00067     return m->rules;
00068 }
00069 
00070 /*
00071  * delete_module() - wipe out the module's rules and variables
00072  */
00073 static void delete_rule_( void* xrule, void* data )
00074 {
00075     rule_free( (RULE*)xrule );
00076 }
00077 
00078 void delete_module( module_t* m )
00079 {
00080     /* clear out all the rules */
00081     if ( m->rules )
00082     {
00083         hashenumerate( m->rules, delete_rule_, (void*)0 );
00084         hashdone( m->rules );
00085         m->rules = 0;
00086     }
00087 
00088     if ( m->variables )
00089     {
00090         var_hash_swap( &m->variables );
00091         var_done();
00092         var_hash_swap( &m->variables );
00093         m->variables = 0;
00094     }
00095 }
00096 
00097 module_t* root_module()
00098 {
00099     static module_t* root = 0;
00100     if ( !root )
00101         root = bindmodule(0);
00102     return root;
00103 }
00104 
00105 void enter_module( module_t* m )
00106 {
00107     var_hash_swap( &m->variables );
00108 }
00109 
00110 void exit_module( module_t* m )
00111 {
00112     var_hash_swap( &m->variables );
00113 }
00114 
00115 void import_module(LIST* module_names, module_t* target_module)
00116 { 
00117     struct hash* h;
00118 
00119     if (!target_module->imported_modules)
00120         target_module->imported_modules = hashinit( sizeof(char*), "imported");
00121     h = target_module->imported_modules;
00122 
00123     for(;module_names; module_names = module_names->next) {
00124         
00125         char* s = module_names->string;
00126         char** ss = &s;
00127         
00128         hashenter(h, (HASHDATA**)&ss);
00129     }
00130 }
00131 
00132 static void add_module_name( void* r_, void* result_ )
00133 {
00134     char** r = (char**)r_;
00135     LIST** result = (LIST**)result_;
00136 
00137     *result = list_new( *result, copystr( *r ) );
00138 }
00139 
00140 LIST* imported_modules(module_t* module)
00141 {
00142     LIST *result = L0;
00143 
00144     if ( module->imported_modules )
00145         hashenumerate( module->imported_modules, add_module_name, &result );
00146 
00147     return result;
00148 }
00149 

Generated on Mon Nov 8 17:07:41 2004 for MPT by  doxygen 1.3.9.1