| 1 |
{%MainUnit castledownload.pas} |
| 2 |
{ |
| 3 |
Copyright 2013-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
type |
| 20 |
{ Common class for reading or writing a stream like a text file. } |
| 21 |
TTextReaderWriter = class |
| 22 |
private |
| 23 |
FOwnsStream: boolean; |
| 24 |
FStream: TStream; |
| 25 |
public |
| 26 |
{ Open a stream. If AOwnsStream then in destructor we will free |
| 27 |
given AStream object. } |
| 28 |
constructor Create(AStream: TStream; AOwnsStream: boolean); overload; |
| 29 |
destructor Destroy; override; |
| 30 |
end; |
| 31 |
|
| 32 |
{ Read any stream like a text file. You can read an arbitrary TStream |
| 33 |
instance, or you can read an URL. Reading from URL supports |
| 34 |
all URL protocols supportted by the Castle Game Engine engine |
| 35 |
(see https://castle-engine.io/manual_network.php ). |
| 36 |
|
| 37 |
Includes comfortable @link(Readln) routine to read line by line |
| 38 |
(lines may be terminated in any OS convention). |
| 39 |
Includes comfortable @link(Read) to read next non-whitespace |
| 40 |
characters, @link(ReadInteger) to read next integer and such. |
| 41 |
|
| 42 |
Do not use the underlying stream once you started reading it with |
| 43 |
this class. We will move the position within this stream ourselves. } |
| 44 |
TTextReader = class(TTextReaderWriter) |
| 45 |
private |
| 46 |
{ For now, Stream.Read call assumes that ReadBuf is 8-bit. |
| 47 |
Changing ReadBuf to String on Delphi would break e.g. Spine atlas reading, |
| 48 |
TTextReader.Readln would be garbage. } |
| 49 |
ReadBuf: AnsiString; |
| 50 |
{ Try to read more data from underlying stream and add it to ReadBuf. |
| 51 |
Returns if we succeded, @false means that the stream ends. |
| 52 |
When it returns @true, you can be sure that Length(ReadBuf) increased. } |
| 53 |
function IncreaseReadBuf: boolean; |
| 54 |
public |
| 55 |
{ Download and open a file. } |
| 56 |
constructor Create(const Url: String); overload; |
| 57 |
|
| 58 |
{ Read next line from Stream. Returned string does not contain |
| 59 |
any end-of-line characters. } |
| 60 |
function Readln: string; |
| 61 |
|
| 62 |
{ Read the next string of non-whitespace characters. |
| 63 |
This skips any whitespace (including newlines) we currently see, |
| 64 |
then reads all non-whitespace characters as far as it can. |
| 65 |
It does not consume any whitespace characters after the string. |
| 66 |
|
| 67 |
Returns empty string if and only if the stream ended. |
| 68 |
Otherwise, returns the read non-whitespace characters. } |
| 69 |
function Read: string; |
| 70 |
|
| 71 |
{ Read the next Integer from stream. Reads next string of non-whitespace |
| 72 |
characters, like @link(Read), and then converts it to Integer. |
| 73 |
|
| 74 |
@raises(EConvertError If the next non-whitespace string |
| 75 |
cannot be converted to Integer. This includes situations |
| 76 |
when stream ended (@link(Read) would return empty string in this |
| 77 |
case).) } |
| 78 |
function ReadInteger: Integer; |
| 79 |
|
| 80 |
{ Read the next Single value from stream. |
| 81 |
Reads next string of non-whitespace |
| 82 |
characters, like @link(Read), and then converts it to Single. |
| 83 |
|
| 84 |
@raises(EConvertError If the next non-whitespace string |
| 85 |
cannot be converted to Single. This includes situations |
| 86 |
when stream ended (@link(Read) would return empty string in this |
| 87 |
case).) } |
| 88 |
function ReadSingle: Single; |
| 89 |
|
| 90 |
{ Read the next vector from a stream, simply reading 3 Single values |
| 91 |
in sequence. |
| 92 |
|
| 93 |
@raises(EConvertError If one of the components cannot be converted |
| 94 |
to Single, or when stream ended prematurely.) } |
| 95 |
function ReadVector3: TVector3; |
| 96 |
function ReadVector3Single: TVector3; deprecated 'use ReadVector3'; |
| 97 |
|
| 98 |
function Eof: boolean; |
| 99 |
end; |
| 100 |
|
| 101 |
{ Write to a stream like to a text file. |
| 102 |
|
| 103 |
You can write to an arbitrary TStream instance, |
| 104 |
or you can write to an URL. Writing to an URL supports all |
| 105 |
URL protocols supportted by @link(UrlSaveStream), which for now |
| 106 |
doesn't include much: only @code(file) protocol. But it will produce |
| 107 |
a nice exception message in case of unsupprted URL protocol. } |
| 108 |
TTextWriter = class(TTextReaderWriter) |
| 109 |
public |
| 110 |
constructor Create(const Url: String); overload; |
| 111 |
procedure Write(const S: string); overload; |
| 112 |
procedure Write(const S: string; const Args: array of const); overload; |
| 113 |
procedure Writeln(const S: string = ''); overload; |
| 114 |
procedure Writeln(const S: string; const Args: array of const); overload; |
| 115 |
end; |
| 116 |
|
| 117 |
{$endif} |
| 118 |
|
| 119 |
{$ifdef read_implementation} |
| 120 |
|
| 121 |
{ TTextReaderWriter ---------------------------------------------------------- } |
| 122 |
|
| 123 |
constructor TTextReaderWriter.Create(AStream: TStream; AOwnsStream: boolean); |
| 124 |
begin |
| 125 |
inherited Create; |
| 126 |
FStream := Astream; |
| 127 |
FOwnsStream := AOwnsStream; |
| 128 |
end; |
| 129 |
|
| 130 |
destructor TTextReaderWriter.Destroy; |
| 131 |
begin |
| 132 |
if FOwnsStream then FStream.Free; |
| 133 |
inherited; |
| 134 |
end; |
| 135 |
|
| 136 |
{ TTextReader ---------------------------------------------------------------- } |
| 137 |
|
| 138 |
constructor TTextReader.Create(const Url: String); |
| 139 |
begin |
| 140 |
inherited Create(Download(Url), true); |
| 141 |
end; |
| 142 |
|
| 143 |
function TTextReader.IncreaseReadBuf: boolean; |
| 144 |
const |
| 145 |
BufferIncrease = 100; |
| 146 |
var |
| 147 |
ReadCnt: Integer; |
| 148 |
begin |
| 149 |
SetLength(ReadBuf, Length(ReadBuf) + BufferIncrease); |
| 150 |
ReadCnt := FStream.Read(ReadBuf[Length(ReadBuf) - BufferIncrease + 1], BufferIncrease); |
| 151 |
SetLength(ReadBuf, Length(ReadBuf) - BufferIncrease + ReadCnt); |
| 152 |
Result := ReadCnt <> 0; |
| 153 |
end; |
| 154 |
|
| 155 |
function TTextReader.Readln: string; |
| 156 |
var |
| 157 |
I, DeleteCount: integer; |
| 158 |
begin |
| 159 |
I := 1; |
| 160 |
|
| 161 |
{ Note that ReadBuf may contain data that we |
| 162 |
already read from stream at some time but did not returned it to |
| 163 |
user of this class |
| 164 |
(because we realized we have read too much). } |
| 165 |
|
| 166 |
repeat |
| 167 |
if (I > Length(ReadBuf)) and not IncreaseReadBuf then |
| 168 |
begin |
| 169 |
Result := ReadBuf; |
| 170 |
ReadBuf := ''; |
| 171 |
Exit; |
| 172 |
end; |
| 173 |
|
| 174 |
if CharInSet(ReadBuf[I], [#10, #13]) then |
| 175 |
begin |
| 176 |
Result := Copy(ReadBuf, 1, I - 1); |
| 177 |
DeleteCount := I; |
| 178 |
|
| 179 |
{ If this is followed by 2nd newline character, we want to consume it. |
| 180 |
To do this, we may have to increase ReadBuf. } |
| 181 |
if ( (I < Length(ReadBuf)) or IncreaseReadBuf ) and |
| 182 |
{ check we have #13 followed by #10 or #10 followed by #13. |
| 183 |
Be careful to *not* eat #10 followed by #10, as that would |
| 184 |
make us silently consume empty lines in files with Unix line ending. } |
| 185 |
( ( (ReadBuf[I] = #10) and (ReadBuf[I + 1] = #13) ) or |
| 186 |
( (ReadBuf[I] = #13) and (ReadBuf[I + 1] = #10) ) ) then |
| 187 |
Inc(DeleteCount); |
| 188 |
|
| 189 |
Delete(ReadBuf, 1, DeleteCount); |
| 190 |
Exit; |
| 191 |
end; |
| 192 |
|
| 193 |
Inc(I); |
| 194 |
until false; |
| 195 |
end; |
| 196 |
|
| 197 |
function TTextReader.Read: string; |
| 198 |
var |
| 199 |
Start, I: integer; |
| 200 |
begin |
| 201 |
I := 1; |
| 202 |
|
| 203 |
repeat |
| 204 |
if (I > Length(ReadBuf)) and not IncreaseReadBuf then |
| 205 |
Exit(''); |
| 206 |
if not CharInSet(ReadBuf[I], WhiteSpaces) then |
| 207 |
Break; |
| 208 |
Inc(I); |
| 209 |
until false; |
| 210 |
|
| 211 |
Start := I; |
| 212 |
|
| 213 |
repeat |
| 214 |
if (I > Length(ReadBuf)) and not IncreaseReadBuf then |
| 215 |
Break; |
| 216 |
if CharInSet(ReadBuf[I], WhiteSpaces) then |
| 217 |
Break; |
| 218 |
Inc(I); |
| 219 |
until false; |
| 220 |
|
| 221 |
Dec(I); { we know we're 1 position too far now } |
| 222 |
Assert(I > 0); |
| 223 |
Result := CopyPos(ReadBuf, Start, I); |
| 224 |
Delete(ReadBuf, 1, I); |
| 225 |
end; |
| 226 |
|
| 227 |
function TTextReader.ReadInteger: Integer; |
| 228 |
begin |
| 229 |
Result := StrToInt(Read); |
| 230 |
end; |
| 231 |
|
| 232 |
function TTextReader.ReadSingle: Single; |
| 233 |
begin |
| 234 |
Result := StrToFloatDot(Read); |
| 235 |
end; |
| 236 |
|
| 237 |
function TTextReader.ReadVector3: TVector3; |
| 238 |
begin |
| 239 |
Result.X := ReadSingle; |
| 240 |
Result.Y := ReadSingle; |
| 241 |
Result.Z := ReadSingle; |
| 242 |
end; |
| 243 |
|
| 244 |
function TTextReader.ReadVector3Single: TVector3; |
| 245 |
begin |
| 246 |
Result := ReadVector3; |
| 247 |
end; |
| 248 |
|
| 249 |
function TTextReader.Eof: boolean; |
| 250 |
begin |
| 251 |
Result := (ReadBuf = '') and not IncreaseReadBuf; |
| 252 |
end; |
| 253 |
|
| 254 |
{ TTextWriter ---------------------------------------------------------------- } |
| 255 |
|
| 256 |
constructor TTextWriter.Create(const Url: String); |
| 257 |
begin |
| 258 |
inherited Create(UrlSaveStream(Url), true); |
| 259 |
end; |
| 260 |
|
| 261 |
procedure TTextWriter.Write(const S: string); |
| 262 |
begin |
| 263 |
WriteStr(FStream, S); |
| 264 |
end; |
| 265 |
|
| 266 |
procedure TTextWriter.Writeln(const S: string); |
| 267 |
begin |
| 268 |
WritelnStr(FStream, S); |
| 269 |
end; |
| 270 |
|
| 271 |
procedure TTextWriter.Write(const S: string; const Args: array of const); |
| 272 |
begin |
| 273 |
WriteStr(FStream, Format(S, Args)); |
| 274 |
end; |
| 275 |
|
| 276 |
procedure TTextWriter.Writeln(const S: string; const Args: array of const); |
| 277 |
begin |
| 278 |
WritelnStr(FStream, Format(S, Args)); |
| 279 |
end; |
| 280 |
|
| 281 |
{$endif} |