leocad/common/lc_file.cpp

150 lines
2.3 KiB
C++
Raw Normal View History

#include "lc_global.h"
2011-09-07 23:06:51 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "lc_file.h"
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 = nullptr;
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
}
2019-05-18 20:33:27 +02:00
void lcMemFile::Seek(qint64 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
{
2016-03-06 21:19:02 +01:00
return (long)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::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 = nullptr;
2011-09-07 23:06:51 +02:00
}
2016-02-17 00:11:52 +01:00
size_t lcMemFile::ReadBuffer(void* Buffer, size_t 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
}
2016-02-17 00:11:52 +01:00
size_t lcMemFile::WriteBuffer(const void* Buffer, size_t 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
2017-04-03 02:15:09 +02:00
if (mBuffer)
{
unsigned char* NewBuffer = (unsigned char*)realloc(mBuffer, NewLength);
if (!NewBuffer)
return;
mBuffer = NewBuffer;
}
2012-03-23 00:44:56 +01:00
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 nullptr;
2011-09-07 23:06:51 +02:00
2012-03-23 00:44:56 +01:00
if (mPosition >= mFileSize)
return nullptr;
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
}