FileBuffer.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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, "\xEF\xBB\xBF", 3) == 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. // Disabled, not worth risking it is not safe enough, for the bugfix release
  117. #if 0
  118. bool PrevToken = Token;
  119. Token = false;
  120. #endif
  121. for (int Index = 0; Index < Size; Index++)
  122. {
  123. // EOL already in wanted format, make sure to pass unmodified
  124. if ((Index < Size - 1) && (*Ptr == Dest[0]) && (*(Ptr+1) == Dest[1]))
  125. {
  126. Index++;
  127. Ptr++;
  128. }
  129. #if 0
  130. // last buffer ended with the first char of wanted EOL format,
  131. // which got expanded to wanted format.
  132. // now we got the second char, so get rid of it.
  133. else if ((Index == 0) && PrevToken && (*Ptr == Dest[1]))
  134. {
  135. Delete(Index, 1);
  136. }
  137. #endif
  138. else if (*Ptr == Source[0])
  139. {
  140. #if 0
  141. if ((*Ptr == Dest[0]) && (Index == Size - 1))
  142. {
  143. Token = true;
  144. }
  145. #endif
  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, soFromCurrent);
  223. }
  224. catch(EWriteError &)
  225. {
  226. RaiseLastOSError();
  227. }
  228. }
  229. //---------------------------------------------------------------------------
  230. //---------------------------------------------------------------------------
  231. __fastcall TSafeHandleStream::TSafeHandleStream(int AHandle) :
  232. THandleStream(AHandle)
  233. {
  234. }
  235. //---------------------------------------------------------------------------
  236. int __fastcall TSafeHandleStream::Read(void * Buffer, int Count)
  237. {
  238. int Result = FileRead(FHandle, Buffer, Count);
  239. if (Result == -1)
  240. {
  241. RaiseLastOSError();
  242. }
  243. return Result;
  244. }
  245. //---------------------------------------------------------------------------
  246. int __fastcall TSafeHandleStream::Write(const void * Buffer, int Count)
  247. {
  248. int Result = FileWrite(FHandle, Buffer, Count);
  249. if (Result == -1)
  250. {
  251. RaiseLastOSError();
  252. }
  253. return Result;
  254. };