-
Notifications
You must be signed in to change notification settings - Fork 6
/
streamutils.pas
209 lines (181 loc) · 4.59 KB
/
streamutils.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
unit streamutils;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
procedure SaveStringToStreamByte(Str: String; Stream: TStream);
function LoadStringFromStreamByte(Stream: TStream): String;
procedure SaveUTF8StringToStreamByte(Str: UTF8String; Stream: TStream);
function LoadUTF8StringFromStreamByte(Stream: TStream): UTF8String;
procedure SaveStringToStreamWord(Str: String; Stream: TStream);
function LoadStringFromStreamWord(Stream: TStream): String;
procedure SaveUTF8StringToStreamWord(Str: UTF8String; Stream: TStream);
function LoadUTF8StringFromStreamWord(Stream: TStream): UTF8String;
procedure SaveStringToStreamInt(Str: String; Stream: TStream);
function LoadStringFromStreamInt(Stream: TStream): String;
procedure SaveUTF8StringToStreamInt(Str: UTF8String; Stream: TStream);
function LoadUTF8StringFromStreamInt(Stream: TStream): UTF8String;
procedure SaveStringToStream(Str: String; Stream: TStream);
function LoadStringFromStream(Stream: TStream): String;
procedure SaveUTF8StringToStream(Str: UTF8String; Stream: TStream);
function LoadUTF8StringFromStream(Stream: TStream): UTF8String;
procedure SaveStringsToStream(Strings: TStrings; Stream: TStream);
procedure LoadStringsFromStream(Strings: TStrings; Stream: TStream);
implementation
uses
LazUTF8;
procedure SaveStringToStreamByte(Str: String; Stream: TStream);
var
L: Byte;
begin
L := Length(Str);
Stream.Write(L,SizeOf(Byte));
Stream.Write(PChar(Str)^,L);
end;
function LoadStringFromStreamByte(Stream: TStream): String;
var
L: Byte;
begin
Stream.Read(L,SizeOf(Byte));
SetLength(Result,L);
if L > 0 then
Stream.Read(PChar(Result)^,L);
end;
procedure SaveUTF8StringToStreamByte(Str: UTF8String; Stream: TStream);
var
L: Byte;
begin
L := UTF8Length(Str);
Stream.Write(L,SizeOf(Byte));
Stream.Write(PChar(Str)^,L);
end;
function LoadUTF8StringFromStreamByte(Stream: TStream): UTF8String;
var
S: UTF8String;
L: Byte;
begin
Stream.Read(L,SizeOf(Byte));
SetLength(S,L);
if L > 0 then
Stream.Read(PChar(S)^,L);
Result := S;
end;
procedure SaveStringToStreamWord(Str: String; Stream: TStream);
var
L: Word;
begin
L := Length(Str);
Stream.Write(L,SizeOf(Word));
Stream.Write(PChar(Str)^,L);
end;
function LoadStringFromStreamWord(Stream: TStream): String;
var
S: String;
L: Word;
begin
Stream.Read(L,SizeOf(Word));
SetLength(S,L);
if L > 0 then
Stream.Read(PChar(S)^,L);
Result := S;
end;
procedure SaveUTF8StringToStreamWord(Str: UTF8String; Stream: TStream);
var
L: Word;
begin
L := UTF8Length(Str);
Stream.Write(L,SizeOf(Word));
Stream.Write(PChar(Str)^,L);
end;
function LoadUTF8StringFromStreamWord(Stream: TStream): UTF8String;
var
S: UTF8String;
L: Word;
begin
Stream.Read(L,SizeOf(Word));
SetLength(S,L);
if L > 0 then
Stream.Read(PChar(S)^,L);
Result := S;
end;
procedure SaveStringToStreamInt(Str: String; Stream: TStream);
var
L: Integer;
begin
L := Length(Str);
Stream.Write(L,SizeOf(Integer));
Stream.Write(PChar(Str)^,L);
end;
function LoadStringFromStreamInt(Stream: TStream): String;
var
L: Integer;
begin
Stream.Read(L,SizeOf(Integer));
SetLength(Result,L);
if L > 0 then
Stream.Read(PChar(Result)^,L);
end;
procedure SaveUTF8StringToStreamInt(Str: UTF8String; Stream: TStream);
var
L: Integer;
begin
L := UTF8Length(Str);
Stream.Write(L,SizeOf(Integer));
Stream.Write(PChar(Str)^,L);
end;
function LoadUTF8StringFromStreamInt(Stream: TStream): UTF8String;
var
S: UTF8String;
L: Integer;
begin
Stream.Read(L,SizeOf(Integer));
SetLength(S,L);
if L > 0 then
Stream.Read(PChar(S)^,L);
Result := S;
end;
procedure SaveStringToStream(Str: String; Stream: TStream);
begin
SaveStringToStreamWord(Str,Stream);
end;
function LoadStringFromStream(Stream: TStream): String;
begin
Result := LoadStringFromStreamWord(Stream);
end;
procedure SaveUTF8StringToStream(Str: UTF8String; Stream: TStream);
begin
SaveUTF8StringToStreamWord(Str,Stream);
end;
function LoadUTF8StringFromStream(Stream: TStream): UTF8String;
begin
Result := LoadUTF8StringFromStreamWord(Stream);
end;
procedure SaveStringsToStream(Strings: TStrings; Stream: TStream);
var
I,J: Word;
S: String;
begin
J := Strings.Count;
Stream.Write(J,SizeOf(J));
if J > 0 then
for I := 0 to J - 1 do
begin
S := Strings[I];
SaveStringToStream(S,Stream);
end;
end;
procedure LoadStringsFromStream(Strings: TStrings; Stream: TStream);
var
I,J: Word;
S: String;
begin
Strings.Clear;
Stream.Read(J,SizeOf(J));
if J > 0 then
for I := 0 to J - 1 do
begin
S := LoadStringFromStream(Stream);
Strings.Add(S);
end;
end;
end.