TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Kurt Kuzba
from: Roy J. Tellason
date: 2004-01-11 20:17:26
subject: [C] An interesting question

Kurt Kuzba wrote in a message to Roy.J..Tellason!1.270.615.0{at}filegat:

 KK> From: "Kurt Kuzba" 

 KK> From: Roy.J..Tellason!1.270.615.0{at}filegate.net

Hm,  looks like something still needs to be fiddled with at the gateway...

 KK> Neil Heller wrote in a message to All:
RT> NH>  I want to write a program that would contain an unknown
RT> NH>  number of modules.  None of the modules would contain or
RT> NH>  use global variables and  none would be dependent on
RT> NH>  anything outside that module itself.  What I  envision is
RT> NH>  some way for a module to be able to tell a controller:
RT> NH>  1.  that it exists.
RT>  Make one of the modules mandatory ("main{}" :-) and use it to
RT>  "register" whatever else gets included.
RT> NH>  2.  what it does.  This would be a string supplied by the
RT> NH>  module itself (such as: "What is the meaning of life").
RT>  That's kinda application-dependent,  isn't it?
RT> NH>  3. what function would have to be called in order for the
RT> NH>  module to spring to life.
RT>  Pass a pointer to a function?
RT> NH>  Does anybody have an idea how I could implement such a
RT> NH>  beast?
RT>  See above.  

 KK>     One could use this as a model for a .dll file, actually.

I know nothing about .dll files.

 KK> As far as registering the modules goes, this can not really be 
 KK> done without your intervention at compile time, TTBOMK.

I was thinking of a more dynamic model,  something that would be handled at
runtime,  actually.

 KK> If you just want a list of available modules, then you could have 
 KK> a function which tests for their modulename_What_am_I() function 
 KK> and registers them in a list of modules.  If the module is not 
 KK> present at compile time,  it will result in an unresolved external 
 KK> symbol error.  Given this external module:
 KK> /*testmod.h*/
 KK> char* testmod_What_am_I()
 KK> { return "I'm just a test module."; }
 KK> /*end testmod.h*/

 KK>  and this program:
 KK> /*testapp.c*/
 KK> #include 
 KK> #include "testmod.h"
 KK> typedef struct{ int testmod; int nullmod; } linked_modules; int
 KK> main(void){  linked_modules MyMods={0,0};
 KK>  if (testmod_What_am_I())
 KK>  {  MyMods.testmod=1;
 KK>     puts(testmod_What_am_I()); }
 KK> /*  MISSING MODULE
 KK>  if (nullmod_What_am_I())
 KK>  {  MyMods.nullmod=1;
 KK>     puts(nullmod_What_am_I()); }*/
 KK>  return 0;
 KK> }
 KK> /*end testapp.c*/

 KK> You will get a linker error for nullmod_What_am_I(), which means 
 KK> you have to delete or comment out the reference for nullmod. 
 KK> Simply commenting it out will document your source.  Your MyMods 
 KK> struct will hold one int for each possible module, and the value 
 KK> of each variable will give you their status.  A separate char 
 KK> array of module names will give you module prefixes for calling 
 KK> the functions, which may then be matched to the struct to see if 
 KK> there is anything there to be called.  The same struct, array, and 
 KK> initialization routines may be used for all subsequent compilations
 KK> using the given library of available modules.  The use of static 
 KK> arrays and a static struct ensures that all modules will be set to 
 KK> zero upon initialization and only set to 1 upon having successfully
 KK> been compiled and tested for without manually setting the variable 
 KK> for each compilation.

I can see where something like this might have its uses.

 KK> A sample function for this might look like this:

 KK> typedef struct{
 KK>     char** Module_list;
 KK>     int*   Module_available;
 KK>     int    Module_count;
 KK> } Module_Registration;
 KK> Module_Registration* Module_Register(void){
 KK>     static char** Modules = "testmod",
"nullmod", "stringmod",    
 KK> "linklistmod"};
 KK>     static int Isit[4];
 KK>     static Module_Registration ModReg;
 KK>     ModReg.Module_count += (Isit[0] = !!testmod_What_am_I()); /*
 KK> NOT LINKED ---
 KK>     ModReg.Module_count += (Isit[1] = !!nullmod_What_am_I());*/    
 KK> ModReg.Module_count += (Isit[2] = !!stringmod_What_am_I());    
 KK> ModReg.Module_count += (Isit[3] = !!linklistmod_What_am_I());    
 KK> ModReg.Module_list = Modules;
 KK>     ModReg.Module_available = Isit;
 KK>     return ModReg;
 KK> }

 KK>  In this example, you have a single struct with two lists.  One of 
 KK> the lists has the function module names and the other holds their 
 KK> status in this compilation.  This particular implementation is not 
 KK> tested and may be in serious need of debugging.  The original test 
 KK> app was successfully compiled and run under VC++5.0 on WinXP. 


--- 
* Origin: TANSTAAFL BBS 717-838-8539 (1:270/615)
SEEN-BY: 633/267 270
@PATH: 270/615 150/220 3613/1275 123/500 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.