📄 stream.cpp
字号:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Stream.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
void* __fastcall TCNStream::GlobalAllocPtr(UINT Flag,DWORD Bytes)
{
return GlobalLock(GlobalAlloc(Flag,Bytes));
}
void* __fastcall TCNStream::GlobalReAllocPtr(void *Ptr,DWORD Bytes,UINT Flag)
{
HGLOBAL hMem;
hMem = GlobalHandle(Ptr);
GlobalUnlock(hMem);
hMem = GlobalReAlloc(hMem,Bytes,Flag);
return GlobalLock(hMem);
}
HGLOBAL __fastcall TCNStream::GlobalFreePtr(void *Ptr)
{
HGLOBAL hMem;
hMem = GlobalHandle(Ptr);
GlobalUnlock(hMem);
hMem = GlobalFree(hMem);
return hMem;
}
int __fastcall TCNStream::GetPosition(void)
{
return Seek(0,1);
}
void __fastcall TCNStream::SetPosition(int Pos)
{
Seek(Pos,0);
}
int __fastcall TCNStream::GetSize(void)
{
int Pos;
int Size;
Pos = Seek(0,1);
Size = Seek(0,2);
Seek(Pos,0);
return Size;
}
void __fastcall TCNStream::SetSize(int NewSize)
{
}
void __fastcall TCNStream::ReadBuffer(void *Buffer, int Count)
{
if( (Count != 0) && (Read(Buffer,Count) != Count) )
{
//throw error
}
}
void __fastcall TCNStream::WriteBuffer(const void *Buffer, int Count)
{
if( (Count != 0) && (Write(Buffer,Count) != Count) )
{
//throw error
}
}
int __fastcall TCNStream::CopyFrom(TCNStream* Source, int Count)
{
int BufSize,N;
char *Buffer;
int Result;
if( Count == 0)
{
Source->SetPosition(0);
Count = Source->GetSize();
}
Result = Count;
if( Count > 0xF000)
BufSize = 0xF000;
else BufSize = Count;
Buffer = new char[BufSize];
__try{
while( Count != 0)
{
if( Count > BufSize )
N = BufSize;
else N = Count;
Source->ReadBuffer(Buffer, N);
WriteBuffer(Buffer,N);
Count -= N;
}
}
__finally{
delete Buffer;
}
return Result;
}
void __fastcall TCNCustomMemoryStream::SetPointer(void * Ptr, int Size)
{
FMemory = Ptr;
FSize = Size;
}
int __fastcall TCNCustomMemoryStream::Read(void *Buffer, int Count)
{
int Result;
if( (FPosition >= 0) && (Count >= 0))
{
Result = FSize - FPosition;
if( Result > 0)
{
if( Result > Count ) Result = Count;
memcpy((char *)Buffer,(char *)FMemory + FPosition, Result);
FPosition += Result;
return Result;
}
}
return 0;
}
int __fastcall TCNCustomMemoryStream::Seek(int Offset, WORD Origin)
{
switch(Origin)
{
case 0: FPosition = Offset;break;
case 1: FPosition += Offset;break;
case 2: FPosition = FSize + Offset;break;
}
return FPosition;
}
void __fastcall TCNCustomMemoryStream::SaveToStream(TCNStream* Stream)
{
if (FSize != 0) Stream->WriteBuffer(FMemory, FSize);
}
__fastcall TCNMemoryStream::~TCNMemoryStream(void)
{
Clear();
}
void __fastcall TCNMemoryStream::Clear(void)
{
SetCapacity(0);
FSize = 0;
FPosition = 0;
}
void __fastcall TCNMemoryStream::LoadFromStream(TCNStream* Stream)
{
int Count;
Stream->SetPosition(0);
Count = Stream->GetSize();
SetSize(Count);
if( Count != 0) Stream->ReadBuffer(FMemory,Count);
}
void __fastcall TCNMemoryStream::SetCapacity(int NewCapacity)
{
SetPointer(Realloc(NewCapacity), FSize);
FCapacity = NewCapacity;
}
void __fastcall TCNMemoryStream::SetSize(int NewSize)
{
int OldPosition;
OldPosition = FPosition;
SetCapacity(NewSize);
FSize = NewSize;
if (OldPosition > NewSize) Seek(0,2);
}
void * __fastcall TCNMemoryStream::Realloc(int &NewCapacity)
{
void *Result;
if(NewCapacity > 0 )
{
// NewCapacity = (NewCapacity + (MemoryDelta - 1)) and not (MemoryDelta - 1);
}
Result = FMemory;
if (NewCapacity != FCapacity)
{
if( NewCapacity == 0)
{
GlobalFreePtr(FMemory);
FMemory = NULL;
}
else
{
if( FCapacity == 0)
{
Result = GlobalAllocPtr(GMEM_MOVEABLE,NewCapacity);
}
else
{
Result = GlobalReAllocPtr(FMemory,NewCapacity,GMEM_MOVEABLE);
}
}
}
return Result;
}
int __fastcall TCNMemoryStream::Write(const void *Buffer, int Count)
{
int Pos;
if( (FPosition >= 0) && (Count >= 0))
{
Pos = FPosition + Count;
if( Pos > 0)
{
if( Pos > FSize)
{
if( Pos > FCapacity)
{
SetCapacity(Pos);
FSize = Pos;
}
}
memcpy((char *)FMemory + FPosition,Buffer, Count);
FPosition = Pos;
return Count;
}
}
return 0;
}
//////////////////////////////////////////////////////////////////////
int __fastcall TCNHandleStream::Read(void *Buffer, int Count)
{
long Result;
Result = FileRead(FHandle, Buffer, Count);
if( Result == -1) Result = 0;
return Result;
}
int __fastcall TCNHandleStream::Write(const void *Buffer, int Count)
{
long Result;
Result = FileWrite(FHandle, (char *)Buffer, Count);
if( Result == -1) Result = 0;
return Result;
}
int __fastcall TCNHandleStream::Seek(int Offset, WORD Origin)
{
return FileSeek(FHandle, Offset, Origin);
}
//***************TCNFileStream*************************
TCNFileStream::TCNFileStream(TCNString FileName, WORD Mode)
{
if( Mode == cfmCreate)
{
FHandle = FileCreate(FileName);
}
else
{
FHandle = FileOpen(FileName, Mode);
}
}
TCNFileStream::~TCNFileStream(void)
{
//TODO: Add your source code here
if( FHandle != NULL) FileClose(FHandle);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -