home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / Jikes / Source / src / jikesapi.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-03  |  10.6 KB  |  479 lines

  1. /*
  2.  * $Id: jikesapi.cpp,v 1.25 2001/02/27 06:27:40 mdejong Exp $
  3.  */
  4.  
  5.  
  6. #include "platform.h"
  7. #include "control.h"
  8. #include "jikesapi.h"
  9.  
  10. /*
  11. FIXME: Need to readdress include issue.
  12. #ifdef HAVE_STDLIB_H
  13. # include <stdlib.h>
  14. #endif
  15.  
  16. #ifdef HAVE_STDIO_H
  17. # include <stdio.h>
  18. #endif
  19.  
  20. #ifdef HAVE_WINDOWS_H
  21. # include <windows.h>
  22. #endif
  23. */
  24.  
  25. #ifdef    HAVE_JIKES_NAMESPACE
  26. using namespace Jikes;
  27. #endif
  28.  
  29. // Note: JikesAPI classes only use the Jikes namespace, they
  30. // are never defined in the Jikes namespace. The use of the Jikes
  31. // namespace is a compile time option and jikesapi.h can not
  32. // include build files like platform.h or config.h. Only
  33. // the Default* classes in this file can live in the Jikes namespace.
  34.  
  35. #ifdef    HAVE_JIKES_NAMESPACE
  36. namespace Jikes {
  37. #endif
  38.  
  39. /**
  40.  * A default implementation of ReadObject that read from the file sysytem.
  41.  */ 
  42. class DefaultFileReader: public JikesAPI::FileReader
  43. {
  44.     public:
  45.     
  46.     DefaultFileReader(const char *fileName);
  47.     virtual  ~DefaultFileReader();
  48.     
  49.     virtual const char     *getBuffer()      {return(buffer);}
  50.     virtual       size_t    getBufferSize()  {return(size);}
  51.     
  52.     private:
  53.     
  54.     const char     *buffer;
  55.     size_t    size;
  56. // FIXME : need to move into platform.h
  57. #ifdef   WIN32_FILE_SYSTEM
  58.     HANDLE    srcfile;
  59.     HANDLE    mapfile;
  60. #endif 
  61. };
  62.  
  63. /**
  64.  * A default implementaion of WriteObject that writes to the file system.
  65.  */
  66. class DefaultFileWriter: public JikesAPI::FileWriter
  67. {
  68.     public:
  69.     DefaultFileWriter(const char *fileName,size_t maxSize);
  70.     virtual  ~DefaultFileWriter();
  71.     
  72.     virtual  int      isValid();
  73.     
  74.     private:
  75.     
  76.     virtual  size_t    doWrite(const unsigned char *data,size_t size);
  77.  
  78.     // Note that we don't use the bool type anywhere in jikesapi.h
  79.     // since it is not supported by some compilers and we can't
  80.     // depend on the typedef in platform.h because jikesapi.h
  81.     // would never include build time files
  82.     int      valid;
  83. // FIXME: need to clean this up, why is this not wrapped in a platform.h function?
  84. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  85.     FILE     *file;
  86. #elif defined(WIN32_FILE_SYSTEM)
  87.     HANDLE    file;
  88.     HANDLE    mapfile;
  89.     u1       *string_buffer;
  90.     size_t    dataWritten;
  91. #endif
  92. };
  93.  
  94. #ifdef    HAVE_JIKES_NAMESPACE
  95. }            // Close namespace Jikes block
  96. #endif
  97.  
  98. JikesOption::~JikesOption()
  99. {
  100.     delete [] classpath ;
  101.     delete [] directory ;
  102.     delete [] encoding  ;
  103. }
  104.  
  105. JikesOption::JikesOption():
  106.     classpath(NULL),
  107.     directory(NULL),
  108.     encoding(NULL),
  109.     nowrite(false),
  110.     deprecation(false),
  111.     O(false),
  112.     g(false),
  113.     verbose(false),
  114.     depend(false),
  115.     nowarn(false),
  116.     old_classpath_search_order(false),
  117.     zero_defect(false)
  118. {
  119. }
  120.  
  121. JikesAPI * JikesAPI::instance = NULL;
  122.  
  123. JikesAPI::JikesAPI()
  124. :option(NULL),
  125. parsedOptions(NULL)
  126. {
  127.     SetNewHandler();
  128.     FloatingPointCheck();
  129.     instance = this;
  130. }
  131.  
  132. JikesAPI * JikesAPI::getInstance()
  133. {
  134.     return instance;
  135. }
  136.  
  137.  
  138. JikesAPI::~JikesAPI()
  139. {
  140.     cleanupOptions();
  141. }
  142.  
  143. void JikesAPI::cleanupOptions() {
  144.     delete option;
  145.  
  146.     if (parsedOptions) {
  147.         for(char ** parsed = parsedOptions; *parsed != NULL ; parsed++) {
  148.             delete [] *parsed;
  149.         }
  150.         delete [] parsedOptions;
  151.         parsedOptions = NULL;
  152.     }
  153. }
  154.  
  155. char ** JikesAPI::parseOptions(int argc, char **argv)
  156. {
  157.     cleanupOptions();
  158.  
  159.     ArgumentExpander *args = new ArgumentExpander(argc, argv);
  160.     Option* opt = new Option(*args);
  161.     option = opt;
  162.     int n = args->argc - opt->first_file_index;
  163.  
  164.     if (n <= 0)
  165.     {
  166.         delete args;
  167.         return NULL;
  168.     }
  169.     else
  170.     {
  171.         parsedOptions = new char*[n+1];
  172.         for (int i=0; i<n; i++)
  173.         {
  174.             const char *o = args->argv[opt->first_file_index+i];
  175.             if (o)
  176.             {
  177.                 parsedOptions[i] = new char[strlen(o)+1];
  178.                 strcpy(parsedOptions[i],o);
  179.             } else
  180.             {
  181.                 parsedOptions[i] = NULL;
  182.                 break;
  183.             }
  184.         }
  185.         parsedOptions[n] = NULL;
  186.         delete args;
  187.         return parsedOptions;
  188.     }
  189. }
  190.  
  191. JikesOption* JikesAPI::getOptions()
  192. {
  193.     return option;
  194. }
  195.  
  196. /**
  197.  * Compile given list of files.
  198.  */
  199. int JikesAPI::compile(char **filenames)
  200. {
  201.     // Cast the JikesOption to an Option instance.
  202.     // Note that the reason we don't use an Option
  203.     // member type in the declaration of JikesAPI
  204.     // is so that the jikespai.h header does not
  205.     // need to include option.h.
  206.  
  207.     Control *control = new Control(filenames , *((Option*)option));
  208.     int return_code = control -> return_code;
  209.     delete control;
  210.     return return_code;
  211. }
  212.  
  213. /**
  214.  * This method will be called for each error reported.
  215.  */
  216. void JikesAPI::reportError(JikesError *error)
  217. {
  218.     Coutput << error->getErrorReport();
  219.     Coutput.flush();
  220. }
  221.  
  222. const char *JikesError::getSeverityString() 
  223. {
  224.     switch(getSeverity())
  225.     {
  226.     case JIKES_ERROR  : return "Error"   ;
  227.     case JIKES_WARNING: return "Warning" ;
  228.     case JIKES_CAUTION: return "Caution" ;
  229.     default: return "Unknown";
  230.     }
  231. }
  232.  
  233.  
  234. /*
  235.  * Default file stat/reading and writting:
  236.  * The following provide the base classes and the
  237.  * default implamentations to read files from the file systems.
  238.  */
  239.  
  240.  
  241. /**
  242.  *  By default just ask the system for stat information.
  243.  */
  244. int JikesAPI::stat(const char *fileName,struct stat *status)
  245. {
  246.     return SystemStat(fileName,status);
  247. }
  248.  
  249. /**
  250.  * By default return an object that reads from the file system.
  251.  */
  252. JikesAPI::FileReader *JikesAPI::read(const char *fileName)
  253. {
  254.     FileReader  *result  =  new DefaultFileReader(fileName);
  255.  
  256.     // NB even if a file is empty (0 bytes)
  257.     // This will return a pointer to 0 length array
  258.     // and should not be NULL.
  259.     if(result && (result->getBuffer() == NULL))  
  260.     {                                
  261.         delete result;                
  262.         result  = NULL;
  263.     }
  264.     return result;
  265. }
  266.  
  267. /**
  268.  * By Default return an object that reads from the file system.
  269.  */
  270. JikesAPI::FileWriter *JikesAPI::write(const char *fileName, size_t bytes) 
  271. {
  272.     FileWriter *result  = new DefaultFileWriter(fileName, bytes);
  273.     
  274.     if(result && (!result->isValid()))
  275.     {
  276.         delete result;
  277.         result = NULL;
  278.     }
  279.     return result;
  280. }
  281.  
  282. /**
  283.  * The Write() mewthod on all WriteObject(s) makes sure that we do not
  284.  * send too much data to the virtual function.
  285.  */
  286. size_t JikesAPI::FileWriter::write(const unsigned char *data,size_t size)
  287. {
  288.     size_t result   = 0;
  289.  
  290.     if(size <= maxSize)
  291.     {
  292.         result   = this->doWrite(data,size);
  293.         maxSize  -= size;
  294.     }
  295.    
  296.     return(result);
  297. }
  298.  
  299.  
  300. #ifdef    HAVE_JIKES_NAMESPACE
  301. namespace Jikes {
  302. #endif
  303.  
  304.  
  305. #if defined(UNIX_FILE_SYSTEM) || defined(AMIGAOS_FILE_SYSTEM)
  306. // The following methods define UNIX specific methods for
  307. // reading files from the file system. WINDOWS method follow in
  308. // the subsequent section.
  309.  
  310.  
  311. /**
  312.  * When the ReadObject is created. read the whole file into a buffer
  313.  * held by the object.
  314.  */ 
  315. DefaultFileReader::DefaultFileReader(const char *fileName)
  316. {
  317.     size   = 0;
  318.     buffer = NULL;
  319.  
  320.     struct stat status;
  321.     JikesAPI::getInstance()->stat(fileName, &status);
  322.     size   = status.st_size;
  323.  
  324.     FILE *srcfile = SystemFopen(fileName, "rb");
  325.     if (srcfile != NULL)
  326.     {
  327.         buffer = new char[size];
  328.         size_t numread = SystemFread(
  329. #ifdef HAVE_CONST_CAST
  330.             const_cast<char*>(buffer)
  331. #else
  332.             (char *) buffer
  333. #endif
  334.             , sizeof(char), size, srcfile);
  335.         //assert(numread == size); // FIXME: uncomment when SystemFread uses "b"
  336.         fclose(srcfile);
  337.     }
  338. }
  339.  
  340.  
  341. /**
  342.  * When the ReadObject is destroyed the release the memory buffer.
  343.  */
  344. DefaultFileReader::~DefaultFileReader()
  345. {
  346.     delete [] buffer;
  347. }
  348.  
  349.  
  350. /**
  351.  * Open a standard FILE pointer and get ready to write.
  352.  */
  353. DefaultFileWriter::DefaultFileWriter(const char *fileName,size_t maxSize):
  354.     FileWriter(maxSize)
  355. {
  356.     valid  = false;
  357.     file = SystemFopen(fileName, "wb");
  358.     if (file  ==  (FILE *) NULL)
  359.         return;
  360.     valid  = true;
  361. }
  362.  
  363. /**
  364.  * Close the file when the write object is destroyed.
  365.  */
  366. DefaultFileWriter::~DefaultFileWriter()
  367. {
  368.     if (valid)
  369.         fclose(file);
  370. }
  371.  
  372. int DefaultFileWriter::isValid()  {return(valid);}
  373.  
  374. /**
  375.  * Copy the data buffer to the file.
  376.  */
  377. size_t DefaultFileWriter::doWrite(const unsigned char *data,size_t size)
  378. {
  379.     return fwrite(data, sizeof(u1),size, file);
  380. }
  381.  
  382. #elif defined(WIN32_FILE_SYSTEM)
  383.  
  384.  
  385.  
  386.  
  387. // Open a windows file and map the file onto processor memory.
  388. DefaultFileReader::DefaultFileReader(const char *fileName)
  389. {
  390.     size   = 0;
  391.     buffer = NULL;
  392.  
  393.     srcfile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
  394.     if (srcfile != INVALID_HANDLE_VALUE)
  395.     {
  396.         mapfile = CreateFileMapping(srcfile, NULL, PAGE_READONLY, 0, 0, NULL);
  397.         if (mapfile != INVALID_HANDLE_VALUE)
  398.         {
  399.             buffer = (char *) MapViewOfFile(mapfile, FILE_MAP_READ, 0, 0, 0);
  400.             size = (size_t)GetFileSize(srcfile, NULL);
  401.         }
  402.     }
  403. }
  404.  
  405.  
  406. // When the ReadObject is destroyed close all the associated files.
  407. // and unmap the memory.
  408. DefaultFileReader::~DefaultFileReader()
  409. {
  410.     if (srcfile != INVALID_HANDLE_VALUE)
  411.     {
  412.         if (mapfile != INVALID_HANDLE_VALUE)
  413.         {
  414.             if (buffer)
  415.             {
  416.                 UnmapViewOfFile((void *) buffer);
  417.             }
  418.             CloseHandle(mapfile);
  419.         }
  420.         CloseHandle(srcfile);
  421.     }
  422. }
  423.  
  424.  
  425. // Create a windows file and map the file onto processor memory.
  426. DefaultFileWriter::DefaultFileWriter(const char *fileName,size_t maxSize):
  427.     FileWriter(maxSize)
  428. {
  429.     valid  = false;
  430.     dataWritten    = 0;
  431.     file           = CreateFile(fileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  432.     if (file == INVALID_HANDLE_VALUE)
  433.         return;
  434.  
  435.     mapfile        = CreateFileMapping(file, NULL, PAGE_READWRITE, 0, maxSize, NULL);
  436.     if (mapfile == INVALID_HANDLE_VALUE)
  437.         return;
  438.  
  439.     string_buffer  = (u1 *) MapViewOfFile(mapfile, FILE_MAP_WRITE, 0, 0, maxSize);
  440.     assert(string_buffer);
  441.     valid  = true;
  442. }
  443.  
  444. // When the WriteObject is destroyed close all the associated files,
  445. // Thus writting the mory to the file system.
  446. DefaultFileWriter::~DefaultFileWriter()
  447. {
  448.     if (file != INVALID_HANDLE_VALUE)
  449.     {
  450.         if (mapfile != INVALID_HANDLE_VALUE)
  451.         {
  452.             UnmapViewOfFile(string_buffer);
  453.             CloseHandle(mapfile);
  454.         }
  455.         CloseHandle(file);
  456.     }
  457. }
  458.  
  459. int DefaultFileWriter::isValid()  
  460. {
  461.     return(valid);
  462. }
  463.  
  464. // Copy the input data to the mapped memory.
  465. size_t DefaultFileWriter::doWrite(const unsigned char *data,size_t size)
  466. {
  467.     memmove(&string_buffer[dataWritten], data, size * sizeof(u1));
  468.     dataWritten += size;
  469.     return(size);
  470. }
  471.  
  472. #endif // UNIX_FILE_SYSTEM
  473.  
  474.  
  475. #ifdef    HAVE_JIKES_NAMESPACE
  476. }            // Close namespace Jikes block
  477. #endif
  478.  
  479.