1
0

FileBuffer.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "FileBuffer.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. const wchar_t * EOLTypeNames = L"LF;CRLF;CR";
  10. //---------------------------------------------------------------------------
  11. const char * __fastcall EOLToStr(TEOLType EOLType)
  12. {
  13. switch (EOLType) {
  14. case eolLF: return "\n";
  15. case eolCRLF: return "\r\n";
  16. case eolCR: return "\r";
  17. default: DebugFail(); return "";
  18. }
  19. }
  20. //---------------------------------------------------------------------------
  21. __fastcall TFileBuffer::TFileBuffer()
  22. {
  23. FMemory = new TMemoryStream();
  24. FSize = 0;
  25. }
  26. //---------------------------------------------------------------------------
  27. __fastcall TFileBuffer::~TFileBuffer()
  28. {
  29. delete FMemory;
  30. }
  31. //---------------------------------------------------------------------------
  32. void __fastcall TFileBuffer::SetSize(int value)
  33. {
  34. if (FSize != value)
  35. {
  36. FMemory->Size = value;
  37. FSize = value;
  38. }
  39. }
  40. //---------------------------------------------------------------------------
  41. void TFileBuffer::Reset()
  42. {
  43. FMemory->Position = 0;
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall TFileBuffer::ProcessRead(DWORD Len, DWORD Result)
  47. {
  48. if (Result != Len)
  49. {
  50. Size = Size - Len + Result;
  51. }
  52. FMemory->Seek(Result, soCurrent);
  53. }
  54. //---------------------------------------------------------------------------
  55. void TFileBuffer::NeedSpace(DWORD Len)
  56. {
  57. Size = GetPosition() + Len;
  58. }
  59. //---------------------------------------------------------------------------
  60. DWORD __fastcall TFileBuffer::ReadStream(TStream * Stream, const DWORD Len, bool ForceLen)
  61. {
  62. DWORD Result;
  63. try
  64. {
  65. NeedSpace(Len);
  66. if (ForceLen)
  67. {
  68. Stream->ReadBuffer(GetPointer(), Len);
  69. Result = Len;
  70. }
  71. else
  72. {
  73. Result = Stream->Read(GetPointer(), Len);
  74. }
  75. ProcessRead(Len, Result);
  76. }
  77. catch(EReadError &)
  78. {
  79. RaiseLastOSError();
  80. }
  81. return Result;
  82. }
  83. //---------------------------------------------------------------------------
  84. DWORD __fastcall TFileBuffer::LoadStream(TStream * Stream, const DWORD Len, bool ForceLen)
  85. {
  86. FMemory->Seek(0, soFromBeginning);
  87. return ReadStream(Stream, Len, ForceLen);
  88. }
  89. //---------------------------------------------------------------------------
  90. DWORD __fastcall TFileBuffer::LoadFromIn(TTransferInEvent OnTransferIn, TObject * Sender, DWORD Len)
  91. {
  92. FMemory->Seek(0, soFromBeginning);
  93. DebugAssert(GetPosition() == 0);
  94. NeedSpace(Len);
  95. size_t Result = OnTransferIn(Sender, reinterpret_cast<unsigned char *>(GetPointer()), Len);
  96. ProcessRead(Len, Result);
  97. return Result;
  98. }
  99. //---------------------------------------------------------------------------
  100. void __fastcall TFileBuffer::Convert(const char * Source, const char * Dest, int Params,
  101. bool & Token)
  102. {
  103. DebugAssert(strlen(Source) <= 2);
  104. DebugAssert(strlen(Dest) <= 2);
  105. if (FLAGSET(Params, cpRemoveBOM) && (Size >= 3) &&
  106. (memcmp(Data, Bom, strlen(Bom)) == 0))
  107. {
  108. Delete(0, 3);
  109. }
  110. if (FLAGSET(Params, cpRemoveCtrlZ) && (Size > 0) && ((*(Data + Size - 1)) == '\x1A'))
  111. {
  112. Delete(Size-1, 1);
  113. }
  114. if (strcmp(Source, Dest) == 0)
  115. {
  116. return;
  117. }
  118. char * Ptr = Data;
  119. // one character source EOL
  120. if (!Source[1])
  121. {
  122. bool PrevToken = Token;
  123. Token = false;
  124. for (int Index = 0; Index < Size; Index++)
  125. {
  126. // EOL already in destination format, make sure to pass it unmodified
  127. if ((Index < Size - 1) && (*Ptr == Dest[0]) && (*(Ptr+1) == Dest[1]))
  128. {
  129. Index++;
  130. Ptr++;
  131. }
  132. // last buffer ended with the first char of destination 2-char EOL format,
  133. // which got expanded to full destination format.
  134. // now we got the second char, so get rid of it.
  135. else if ((Index == 0) && PrevToken && (*Ptr == Dest[1]))
  136. {
  137. Delete(Index, 1);
  138. Index--;
  139. Ptr = Data + Index;
  140. }
  141. // we are ending with the first char of destination 2-char EOL format,
  142. // append the second char and make sure we strip it from the next buffer, if any
  143. else if ((*Ptr == Dest[0]) && (Index == Size - 1) && Dest[1])
  144. {
  145. Token = true;
  146. Insert(Index+1, Dest+1, 1);
  147. Index++;
  148. Ptr = Data + Index;
  149. }
  150. else if (*Ptr == Source[0])
  151. {
  152. *Ptr = Dest[0];
  153. if (Dest[1])
  154. {
  155. Insert(Index+1, Dest+1, 1);
  156. Index++;
  157. Ptr = Data + Index;
  158. }
  159. }
  160. Ptr++;
  161. }
  162. }
  163. // two character source EOL
  164. else
  165. {
  166. int Index;
  167. for (Index = 0; Index < Size - 1; Index++)
  168. {
  169. if ((*Ptr == Source[0]) && (*(Ptr+1) == Source[1]))
  170. {
  171. *Ptr = Dest[0];
  172. if (Dest[1])
  173. {
  174. *(Ptr+1) = Dest[1];
  175. Index++; Ptr++;
  176. }
  177. else
  178. {
  179. Delete(Index+1, 1);
  180. Ptr = Data + Index;
  181. }
  182. }
  183. Ptr++;
  184. }
  185. if ((Index < Size) && (*Ptr == Source[0]))
  186. {
  187. Delete(Index, 1);
  188. }
  189. }
  190. }
  191. //---------------------------------------------------------------------------
  192. void __fastcall TFileBuffer::Convert(TEOLType Source, TEOLType Dest, int Params,
  193. bool & Token)
  194. {
  195. Convert(EOLToStr(Source), EOLToStr(Dest), Params, Token);
  196. }
  197. //---------------------------------------------------------------------------
  198. void __fastcall TFileBuffer::Convert(const char * Source, TEOLType Dest, int Params,
  199. bool & Token)
  200. {
  201. Convert(Source, EOLToStr(Dest), Params, Token);
  202. }
  203. //---------------------------------------------------------------------------
  204. void __fastcall TFileBuffer::Convert(TEOLType Source, const char * Dest, int Params,
  205. bool & Token)
  206. {
  207. Convert(EOLToStr(Source), Dest, Params, Token);
  208. }
  209. //---------------------------------------------------------------------------
  210. void __fastcall TFileBuffer::Insert(int Index, const char * Buf, int Len)
  211. {
  212. Size += Len;
  213. memmove(Data + Index + Len, Data + Index, Size - Index - Len);
  214. memmove(Data + Index, Buf, Len);
  215. }
  216. //---------------------------------------------------------------------------
  217. void __fastcall TFileBuffer::Delete(int Index, int Len)
  218. {
  219. memmove(Data + Index, Data + Index + Len, Size - Index - Len);
  220. Size -= Len;
  221. }
  222. //---------------------------------------------------------------------------
  223. void __fastcall TFileBuffer::WriteToStream(TStream * Stream, const DWORD Len)
  224. {
  225. try
  226. {
  227. Stream->WriteBuffer(GetPointer(), Len);
  228. FMemory->Seek(Len, soCurrent);
  229. }
  230. catch(EWriteError &)
  231. {
  232. RaiseLastOSError();
  233. }
  234. }
  235. //---------------------------------------------------------------------------
  236. void __fastcall TFileBuffer::WriteToOut(TTransferOutEvent OnTransferOut, TObject * Sender, const DWORD Len)
  237. {
  238. OnTransferOut(Sender, reinterpret_cast<const unsigned char *>(GetPointer()), Len);
  239. FMemory->Seek(Len, soCurrent);
  240. }
  241. //---------------------------------------------------------------------------
  242. //---------------------------------------------------------------------------
  243. __fastcall TSafeHandleStream::TSafeHandleStream(int AHandle) :
  244. THandleStream(AHandle),
  245. FSource(NULL)
  246. {
  247. }
  248. //---------------------------------------------------------------------------
  249. __fastcall TSafeHandleStream::TSafeHandleStream(THandleStream * Source, bool Own) :
  250. THandleStream(Source->Handle)
  251. {
  252. FSource = Own ? Source : NULL;
  253. }
  254. //---------------------------------------------------------------------------
  255. TSafeHandleStream * TSafeHandleStream::CreateFromFile(const UnicodeString & FileName, unsigned short Mode)
  256. {
  257. return new TSafeHandleStream(new TFileStream(ApiPath(FileName), Mode), true);
  258. }
  259. //---------------------------------------------------------------------------
  260. __fastcall TSafeHandleStream::~TSafeHandleStream()
  261. {
  262. SAFE_DESTROY(FSource);
  263. }
  264. //---------------------------------------------------------------------------
  265. int __fastcall TSafeHandleStream::Read(void * Buffer, int Count)
  266. {
  267. int Result = FileRead(FHandle, Buffer, Count);
  268. if (Result == -1)
  269. {
  270. RaiseLastOSError();
  271. }
  272. return Result;
  273. }
  274. //---------------------------------------------------------------------------
  275. int __fastcall TSafeHandleStream::Write(const void * Buffer, int Count)
  276. {
  277. int Result = FileWrite(FHandle, Buffer, Count);
  278. if (Result == -1)
  279. {
  280. RaiseLastOSError();
  281. }
  282. return Result;
  283. }
  284. //---------------------------------------------------------------------------
  285. int __fastcall TSafeHandleStream::Read(System::DynamicArray<System::Byte> Buffer, int Offset, int Count)
  286. {
  287. // This is invoked for example via CopyFrom from TParallelOperation::Done
  288. int Result = FileRead(FHandle, Buffer, Offset, Count);
  289. if (Result == -1)
  290. {
  291. RaiseLastOSError();
  292. }
  293. return Result;
  294. }
  295. //---------------------------------------------------------------------------
  296. int __fastcall TSafeHandleStream::Write(const System::DynamicArray<System::Byte> Buffer, int Offset, int Count)
  297. {
  298. // This is invoked for example by TIniFileStorage::Flush or via CopyFrom from TParallelOperation::Done
  299. int Result = FileWrite(FHandle, Buffer, Offset, Count);
  300. if (Result == -1)
  301. {
  302. RaiseLastOSError();
  303. }
  304. return Result;
  305. }