home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Source Code / CSourceFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  5.1 KB  |  217 lines  |  [TEXT/ALFA]

  1. /*
  2.     Harvest C
  3.     Copyright 1992 Eric W. Sink.  All rights reserved.
  4.     
  5.     This file is part of Harvest C.
  6.     
  7.     Harvest C is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU Generic Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.     
  12.     Harvest C is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.     
  17.     You should have received a copy of the GNU General Public License
  18.     along with Harvest C; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.     
  21.     Harvest C is not in any way a product of the Free Software Foundation.
  22.     Harvest C is not GNU software.
  23.     Harvest C is not public domain.
  24.  
  25.     This file may have other copyrights which are applicable as well.
  26.  
  27. */
  28.  
  29. /* CSourceFile.c */
  30.  
  31. #include "CSourceFile.h"
  32. #include "CHarvestApp.h"
  33. #include "CDataFile.h"
  34. #include "CWindow.h"
  35. #include "CErrorLog.h"
  36. #include "structs.h"
  37.  
  38. extern CSourceFile *gCurSourceFile;
  39. extern CHarvestApp *gApplication;
  40. extern CErrorLog *gErrs;
  41.  
  42. int next_pool = 1234;
  43.  
  44. void CSourceFile::ISourceFile(CDataFile *aFile)
  45. {
  46.     theFile = aFile;
  47.     itsOptions = NULL;
  48.     itsErrorLog = NULL;
  49.     itsHeaders = NULL;
  50.     itsKind = H_SourceFile;
  51.     itsPool = next_pool++;
  52.     new_malloc_pool(itsPool,1024*16);
  53. }
  54.  
  55.  
  56.  
  57. char *CSourceFile::GetPathName(char *s)
  58. {
  59.     CInfoPBRec    block;
  60.     Str255 directoryName;
  61.     OSErr err;
  62.     
  63.     *s = 0;
  64.     block.dirInfo.ioNamePtr = directoryName;
  65.     block.dirInfo.ioDrParID = theFile->dirID;
  66.     
  67.     do {
  68.         block.dirInfo.ioVRefNum = theFile->volNum;
  69.         block.dirInfo.ioFDirIndex = -1;
  70.         block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
  71.         
  72.         err = PBGetCatInfo(&block,FALSE);
  73.  
  74.         ConcatPStrings(directoryName,"\p:");
  75.         ConcatPStrings(directoryName,s);
  76.         CopyPString(directoryName,s);
  77.     } while (block.dirInfo.ioDrDirID != 2);
  78.     
  79.     ConcatPStrings(s,theFile->name);
  80.     return s;
  81. }
  82.  
  83. FILE *CSourceFile::StdOpen(char *mode)
  84. {
  85.     char path[512];
  86.     GetPathName(path);
  87.     p2cstr(path);
  88.     return fopen(path,mode);
  89. }
  90.  
  91. int CSourceFile::Compile(void)
  92. {
  93.     int                         result = 0;
  94.     SymImageVia_t                gotdec;
  95.     short                        filenum;
  96.     FSSpec aFileSpec;
  97.     CursHandle curs;
  98.     Boolean wasLocked;
  99.     CWindow *theWindow;
  100.     char mesg[64];
  101.     
  102.         wasLocked = Lock(TRUE);
  103.     
  104.         gCurSourceFile = this;
  105.         itsErrorLog = new CErrorLog;
  106.         itsErrorLog->IErrorLog();
  107.         itsErrorLog->BeginDialog();
  108.         gErrs = itsErrorLog;
  109.         
  110.         gApplication->SpinCursor();
  111.       
  112.           theWindow = gErrs->GetWindow();
  113.         sprintf(mesg,"Compilation of %#s",theFile->name);
  114.         c2pstr(mesg);
  115.         theWindow->SetTitle((unsigned char *) mesg);
  116.     
  117.         set_default_pool(itsPool);
  118.  
  119.         theFile->GetFSSpec(&aFileSpec);
  120.         p2cstr(aFileSpec.name);
  121.         if (!ReInit((char *) aFileSpec.name, aFileSpec.vRefNum, aFileSpec.parID)) {
  122.             Lock(wasLocked);
  123.             return 0;
  124.         }
  125.     
  126.         if (!gAbortCompile) {
  127.             FreeAllRegs();        /* Is this necessary ? */
  128.             gotdec = Do_external_declaration(filenum);
  129.             if (gotdec) {
  130.                 result = 1;
  131.                 FreeSymImage(gotdec);
  132.             }
  133.             if (!gotdec && !gAllDone) {
  134.                 SyntaxError("Expected external declaration");
  135.             }
  136.         }
  137.         while (gotdec && !gAllDone && !gAbortCompile) {
  138.             gApplication->SpinCursor();
  139.             FreeAllRegs();        /* Is this necessary ? */
  140.             /* Then, we generate code for everything. */
  141.             if ((gotdec = Do_external_declaration(filenum)) != 0) {
  142.                 result++;
  143.                 FreeSymImage(gotdec);
  144.             }
  145.         }
  146.         if (!NumErrors) {
  147.             GenGlobal(GlobalSymbolTable, GlobalCodes, 1);
  148.             GenSegments(GlobalCodes);
  149.             GenStringLits(GlobalCodes);
  150.             GenFloatLits(GlobalCodes);
  151.             GenStatics(GlobalCodes);
  152.             Optimize68(GlobalCodes);
  153.          }
  154.         if (!NumErrors && !gAbortCompile) {
  155.             filenum = InitOMF(GlobalCodes, (char *) aFileSpec.name, aFileSpec.vRefNum, aFileSpec.parID);
  156.             if (GlobalCodes && GlobalRecords)
  157.                 DumpCodeList(GlobalCodes, GlobalRecords);
  158.             FinishOMF(filenum);
  159.         }
  160.         OneFileCleanUp();
  161.         free_pool_memory();
  162.  
  163.         InitCursor();
  164.         Lock(wasLocked);
  165.         return (!NumErrors);
  166. }
  167.  
  168. CDataFile *CSourceFile::GetObjectFile(void)
  169. {
  170.     Str63 objName;
  171.     CDataFile *objFile;
  172.  
  173.     CopyPString(theFile->name,objName);
  174.  
  175.     objName[objName[0]] = 'o';
  176.     objFile = new CDataFile;
  177.     objFile->IDataFile();
  178.     objFile->SpecifyHFS(objName,theFile->volNum,theFile->dirID);
  179.     return objFile;
  180. }
  181.  
  182. static unsigned long
  183. GetFileModDate(Str63 filename, short volrefnum, long dirID)
  184. {
  185.     HFileParam                      k;
  186.     OSErr                           bad;
  187.     k.ioCompletion = NULL;
  188.     k.ioNamePtr = filename;
  189.     k.ioVRefNum = volrefnum;
  190.     k.ioFDirIndex = 0;
  191.     k.ioDirID = dirID;
  192.     bad = PBHGetFInfo((HParmBlkPtr) & k, false);
  193.     if (!bad)
  194.         return k.ioFlMdDat;
  195.     else
  196.         return 0;
  197. }
  198.  
  199. unsigned long CSourceFile::GetSourceModDate(void)
  200. {    
  201.     return GetFileModDate(theFile->name,theFile->volNum,theFile->dirID);
  202. }
  203.  
  204. unsigned long CSourceFile::GetObjectModDate(void)
  205. {
  206.     CDataFile *objFile;
  207.     objFile = GetObjectFile();
  208.     if (objFile->ExistsOnDisk())
  209.         return GetFileModDate(objFile->name,objFile->volNum,objFile->dirID);
  210.     else return 0;
  211. }
  212.  
  213. void CSourceFile::Dispose(void)
  214. {
  215.     theFile->Dispose();
  216.     inherited::Dispose();
  217. }