summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorleocad1999-11-23 21:05:20 +0000
committerleocad1999-11-23 21:05:20 +0000
commit247faeedad984ad8b033b74b69a955afbf0e9aff (patch)
treeb6b5a9a78569f20accce52074e3276b3ce7f7f75
parent73dd484ef0c6a10ac3b229c24f3aae4f2175e8a9 (diff)
Split the File class into FileMem and FileDisk
git-svn-id: http://svn.leocad.org/trunk@17 c7d43263-9d01-0410-8a33-9dba5d9f93d6
-rw-r--r--common/file.cpp470
-rw-r--r--common/file.h75
-rw-r--r--common/image.cpp4
-rw-r--r--common/project.cpp16
-rw-r--r--common/project.h4
5 files changed, 302 insertions, 267 deletions
diff --git a/common/file.cpp b/common/file.cpp
index 2791f7a..598d352 100644
--- a/common/file.cpp
+++ b/common/file.cpp
@@ -9,329 +9,315 @@
/////////////////////////////////////////////////////////////////////////////
// File construction/destruction
-File::File(bool bMemFile)
-{
- m_bMemFile = bMemFile;
-
- if (m_bMemFile)
- {
- m_nGrowBytes = 1024;
- m_nPosition = 0;
- m_nBufferSize = 0;
- m_nFileSize = 0;
- m_pBuffer = NULL;
- m_bAutoDelete = true;
- }
- else
- {
- m_hFile = NULL;
- m_bCloseOnDelete = false;
- }
+File::File()
+{
}
File::~File()
{
- if (m_bMemFile)
- {
- if (m_pBuffer)
- Close();
+}
- m_nGrowBytes = 0;
- m_nPosition = 0;
- m_nBufferSize = 0;
- m_nFileSize = 0;
- }
- else
- {
- if (m_hFile != NULL && m_bCloseOnDelete)
- Close();
- }
+FileMem::FileMem()
+{
+ m_nGrowBytes = 1024;
+ m_nPosition = 0;
+ m_nBufferSize = 0;
+ m_nFileSize = 0;
+ m_pBuffer = NULL;
+ m_bAutoDelete = true;
+}
+
+FileDisk::FileDisk()
+{
+ m_hFile = NULL;
+ m_bCloseOnDelete = false;
+}
+
+FileMem::~FileMem()
+{
+ if (m_pBuffer)
+ Close();
+
+ m_nGrowBytes = 0;
+ m_nPosition = 0;
+ m_nBufferSize = 0;
+ m_nFileSize = 0;
+}
+
+FileDisk::~FileDisk()
+{
+ if (m_hFile != NULL && m_bCloseOnDelete)
+ Close();
}
/////////////////////////////////////////////////////////////////////////////
// File operations
-char* File::ReadString(char* pBuf, unsigned long nMax)
+char* FileMem::ReadString(char* pBuf, unsigned long nMax)
{
- if (m_bMemFile)
- {
- int nRead = 0;
- unsigned char ch;
+ int nRead = 0;
+ unsigned char ch;
- if (nMax <= 0)
- return NULL;
- if (m_nPosition >= m_nFileSize)
- return NULL;
+ if (nMax <= 0)
+ return NULL;
+ if (m_nPosition >= m_nFileSize)
+ return NULL;
- while (--nMax)
- {
- if (m_nPosition == m_nFileSize)
- break;
+ while (--nMax)
+ {
+ if (m_nPosition == m_nFileSize)
+ break;
- ch = m_pBuffer[m_nPosition];
- m_nPosition++;
- pBuf[nRead++] = ch;
+ ch = m_pBuffer[m_nPosition];
+ m_nPosition++;
+ pBuf[nRead++] = ch;
- if (ch == '\n')
- break;
- }
+ if (ch == '\n')
+ break;
+ }
- pBuf[nRead] = '\0';
- return pBuf;
- }
- else
- return fgets(pBuf, nMax, m_hFile);
+ pBuf[nRead] = '\0';
+ return pBuf;
}
-unsigned long File::Read(void* pBuf, unsigned long nCount)
+char* FileDisk::ReadString(char* pBuf, unsigned long nMax)
{
- if (nCount == 0)
- return 0;
+ return fgets(pBuf, nMax, m_hFile);
+}
+
+unsigned long FileMem::Read(void* pBuf, unsigned long nCount)
+{
+ if (nCount == 0)
+ return 0;
- if (m_bMemFile)
- {
- if (m_nPosition > m_nFileSize)
- return 0;
+ if (m_nPosition > m_nFileSize)
+ return 0;
- unsigned long nRead;
- if (m_nPosition + nCount > m_nFileSize)
- nRead = (unsigned long)(m_nFileSize - m_nPosition);
- else
- nRead = nCount;
+ unsigned long nRead;
+ if (m_nPosition + nCount > m_nFileSize)
+ nRead = (unsigned long)(m_nFileSize - m_nPosition);
+ else
+ nRead = nCount;
- memcpy((unsigned char*)pBuf, (unsigned char*)m_pBuffer + m_nPosition, nRead);
- m_nPosition += nRead;
+ memcpy((unsigned char*)pBuf, (unsigned char*)m_pBuffer + m_nPosition, nRead);
+ m_nPosition += nRead;
- return nRead;
- }
- else
- return fread(pBuf, 1, nCount, m_hFile);
+ return nRead;
+}
+
+unsigned long FileDisk::Read(void* pBuf, unsigned long nCount)
+{
+ return fread(pBuf, 1, nCount, m_hFile);
}
-int File::GetChar()
+int FileMem::GetChar()
{
- if (m_bMemFile)
- {
- if (m_nPosition > m_nFileSize)
- return 0;
+ if (m_nPosition > m_nFileSize)
+ return 0;
- unsigned char* ret = (unsigned char*)m_pBuffer + m_nPosition;
- m_nPosition++;
+ unsigned char* ret = (unsigned char*)m_pBuffer + m_nPosition;
+ m_nPosition++;
- return *ret;
- }
- else
- return fgetc(m_hFile);
+ return *ret;
}
-unsigned long File::Write(const void* pBuf, unsigned long nCount)
+int FileDisk::GetChar()
{
- if (nCount == 0)
- return 0;
+ return fgetc(m_hFile);
+}
+
+unsigned long FileMem::Write(const void* pBuf, unsigned long nCount)
+{
+ if (nCount == 0)
+ return 0;
- if (m_bMemFile)
- {
- if (m_nPosition + nCount > m_nBufferSize)
- GrowFile(m_nPosition + nCount);
+ if (m_nPosition + nCount > m_nBufferSize)
+ GrowFile(m_nPosition + nCount);
- memcpy((unsigned char*)m_pBuffer + m_nPosition, (unsigned char*)pBuf, nCount);
+ memcpy((unsigned char*)m_pBuffer + m_nPosition, (unsigned char*)pBuf, nCount);
- m_nPosition += nCount;
+ m_nPosition += nCount;
- if (m_nPosition > m_nFileSize)
- m_nFileSize = m_nPosition;
+ if (m_nPosition > m_nFileSize)
+ m_nFileSize = m_nPosition;
- return nCount;
- }
- else
- return fwrite(pBuf, 1, nCount, m_hFile);
+ return nCount;
+}
+
+unsigned long FileDisk::Write(const void* pBuf, unsigned long nCount)
+{
+ return fwrite(pBuf, 1, nCount, m_hFile);
}
-int File::PutChar(int c)
+int FileMem::PutChar(int c)
{
- if (m_bMemFile)
- {
- if (m_nPosition + 1 > m_nBufferSize)
- GrowFile(m_nPosition + 1);
+ if (m_nPosition + 1 > m_nBufferSize)
+ GrowFile(m_nPosition + 1);
- unsigned char* bt = (unsigned char*)m_pBuffer + m_nPosition;
- *bt = c;
+ unsigned char* bt = (unsigned char*)m_pBuffer + m_nPosition;
+ *bt = c;
- m_nPosition++;
+ m_nPosition++;
- if (m_nPosition > m_nFileSize)
- m_nFileSize = m_nPosition;
+ if (m_nPosition > m_nFileSize)
+ m_nFileSize = m_nPosition;
- return 1;
- }
- else
- return fputc(c, m_hFile);
+ return 1;
}
-bool File::Open(const char *filename, const char *mode)
+int FileDisk::PutChar(int c)
{
- if (m_bMemFile)
- return false;
- else
- {
- m_hFile = fopen(filename, mode);
- m_bCloseOnDelete = true;
-
- return (m_hFile != NULL);
- }
+ return fputc(c, m_hFile);
}
-void File::Close()
+bool FileDisk::Open(const char *filename, const char *mode)
{
- if (m_bMemFile)
- {
- m_nGrowBytes = 0;
- m_nPosition = 0;
- m_nBufferSize = 0;
- m_nFileSize = 0;
- if (m_pBuffer && m_bAutoDelete)
- free(m_pBuffer);
- m_pBuffer = NULL;
- }
- else
- {
- if (m_hFile != NULL)
- fclose(m_hFile);
+ m_hFile = fopen(filename, mode);
+ m_bCloseOnDelete = true;
+
+ return (m_hFile != NULL);
+}
- m_hFile = NULL;
- m_bCloseOnDelete = false;
-// m_strFileName.Empty();
- }
+void FileMem::Close()
+{
+ m_nGrowBytes = 0;
+ m_nPosition = 0;
+ m_nBufferSize = 0;
+ m_nFileSize = 0;
+ if (m_pBuffer && m_bAutoDelete)
+ free(m_pBuffer);
+ m_pBuffer = NULL;
}
-unsigned long File::Seek(long lOff, int nFrom)
+void FileDisk::Close()
{
- if (m_bMemFile)
- {
- unsigned long lNewPos = m_nPosition;
+ if (m_hFile != NULL)
+ fclose(m_hFile);
- if (nFrom == SEEK_SET)
- lNewPos = lOff;
- else if (nFrom == SEEK_CUR)
- lNewPos += lOff;
- else if (nFrom == SEEK_END)
- lNewPos = m_nFileSize + lOff;
- else
- return (unsigned long)-1;
+ m_hFile = NULL;
+ m_bCloseOnDelete = false;
+}
+
+unsigned long FileMem::Seek(long lOff, int nFrom)
+{
+ unsigned long lNewPos = m_nPosition;
- m_nPosition = lNewPos;
+ if (nFrom == SEEK_SET)
+ lNewPos = lOff;
+ else if (nFrom == SEEK_CUR)
+ lNewPos += lOff;
+ else if (nFrom == SEEK_END)
+ lNewPos = m_nFileSize + lOff;
+ else
+ return (unsigned long)-1;
- return m_nPosition;
- }
- else
- {
- fseek (m_hFile, lOff, nFrom);
+ m_nPosition = lNewPos;
- return ftell(m_hFile);
- }
+ return m_nPosition;
}
-unsigned long File::GetPosition() const
+unsigned long FileDisk::Seek(long lOff, int nFrom)
{
- if (m_bMemFile)
- return m_nPosition;
- else
- return ftell(m_hFile);
+ fseek (m_hFile, lOff, nFrom);
+
+ return ftell(m_hFile);
}
-void File::GrowFile(unsigned long nNewLen)
+unsigned long FileMem::GetPosition() const
{
- if (m_bMemFile)
- {
- if (nNewLen > m_nBufferSize)
- {
- // grow the buffer
- unsigned long nNewBufferSize = m_nBufferSize;
+ return m_nPosition;
+}
- // determine new buffer size
- while (nNewBufferSize < nNewLen)
- nNewBufferSize += m_nGrowBytes;
+unsigned long FileDisk::GetPosition() const
+{
+ return ftell(m_hFile);
+}
- // allocate new buffer
- unsigned char* lpNew;
- if (m_pBuffer == NULL)
- lpNew = (unsigned char*)malloc(nNewBufferSize);
- else
- lpNew = (unsigned char*)realloc(m_pBuffer, nNewBufferSize);
+void FileMem::GrowFile(unsigned long nNewLen)
+{
+ if (nNewLen > m_nBufferSize)
+ {
+ // grow the buffer
+ unsigned long nNewBufferSize = m_nBufferSize;
+
+ // determine new buffer size
+ while (nNewBufferSize < nNewLen)
+ nNewBufferSize += m_nGrowBytes;
+
+ // allocate new buffer
+ unsigned char* lpNew;
+ if (m_pBuffer == NULL)
+ lpNew = (unsigned char*)malloc(nNewBufferSize);
+ else
+ lpNew = (unsigned char*)realloc(m_pBuffer, nNewBufferSize);
+
+ m_pBuffer = lpNew;
+ m_nBufferSize = nNewBufferSize;
+ }
+}
- m_pBuffer = lpNew;
- m_nBufferSize = nNewBufferSize;
- }
- }
+void FileMem::Flush()
+{
+ // Nothing to be done
}
-void File::Flush()
+void FileDisk::Flush()
{
- if (m_bMemFile)
- {
+ if (m_hFile == NULL)
+ return;
- }
- else
- {
- if (m_hFile == NULL)
- return;
+ fflush(m_hFile);
+}
- fflush(m_hFile);
- }
+void FileMem::Abort()
+{
+ Close();
}
-void File::Abort()
-{
- if (m_bMemFile)
- Close();
- else
- {
- if (m_hFile != NULL)
- {
- // close but ignore errors
- if (m_bCloseOnDelete)
- fclose(m_hFile);
- m_hFile = NULL;
- m_bCloseOnDelete = false;
- }
-// m_strFileName.Empty();
- }
+void FileDisk::Abort()
+{
+ if (m_hFile != NULL)
+ {
+ // close but ignore errors
+ if (m_bCloseOnDelete)
+ fclose(m_hFile);
+ m_hFile = NULL;
+ m_bCloseOnDelete = false;
+ }
}
-void File::SetLength(unsigned long nNewLen)
+void FileMem::SetLength(unsigned long nNewLen)
{
- if (m_bMemFile)
- {
- if (nNewLen > m_nBufferSize)
- GrowFile(nNewLen);
+ if (nNewLen > m_nBufferSize)
+ GrowFile(nNewLen);
+
+ if (nNewLen < m_nPosition)
+ m_nPosition = nNewLen;
+
+ m_nFileSize = nNewLen;
+}
- if (nNewLen < m_nPosition)
- m_nPosition = nNewLen;
+void FileDisk::SetLength(unsigned long nNewLen)
+{
+ fseek(m_hFile, nNewLen, SEEK_SET);
+}
- m_nFileSize = nNewLen;
- }
- else
- {
- fseek(m_hFile, nNewLen, SEEK_SET);
- }
+unsigned long FileMem::GetLength() const
+{
+ return m_nBufferSize;
}
-unsigned long File::GetLength() const
+unsigned long FileDisk::GetLength() const
{
- if (m_bMemFile)
- {
- return m_nBufferSize;
- }
- else
- {
- unsigned long nLen, nCur;
+ unsigned long nLen, nCur;
- // Seek is a non const operation
- nCur = ftell(m_hFile);
- fseek(m_hFile, 0, SEEK_END);
- nLen = ftell(m_hFile);
- fseek(m_hFile, nCur, SEEK_SET);
+ // Seek is a non const operation
+ nCur = ftell(m_hFile);
+ fseek(m_hFile, 0, SEEK_END);
+ nLen = ftell(m_hFile);
+ fseek(m_hFile, nCur, SEEK_SET);
- return nLen;
- }
+ return nLen;
}
+
+
diff --git a/common/file.h b/common/file.h
index 8608092..90b3395 100644
--- a/common/file.h
+++ b/common/file.h
@@ -11,13 +11,36 @@ class File
{
public:
// Constructors
- File(bool bMemFile);
- ~File();
+ File();
+ virtual ~File();
// Implementation
-protected:
- bool m_bMemFile;
+public:
+ virtual unsigned long GetPosition() const = 0;
+ virtual unsigned long Seek(long lOff, int nFrom) = 0;
+ virtual void SetLength(unsigned long nNewLen) = 0;
+ virtual unsigned long GetLength() const = 0;
+
+ virtual char* ReadString(char* pBuf, unsigned long nMax)=0;
+ virtual unsigned long Read(void* pBuf, unsigned long nCount)=0;
+ virtual unsigned long Write(const void* pBuf, unsigned long nCount)=0;
+ virtual int GetChar()=0;
+ virtual int PutChar(int c)=0;
+
+ virtual void Abort()=0;
+ virtual void Flush()=0;
+ virtual void Close()=0;
+};
+
+class FileMem : public File
+{
+public:
+// Constructors
+ FileMem();
+ ~FileMem();
+// Implementation
+protected:
// MemFile specific:
unsigned long m_nGrowBytes;
unsigned long m_nPosition;
@@ -27,6 +50,33 @@ protected:
bool m_bAutoDelete;
void GrowFile(unsigned long nNewLen);
+public:
+ unsigned long GetPosition() const;
+ unsigned long Seek(long lOff, int nFrom);
+ void SetLength(unsigned long nNewLen);
+ unsigned long GetLength() const;
+
+ char* ReadString(char* pBuf, unsigned long nMax);
+ unsigned long Read(void* pBuf, unsigned long nCount);
+ unsigned long Write(const void* pBuf, unsigned long nCount);
+ int GetChar();
+ int PutChar(int c);
+
+ void Abort();
+ void Flush();
+ void Close();
+ bool Open(const char *filename, const char *mode);
+};
+
+class FileDisk : public File
+{
+public:
+// Constructors
+ FileDisk();
+ ~FileDisk();
+
+// Implementation
+protected:
// DiscFile specific:
FILE* m_hFile;
bool m_bCloseOnDelete;
@@ -47,17 +97,16 @@ public:
void Flush();
void Close();
bool Open(const char *filename, const char *mode);
+};
+
+
+
+
+
+
+
-public:
-// Attributes
-// CString GetFileName() const;
-// CString GetFileTitle() const;
-// CString GetFilePath() const;
-// void SetFilePath(LPCTSTR lpszNewName);
-protected:
-// CString m_strFileName;
-};
#endif // _FILE_H_
diff --git a/common/image.cpp b/common/image.cpp
index 456c098..90518df 100644
--- a/common/image.cpp
+++ b/common/image.cpp
@@ -157,7 +157,7 @@ LC_IMAGE* OpenImage(char* filename)
return ResizeImage(OpenBMP(filename));
if ((strcmp (ext, "gif") == 0) || (strcmp (ext, "tmp") == 0))
{
- File file(false);
+ FileDisk file;
if (!file.Open(filename, "rb"))
return NULL;
LC_IMAGE* image = ResizeImage(OpenGIF(&file));
@@ -193,7 +193,7 @@ bool SaveImage(char* filename, LC_IMAGE* image, LC_IMAGE_OPTS* opts)
if (strcmp (ext, "gif") == 0)
{
- File file(false);
+ FileDisk file;
if (!file.Open(filename, "wb"))
return false;
diff --git a/common/project.cpp b/common/project.cpp
index 80355bc..fcbffa2 100644
--- a/common/project.cpp
+++ b/common/project.cpp
@@ -328,7 +328,7 @@ bool Project::Initialize(int argc, char *argv[], char* libpath)
// Load the piece library
bool Project::LoadPieceLibrary()
{
- File idx(false);
+ FileDisk idx;
char filename[LC_MAXPATH];
unsigned char version;
unsigned short count, movedcount;
@@ -1281,7 +1281,7 @@ void Project::FileReadLDraw(File* file, Matrix* prevmat, int* nOk, int DefColor,
if (read)
{
- File tf(false);
+ FileDisk tf;
if (tf.Open(pn, "rt"))
FileReadLDraw(&tf, &tmpmat, nOk, cl, nStep);
}
@@ -1315,7 +1315,7 @@ bool Project::DoFileSave()
// if 'bReplace' is FALSE will not change path name (SaveCopyAs)
bool Project::DoSave(char* lpszPathName, bool bReplace)
{
- File file(false);
+ FileDisk file;
char newName[LC_MAXPATH];
memset(newName, 0, sizeof(newName));
if (lpszPathName)
@@ -1516,7 +1516,7 @@ bool Project::OnNewDocument()
bool Project::OnOpenDocument(char* lpszPathName)
{
- File file(false);
+ FileDisk file;
bool bSuccess = false;
if (!file.Open(lpszPathName, "rb"))
@@ -3082,7 +3082,7 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam)
if (SystemDoDialog(LC_DLG_FILE_MERGE, filename))
{
- File file(false);
+ FileDisk file;
if (file.Open(filename, "rb"))
{
// CWaitCursor wait;
@@ -3895,7 +3895,7 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam)
case LC_FILE_LIBRARY:
{
- File file(true);
+ FileMem file;
FileSave(&file, true);
if (SystemDoDialog(LC_DLG_LIBRARY, NULL))
@@ -4016,7 +4016,7 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam)
if (m_pClipboard[m_nCurClipboard] != NULL)
delete m_pClipboard[m_nCurClipboard];
- m_pClipboard[m_nCurClipboard] = new File(true);
+ m_pClipboard[m_nCurClipboard] = new FileMem;
int i = 0;
Piece* pPiece;
@@ -5919,7 +5919,7 @@ void Project::StartTracking(int mode)
{
SystemCaptureMouse();
m_nTracking = mode;
- m_pTrackFile = new File(true);
+ m_pTrackFile = new FileMem;
FileSave(m_pTrackFile, true);
}
diff --git a/common/project.h b/common/project.h
index c00f802..9336133 100644
--- a/common/project.h
+++ b/common/project.h
@@ -26,10 +26,10 @@ class Matrix;
typedef struct UNDOINFO
{
- File file;
+ FileMem file;
char strText[21];
UNDOINFO* pNext;
- UNDOINFO() : file(true) { pNext = NULL; };
+ UNDOINFO() { pNext = NULL; };
} UNDOINFO;
class Project