FileBuffer.cpp 9.4 KB

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