TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Roy.J..Tellason!1.270.615.0{at}filegat
from: Kurt Kuzba
date: 2004-01-10 16:30:20
subject: [C] An interesting question

From: "Kurt Kuzba" 


From: Roy.J..Tellason!1.270.615.0{at}filegate.net
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.  

    One could use this as a model for a .dll file, actually.
 As far as registering the modules goes, this can not really be done
 without your intervention at compile time, TTBOMK.  If you just want
 a list of available modules, then you could have a function which
 tests for their modulename_What_am_I() function and registers them
 in a list of modules.  If the module is not present at compile time,
 it will result in an unresolved external symbol error.
 Given this external module:
/*testmod.h*/
char* testmod_What_am_I()
{ return "I'm just a test module."; }
/*end testmod.h*/

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

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

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

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

>  kkuzba{at}centurytel.net  http://home.centurytel.net/kkuzba
>  But now I will put Queen Arwen Evenstar first,

--- BBBS/LiI v4.01 Flag-5
* Origin: Prism's_Point (1:261/38.1)
SEEN-BY: 633/267 270
@PATH: 261/38 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™.