leocad/common/lc_file.cpp

280 lines
4.4 KiB
C++
Raw Normal View History

#include "lc_global.h"
2011-09-07 23:06:51 +02:00
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <string.h>
#include "lc_file.h"
2011-09-07 23:06:51 +02:00
// =============================================================================
2012-03-23 00:44:56 +01:00
// lcFile
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
lcFile::lcFile()
2011-09-07 23:06:51 +02:00
{
}
2012-03-23 00:44:56 +01:00
lcFile::~lcFile()
2011-09-07 23:06:51 +02:00
{
}
2012-03-23 00:44:56 +01:00
// =============================================================================
// lcMemFile
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
lcMemFile::lcMemFile()
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
mGrowBytes = 1024;
mPosition = 0;
mBufferSize = 0;
mFileSize = 0;
mBuffer = NULL;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
lcMemFile::~lcMemFile()
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
Close();
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
void lcMemFile::Seek(long Offset, int From)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
if (From == SEEK_SET)
mPosition = Offset;
else if (From == SEEK_CUR)
mPosition += Offset;
else if (From == SEEK_END)
mPosition = mFileSize + Offset;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
long lcMemFile::GetPosition() const
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
return mPosition;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
void lcMemFile::SetLength(size_t NewLength)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
if (NewLength > mBufferSize)
GrowFile(NewLength);
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
if (NewLength < mPosition)
mPosition = NewLength;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
mFileSize = NewLength;
2011-09-07 23:06:51 +02:00
}
size_t lcMemFile::GetLength() const
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
return mFileSize;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
void lcMemFile::Flush()
2011-09-07 23:06:51 +02:00
{
}
2012-03-23 00:44:56 +01:00
void lcMemFile::Close()
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
if (!mBuffer)
return;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
mPosition = 0;
mBufferSize = 0;
mFileSize = 0;
free(mBuffer);
mBuffer = NULL;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
size_t lcMemFile::ReadBuffer(void* Buffer, long Bytes)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
if (Bytes == 0 || mPosition > mFileSize)
return 0;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
size_t BytesToRead;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
if (mPosition + Bytes > mFileSize)
BytesToRead = mFileSize - mPosition;
else
BytesToRead = Bytes;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
memcpy(Buffer, mBuffer + mPosition, BytesToRead);
mPosition += BytesToRead;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
return BytesToRead;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
size_t lcMemFile::WriteBuffer(const void* Buffer, long Bytes)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
if (Bytes == 0)
return 0;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
if (mPosition + Bytes > mBufferSize)
GrowFile(mPosition + Bytes);
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
memcpy(mBuffer + mPosition, Buffer, Bytes);
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
mPosition += Bytes;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
if (mPosition > mFileSize)
mFileSize = mPosition;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
return Bytes;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
void lcMemFile::GrowFile(size_t NewLength)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
if (NewLength <= mBufferSize)
return;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
NewLength = ((NewLength + mGrowBytes - 1) / mGrowBytes) * mGrowBytes;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
if (mBuffer != NULL)
mBuffer = (unsigned char*)realloc(mBuffer, NewLength);
else
mBuffer = (unsigned char*)malloc(NewLength);
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
mBufferSize = NewLength;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
char* lcMemFile::ReadLine(char* Buffer, size_t BufferSize)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
int BytesRead = 0;
unsigned char ch;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
if (BufferSize == 0)
return NULL;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
if (mPosition >= mFileSize)
return NULL;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
while ((--BufferSize))
{
if (mPosition == mFileSize)
break;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
ch = mBuffer[mPosition];
mPosition++;
Buffer[BytesRead++] = ch;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
if (ch == '\n')
break;
}
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
Buffer[BytesRead] = 0;
return Buffer;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
void lcMemFile::CopyFrom(lcFile& Source)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
size_t Length = Source.GetLength();
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
SetLength(Length);
Seek(0, SEEK_SET);
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
Source.Seek(0, SEEK_SET);
Source.ReadBuffer(mBuffer, Length);
2011-09-07 23:06:51 +02:00
}
2014-09-11 21:55:34 +02:00
void lcMemFile::CopyFrom(lcMemFile& Source)
{
size_t Length = Source.GetLength();
SetLength(Length);
Seek(0, SEEK_SET);
Source.Seek(0, SEEK_SET);
Source.ReadBuffer(mBuffer, Length);
}
2012-03-23 00:44:56 +01:00
// =============================================================================
// lcDiskFile
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
lcDiskFile::lcDiskFile()
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
mFile = NULL;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
lcDiskFile::~lcDiskFile()
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
Close();
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
long lcDiskFile::GetPosition() const
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
return ftell(mFile);
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
void lcDiskFile::Seek(long Offset, int From)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
fseek(mFile, Offset, From);
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
void lcDiskFile::SetLength(size_t NewLength)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
fseek(mFile, NewLength, SEEK_SET);
2011-09-07 23:06:51 +02:00
}
size_t lcDiskFile::GetLength() const
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
long Length, Current;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
Current = ftell(mFile);
fseek(mFile, 0, SEEK_END);
Length = ftell(mFile);
fseek(mFile, Current, SEEK_SET);
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
return Length;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
void lcDiskFile::Flush()
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
if (mFile == NULL)
return;
fflush(mFile);
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
void lcDiskFile::Close()
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
if (mFile == NULL)
return;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
fclose(mFile);
mFile = NULL;
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
size_t lcDiskFile::ReadBuffer(void* pBuf, long Bytes)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
return fread(pBuf, 1, Bytes, mFile);
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
size_t lcDiskFile::WriteBuffer(const void* pBuf, long Bytes)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
return fwrite(pBuf, 1, Bytes, mFile);
2011-09-07 23:06:51 +02:00
}
bool lcDiskFile::Open(const QString& FileName, const char* Mode)
{
return Open(FileName.toLatin1().constData(), Mode); // todo: qstring
}
2012-03-23 00:44:56 +01:00
bool lcDiskFile::Open(const char* FileName, const char* Mode)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
if (*FileName == 0)
return false;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
Close();
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
mFile = fopen(FileName, Mode);
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
return (mFile != NULL);
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
char* lcDiskFile::ReadLine(char* Buffer, size_t BufferSize)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
return fgets(Buffer, BufferSize, mFile);
2011-09-07 23:06:51 +02:00
}
2012-03-23 00:44:56 +01:00
void lcDiskFile::CopyFrom(lcMemFile& Source)
2011-09-07 23:06:51 +02:00
{
2012-03-23 00:44:56 +01:00
size_t Length = Source.GetLength();
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
Seek(0, SEEK_SET);
WriteBuffer(Source.mBuffer, Length);
2011-09-07 23:06:51 +02:00
}