getmodules.c

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------------*/
00002 /* INRIA 2006 */
00003 /* Allan CORNET */
00004 /*-----------------------------------------------------------------------------------*/ 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include "getmodules.h"
00008 #include "machine.h"
00009 #include "MALLOC.h"
00010 #include "setgetSCIpath.h"
00011 #include "string.h"
00012 #include "sciprint.h"
00013 /*-----------------------------------------------------------------------------------*/ 
00014 #define basenamemodulesfile "modules/modules" 
00015 /*-----------------------------------------------------------------------------------*/ 
00016 extern BOOL FileExist(char *filename);
00017 /*-----------------------------------------------------------------------------------*/ 
00018 static struct MODULESLIST *ScilabModules=NULL;
00019 /*-----------------------------------------------------------------------------------*/ 
00020 static BOOL AddModules(void);
00021 static BOOL ReadModulesFile(void);
00022 static int GetNumberOfLinesInFile(char *filename);
00023 static BOOL AppendModules(char *filename);
00024 static void cleaningLine(char *source);
00025 static BOOL VerifyModule(char *ModuleName);
00026 /*-----------------------------------------------------------------------------------*/ 
00027 struct MODULESLIST *getmodules(void)
00028 {
00029         if (ScilabModules==NULL)
00030         {
00031                 ScilabModules=(struct MODULESLIST *)MALLOC(sizeof(struct MODULESLIST));
00032                 ReadModulesFile();
00033         }
00034         return ScilabModules;
00035 }
00036 /*-----------------------------------------------------------------------------------*/ 
00037 BOOL DisposeModulesInfo(void)
00038 {
00039         BOOL bOK=FALSE;
00040         if (ScilabModules)
00041         {
00042                 int i=0;
00043                 for (i=0;i<ScilabModules->numberofModules;i++)
00044                 {
00045                         if (ScilabModules->ModuleList[i])
00046                         {
00047                                 FREE(ScilabModules->ModuleList[i]);
00048                                 ScilabModules->ModuleList[i]=NULL;
00049                         }
00050                 }
00051                 if (ScilabModules->ModuleList)
00052                 {
00053                         FREE(ScilabModules->ModuleList);
00054                         ScilabModules->ModuleList=NULL;
00055                 }
00056                 ScilabModules->numberofModules=0;
00057                 FREE(ScilabModules);
00058                 ScilabModules=NULL;
00059 
00060         }
00061 
00062         return bOK;
00063 }
00064 /*-----------------------------------------------------------------------------------*/ 
00065 static BOOL ReadModulesFile(void)
00066 {
00067         BOOL bOK=FALSE;
00068         char *ModulesFilename=NULL;
00069         char *SciPath=NULL;
00070 
00071         SciPath=getSCIpath();
00072         if (SciPath==NULL)
00073         {
00074                 sciprint("The SCI environment variable is not set\n");
00075                 return FALSE;
00076         }
00077 
00078         ModulesFilename=(char*)MALLOC((strlen(SciPath)+strlen("/")+strlen(basenamemodulesfile)+1)*sizeof(char));
00079         sprintf(ModulesFilename,"%s/%s",SciPath,basenamemodulesfile);
00080         FREE(SciPath);
00081         SciPath=NULL;
00082         
00083         if (FileExist(ModulesFilename))
00084         {
00085                 int NumberofLines=GetNumberOfLinesInFile(ModulesFilename);
00086                 ScilabModules->ModuleList=(char**)MALLOC(sizeof(char*)*NumberofLines);
00087                 ScilabModules->numberofModules=NumberofLines;
00088                 AppendModules(ModulesFilename);
00089                 FREE(ModulesFilename);
00090                 ModulesFilename=NULL;
00091         }
00092         else
00093         {
00094                 sciprint("Cannot load the module declaration file : %s.\n",ModulesFilename);
00095                 FREE(ModulesFilename);
00096                 ModulesFilename=NULL;
00097                 return FALSE;
00098         }
00099         return bOK;
00100 }
00101 /*-----------------------------------------------------------------------------------*/ 
00102 static int GetNumberOfLinesInFile(char *filename)
00103 {
00104         #define LineMaxSize 1024
00105         int ret=0;
00106         FILE *pFile;
00107 
00108         if (FileExist(filename))
00109         {
00110                 char Line[LineMaxSize];
00111                 pFile=fopen(filename,"rt");
00112                 while (fgets(Line, LineMaxSize,pFile))
00113                 {
00114                         if ( VerifyModule(Line) )
00115                         {
00116                                 ret++;
00117                         }
00118                 }
00119                 fclose(pFile);
00120         }
00121 
00122         return ret;
00123 }
00124 /*-----------------------------------------------------------------------------------*/ 
00125 static BOOL AppendModules(char *filename)
00126 {
00127         #define LineMaxSize 1024
00128         BOOL bOK=FALSE;
00129         char Line[LineMaxSize];
00130         int i=0;
00131         FILE *pFile;
00132 
00133         pFile=fopen(filename,"rt");
00134         while (fgets(Line, LineMaxSize,pFile))
00135         {
00136                 if ( VerifyModule(Line) )
00137                 {
00138                         ScilabModules->ModuleList[i]=(char*)MALLOC(sizeof(char)*(strlen(Line)+1));
00139                         sprintf(ScilabModules->ModuleList[i],"%s",Line);
00140                         i++;
00141                 }
00142                 else
00143                 {
00144                         if (Line[0]!=';') /* Starts with a ; => it is a comment */
00145                         {
00146                                 sciprint("%s module not found.\n",Line);
00147                         }
00148                 }
00149         }
00150         fclose(pFile);
00151         return bOK;
00152 }
00153 /*-----------------------------------------------------------------------------------*/ 
00154 static void cleaningLine(char *source)
00155 {
00156         int i=0;
00157         for (i=0;i<(int)strlen(source);i++)
00158         {
00159                 if ( (source[i]==' ') || (source[i]=='\n') || (source[i]=='\r') )
00160                 {
00161                         source[i]='\0';
00162                         break;
00163                 }
00164         }
00165 }
00166 /*-----------------------------------------------------------------------------------*/ 
00167 static BOOL VerifyModule(char *ModuleName)
00168 {
00169         BOOL bOK=FALSE;
00170         char *SciPath=NULL;
00171         char *FullPathModuleName=NULL;
00172 
00173         cleaningLine(ModuleName);
00174 
00175         /* ';' is a comment into the declaration file (modules/modules) */
00176         if (ModuleName[0]==';') return FALSE;
00177 
00178         SciPath=getSCIpath();
00179         if (SciPath==NULL)
00180         {
00181                 sciprint("The SCI environment variable is not set\n");
00182                 return FALSE;
00183         }
00184 
00185         FullPathModuleName=(char*)MALLOC((strlen(SciPath)+strlen("%s/modules/%s/etc/%s.start")+(strlen(ModuleName)*2)+1)*sizeof(char));
00186         sprintf(FullPathModuleName,"%s/modules/%s/etc/%s.start",SciPath,ModuleName,ModuleName);
00187         FREE(SciPath);
00188         SciPath=NULL;
00189 
00190         /* ajouter d'autres tests d'existences */
00191 
00192         if (FileExist(FullPathModuleName))
00193         {
00194                 bOK=TRUE;
00195         }
00196         FREE(FullPathModuleName);
00197         FullPathModuleName=NULL;
00198 
00199         return bOK;
00200 }
00201 /*-----------------------------------------------------------------------------------*/ 

Generated on Sun Mar 4 15:03:46 2007 for Scilab [trunk] by  doxygen 1.5.1