00001
00009 #if defined WIN32 && ! defined MINGW
00010 #include <io.h>
00011 #else
00012 #include <unistd.h>
00013 #endif
00014 #include <math.h>
00015
00016 #ifdef HAVE_CONFIG_H
00017 #include "config.h"
00018 #endif
00019
00020 #if defined WIN32 && ! defined MINGW
00021 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
00022 #define _WIN32_WINNT 0x0400
00023 #include <windows.h>
00024 #ifndef DLEXT
00025 #define DLEXT ".dll"
00026 #endif
00027 #define DLLOAD(P) LoadLibrary(P)
00028 #define DLSYM(H,S) GetProcAddress((HINSTANCE)H,S)
00029 #define snprintf _snprintf
00030 #else
00031 #ifndef MINGW
00032 #include "dlfcn.h"
00033 #endif
00034 #ifndef DLEXT
00035 #define DLEXT ".so"
00036 #endif
00037 #ifndef MINGW
00038 #define DLLOAD(P) dlopen(P,RTLD_LAZY)
00039 #else
00040 #define DLLOAD(P) dlopen(P)
00041 #endif
00042 #define DLSYM(H,S) dlsym(H,S)
00043 #endif
00044
00045 #if !defined(HAVE_CONFIG_H) || defined(HAVE_MALLOC_H)
00046 #include <malloc.h>
00047 #endif
00048
00049 #include <errno.h>
00050 #include "globals.h"
00051 #include "output.h"
00052 #include "module.h"
00053 #include "find.h"
00054 #include "random.h"
00055 #include "test_callbacks.h"
00056 #include "exception.h"
00057 #include "unit.h"
00058 #include "interpolate.h"
00059 #ifndef MINGW
00060 #include "lock.h"
00061 #endif
00062 #include "schedule.h"
00063
00064 #include "matlab.h"
00065
00066 int get_exe_path(char *buf, int len, void *mod){
00067 int rv = 0, i = 0;
00068 if(buf == NULL)
00069 return 0;
00070 if(len < 1)
00071 return 0;
00072 #if defined WIN32 && ! defined MINGW
00073 rv = GetModuleFileName((HMODULE) mod, buf, len);
00074 if(rv){
00075 for(i = rv; ((buf[i] != '/') && (buf[i] != '\\') && (i >= 0)); --i){
00076 buf[i] = 0;
00077 --rv;
00078 }
00079 }
00080 #else
00081 if(mod == NULL){
00082 ;
00083 } else {
00084 ;
00085 }
00086 #endif
00087 return rv;
00088 }
00089
00090 int module_get_exe_path(char *buf, int len){
00091 return get_exe_path(buf, len, NULL);
00092 }
00093
00094 int module_get_path(char *buf, int len, MODULE *mod){
00095 return get_exe_path(buf, len, mod->hLib);
00096 }
00097
00098 void dlload_error(const char *filename)
00099 {
00100 #ifndef MINGW
00101 #if defined WIN32
00102 LPTSTR error;
00103 LPTSTR end;
00104 DWORD result = FormatMessage(
00105 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
00106 NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00107 (LPTSTR) &error, 0, NULL);
00108 if (!result)
00109 error = TEXT("[FormatMessage failed]");
00110 else for (end = error + strlen(error) - 1; end >= error && isspace(*end); end--)
00111 *end = 0;
00112 #else
00113 char *error = dlerror();
00114 #endif
00115 #else
00116 char *error = "unknown error";
00117 #endif
00118 output_debug("%s: %s (LD_LIBRARY_PATH=%s)", filename, error,getenv("LD_LIBRARY_PATH"));
00119 #if defined WIN32 && ! defined MINGW
00120 if (result)
00121 LocalFree(error);
00122 #endif
00123 }
00124
00125
00126
00127 int64 lock_count;
00128 int64 lock_spin;
00129 static CALLBACKS callbacks = {
00130 &global_clock,
00131 output_verbose,
00132 output_message,
00133 output_warning,
00134 output_error,
00135 output_debug,
00136 output_test,
00137 class_register,
00138 {object_create_single,object_create_array,object_create_foreign},
00139 class_define_map,
00140 class_get_class_from_classname,
00141 {class_define_function,class_get_function},
00142 class_define_enumeration_member,
00143 class_define_set_member,
00144 object_set_dependent,
00145 object_set_parent,
00146 object_set_rank,
00147 {object_get_property, object_set_value_by_addr,object_get_value_by_addr, object_set_value_by_name,object_get_value_by_name,object_get_reference,object_get_unit,object_get_addr,class_string_to_propertytype},
00148 {find_objects,find_next,findlist_copy,findlist_add,findlist_del,findlist_clear},
00149 class_find_property,
00150 malloc,
00151 free,
00152 aggregate_mkgroup,
00153 aggregate_value,
00154 module_getvar_addr,
00155 module_depends,
00156 {random_uniform, random_normal, random_bernoulli, random_pareto, random_lognormal, random_sampled, random_exponential, random_type, random_value, pseudorandom_value, random_triangle, random_beta, random_gamma, random_weibull, random_rayleigh},
00157 object_isa,
00158 class_register_type,
00159 class_define_type,
00160 {mkdatetime,strdatetime,timestamp_to_days,timestamp_to_hours,timestamp_to_minutes,timestamp_to_seconds,local_datetime,convert_to_timestamp,convert_from_timestamp},
00161 unit_convert, unit_convert_ex, unit_find,
00162 {create_exception_handler,delete_exception_handler,throw_exception,exception_msg},
00163 {global_create, global_setvar, global_getvar, global_find},
00164 #ifndef NOLOCKS
00165 &lock_count, &lock_spin,
00166 #endif
00167 {find_file},
00168 {object_get_complex, object_get_enum, object_get_int16, object_get_int32, object_get_int64, object_get_double, object_get_string, object_get_object},
00169 {object_get_complex_by_name, object_get_enum_by_name, object_get_int16_by_name, object_get_int32_by_name, object_get_int64_by_name,
00170 object_get_double_by_name, object_get_string_by_name, object_get_object_by_name},
00171 {class_string_to_property, class_property_to_string,},
00172 module_find,
00173 object_find_name,
00174 object_build_name,
00175 object_get_oflags,
00176 object_get_count,
00177 {schedule_create, schedule_index, schedule_value, schedule_dtnext, schedule_find_byname},
00178 {loadshape_create,loadshape_init},
00179 {enduse_create,enduse_sync},
00180 {interpolate_linear, interpolate_quadratic}
00181 };
00182
00183 MODULE *first_module = NULL;
00184 MODULE *last_module = NULL;
00185
00193 typedef MODULE *(*LOADER)(const char *, int, char *[]);
00194 MODULE *module_load(const char *file,
00195 int argc,
00196 char *argv[])
00197 {
00198
00199 MODULE *mod = module_find((char *)file);
00200 char buffer[FILENAME_MAX+1];
00201 char *fmod;
00202 bool isforeign = false;
00203 char pathname[1024];
00204 char *tpath = NULL;
00205 #ifdef WIN32
00206 char from='/', to='\\';
00207 #else
00208 char from='\\', to='/';
00209 #endif
00210 char *p = NULL;
00211 void *hLib = NULL;
00212 LIBINIT init = NULL;
00213 int *pMajor = NULL, *pMinor = NULL;
00214 CLASS *previous = NULL;
00215 CLASS *c;
00216
00217 if (mod!=NULL)
00218 {
00219 output_verbose("%s(%d): module '%s' already loaded", __FILE__, __LINE__, file);
00220 return mod;
00221 }
00222 else
00223 {
00224 output_verbose("%s(%d): module '%s' not yet loaded", __FILE__, __LINE__, file);
00225 }
00226
00227
00228 strcpy(buffer,file);
00229 fmod = strtok(buffer,"::");
00230 if (fmod!=NULL && strcmp(fmod, file) != 0)
00231 {
00232 char *modname = strtok(NULL,"::");
00233 MODULE *parent_mod = module_find(fmod);
00234 if(parent_mod == NULL)
00235 parent_mod = module_load(fmod, 0, NULL);
00236 previous = class_get_last_class();
00237 if(parent_mod != NULL && parent_mod->subload != NULL)
00238 {
00239 MODULE *child_mod;
00240 if(module_find(fmod) == NULL)
00241 module_load(fmod, 0, NULL);
00242 child_mod = parent_mod->subload(modname, &mod, (previous ? &(previous->next) : &previous), argc, argv);
00243 if(child_mod == NULL)
00244 {
00245 output_error("module_load(file='%s::%s'): subload failed", fmod, modname);
00246 return NULL;
00247 }
00248 if (mod != NULL)
00249 {
00250 last_module->next = mod;
00251 last_module = mod;
00252 mod->oclass = previous ? previous->next : class_get_first_class();
00253 }
00254 return last_module;
00255 } else {
00256 struct {
00257 char *name;
00258 LOADER loader;
00259 } fmap[] = {
00260 {"matlab",NULL},
00261 {"java",load_java_module},
00262 {"python",load_python_module},
00263 {NULL,NULL}
00264 }, *p;
00265 for (p=fmap; p->name!=NULL; p++)
00266 {
00267 if (strcmp(p->name, fmod)==0)
00268 {
00269 static char *args[1];
00270 isforeign = true;
00271 if (p->loader!=NULL)
00272
00273 return p->loader(modname,argc,argv);
00274
00275
00276 argv = args;
00277 argc=1;
00278 argv[0] = modname;
00279 file=buffer;
00280 break;
00281 }
00282 }
00283 if (p==NULL)
00284 {
00285 output_error("module_load(file='%s',...): foreign module type %s not recognized or supported", fmod);
00286 return NULL;
00287 }
00288 }
00289 }
00290
00291
00292 mod = (MODULE *)malloc(sizeof(MODULE));
00293 if (mod==NULL)
00294 {
00295 output_verbose("%s(%d): module '%s' memory allocation failed", __FILE__, __LINE__, file);
00296 errno=ENOMEM;
00297 return NULL;
00298 }
00299 else
00300 output_verbose("%s(%d): module '%s' memory allocated", __FILE__, __LINE__, file);
00301
00302
00303 snprintf(pathname, 1024, "%s" DLEXT, file);
00304 tpath = find_file(pathname, NULL, X_OK|R_OK);
00305 if(tpath == NULL)
00306 {
00307 output_verbose("unable to locate %s in GLPATH, using library loader instead", pathname);
00308 tpath=pathname;
00309 }
00310 else
00311 output_verbose("full path to library '%s' is '%s'", file, tpath);
00312
00313
00314 for (p=strchr(tpath,from); p!=NULL; p=strchr(p,from))
00315 *p=to;
00316
00317
00318 hLib = DLLOAD(tpath);
00319 if (hLib==NULL)
00320 {
00321 #if defined WIN32 && ! defined MINGW
00322 output_verbose("%s(%d): module '%s' load failed - %s (error code %d)", __FILE__, __LINE__, file, strerror(errno), GetLastError());
00323 #else
00324 output_verbose("%s(%d): module '%s' load failed - %s", __FILE__, __LINE__, file, dlerror());
00325 #endif
00326 dlload_error(pathname);
00327 errno = ENOENT;
00328 free(mod);
00329 return NULL;
00330 }
00331 else
00332 output_verbose("%s(%d): module '%s' loaded ok", __FILE__, __LINE__, file);
00333
00334
00335 init = (LIBINIT)DLSYM(hLib,"init");
00336 if (init==NULL)
00337 {
00338 output_error("%s(%d): module '%s' does not export init()", __FILE__, __LINE__, file);
00339 dlload_error(pathname);
00340 errno = ENOEXEC;
00341 free(mod);
00342 return NULL;
00343 }
00344 else
00345 output_verbose("%s(%d): module '%s' exports init()", __FILE__, __LINE__, file);
00346
00347
00348 mod->hLib = (void*)hLib;
00349 pMajor = (int*)DLSYM(hLib, "major");
00350 pMinor = (int*)DLSYM(hLib, "minor");
00351 mod->major = pMajor?*pMajor:0;
00352 mod->minor = pMinor?*pMinor:0;
00353 mod->import_file = (int(*)(char*))DLSYM(hLib,"import_file");
00354 mod->export_file = (int(*)(char*))DLSYM(hLib,"export_file");
00355 mod->setvar = (int(*)(char*,char*))DLSYM(hLib,"setvar");
00356 mod->getvar = (void*(*)(char*,char*,unsigned int))DLSYM(hLib,"getvar");
00357 mod->check = (int(*)())DLSYM(hLib,"check");
00358 #ifndef _NO_CPPUNIT
00359 mod->module_test = (int(*)(TEST_CALLBACKS*,int,char*[]))DLSYM(hLib,"module_test");
00360 #endif
00361 mod->cmdargs = (int(*)(int,char**))DLSYM(hLib,"cmdargs");
00362 mod->kmldump = (int(*)(FILE*,OBJECT*))DLSYM(hLib,"kmldump");
00363 mod->subload = (MODULE *(*)(char *, MODULE **, CLASS **, int, char **))DLSYM(hLib, "subload");
00364 mod->test = (void(*)(int,char*[]))DLSYM(hLib,"test");
00365 mod->globals = NULL;
00366 strcpy(mod->name,file);
00367 mod->next = NULL;
00368
00369
00370 mod->oclass = (*init)(&callbacks,(void*)mod,argc,argv);
00371 if (mod->oclass==NULL)
00372 return NULL;
00373
00374
00375 for (c=mod->oclass; c!=NULL; c=c->next) {
00376 char fname[1024];
00377 struct {
00378 FUNCTIONADDR *func;
00379 char *name;
00380 int optional;
00381 } map[] = {
00382 {&c->create,"create",FALSE},
00383 {&c->init,"init",TRUE},
00384 {&c->sync,"sync",TRUE},
00385 {&c->commit,"commit",TRUE},
00386 {&c->notify,"notify",TRUE},
00387 {&c->isa,"isa",TRUE},
00388 {&c->plc,"plc",TRUE},
00389 {&c->recalc,"recalc",TRUE},
00390 };
00391 int i;
00392 for (i=0; i<sizeof(map)/sizeof(map[0]); i++)
00393 {
00394 snprintf(fname, 1024,"%s_%s",map[i].name,isforeign?fmod:c->name);
00395 if ((*(map[i].func) = (FUNCTIONADDR)DLSYM(hLib,fname))==NULL && !map[i].optional)
00396 {
00397 output_fatal("intrinsic %s is not defined in class %s", fname,file);
00398
00399
00400
00401 errno=EINVAL;
00402 return NULL;
00403 }
00404 else
00405 if(!map[i].optional)
00406 output_verbose("%s(%d): module '%s' intrinsic %s found", __FILE__, __LINE__, file, fname);
00407 }
00408 }
00409
00410
00411 if (first_module==NULL)
00412 first_module = mod;
00413 else
00414 last_module->next = mod;
00415 last_module = mod;
00416 return last_module;
00417 }
00418
00419 int module_setvar(MODULE *mod, char *varname, char *value)
00420 {
00421 char modvarname[1024];
00422 sprintf(modvarname,"%s::%s",mod->name,varname);
00423 return global_setvar(modvarname,value)==SUCCESS;
00424 }
00425
00426 void* module_getvar(MODULE *mod, char *varname, char *value, unsigned int size)
00427 {
00428 char modvarname[1024];
00429 sprintf(modvarname,"%s::%s",mod->name,varname);
00430 return global_getvar(modvarname,value,size);
00431 }
00432
00433 void* module_getvar_old(MODULE *mod, char *varname, char *value, unsigned int size)
00434 {
00435 if (mod->getvar!=NULL)
00436 {
00437 if (strcmp(varname,"major")==0)
00438 {
00439 sprintf(value,"%d",mod->major);
00440 return value;
00441 }
00442 else if (strcmp(varname,"minor")==0)
00443 {
00444 sprintf(value,"%d",mod->minor);
00445 return value;
00446 }
00447 else
00448 return (*mod->getvar)(varname,value,size);
00449 }
00450 else
00451 return 0;
00452 }
00453
00454 double* module_getvar_addr(MODULE *mod, char *varname)
00455 {
00456 char modvarname[1024];
00457 GLOBALVAR *var;
00458 sprintf(modvarname,"%s::%s",mod->name,varname);
00459 var = global_find(modvarname);
00460 if (var!=NULL)
00461 return var->prop->addr;
00462 else
00463 return NULL;
00464 }
00465
00466 int module_saveall(FILE *fp)
00467 {
00468 MODULE *mod;
00469 int count=0;
00470 CLASS *oclass = NULL;
00471 char32 varname;
00472 count += fprintf(fp,"\n########################################################\n");
00473 count += fprintf(fp,"# modules\n");
00474 for (mod=first_module; mod!=NULL; mod=mod->next)
00475 {
00476 varname[0] = '\0';
00477 oclass = NULL;
00478
00479 count += fprintf(fp,"module %s {\n",mod->name);
00480 if (mod->major>0 || mod->minor>0)
00481 count += fprintf(fp,"\tmajor %d;\n\tminor %d;\n",mod->major,mod->minor);
00482 for (oclass=mod->oclass; oclass!=NULL ; oclass=oclass->next)
00483 {
00484 if (oclass->module==mod)
00485 count += fprintf(fp,"\tclass %s;\n",oclass->name);
00486 }
00487
00488 while (module_getvar(mod,varname,NULL,0))
00489 {
00490 char32 value;
00491 if (module_getvar(mod,varname,value,sizeof(value)))
00492 count += fprintf(fp,"\t%s %s;\n",varname,value);
00493 }
00494 count += fprintf(fp,"}\n");
00495 }
00496 return count;
00497 }
00498
00499 int module_saveall_xml(FILE *fp){
00500 MODULE *mod;
00501 int count = 0;
00502 char32 varname = "";
00503 char32 value = "";
00504 GLOBALVAR *gvptr = NULL;
00505 char1024 buffer;
00506
00507 for (mod = first_module; mod != NULL; mod = mod->next){
00508 char tname[67];
00509 size_t tlen;
00510 gvptr = global_getnext(NULL);
00511 sprintf(tname, "%s::", mod->name);
00512 tlen = strlen(tname);
00513 count += fprintf(fp, "\t<module type=\"%s\" ", mod->name);
00514 if (mod->major > 0){
00515 count += fprintf(fp, "major=\"%d\" minor=\"%d\">\n", mod->major, mod->minor);
00516 } else {
00517 count += fprintf(fp, ">\n");
00518 }
00519 count += fprintf(fp, "\t\t<properties>\n");
00520 while(gvptr != NULL){
00521 if(strncmp(tname, gvptr->name, tlen) == 0){
00522 count += fprintf(fp, "\t\t\t<%s>%s</%s>\n", gvptr->name+tlen, class_property_to_string(gvptr->prop,(void*)gvptr->prop->addr,buffer,1024)>0 ? buffer : "...", gvptr->name+tlen);
00523 }
00524 gvptr = global_getnext(gvptr);
00525 }
00526 count += fprintf(fp, "\t\t</properties>\n");
00527 module_saveobj_xml(fp, mod);
00528 count += fprintf(fp,"\t</module>\n");
00529 }
00530 return count;
00531 }
00532
00533 #if defined WIN32 && ! defined MINGW
00534 #define isnan _isnan
00535 #endif
00536
00537 int module_saveobj_xml(FILE *fp, MODULE *mod){
00538 unsigned count = 0;
00539 char buffer[1024];
00540 PROPERTY *prop = NULL;
00541 OBJECT *obj;
00542 CLASS *oclass=NULL;
00543 CLASS *pclass = NULL;
00544
00545 for(obj = object_get_first(); obj != NULL; obj = obj->next){
00546 char32 oname = "(unidentified)";
00547 if(obj->oclass->module != mod){
00548 continue;
00549 }
00550
00551 if(obj->name != NULL){
00552 strcpy(oname, obj->name);
00553 } else {
00554 sprintf(oname, "%s:%i", obj->oclass->name, obj->id);
00555 }
00556 if ((oclass == NULL) || (obj->oclass != oclass))
00557 oclass = obj->oclass;
00558 count += fprintf(fp,"\t\t<object type=\"%s\" id=\"%i\" name=\"%s\">\n", obj->oclass->name, obj->id, oname);
00559
00560
00561 if (obj->parent!=NULL){
00562 if(obj->parent->name != NULL){
00563 strcpy(oname, obj->parent->name);
00564 } else {
00565 sprintf(oname, "%s:%i", obj->parent->oclass->name, obj->parent->id);
00566 }
00567 count += fprintf(fp,"\t\t\t<parent>%s</parent>\n", oname);
00568 } else {
00569 count += fprintf(fp,"\t\t\t<parent>root</parent>\n");
00570 }
00571 count += fprintf(fp,"\t\t\t<rank>%d</rank>\n", obj->rank);
00572 count += fprintf(fp,"\t\t\t<clock>\n", obj->clock);
00573 count += fprintf(fp,"\t\t\t\t <timestamp>%s</timestamp>\n", convert_from_timestamp(obj->clock,buffer,sizeof(buffer))>0?buffer:"(invalid)");
00574 count += fprintf(fp,"\t\t\t</clock>\n");
00575
00576 if (!isnan(obj->latitude))
00577 count += fprintf(fp,"\t\t\t<latitude>%s</latitude>\n", convert_from_latitude(obj->latitude,buffer,sizeof(buffer))?buffer:"(invalid)");
00578 else
00579 count += fprintf(fp, "\t\t\t<latitude>NONE</latitude>\n");
00580 if (!isnan(obj->longitude))
00581 count += fprintf(fp,"\t\t\t<longitude>%s</longitude>\n",convert_from_longitude(obj->longitude,buffer,sizeof(buffer))?buffer:"(invalid)");
00582 else
00583 count += fprintf(fp,"\t\t\t<longitude>NONE</longitude>\n");
00584
00585
00586 for (prop=oclass->pmap;prop!=NULL && prop->oclass==oclass;prop=prop->next)
00587 {
00588 char *value = NULL;
00589 if((prop->access != PA_PUBLIC) && (prop->access != PA_REFERENCE))
00590 continue;
00591 value = object_property_to_string(obj,prop->name);
00592 if (value!=NULL){
00593 count += fprintf(fp, "\t\t\t<%s>%s</%s>\n", prop->name, value, prop->name);
00594 }
00595 }
00596 pclass = oclass->parent;
00597 while(pclass != NULL){
00598 for (prop=pclass->pmap;prop!=NULL && prop->oclass==pclass;prop=prop->next){
00599 char *value = object_property_to_string(obj,prop->name);
00600 if (value!=NULL){
00601 count += fprintf(fp, "\t\t\t<%s>%s</%s>\n", prop->name, value, prop->name);
00602 }
00603 }
00604 pclass = pclass->parent;
00605 }
00606 count += fprintf(fp,"\t\t</object>\n");
00607 }
00608 return count;
00609 }
00610
00611 MODULE *module_get_first(void)
00612 {
00613 return first_module;
00614 }
00615
00616 int module_saveall_xml_old(FILE *fp);
00617
00618 int module_saveall_xml_old(FILE *fp)
00619 {
00620 MODULE *mod;
00621 int count=0;
00622 count += fprintf(fp,"\t<modules>\n");
00623 for (mod=first_module; mod!=NULL; mod=mod->next)
00624 {
00625 CLASS *oclass;
00626 char32 varname="";
00627 count += fprintf(fp,"\t\t<module> \n");
00628 count += fprintf(fp,"\t\t\t<name>%s</name>\n",mod->name);
00629 if(mod->major > 0)
00630 count += fprintf(fp,"\t\t\t<major>%d</major>\n",mod->major );
00631 if(mod->minor > 0)
00632 count += fprintf(fp,"\t\t\t<minor>%d</minor>\n",mod->minor);
00633 count += fprintf(fp,"\t\t\t<classes>\n");
00634 for (oclass=mod->oclass; oclass!=NULL ; oclass=oclass->next)
00635 {
00636 if (oclass->module==mod){
00637 count += fprintf(fp, "\t\t\t\t<class> \n");
00638 count += fprintf(fp, "\t\t\t\t\t<classname>%s</classname>\n", oclass->name);
00639 count += fprintf(fp, "\t\t\t\t\t<module name=\"%s\" />\n", mod->name);
00640 count += fprintf(fp, "\t\t\t\t</class>\n");
00641 }
00642 }
00643 count += fprintf(fp,"\t\t\t</classes>\n");
00644 count += fprintf(fp,"\t\t\t<properties>\n");
00645 while (module_getvar(mod,varname,NULL,0))
00646 {
00647 char32 value;
00648 if (module_getvar(mod,varname,value,sizeof(value)))
00649 {
00650 count += fprintf(fp,"\t\t\t\t<property> \n");
00651 count += fprintf(fp,"\t\t\t\t\t <type>double</type>\n", varname);
00652 count += fprintf(fp,"\t\t\t\t\t <name>%s</name>\n", value);
00653 count += fprintf(fp,"\t\t\t\t</property> \n");
00654 }
00655 }
00656 count += fprintf(fp,"\t\t\t</properties>\n");
00657 count += fprintf(fp,"\t\t</module>\n");
00658 }
00659 count += fprintf(fp,"\t</modules>\n");
00660 return count;
00661 }
00662
00663 MODULE *module_find(char *modname)
00664 {
00665 MODULE *mod = NULL;
00666 for (mod=first_module; mod!=NULL; mod=mod->next)
00667 {
00668 if (strcmp(mod->name,modname)==0)
00669 break;
00670 }
00671 return mod;
00672
00673 }
00674
00675 int module_import(MODULE *mod, char *filename)
00676 {
00677 if (mod->import_file == NULL)
00678 {
00679 errno = ENOENT;
00680 return 0;
00681 }
00682 return (*mod->import_file)(filename);
00683 }
00684
00685 int module_save(MODULE *mod, char *filename)
00686 {
00687 if (mod->export_file == NULL)
00688 {
00689 errno = ENOENT;
00690 return 0;
00691 }
00692 return (*mod->export_file)(filename);
00693 }
00694
00695 int module_dumpall(void)
00696 {
00697 MODULE *mod;
00698 int count=0;
00699 for (mod=first_module; mod!=NULL; mod=mod->next)
00700 {
00701 if (mod->export_file!=NULL)
00702 count += module_save(mod,NULL);
00703 }
00704 return count;
00705 }
00706
00707 int module_checkall(void)
00708 {
00709 MODULE *mod;
00710 int count=0;
00711 for (mod=first_module; mod!=NULL; mod=mod->next)
00712 count += module_check(mod);
00713 return count;
00714 }
00715
00716 int module_check(MODULE *mod)
00717 {
00718 if (mod->check==NULL)
00719 return 0;
00720 return (*mod->check)();
00721 }
00722
00723 void module_libinfo(char *module_name)
00724 {
00725 MODULE *mod = module_load(module_name,0,NULL);
00726 if (mod!=NULL)
00727 {
00728 CLASS *c;
00729 PROPERTY *p;
00730 GLOBALVAR *v=NULL;
00731 output_raw("Module name....... %s\n", mod->name);
00732 output_raw("Major version..... %d\n", mod->major);
00733 output_raw("Minor version..... %d\n", mod->minor);
00734 output_raw("Classes........... ");
00735 for (c=mod->oclass; c!=NULL; c=c->next)
00736 output_raw("%s%s", c->name, c->next!=NULL?", ":"");
00737 output_raw("\n");
00738 output_raw("Implementations... ");
00739 if (mod->cmdargs!=NULL) output_raw("cmdargs ");
00740 if (mod->getvar!=NULL) output_raw("getvar ");
00741 if (mod->setvar!=NULL) output_raw("setvar ");
00742 if (mod->import_file!=NULL) output_raw("import_file ");
00743 if (mod->export_file!=NULL) output_raw("export_file ");
00744 if (mod->check!=NULL) output_raw("check ");
00745 if (mod->kmldump!=NULL) output_raw("kmldump ");
00746 #ifndef _NO_CPPUNIT
00747 if (mod->module_test!=NULL) output_raw("module_test ");
00748 #endif
00749 output_raw("\nGlobals........... ");
00750 for (p=mod->globals; p!=NULL; p=p->next)
00751 output_raw("%s ", p->name);
00752 while ((v=global_getnext(v))!=NULL)
00753 {
00754 if (strncmp(v->name,module_name,strlen(module_name))==0)
00755 {
00756 char *vn = strstr(v->name,"::");
00757 if (vn!=NULL)
00758 output_raw("%s ", vn+2);
00759 }
00760 }
00761 output_raw("\n");
00762 }
00763 else
00764 output_error("Module %s load failed", module_name);
00765 }
00766
00767 int module_cmdargs(int argc, char **argv)
00768 {
00769 MODULE *mod;
00770 for (mod=first_module; mod!=NULL; mod=mod->next)
00771 {
00772 if (mod!=NULL && mod->cmdargs!=NULL)
00773 return (*(mod->cmdargs))(argc,argv);
00774 }
00775 return 0;
00776 }
00777
00778 int module_depends(char *name, unsigned char major, unsigned char minor, unsigned short build)
00779 {
00780 MODULE *mod;
00781 for (mod=first_module; mod!=NULL; mod=mod->next)
00782 {
00783 if (strcmp(mod->name,name)==0)
00784 if(mod->major > 0)
00785 if(mod->major==major && mod->minor>=minor)
00786 return 1;
00787 }
00788 return 0;
00789 }
00790
00791