Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

SCompiler.cpp

00001 /* QuestDesigner - Open Zelda's Project
00002    Copyright (C) 2003 Kronuz
00003    Copyright (C) 2001/2003 Open Zelda's Project
00004  
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU General Public License
00007    as published by the Free Software Foundation; either version 2
00008    of the License, or (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 */
00019 
00021 
00022 #include "stdafx.h"
00023 #include "SCompiler.h"
00024 
00025 #define NO_MAIN
00026 #define SC_FUNC    static
00027 #define SC_VDEFINE static
00028 #define SC_SKIP_VDECL   /* skip variable "forward declaration" */
00029 #define SC_LIGHT
00030 #include "sc.h"
00031 
00032 #include "scvars.c"
00033 #include "sc1.c"
00034 #include "sc2.c"
00035 #include "sc3.c"
00036 #include "sc4.c"
00037 #include "sc5.c"
00038 #include "sc6.c"
00039 #include "sc7.c"
00040 #include "sclist.c"
00041 #include "scexpand.c"
00042 
00044 // Externals
00045 HWND SCompiler::m_shWnd;
00046 HANDLE SCompiler::m_Mutex;
00047 bool SCompiler::m_bInUse;
00048 
00050 
00051 int sc_printf(const char *message,...)
00052 {
00053   int ret;
00054   va_list argptr;
00055 
00056   va_start(argptr,message);
00057   ret=SCompiler::sc_printf(message, argptr);
00058   va_end(argptr);
00059 
00060   return ret;
00061     
00062 }
00063 int sc_error(int number,char *message,char *filename,int firstline,int lastline,va_list argptr)
00064 {
00065     return SCompiler::sc_error(number,message,filename,firstline,lastline,argptr);
00066 }
00067 void *sc_opensrc(char *filename)
00068 {
00069     return SCompiler::sc_opensrc(filename);
00070 }
00071 void sc_closesrc(void *handle)
00072 {
00073     SCompiler::sc_closesrc(handle);
00074 }
00075 void sc_resetsrc(void *handle,void *position)
00076 {
00077     SCompiler::sc_resetsrc(handle,position);
00078 }
00079 char *sc_readsrc(void *handle,char *target,int maxchars)
00080 {
00081     return SCompiler::sc_readsrc(handle,target,maxchars);
00082 }
00083 void *sc_getpossrc(void *handle)
00084 {
00085     return SCompiler::sc_getpossrc(handle);
00086 }
00087 int sc_eofsrc(void *handle)
00088 {
00089     return SCompiler::sc_eofsrc(handle);
00090 }
00091 void *sc_openasm(char *filename)
00092 {
00093     return SCompiler::sc_openasm(filename);
00094 }
00095 void sc_closeasm(void *handle, int deletefile)
00096 {
00097     SCompiler::sc_closeasm(handle, deletefile);
00098 }
00099 void sc_resetasm(void *handle)
00100 {
00101     SCompiler::sc_resetasm(handle);
00102 }
00103 int sc_writeasm(void *handle,char *st)
00104 {
00105     return SCompiler::sc_writeasm(handle,st);
00106 }
00107 char *sc_readasm(void *handle, char *target, int maxchars)
00108 {
00109     return SCompiler::sc_readasm(handle, target, maxchars);
00110 }
00111 void *sc_openbin(char *filename)
00112 {
00113     return SCompiler::sc_openbin(filename);
00114 }
00115 void sc_closebin(void *handle,int deletefile)
00116 {
00117     SCompiler::sc_closebin(handle,deletefile);
00118 }
00119 void sc_resetbin(void *handle)
00120 {
00121     SCompiler::sc_resetbin(handle);
00122 }
00123 int sc_writebin(void *handle,void *buffer,int size)
00124 {
00125     return SCompiler::sc_writebin(handle,buffer,size);
00126 }
00127 long sc_lengthbin(void *handle)
00128 {
00129     return SCompiler::sc_lengthbin(handle);
00130 }
00131 
00132 /* sc_opensrc
00133  * Opens a source file (or include file) for reading. The "file" does not have
00134  * to be a physical file, one might compile from memory.
00135  *    filename    the name of the "file" to read from
00136  * Return:
00137  *    The function must return a pointer, which is used as a "magic cookie" to
00138  *    all I/O functions. When failing to open the file for reading, the
00139  *    function must return NULL.
00140  */
00141 void *SCompiler::sc_opensrc(char *filename)
00142 {
00143   return fopen(filename,"rt");
00144 }
00145 
00146 /* sc_closesrc
00147  * Closes a source file (or include file). The "handle" parameter has the
00148  * value that sc_opensrc() returned in an earlier call.
00149  */
00150 void SCompiler::sc_closesrc(void *handle)
00151 {
00152   assert(handle!=NULL);
00153   fclose((FILE*)handle);
00154 }
00155 
00156 /* sc_resetsrc
00157  * "position" may only hold a pointer that was previously obtained from
00158  * sc_getpossrc() */
00159 void SCompiler::sc_resetsrc(void *handle,void *position)
00160 {
00161   assert(handle!=NULL);
00162   fsetpos((FILE*)handle,(fpos_t *)position);
00163 }
00164 
00165 char *SCompiler::sc_readsrc(void *handle,char *target,int maxchars)
00166 {
00167   return fgets(target,maxchars,(FILE*)handle);
00168 }
00169 
00170 void *SCompiler::sc_getpossrc(void *handle)
00171 {
00172   static fpos_t lastpos;
00173 
00174   fgetpos((FILE*)handle,&lastpos);
00175   return &lastpos;
00176 }
00177 
00178 int SCompiler::sc_eofsrc(void *handle)
00179 {
00180   return feof((FILE*)handle);
00181 }
00182 
00183 /* should return a pointer, which is used as a "magic cookie" to all I/O
00184  * functions; return NULL for failure
00185  */
00186 void *SCompiler::sc_openasm(char *filename)
00187 {
00188   return fopen(filename,"w+t");
00189 }
00190 
00191 void SCompiler::sc_closeasm(void *handle, int deletefile)
00192 {
00193   fclose((FILE*)handle);
00194   if (deletefile)
00195     unlink(outfname);
00196 }
00197 
00198 void SCompiler::sc_resetasm(void *handle)
00199 {
00200   fflush((FILE*)handle);
00201   fseek((FILE*)handle,0,SEEK_SET);
00202 }
00203 
00204 int SCompiler::sc_writeasm(void *handle,char *st)
00205 {
00206   return fputs(st,(FILE*)handle) >= 0;
00207 }
00208 
00209 char *SCompiler::sc_readasm(void *handle, char *target, int maxchars)
00210 {
00211   return fgets(target,maxchars,(FILE*)handle);
00212 }
00213 
00214 /* Should return a pointer, which is used as a "magic cookie" to all I/O
00215  * functions; return NULL for failure.
00216  */
00217 void *SCompiler::sc_openbin(char *filename)
00218 {
00219   return fopen(filename,"wb");
00220 }
00221 
00222 void SCompiler::sc_closebin(void *handle,int deletefile)
00223 {
00224   fclose((FILE*)handle);
00225   if (deletefile)
00226     unlink(binfname);
00227 }
00228 
00229 void SCompiler::sc_resetbin(void *handle)
00230 {
00231   fflush((FILE*)handle);
00232   fseek((FILE*)handle,0,SEEK_SET);
00233 }
00234 
00235 int SCompiler::sc_writebin(void *handle,void *buffer,int size)
00236 {
00237   return fwrite(buffer,1,size,(FILE*)handle) == size;
00238 }
00239 
00240 long SCompiler::sc_lengthbin(void *handle)
00241 {
00242   return ftell((FILE*)handle);
00243 }
00244 
00245 SCompiler::SCompiler()
00246 {
00247     m_shWnd = NULL;
00248     m_bInUse = false;
00249     m_Mutex = CreateMutex( NULL, FALSE, NULL );
00250 
00251 }
00252 SCompiler::~SCompiler()
00253 {
00254     CloseHandle(m_Mutex);
00255 }
00256 
00257 struct _thrdParam {
00258     int argc;
00259     char **argv;
00260 };
00261 DWORD WINAPI ThreadProc( LPVOID lpParameter )
00262 {
00263     _thrdParam *pTPT = (_thrdParam *)lpParameter;
00264 
00265     WaitForSingleObject(SCompiler::m_Mutex, INFINITE);
00266     SCompiler::m_bInUse = true;
00267     SendMessage(SCompiler::m_shWnd, WMQD_BEGIN, 0, 0);
00268     sc_compile(pTPT->argc, pTPT->argv);
00269     SendMessage(SCompiler::m_shWnd, WMQD_END, 0, 0);
00270     SCompiler::m_bInUse = false;
00271     ReleaseMutex(SCompiler::m_Mutex);
00272 
00273     for(int i=0; i<pTPT->argc; i++) 
00274         delete []pTPT->argv[i];
00275     delete []pTPT->argv;
00276     delete pTPT;
00277     return 0;
00278 }
00279 
00280 void SCompiler::StartThread(int argc, char *argv[])
00281 {
00282     _thrdParam *pTPT = new _thrdParam;
00283     pTPT->argc = argc;
00284     pTPT->argv = argv;
00285 
00286     CreateThread(NULL, 0, (PTHREAD_START_ROUTINE)(void*)ThreadProc, (void*)pTPT, 0, NULL);
00287 }
00288 
00289 int SCompiler::Compile(LPCSTR szInludeDir, LPCSTR szSrcFile, LPCSTR szDestFile)
00290 {
00291     int argc = 0;
00292     char **argv;
00293     char buff[_MAX_PATH];
00294     buff[0] = '-';
00295 
00296     if(m_bInUse) return 1;
00297 
00298     // Printing a message
00299     char *aux = strrchr(szSrcFile, '\\');
00300     if(!aux) aux = (char *)szSrcFile;
00301     else aux++;
00302     ::sc_printf("%s\n", aux);
00303 
00304     argv = new char*[5];
00305     argv[argc] = new char[strlen("QuestDesigner")+1];
00306     strcpy(argv[argc++], "QuestDesigner");
00307 
00308     argv[argc] = new char[strlen(szSrcFile)+1];
00309     strcpy(argv[argc++], szSrcFile);
00310 
00311     if(szDestFile) {
00312         buff[1] = 'o'; // Output file option
00313         strcpy(&buff[2], szDestFile);
00314         argv[argc] = new char[strlen(buff)+1];
00315         strcpy(argv[argc++], buff);
00316     }
00317 
00318     if(szInludeDir) {
00319         buff[1] = 'i'; // Includes directory option
00320         strcpy(&buff[2], szInludeDir);
00321         argv[argc] = new char[strlen(buff)+1];
00322         strcpy(argv[argc++], buff);
00323     }
00324 
00325     StartThread(argc, argv);
00326 
00327     return 0;
00328 
00329 }
00330 
00332 
00348 int SCompiler::sc_printf(const char *format, va_list argptr)
00349 {
00350     SCStruct qscs;
00351     qscs.type = t_printf;
00352     qscs.message = format;
00353     qscs.argptr = argptr;
00354 
00355     ATLASSERT(::IsWindow(m_shWnd));
00356     SendMessage(m_shWnd, WMQD_MESSAGE, 0, (LPARAM)&qscs);
00357     return 1;
00358 
00359     //  return vprintf(format, argptr);
00360 }
00361 
00362 /* sc_error
00363  * Called for producing error output.
00364  *    number      the error number (as documented in the manual)
00365  *    message     a string describing the error with embedded %d and %s tokens
00366  *    filename    the name of the file currently being parsed
00367  *    firstline   the line number at which the expression started on which
00368  *                the error was found, or -1 if there is no "starting line"
00369  *    lastline    the line number at which the error was detected
00370  *    argptr      a pointer to the first of a series of arguments (for macro
00371  *                "va_arg")
00372  * Return:
00373  *    If the function returns 0, the parser attempts to continue compilation.
00374  *    On a non-zero return value, the parser aborts.
00375  */
00376 int SCompiler::sc_error(int number, char *message, char *filename, int firstline, int lastline, va_list argptr)
00377 {
00378 /*
00379     static char *prefix[3]={ "Error", "Fatal", "Warning" };
00380 
00381   if (number!=0) {
00382     char *pre;
00383 
00384     pre=prefix[number/100];
00385     if (firstline>=0)
00386       printf("%s(%d -- %d) %s [%03d]: ",filename,firstline,lastline,pre,number);
00387     else
00388       printf("%s(%d) %s [%03d]: ",filename,lastline,pre,number);
00389   } 
00390   vprintf(message,argptr);
00391   fflush(stdout);
00392 */
00393 
00394     SCStruct qscs;
00395     qscs.type = t_error;
00396     qscs.number = number;
00397     qscs.message = message;
00398     qscs.filename = filename;
00399     qscs.firstline = firstline;
00400     qscs.lastline = lastline;
00401     qscs.argptr = argptr;
00402 
00403     ATLASSERT(::IsWindow(m_shWnd));
00404     SendMessage(m_shWnd, WMQD_MESSAGE, 0, (LPARAM)&qscs);
00405 
00406   return 0;
00407 }
00408 
00409 

Generated on Wed Apr 16 19:12:22 2003 for QuestDesigner by doxygen1.2.18