1,190c1,162
< //#include <cstdlib> // _itoa
< #include <iostream>
< #include <string>
< 
< #include <cstring> // strcpy
< 
< #include <unistd.h> // truncate
< 
< #include "File.h"
< 
< #include "ReadException.h"
< #include "WriteException.h"
< 
< using namespace std;
< 
< // *** classes
< 
< /* inlined
< File::File(const string& name) : m_isOpen(false), m_name(name) // Start with file not open
< {
< 	// Store the file name.
< 	//strcpy(m_name, name);
< }
< 
< 
< File::~File()
< {
< 	// in case someone forgot to close us
< 	if (m_isOpen)
< 	{
< 	    this->Close();
< 	}
< }
< */
< 
< void File::Open(const string& openMode) throw (std::exception, OpenException)
< {
<     // Don't re-open if already open.
<     if (m_isOpen)
<     {
<     	return;
<     }
< 
<     // Open the file and check for errors.
<     try
<     {
<         m_pFile = new FilePointer(m_name.c_str(), openMode.c_str()); // a+ means create or append read-write access (see: man fopen)
<         assert(0 != m_pFile); // assert FilePointer object was correctly allocated
< 
<         if (m_pFile->isNull()) // if there was an error opening the file
<         {
<             string message = "Error opening file: ";
<             message += m_name;
< 
<             bool isEOF = false;
<             if (openMode == "r") // if we are reading, the file probably doesn't exist!
<             {
<                 isEOF = true;
<                 message += ". \n\nYou must run fssim in binary write mode ( command-line argument '-u')";
<                 message += " before you can read ('-b'). \n\nSee documentation (/tsas/system/docs/fss4.0)";
<                 message += " for more detailed instructions.";
<             }
< 
<             OpenException e(isEOF, true, message);
<             throw e;
<         }
< 
<         rewind(*m_pFile); // set file pointer to beginning of file
< 
<         if (0 != ferror(*m_pFile)) // check for failure
<         {
<             throw OpenException();
<         }
<     }
<     catch (OpenException& e)
<     {
<         if (e.isEndOfFile())
<         {
<             cout << "End of file (see below for details)" << endl;
<         }
<         //cerr << e.FileException::what() << endl << endl;
< 
<         throw; // this is a fatal exception: bail out to main & clean up (exit)
<     }
< 
<     // Now we're open.
<     m_isOpen = true;
< }
< 
< void File::Close()
< {
<     // Don't close if already closed.
<     if (!m_isOpen)
<     {
<     	return; // Kevin Belanger: use if-block braces for clarity
<     }
< 
<     // Kevin Belanger: replaced _lclose with CloseHandle
<     // Close the file and ignore errors.
<     //fclose(m_pFile);
< 
<     // file is closed by FilePointer destructor
<     delete m_pFile;
< 
<     // Now we're closed.
<     m_isOpen = false;
< }
< 
< void File::clear()
< {
<     if (m_isOpen)
<     {
<         truncate(m_name.c_str(), 0);
<     }
< }
< 
< 
< bool File::Seek(const UInt32 position)// throw (FileException)
< {
< 	// can't if file isn't open
< 	if (!m_isOpen)
<     {
<         string message = "Error: can't seek. File: ";
<         message += m_name;
<         message += " is not open.";
<     	throw ReadException(message);
<     }
< 
<     // Seek and check for errors.
<     fseek(*m_pFile, position, SEEK_SET /*FILE_BEGIN*/);
< 
<     if (0 != feof(*m_pFile)) // end of file
<     {
<         return false;
<     }
< 
<     if (0 != ferror(*m_pFile))
<     {
<         string message = "Error seeking through file: ";
<         message += m_name;
< 	    throw FileException(message);
<     }
<     return true;
< }
< 
< void File::Read(const UInt32 numBytes, void* data) //throw (FileException)
< {
<     fread(data, (UInt16)numBytes, 1, *m_pFile); // read 1 chunk of numBytes bytes
< }
< 
< void File::Write(const UInt32 numBytes, const void* data)
<  //throw (FileException)
< {
< 	// can't if file isn't open
< 	if (!m_isOpen)
<     {
<         string message = "Error: can't write. File: ";
<         message += m_name;
<         message += " is not open.";
<         throw WriteException(message);
<     }
< 
<     // Kevin Belanger: replaced hard-coded magic number
<     // with an enumeration for byte limit
<     // Cannot write more than kMaxNumberOfBytes bytes.
<     if (numBytes > kMaxNumberOfBytes)
<     {
<         // convert decimal int to string
<         char intBuffer[kDigitBuffer];
<         sprintf(intBuffer, "%d", kMaxNumberOfBytes);
< 
<         // throw exception for exceeding maximum bytes
<         string message = "Error: maximum number of bytes: ";
<         message += intBuffer;
<         message += " exceeded while writing to file: ";
<         message += m_name;
<         throw WriteException(message);
<     }
< 
<     UInt16 itemsWritten = fwrite(data, numBytes, 1, *m_pFile); // write 1 chunk of numBytes bytes
<     //bool bOK = WriteFile(m_pFile, data, numBytes, &numBytesWritten, NULL);
<     fflush(*m_pFile);
<     if (0 != ferror(*m_pFile) || 0 == itemsWritten)
<     {
<         string message = "Error writing to file: ";
<         message += m_name;
<         throw WriteException(message);
<     }
< 
< }
---
> #include <iostream>
> #include <string>
> 
> #include <cstring> // strcpy
> #include <cassert>
> #include <unistd.h> // truncate
> 
> #include "File.h"
> 
> #include "ReadException.h"
> #include "WriteException.h"
> 
> using namespace std;
> 
> 
> void File::Open(const string& openMode) throw (std::exception, OpenException)
> {
>     // Don't re-open if already open.
>     if (m_isOpen)
>     {
>     	return;
>     }
> 
>     // Open the file and check for errors.
>     try
>     {
>         m_pFile = new FilePointer(m_name.c_str(), openMode.c_str()); // a+ means create or append read-write access (see: man fopen)
>         assert(0 != m_pFile); // assert FilePointer object was correctly allocated
> 
>         if (m_pFile->isNull()) // if there was an error opening the file
>         {
>             string message = "Error opening file: ";
>             message += m_name;
> 
>             bool isEOF = false;
>             if (openMode == "r") // if we are reading, the file probably doesn't exist!
>             {
>                 isEOF = true;
>                 message += ". \n\nYou must run fssim in binary write mode ( command-line argument '-u')";
>                 message += " before you can read ('-b'). \n\nSee documentation (/tsas/system/docs/fss4.0)";
>                 message += " for more detailed instructions.";
>             }
> 
>             OpenException e(isEOF, true, message);
>             throw e;
>         }
> 
>         rewind(*m_pFile); // set file pointer to beginning of file
> 
>         if (0 != ferror(*m_pFile)) // check for failure
>         {
>             throw OpenException();
>         }
>     }
>     catch (OpenException& e)
>     {
>         if (e.isEndOfFile())
>         {
>             cout << "End of file (see below for details)" << endl;
>         }
>         //cerr << e.FileException::what() << endl << endl;
> 
>         throw; // this is a fatal exception: bail out to main & clean up (exit)
>     }
> 
>     // Now we're open.
>     m_isOpen = true;
> }
> 
> void File::Close()
> {
>     // Don't close if already closed.
>     if (!m_isOpen)
>     {
>     	return; // Kevin Belanger: use if-block braces for clarity
>     }
> 
>     // file is closed by FilePointer destructor
>     delete m_pFile;
> 
>     // Now we're closed.
>     m_isOpen = false;
> }
> 
> void File::clear()
> {
>     if (m_isOpen)
>     {
>         truncate(m_name.c_str(), 0);
>     }
> }
> 
> 
> bool File::Seek(const UInt32 position)
> {
> 	// can't if file isn't open
> 	if (!m_isOpen)
>     {
>         string message = "Error: can't seek. File: ";
>         message += m_name;
>         message += " is not open.";
>     	throw ReadException(message);
>     }
> 
>     // Seek and check for errors.
>     fseek(*m_pFile, position, SEEK_SET);
> 
>     if (0 != feof(*m_pFile)) // end of file
>     {
>         return false;
>     }
> 
>     if (0 != ferror(*m_pFile))
>     {
>         string message = "Error seeking through file: ";
>         message += m_name;
> 	    throw FileException(message);
>     }
>     return true;
> }
> 
> void File::Read(const UInt32 numBytes, void* data)
> {
>     fread(data, (UInt16)numBytes, 1, *m_pFile); // read 1 chunk of numBytes bytes
> }
> 
> void File::Write(const UInt32 numBytes, const void* data)
> {
> 	// can't if file isn't open
> 	if (!m_isOpen)
>     {
>         string message = "Error: can't write. File: ";
>         message += m_name;
>         message += " is not open.";
>         throw WriteException(message);
>     }
> 
>     // Cannot write more than kMaxNumberOfBytes bytes.
>     if (numBytes > kMaxNumberOfBytes)
>     {
>         // convert decimal int to string
>         char intBuffer[kDigitBuffer];
>         sprintf(intBuffer, "%d", kMaxNumberOfBytes);
> 
>         // throw exception for exceeding maximum bytes
>         string message = "Error: maximum number of bytes: ";
>         message += intBuffer;
>         message += " exceeded while writing to file: ";
>         message += m_name;
>         throw WriteException(message);
>     }
> 
>     UInt16 itemsWritten = fwrite(data, numBytes, 1, *m_pFile); // write 1 chunk of numBytes bytes
>     fflush(*m_pFile);
>     if (0 != ferror(*m_pFile) || 0 == itemsWritten)
>     {
>         string message = "Error writing to file: ";
>         message += m_name;
>         throw WriteException(message);
>     }
> 
> }
