BrookReader.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. { Contains classes for reading text line by line from a string, stream or file. }
  26. unit BrookReader;
  27. {$I BrookDefines.inc}
  28. interface
  29. uses
  30. RTLConsts,
  31. SysUtils,
  32. Classes,
  33. BrookExtra;
  34. type
  35. { Abstract class for line reader. }
  36. TBrookTextReader = class abstract
  37. protected
  38. function GetEncoding: TEncoding; virtual; abstract;
  39. procedure SetEncoding(AValue: TEncoding); virtual; abstract;
  40. public
  41. { Resets the reader to its initial state. }
  42. procedure Reset; virtual; abstract;
  43. { Closes the reader. }
  44. procedure Close; virtual; abstract;
  45. { Checks if the reader has reached the End-Of-File.
  46. @returns(@True if the reader has reached the End-Of-File.) }
  47. function IsEOF: Boolean; virtual; abstract;
  48. { Reads a line as bytes.
  49. @param(ALine[out] Line read as bytes.) }
  50. procedure ReadBytes(out ALine: TBytes); overload; virtual; abstract;
  51. { Reads a line returning it as bytes.
  52. @returns(Line read as bytes.) }
  53. function ReadBytes: TBytes; overload; virtual; abstract;
  54. { Reads a line as static string.
  55. @param(ALine[out] read as static string.) }
  56. procedure Read(out ALine: string); overload; virtual; abstract;
  57. { Read a line returning it as a static string.
  58. @returns(Line read as static string.) }
  59. function Read: string; overload; virtual; abstract;
  60. { @True if the reader has reached the End-Of-File. }
  61. property EOF: Boolean read IsEOF;
  62. { Character encoding determined during reading. }
  63. property Encoding: TEncoding read GetEncoding write SetEncoding;
  64. end;
  65. { Line reader which uses stream as source of lines. }
  66. TBrookStreamReader = class(TBrookTextReader)
  67. private
  68. FStream: TStream;
  69. FEncoding: TEncoding;
  70. FBuffer: TBytes;
  71. FBufferRead: Integer;
  72. FBufferPosition: Integer;
  73. FOwnsStream: Boolean;
  74. protected
  75. function GetEncoding: TEncoding; override;
  76. function GetOwnsStream: Boolean; virtual;
  77. function GetStream: TStream; virtual;
  78. procedure SetEncoding(AValue: TEncoding); override;
  79. procedure SetOwnsStream(AValue: Boolean); virtual;
  80. procedure FillBuffer; virtual;
  81. property BufferRead: Integer read FBufferRead write FBufferRead;
  82. property BufferPosition: Integer read FBufferPosition write FBufferPosition;
  83. property Buffer: TBytes read FBuffer write FBuffer;
  84. public
  85. { Creates an instance of @code(TBrookStreamReader)
  86. @param(AEncoding[in] Character encoding determined during reading.)
  87. @param(AStream[in] Stream to be read line by line.)
  88. @param(ABufferSize[in] Buffer size for the line reading.)
  89. @param(AOwnsStream[in] If @True the stream is freed on @code(Destroy).) }
  90. constructor Create(AEncoding: TEncoding; AStream: TStream;
  91. ABufferSize: Cardinal; AOwnsStream: Boolean); reintroduce; overload; virtual;
  92. { Creates an instance of @code(TBrookStreamReader)
  93. @param(AEncoding[in] Character encoding determined during reading.)
  94. @param(AStream[in] Stream to be read line by line.) }
  95. constructor Create(AEncoding: TEncoding; AStream: TStream);
  96. reintroduce; overload; virtual;
  97. { Creates an instance of @code(TBrookStreamReader)
  98. @param(AStream[in] Stream to be read line by line.) }
  99. constructor Create(AStream: TStream); reintroduce; overload; virtual;
  100. { Destroys an instance of @code(TBrookStreamReader). }
  101. destructor Destroy; override;
  102. { Resets the cursor to the beginning of the stream. }
  103. procedure Reset; override;
  104. { Frees the stream if property @code(OwnsStream) is @True. }
  105. procedure Close; override;
  106. { Checks if the reader has reached the End-Of-File.
  107. @returns(@True if the stream has reached the End-Of-File.) }
  108. function IsEOF: Boolean; override;
  109. { Reads a line as bytes.
  110. @param(ALine[out] Line read as bytes.) }
  111. procedure ReadBytes(out ALine: TBytes); overload; override;
  112. { Reads a line returning it as bytes.
  113. @returns(Line read as bytes.) }
  114. function ReadBytes: TBytes; overload; override;
  115. { Reads a line as static string.
  116. @param(ALine[out] read as static string.) }
  117. procedure Read(out ALine: string); overload; override;
  118. { Read a line returning it as a static string.
  119. @returns(Line read as static string.) }
  120. function Read: string; overload; override;
  121. { Source stream containing the lines to be read. }
  122. property Stream: TStream read GetStream;
  123. { If @True the stream is freed on @code(Destroy). }
  124. property OwnsStream: Boolean read GetOwnsStream write SetOwnsStream;
  125. end;
  126. { Base proxied line reader. }
  127. TBrookBaseReader = class(TBrookTextReader)
  128. protected
  129. procedure SetEncoding(AValue: TEncoding); override;
  130. procedure SetProxyReader(AValue: TBrookTextReader); virtual; abstract;
  131. function GetEncoding: TEncoding; override;
  132. function GetProxyReader: TBrookTextReader; virtual; abstract;
  133. property ProxyReader: TBrookTextReader read GetProxyReader
  134. write SetProxyReader;
  135. public
  136. { Destroys an instance of @code(TBrookBaseReader). }
  137. destructor Destroy; override;
  138. { Resets the reader to its initial state. }
  139. procedure Reset; override;
  140. { Closes the reader. }
  141. procedure Close; override;
  142. { Checks if the reader has reached the End-Of-File.
  143. @returns(@True if the reader has reached the End-Of-File.) }
  144. function IsEOF: Boolean; override;
  145. { Reads a line as bytes.
  146. @param(ALine[out] Line read as bytes.) }
  147. procedure ReadBytes(out ALine: TBytes); overload; override;
  148. { Reads a line returning it as bytes.
  149. @returns(Line read as bytes.) }
  150. function ReadBytes: TBytes; overload; override;
  151. { Read a line as a static string.
  152. @param(ALine[out] read as static string.) }
  153. procedure Read(out ALine: string); overload; override;
  154. { Reads a line returning it as static string.
  155. @returns(Line read as static string.) }
  156. function Read: string; overload; override;
  157. end;
  158. { String line reader. }
  159. TBrookStringReader = class(TBrookBaseReader)
  160. private
  161. FProxyReader: TBrookTextReader;
  162. protected
  163. procedure SetProxyReader(AValue: TBrookTextReader); override;
  164. function GetProxyReader: TBrookTextReader; override;
  165. public
  166. { Creates an instance of @code(TBrookStringReader)
  167. @param(AEncoding[in] Character encoding determined during reading.)
  168. @param(AString[in] String to be read line by line.)
  169. @param(ABufferSize[in] Buffer size for the line reading.) }
  170. constructor Create(AEncoding: TEncoding; const AString: string;
  171. ABufferSize: Integer); reintroduce; overload; virtual;
  172. { Creates an instance of @code(TBrookStringReader)
  173. @param(AEncoding[in] Character encoding determined during reading.)
  174. @param(AString[in] String to be read line by line.) }
  175. constructor Create(AEncoding: TEncoding; const AString: string);
  176. reintroduce; overload; virtual;
  177. { Creates an instance of @code(TBrookStringReader)
  178. @param(AString[in] String to be read line by line.) }
  179. constructor Create(const AString: string); reintroduce; overload; virtual;
  180. end;
  181. { File line reader. }
  182. TBrookFileReader = class(TBrookBaseReader)
  183. private
  184. FProxyReader: TBrookTextReader;
  185. protected
  186. procedure SetProxyReader(AValue: TBrookTextReader); override;
  187. function GetProxyReader: TBrookTextReader; override;
  188. public
  189. { Creates an instance of @code(TBrookStringReader)
  190. @param(AEncoding[in] Character encoding determined during reading.)
  191. @param(AFileName[in] File to be read line by line.)
  192. @param(AMode[in] Open mode and (possibly) a share mode OR-ed together.)
  193. @param(ARights[in] Permission bits with which to create the file on Linux.)
  194. @param(ABufferSize[in] Buffer size for the line reading.) }
  195. constructor Create(AEncoding: TEncoding; const AFileName: TFileName;
  196. AMode: Word; ARights: Cardinal;
  197. ABufferSize: Integer); reintroduce; overload; virtual;
  198. { Creates an instance of @code(TBrookStringReader)
  199. @param(AEncoding[in] Character encoding determined during reading.)
  200. @param(AFileName[in] File to be read line by line.)
  201. @param(AMode[in] Open mode and (possibly) a share mode OR-ed together.)
  202. @param(ABufferSize[in] Buffer size for the line reading.) }
  203. constructor Create(AEncoding: TEncoding; const AFileName: TFileName;
  204. AMode: Word; ABufferSize: Integer); reintroduce; overload; virtual;
  205. { Creates an instance of @code(TBrookStringReader)
  206. @param(AEncoding[in] Character encoding determined during reading.)
  207. @param(AFileName[in] File to be read line by line.)
  208. @param(ABufferSize[in] Buffer size for the line reading.) }
  209. constructor Create(AEncoding: TEncoding; const AFileName: TFileName;
  210. ABufferSize: Integer); reintroduce; overload; virtual;
  211. { Creates an instance of @code(TBrookStringReader)
  212. @param(AEncoding[in] Character encoding determined during reading.)
  213. @param(AFileName[in] File to be read line by line.) }
  214. constructor Create(AEncoding: TEncoding;
  215. const AFileName: TFileName); reintroduce; overload; virtual;
  216. { Creates an instance of @code(TBrookStringReader)
  217. @param(AFileName[in] File to be read line by line.) }
  218. constructor Create(
  219. const AFileName: TFileName); reintroduce; overload; virtual;
  220. end;
  221. implementation
  222. { TBrookStreamReader }
  223. constructor TBrookStreamReader.Create(AEncoding: TEncoding; AStream: TStream;
  224. ABufferSize: Cardinal; AOwnsStream: Boolean);
  225. begin
  226. inherited Create;
  227. if not Assigned(AStream) then
  228. raise EArgumentNilException.CreateFmt(SParamIsNil, ['AStream']);
  229. FEncoding := AEncoding;
  230. FStream := AStream;
  231. FOwnsStream := AOwnsStream;
  232. if ABufferSize >= BROOK_MIN_BUFFER_SIZE then
  233. SetLength(FBuffer, ABufferSize)
  234. else
  235. SetLength(FBuffer, BROOK_MIN_BUFFER_SIZE);
  236. end;
  237. constructor TBrookStreamReader.Create(AEncoding: TEncoding; AStream: TStream);
  238. begin
  239. Create(AEncoding, AStream, BROOK_BUFFER_SIZE, False);
  240. end;
  241. constructor TBrookStreamReader.Create(AStream: TStream);
  242. begin
  243. Create(TEncoding.UTF8, AStream);
  244. end;
  245. destructor TBrookStreamReader.Destroy;
  246. begin
  247. Close;
  248. inherited Destroy;
  249. end;
  250. procedure TBrookStreamReader.FillBuffer;
  251. begin
  252. FBufferRead := FStream.Read(FBuffer[0], Pred(Length(FBuffer)));
  253. FBuffer[FBufferRead] := 0;
  254. FBufferPosition := 0;
  255. end;
  256. function TBrookStreamReader.GetEncoding: TEncoding;
  257. begin
  258. Result := FEncoding;
  259. end;
  260. function TBrookStreamReader.GetOwnsStream: Boolean;
  261. begin
  262. Result := FOwnsStream;
  263. end;
  264. function TBrookStreamReader.GetStream: TStream;
  265. begin
  266. Result := FStream;
  267. end;
  268. procedure TBrookStreamReader.SetEncoding(AValue: TEncoding);
  269. begin
  270. FEncoding := AValue;
  271. end;
  272. procedure TBrookStreamReader.SetOwnsStream(AValue: Boolean);
  273. begin
  274. FOwnsStream := AValue;
  275. end;
  276. procedure TBrookStreamReader.Reset;
  277. begin
  278. FBufferRead := 0;
  279. FBufferPosition := 0;
  280. if Assigned(FStream) then
  281. FStream.Seek(0, TSeekOrigin.soBeginning);
  282. end;
  283. procedure TBrookStreamReader.Close;
  284. begin
  285. if FOwnsStream then
  286. begin
  287. FStream.Free;
  288. FStream := nil;
  289. end;
  290. end;
  291. function TBrookStreamReader.IsEOF: Boolean;
  292. begin
  293. if not Assigned(FStream) then
  294. Exit(True);
  295. Result := FBufferPosition >= FBufferRead;
  296. if Result then
  297. begin
  298. FillBuffer;
  299. Result := FBufferRead = 0;
  300. end;
  301. end;
  302. procedure TBrookStreamReader.ReadBytes(out ALine: TBytes);
  303. var
  304. VBuf: PByte;
  305. VPos, VLen, VCount: Integer;
  306. begin
  307. VPos := FBufferPosition;
  308. ALine := nil;
  309. repeat
  310. VBuf := @FBuffer[FBufferPosition];
  311. while (FBufferPosition < FBufferRead) and not (VBuf^ in [10, 13]) do
  312. begin
  313. Inc(VBuf);
  314. Inc(FBufferPosition);
  315. end;
  316. if FBufferPosition = FBufferRead then
  317. begin
  318. VCount := FBufferPosition - VPos;
  319. if VCount > 0 then
  320. begin
  321. VLen := Length(ALine);
  322. SetLength(ALine, VLen + VCount);
  323. Move(FBuffer[VPos], ALine[VLen], VCount);
  324. end;
  325. FillBuffer;
  326. VPos := FBufferPosition;
  327. end;
  328. until (FBufferPosition = FBufferRead) or (VBuf^ in [10, 13]);
  329. VCount := FBufferPosition - VPos;
  330. if VCount > 0 then
  331. begin
  332. VLen := Length(ALine);
  333. SetLength(ALine, VLen + VCount);
  334. Move(FBuffer[VPos], ALine[VLen], VCount);
  335. end;
  336. if (VBuf^ in [10, 13]) and (FBufferPosition < FBufferRead) then
  337. begin
  338. Inc(FBufferPosition);
  339. if VBuf^ = 13 then
  340. begin
  341. if FBufferPosition = FBufferRead then
  342. FillBuffer;
  343. if (FBufferPosition < FBufferRead) and (FBuffer[FBufferPosition] = 10) then
  344. Inc(FBufferPosition);
  345. end;
  346. end;
  347. end;
  348. function TBrookStreamReader.ReadBytes: TBytes;
  349. begin
  350. ReadBytes(Result);
  351. end;
  352. procedure TBrookStreamReader.Read(out ALine: string);
  353. begin
  354. ALine :=
  355. {$IFDEF FPC}string({$ENDIF}FEncoding.GetString(ReadBytes){$IFDEF FPC}){$ENDIF};
  356. end;
  357. function TBrookStreamReader.Read: string;
  358. begin
  359. Read(Result);
  360. end;
  361. { TBrookBaseReader }
  362. destructor TBrookBaseReader.Destroy;
  363. begin
  364. ProxyReader.Free;
  365. ProxyReader := nil;
  366. inherited Destroy;
  367. end;
  368. procedure TBrookBaseReader.SetEncoding(AValue: TEncoding);
  369. begin
  370. ProxyReader.Encoding := AValue;
  371. end;
  372. function TBrookBaseReader.GetEncoding: TEncoding;
  373. begin
  374. Result := ProxyReader.Encoding;
  375. end;
  376. procedure TBrookBaseReader.Reset;
  377. begin
  378. ProxyReader.Reset;
  379. end;
  380. procedure TBrookBaseReader.Close;
  381. begin
  382. ProxyReader.Close;
  383. end;
  384. function TBrookBaseReader.IsEOF: Boolean;
  385. begin
  386. Result := ProxyReader.IsEOF;
  387. end;
  388. procedure TBrookBaseReader.ReadBytes(out ALine: TBytes);
  389. begin
  390. ProxyReader.ReadBytes(ALine);
  391. end;
  392. function TBrookBaseReader.ReadBytes: TBytes;
  393. begin
  394. ProxyReader.ReadBytes(Result);
  395. end;
  396. procedure TBrookBaseReader.Read(out ALine: string);
  397. begin
  398. ProxyReader.Read(ALine);
  399. end;
  400. function TBrookBaseReader.Read: string;
  401. begin
  402. ProxyReader.Read(Result);
  403. end;
  404. { TBrookStringReader }
  405. constructor TBrookStringReader.Create(AEncoding: TEncoding;
  406. const AString: string; ABufferSize: Integer);
  407. var
  408. VStream: TStream;
  409. begin
  410. inherited Create;
  411. {$IFNDEF FPC}
  412. if Assigned(AEncoding) then
  413. VStream := TStringStream.Create(AString, AEncoding)
  414. else
  415. {$ENDIF}
  416. VStream := TStringStream.Create(AString);
  417. FProxyReader := TBrookStreamReader.Create(AEncoding, VStream,
  418. ABufferSize, True);
  419. end;
  420. constructor TBrookStringReader.Create(AEncoding: TEncoding;
  421. const AString: string);
  422. begin
  423. Create(AEncoding, AString, BROOK_BUFFER_SIZE);
  424. end;
  425. constructor TBrookStringReader.Create(const AString: string);
  426. begin
  427. Create(TEncoding.UTF8, AString);
  428. end;
  429. procedure TBrookStringReader.SetProxyReader(AValue: TBrookTextReader);
  430. begin
  431. FProxyReader := AValue;
  432. end;
  433. function TBrookStringReader.GetProxyReader: TBrookTextReader;
  434. begin
  435. Result := FProxyReader;
  436. end;
  437. { TBrookFileReader }
  438. constructor TBrookFileReader.Create(AEncoding: TEncoding;
  439. const AFileName: TFileName; AMode: Word; ARights: Cardinal;
  440. ABufferSize: Integer);
  441. begin
  442. inherited Create;
  443. FProxyReader := TBrookStreamReader.Create(AEncoding,
  444. TFileStream.Create(AFileName, AMode, ARights), ABufferSize, True);
  445. end;
  446. constructor TBrookFileReader.Create(AEncoding: TEncoding;
  447. const AFileName: TFileName; AMode: Word; ABufferSize: Integer);
  448. begin
  449. Create(AEncoding, AFileName, AMode, BROOK_FILE_RIGHTS, ABufferSize);
  450. end;
  451. constructor TBrookFileReader.Create(AEncoding: TEncoding;
  452. const AFileName: TFileName; ABufferSize: Integer);
  453. begin
  454. Create(AEncoding, AFileName, fmOpenRead or fmShareDenyWrite, ABufferSize);
  455. end;
  456. constructor TBrookFileReader.Create(AEncoding: TEncoding;
  457. const AFileName: TFileName);
  458. begin
  459. Create(AEncoding, AFileName, BROOK_BUFFER_SIZE);
  460. end;
  461. constructor TBrookFileReader.Create(const AFileName: TFileName);
  462. begin
  463. Create(TEncoding.UTF8, AFileName);
  464. end;
  465. procedure TBrookFileReader.SetProxyReader(AValue: TBrookTextReader);
  466. begin
  467. FProxyReader := AValue;
  468. end;
  469. function TBrookFileReader.GetProxyReader: TBrookTextReader;
  470. begin
  471. Result := FProxyReader;
  472. end;
  473. end.