forked from geraldholdsworth/DiscImageManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobal.pas
78 lines (66 loc) · 2.2 KB
/
Global.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
unit Global;
{
Copyright (C) 2018-2024 Gerald Holdsworth [email protected]
This source is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public Licence as published by the Free
Software Foundation; either version 3 of the Licence, or (at your option)
any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public Licence for more
details.
A copy of the GNU General Public Licence is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1335, USA.
}
{$mode objfpc}{$H+}
interface
uses Classes;
function ReadLine(var Stream: TFileStream;var Line: string): boolean;
function WriteLine(var Stream: TFileStream;Line: string): boolean;
implementation
{-------------------------------------------------------------------------------
Reads a line from a TStream
-------------------------------------------------------------------------------}
function ReadLine(var Stream: TFileStream;var Line: string): boolean;
var
RawLine: UTF8String;
ch : AnsiChar;
begin
RawLine:='';
Result:=False;
ch:=#0;
while (Stream.Read(ch,1)=1) and (ch<>#13) and (ch<>#10) do
begin
Result:=True;
RawLine:=RawLine+UTF8String(ch);
end;
Line:=String(RawLine);
if ch=#13 then
begin
Result:=True;
if (Stream.Read(ch,1)=1) and (ch<>#10) then
Stream.Seek(-1,soCurrent) // unread it if not LF character.
end;
if ch=#10 then
begin
Result:=True;
if (Stream.Read(ch,1)=1) and (ch<>#13) then
Stream.Seek(-1,soCurrent) // unread it if not CR character.
end;
end;
{-------------------------------------------------------------------------------
Writes a string to the TFileStream, and terminates it with 0x0A
-------------------------------------------------------------------------------}
function WriteLine(var Stream: TFileStream;Line: string): boolean;
var
l,x: Integer;
S: UTF8String;
begin
S:=UTF8String(Line+#10);
l:=Length(S);
x:=Stream.Write(S[1],l);
Result:=x=l;
end;
end.