FileBuffer.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "FileBuffer.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. char * __fastcall EOLToStr(TEOLType EOLType)
  10. {
  11. switch (EOLType) {
  12. case eolLF: return "\n";
  13. case eolCRLF: return "\r\n";
  14. case eolCR: return "\r";
  15. default: assert(false); return "";
  16. }
  17. }
  18. //---------------------------------------------------------------------------
  19. __fastcall TFileBuffer::TFileBuffer()
  20. {
  21. FMemory = new TMemoryStream();
  22. FSize = 0;
  23. }
  24. //---------------------------------------------------------------------------
  25. __fastcall TFileBuffer::~TFileBuffer()
  26. {
  27. delete FMemory;
  28. }
  29. //---------------------------------------------------------------------------
  30. void __fastcall TFileBuffer::SetSize(int value)
  31. {
  32. if (FSize != value)
  33. {
  34. FMemory->Size = value;
  35. FSize = value;
  36. }
  37. }
  38. //---------------------------------------------------------------------------
  39. void __fastcall TFileBuffer::SetPosition(int value)
  40. {
  41. FMemory->Position = value;
  42. }
  43. //---------------------------------------------------------------------------
  44. int __fastcall TFileBuffer::GetPosition() const
  45. {
  46. return (int)FMemory->Position;
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall TFileBuffer::SetMemory(TMemoryStream * value)
  50. {
  51. if (FMemory != value)
  52. {
  53. if (FMemory) delete FMemory;
  54. FMemory = value;
  55. }
  56. }
  57. //---------------------------------------------------------------------------
  58. DWORD __fastcall TFileBuffer::ReadStream(TStream * Stream, const DWORD Len, bool ForceLen)
  59. {
  60. DWORD Result;
  61. try
  62. {
  63. Size = Position + Len;
  64. // C++5
  65. // FMemory->SetSize(FMemory->Position + Len);
  66. if (ForceLen)
  67. {
  68. Stream->ReadBuffer(Data + Position, Len);
  69. Result = Len;
  70. }
  71. else
  72. {
  73. Result = Stream->Read(Data + Position, Len);
  74. }
  75. if (Result != Len)
  76. {
  77. Size = Size - Len + Result;
  78. }
  79. FMemory->Seek(Len, soFromCurrent);
  80. }
  81. catch(EReadError &)
  82. {
  83. RaiseLastOSError();
  84. }
  85. return Result;
  86. }
  87. //---------------------------------------------------------------------------
  88. DWORD __fastcall TFileBuffer::LoadStream(TStream * Stream, const DWORD Len, bool ForceLen)
  89. {
  90. FMemory->Seek(0, soFromBeginning);
  91. return ReadStream(Stream, Len, ForceLen);
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TFileBuffer::Convert(char * Source, char * Dest, int Params,
  95. bool & Token)
  96. {
  97. assert(strlen(Source) <= 2);
  98. assert(strlen(Dest) <= 2);
  99. if (FLAGSET(Params, cpRemoveBOM) && (Size >= 3) &&
  100. (memcmp(Data, Bom, sizeof(Bom)) == 0))
  101. {
  102. Delete(0, 3);
  103. }
  104. if (FLAGSET(Params, cpRemoveCtrlZ) && (Size > 0) && ((*(Data + Size - 1)) == '\x1A'))
  105. {
  106. Delete(Size-1, 1);
  107. }
  108. if (strcmp(Source, Dest) == 0)
  109. {
  110. return;
  111. }
  112. char * Ptr = Data;
  113. // one character source EOL
  114. if (!Source[1])
  115. {
  116. bool PrevToken = Token;
  117. Token = false;
  118. for (int Index = 0; Index < Size; Index++)
  119. {
  120. // EOL already in destination format, make sure to pass it unmodified
  121. if ((Index < Size - 1) && (*Ptr == Dest[0]) && (*(Ptr+1) == Dest[1]))
  122. {
  123. Index++;
  124. Ptr++;
  125. }
  126. // last buffer ended with the first char of destination 2-char EOL format,
  127. // which got expanded to full destination format.
  128. // now we got the second char, so get rid of it.
  129. else if ((Index == 0) && PrevToken && (*Ptr == Dest[1]))
  130. {
  131. Delete(Index, 1);
  132. }
  133. // we are ending with the first char of destination 2-char EOL format,
  134. // append the second char and make sure we strip it from the next buffer, if any
  135. else if ((*Ptr == Dest[0]) && (Index == Size - 1) && Dest[1])
  136. {
  137. Token = true;
  138. Insert(Index+1, Dest+1, 1);
  139. Index++;
  140. Ptr = Data + Index;
  141. }
  142. else if (*Ptr == Source[0])
  143. {
  144. *Ptr = Dest[0];
  145. if (Dest[1])
  146. {
  147. Insert(Index+1, Dest+1, 1);
  148. Index++;
  149. Ptr = Data + Index;
  150. }
  151. }
  152. Ptr++;
  153. }
  154. }
  155. // two character source EOL
  156. else
  157. {
  158. int Index;
  159. for (Index = 0; Index < Size - 1; Index++)
  160. {
  161. if ((*Ptr == Source[0]) && (*(Ptr+1) == Source[1]))
  162. {
  163. *Ptr = Dest[0];
  164. if (Dest[1])
  165. {
  166. *(Ptr+1) = Dest[1];
  167. Index++; Ptr++;
  168. }
  169. else
  170. {
  171. Delete(Index+1, 1);
  172. Ptr = Data + Index;
  173. }
  174. }
  175. Ptr++;
  176. }
  177. if ((Index < Size) && (*Ptr == Source[0]))
  178. {
  179. Delete(Index, 1);
  180. }
  181. }
  182. }
  183. //---------------------------------------------------------------------------
  184. void __fastcall TFileBuffer::Convert(TEOLType Source, TEOLType Dest, int Params,
  185. bool & Token)
  186. {
  187. Convert(EOLToStr(Source), EOLToStr(Dest), Params, Token);
  188. }
  189. //---------------------------------------------------------------------------
  190. void __fastcall TFileBuffer::Convert(char * Source, TEOLType Dest, int Params,
  191. bool & Token)
  192. {
  193. Convert(Source, EOLToStr(Dest), Params, Token);
  194. }
  195. //---------------------------------------------------------------------------
  196. void __fastcall TFileBuffer::Convert(TEOLType Source, char * Dest, int Params,
  197. bool & Token)
  198. {
  199. Convert(EOLToStr(Source), Dest, Params, Token);
  200. }
  201. //---------------------------------------------------------------------------
  202. void __fastcall TFileBuffer::Insert(int Index, const char * Buf, int Len)
  203. {
  204. Size += Len;
  205. memmove(Data + Index + Len, Data + Index, Size - Index - Len);
  206. memmove(Data + Index, Buf, Len);
  207. }
  208. //---------------------------------------------------------------------------
  209. void __fastcall TFileBuffer::Delete(int Index, int Len)
  210. {
  211. memmove(Data + Index, Data + Index + Len, Size - Index - Len);
  212. Size -= Len;
  213. }
  214. //---------------------------------------------------------------------------
  215. void __fastcall TFileBuffer::WriteToStream(TStream * Stream, const DWORD Len)
  216. {
  217. try
  218. {
  219. Stream->WriteBuffer(Data + Position, Len);
  220. FMemory->Seek(Len, soFromCurrent);
  221. }
  222. catch(EWriteError &)
  223. {
  224. RaiseLastOSError();
  225. }
  226. }
  227. //---------------------------------------------------------------------------
  228. //---------------------------------------------------------------------------
  229. __fastcall TSafeHandleStream::TSafeHandleStream(int AHandle) :
  230. THandleStream(AHandle)
  231. {
  232. }
  233. //---------------------------------------------------------------------------
  234. int __fastcall TSafeHandleStream::Read(void * Buffer, int Count)
  235. {
  236. int Result = FileRead(FHandle, Buffer, Count);
  237. if (Result == -1)
  238. {
  239. RaiseLastOSError();
  240. }
  241. return Result;
  242. }
  243. //---------------------------------------------------------------------------
  244. int __fastcall TSafeHandleStream::Write(const void * Buffer, int Count)
  245. {
  246. int Result = FileWrite(FHandle, Buffer, Count);
  247. if (Result == -1)
  248. {
  249. RaiseLastOSError();
  250. }
  251. return Result;
  252. }