FileBuffer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. 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 __fastcall TFileBuffer::SetPosition(int value)
  42. {
  43. FMemory->Position = value;
  44. }
  45. //---------------------------------------------------------------------------
  46. int __fastcall TFileBuffer::GetPosition() const
  47. {
  48. return (int)FMemory->Position;
  49. }
  50. //---------------------------------------------------------------------------
  51. void __fastcall TFileBuffer::SetMemory(TMemoryStream * value)
  52. {
  53. if (FMemory != value)
  54. {
  55. if (FMemory) delete FMemory;
  56. FMemory = value;
  57. }
  58. }
  59. //---------------------------------------------------------------------------
  60. DWORD __fastcall TFileBuffer::ReadStream(TStream * Stream, const DWORD Len, bool ForceLen)
  61. {
  62. DWORD Result;
  63. try
  64. {
  65. Size = Position + Len;
  66. // C++5
  67. // FMemory->SetSize(FMemory->Position + Len);
  68. if (ForceLen)
  69. {
  70. Stream->ReadBuffer(Data + Position, Len);
  71. Result = Len;
  72. }
  73. else
  74. {
  75. Result = Stream->Read(Data + Position, Len);
  76. }
  77. if (Result != Len)
  78. {
  79. Size = Size - Len + Result;
  80. }
  81. FMemory->Seek(Len, soCurrent);
  82. }
  83. catch(EReadError &)
  84. {
  85. RaiseLastOSError();
  86. }
  87. return Result;
  88. }
  89. //---------------------------------------------------------------------------
  90. DWORD __fastcall TFileBuffer::LoadStream(TStream * Stream, const DWORD Len, bool ForceLen)
  91. {
  92. FMemory->Seek(0, soFromBeginning);
  93. return ReadStream(Stream, Len, ForceLen);
  94. }
  95. //---------------------------------------------------------------------------
  96. void __fastcall TFileBuffer::Convert(char * Source, char * Dest, int Params,
  97. bool & Token)
  98. {
  99. DebugAssert(strlen(Source) <= 2);
  100. DebugAssert(strlen(Dest) <= 2);
  101. if (FLAGSET(Params, cpRemoveBOM) && (Size >= 3) &&
  102. (memcmp(Data, Bom, sizeof(Bom)) == 0))
  103. {
  104. Delete(0, 3);
  105. }
  106. if (FLAGSET(Params, cpRemoveCtrlZ) && (Size > 0) && ((*(Data + Size - 1)) == '\x1A'))
  107. {
  108. Delete(Size-1, 1);
  109. }
  110. if (strcmp(Source, Dest) == 0)
  111. {
  112. return;
  113. }
  114. char * Ptr = Data;
  115. // one character source EOL
  116. if (!Source[1])
  117. {
  118. bool PrevToken = Token;
  119. Token = false;
  120. for (int Index = 0; Index < Size; Index++)
  121. {
  122. // EOL already in destination format, make sure to pass it unmodified
  123. if ((Index < Size - 1) && (*Ptr == Dest[0]) && (*(Ptr+1) == Dest[1]))
  124. {
  125. Index++;
  126. Ptr++;
  127. }
  128. // last buffer ended with the first char of destination 2-char EOL format,
  129. // which got expanded to full destination format.
  130. // now we got the second char, so get rid of it.
  131. else if ((Index == 0) && PrevToken && (*Ptr == Dest[1]))
  132. {
  133. Delete(Index, 1);
  134. }
  135. // we are ending with the first char of destination 2-char EOL format,
  136. // append the second char and make sure we strip it from the next buffer, if any
  137. else if ((*Ptr == Dest[0]) && (Index == Size - 1) && Dest[1])
  138. {
  139. Token = true;
  140. Insert(Index+1, Dest+1, 1);
  141. Index++;
  142. Ptr = Data + Index;
  143. }
  144. else if (*Ptr == Source[0])
  145. {
  146. *Ptr = Dest[0];
  147. if (Dest[1])
  148. {
  149. Insert(Index+1, Dest+1, 1);
  150. Index++;
  151. Ptr = Data + Index;
  152. }
  153. }
  154. Ptr++;
  155. }
  156. }
  157. // two character source EOL
  158. else
  159. {
  160. int Index;
  161. for (Index = 0; Index < Size - 1; Index++)
  162. {
  163. if ((*Ptr == Source[0]) && (*(Ptr+1) == Source[1]))
  164. {
  165. *Ptr = Dest[0];
  166. if (Dest[1])
  167. {
  168. *(Ptr+1) = Dest[1];
  169. Index++; Ptr++;
  170. }
  171. else
  172. {
  173. Delete(Index+1, 1);
  174. Ptr = Data + Index;
  175. }
  176. }
  177. Ptr++;
  178. }
  179. if ((Index < Size) && (*Ptr == Source[0]))
  180. {
  181. Delete(Index, 1);
  182. }
  183. }
  184. }
  185. //---------------------------------------------------------------------------
  186. void __fastcall TFileBuffer::Convert(TEOLType Source, TEOLType Dest, int Params,
  187. bool & Token)
  188. {
  189. Convert(EOLToStr(Source), EOLToStr(Dest), Params, Token);
  190. }
  191. //---------------------------------------------------------------------------
  192. void __fastcall TFileBuffer::Convert(char * Source, TEOLType Dest, int Params,
  193. bool & Token)
  194. {
  195. Convert(Source, EOLToStr(Dest), Params, Token);
  196. }
  197. //---------------------------------------------------------------------------
  198. void __fastcall TFileBuffer::Convert(TEOLType Source, char * Dest, int Params,
  199. bool & Token)
  200. {
  201. Convert(EOLToStr(Source), Dest, Params, Token);
  202. }
  203. //---------------------------------------------------------------------------
  204. void __fastcall TFileBuffer::Insert(int Index, const char * Buf, int Len)
  205. {
  206. Size += Len;
  207. memmove(Data + Index + Len, Data + Index, Size - Index - Len);
  208. memmove(Data + Index, Buf, Len);
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall TFileBuffer::Delete(int Index, int Len)
  212. {
  213. memmove(Data + Index, Data + Index + Len, Size - Index - Len);
  214. Size -= Len;
  215. }
  216. //---------------------------------------------------------------------------
  217. void __fastcall TFileBuffer::WriteToStream(TStream * Stream, const DWORD Len)
  218. {
  219. try
  220. {
  221. Stream->WriteBuffer(Data + Position, Len);
  222. FMemory->Seek(Len, soCurrent);
  223. }
  224. catch(EWriteError &)
  225. {
  226. RaiseLastOSError();
  227. }
  228. }
  229. //---------------------------------------------------------------------------
  230. void __fastcall TFileBuffer::WriteToOut(TTransferOutEvent OnTransferOut, TObject * Sender, const DWORD Len)
  231. {
  232. OnTransferOut(Sender, Data + Position, Len);
  233. FMemory->Seek(Len, soCurrent);
  234. }
  235. //---------------------------------------------------------------------------
  236. //---------------------------------------------------------------------------
  237. __fastcall TSafeHandleStream::TSafeHandleStream(int AHandle) :
  238. THandleStream(AHandle)
  239. {
  240. }
  241. //---------------------------------------------------------------------------
  242. int __fastcall TSafeHandleStream::Read(void * Buffer, int Count)
  243. {
  244. int Result = FileRead(FHandle, Buffer, Count);
  245. if (Result == -1)
  246. {
  247. RaiseLastOSError();
  248. }
  249. return Result;
  250. }
  251. //---------------------------------------------------------------------------
  252. int __fastcall TSafeHandleStream::Write(const void * Buffer, int Count)
  253. {
  254. int Result = FileWrite(FHandle, Buffer, Count);
  255. if (Result == -1)
  256. {
  257. RaiseLastOSError();
  258. }
  259. return Result;
  260. }
  261. //---------------------------------------------------------------------------
  262. int __fastcall TSafeHandleStream::Read(System::DynamicArray<System::Byte> Buffer, int Offset, int Count)
  263. {
  264. DebugFail(); // untested
  265. int Result = FileRead(FHandle, Buffer, Offset, Count);
  266. if (Result == -1)
  267. {
  268. RaiseLastOSError();
  269. }
  270. return Result;
  271. }
  272. //---------------------------------------------------------------------------
  273. int __fastcall TSafeHandleStream::Write(const System::DynamicArray<System::Byte> Buffer, int Offset, int Count)
  274. {
  275. // This is invoked for example by TIniFileStorage::Flush
  276. int Result = FileWrite(FHandle, Buffer, Offset, Count);
  277. if (Result == -1)
  278. {
  279. RaiseLastOSError();
  280. }
  281. return Result;
  282. }