FileBuffer.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. {
  96. assert(strlen(Source) <= 2);
  97. assert(strlen(Dest) <= 2);
  98. if (FLAGSET(Params, cpRemoveBOM) && (Size >= 3) &&
  99. (memcmp(Data, "\xEF\xBB\xBF", 3) == 0))
  100. {
  101. Delete(0, 3);
  102. }
  103. if (FLAGSET(Params, cpRemoveCtrlZ) && (Size > 0) && ((*(Data + Size - 1)) == '\x1A'))
  104. {
  105. Delete(Size-1, 1);
  106. }
  107. if (strcmp(Source, Dest) == 0)
  108. {
  109. return;
  110. }
  111. char * Ptr = Data;
  112. // one character source EOL
  113. if (!Source[1])
  114. {
  115. for (int Index = 0; Index < Size; Index++)
  116. {
  117. // EOL already in wanted format, make sure to pass unmodified
  118. if ((Index < Size - 1) && (*Ptr == Dest[0]) && (*(Ptr+1) == Dest[1]))
  119. {
  120. Index++;
  121. Ptr++;
  122. }
  123. else if (*Ptr == Source[0])
  124. {
  125. *Ptr = Dest[0];
  126. if (Dest[1])
  127. {
  128. Insert(Index+1, Dest+1, 1);
  129. Index++;
  130. Ptr = Data + Index;
  131. }
  132. }
  133. Ptr++;
  134. }
  135. }
  136. // two character source EOL
  137. else
  138. {
  139. int Index;
  140. for (Index = 0; Index < Size - 1; Index++)
  141. {
  142. if ((*Ptr == Source[0]) && (*(Ptr+1) == Source[1]))
  143. {
  144. *Ptr = Dest[0];
  145. if (Dest[1])
  146. {
  147. *(Ptr+1) = Dest[1];
  148. Index++; Ptr++;
  149. }
  150. else
  151. {
  152. Delete(Index+1, 1);
  153. Ptr = Data + Index;
  154. }
  155. }
  156. Ptr++;
  157. }
  158. if ((Index < Size) && (*Ptr == Source[0]))
  159. {
  160. Delete(Index, 1);
  161. }
  162. }
  163. }
  164. //---------------------------------------------------------------------------
  165. void __fastcall TFileBuffer::Convert(TEOLType Source, TEOLType Dest, int Params)
  166. {
  167. Convert(EOLToStr(Source), EOLToStr(Dest), Params);
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TFileBuffer::Convert(char * Source, TEOLType Dest, int Params)
  171. {
  172. Convert(Source, EOLToStr(Dest), Params);
  173. }
  174. //---------------------------------------------------------------------------
  175. void __fastcall TFileBuffer::Convert(TEOLType Source, char * Dest, int Params)
  176. {
  177. Convert(EOLToStr(Source), Dest, Params);
  178. }
  179. //---------------------------------------------------------------------------
  180. void __fastcall TFileBuffer::Insert(int Index, const char * Buf, int Len)
  181. {
  182. Size += Len;
  183. memmove(Data + Index + Len, Data + Index, Size - Index - Len);
  184. memmove(Data + Index, Buf, Len);
  185. }
  186. //---------------------------------------------------------------------------
  187. void __fastcall TFileBuffer::Delete(int Index, int Len)
  188. {
  189. memmove(Data + Index, Data + Index + Len, Size - Index - Len);
  190. Size -= Len;
  191. }
  192. //---------------------------------------------------------------------------
  193. void __fastcall TFileBuffer::WriteToStream(TStream * Stream, const DWORD Len)
  194. {
  195. try
  196. {
  197. Stream->WriteBuffer(Data + Position, Len);
  198. FMemory->Seek(Len, soFromCurrent);
  199. }
  200. catch(EWriteError &)
  201. {
  202. RaiseLastOSError();
  203. }
  204. }
  205. //---------------------------------------------------------------------------
  206. //---------------------------------------------------------------------------
  207. __fastcall TSafeHandleStream::TSafeHandleStream(int AHandle) :
  208. THandleStream(AHandle)
  209. {
  210. }
  211. //---------------------------------------------------------------------------
  212. int __fastcall TSafeHandleStream::Read(void * Buffer, int Count)
  213. {
  214. int Result = FileRead(FHandle, Buffer, Count);
  215. if (Result == -1)
  216. {
  217. RaiseLastOSError();
  218. }
  219. return Result;
  220. }
  221. //---------------------------------------------------------------------------
  222. int __fastcall TSafeHandleStream::Write(const void * Buffer, int Count)
  223. {
  224. int Result = FileWrite(FHandle, Buffer, Count);
  225. if (Result == -1)
  226. {
  227. RaiseLastOSError();
  228. }
  229. return Result;
  230. };