| 1 | //  HRBuffers v0.3.1 (03.Aug.2000) | 
|---|
| 2 | //  Simple buffer classes | 
|---|
| 3 | //  by Colin A Ridgewell | 
|---|
| 4 | // | 
|---|
| 5 | //  Copyright (C) 1999,2000 Hayden-R Ltd | 
|---|
| 6 | //  http://www.haydenr.com | 
|---|
| 7 | // | 
|---|
| 8 | //  This program is free software; you can redistribute it and/or modify it | 
|---|
| 9 | //  under the terms of the GNU General Public License as published by the | 
|---|
| 10 | //  Free Software Foundation; either version 2 of the License, or (at your | 
|---|
| 11 | //  option) any later version. | 
|---|
| 12 | // | 
|---|
| 13 | //  This program is distributed in the hope that it will be useful, but | 
|---|
| 14 | //  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | 
|---|
| 15 | //  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | 
|---|
| 16 | //  more details. | 
|---|
| 17 | // | 
|---|
| 18 | //  You should have received a copy of the GNU General Public License along | 
|---|
| 19 | //  with this program (gnu_license.htm); if not, write to the | 
|---|
| 20 | // | 
|---|
| 21 | //  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 22 | // | 
|---|
| 23 | //  To contact us via e-mail use the following addresses... | 
|---|
| 24 | // | 
|---|
| 25 | //  bug@haydenr.u-net.com       - to report a bug | 
|---|
| 26 | //  support@haydenr.u-net.com   - for general support | 
|---|
| 27 | //  wishlist@haydenr.u-net.com  - add new requirement to wish list | 
|---|
| 28 | // | 
|---|
| 29 | unit HRBuffers; | 
|---|
| 30 |  | 
|---|
| 31 | interface | 
|---|
| 32 |  | 
|---|
| 33 | uses | 
|---|
| 34 | Classes, SysUtils; | 
|---|
| 35 |  | 
|---|
| 36 | type | 
|---|
| 37 | {Base buffer.} | 
|---|
| 38 | THRBuffer=class(TObject) | 
|---|
| 39 | private | 
|---|
| 40 | FBuffer:PChar; | 
|---|
| 41 | FSize:LongInt; | 
|---|
| 42 | procedure SetSize(Value:LongInt); | 
|---|
| 43 | procedure CreateBuffer(const Size:LongInt); | 
|---|
| 44 | procedure ResizeBuffer(const Size:LongInt); | 
|---|
| 45 | procedure FreeBuffer; | 
|---|
| 46 | protected | 
|---|
| 47 | function GetItems(Index:LongInt):Char; virtual; | 
|---|
| 48 | procedure SetItems(Index:LongInt;Value:Char); virtual; | 
|---|
| 49 | public | 
|---|
| 50 | constructor Create; virtual; | 
|---|
| 51 | destructor Destroy; override; | 
|---|
| 52 | property Buffer:PChar read FBuffer; | 
|---|
| 53 | property Size:Longint read FSize write SetSize; | 
|---|
| 54 | property Items[Index:LongInt]:Char read GetItems write SetItems; default; | 
|---|
| 55 | end; | 
|---|
| 56 |  | 
|---|
| 57 | {Base buffer with EOB.} | 
|---|
| 58 | THRBufferEOB=class(THRBuffer) | 
|---|
| 59 | private | 
|---|
| 60 | protected | 
|---|
| 61 | function GetEOB:Boolean; virtual; | 
|---|
| 62 | public | 
|---|
| 63 | property EOB:Boolean read GetEOB; | 
|---|
| 64 | end; | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 | {Buffer for holding a series of char.} | 
|---|
| 68 | THRBufferChar=class(THRBufferEOB) | 
|---|
| 69 | private | 
|---|
| 70 | FEOB:Boolean; | 
|---|
| 71 | FPosition:Longint; | 
|---|
| 72 | protected | 
|---|
| 73 | function GetEOB:Boolean; override; | 
|---|
| 74 | function GetItems(Index:LongInt):Char; override; | 
|---|
| 75 | procedure SetItems(Index:LongInt;Value:Char); override; | 
|---|
| 76 | function GetAsPChar:PChar; | 
|---|
| 77 | procedure SetAsPChar(Value:PChar); | 
|---|
| 78 | function GetAsString:string; | 
|---|
| 79 | procedure SetAsString(Value:string); | 
|---|
| 80 | public | 
|---|
| 81 | constructor Create; override; | 
|---|
| 82 | destructor Destroy; override; | 
|---|
| 83 | property Buffer; | 
|---|
| 84 | property Position:Longint read FPosition write FPosition; | 
|---|
| 85 | procedure Write(const Value:Char); | 
|---|
| 86 | function Read:Char; | 
|---|
| 87 | procedure WritePChar(const Str:PChar); | 
|---|
| 88 | procedure WriteString(const Str:String); | 
|---|
| 89 | property AsPChar:PChar read GetAsPChar write SetAsPChar; | 
|---|
| 90 | property AsString:string read GetAsString write SetAsString; | 
|---|
| 91 | end; | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | {Buffer for reading from a stream.} | 
|---|
| 95 | THRBufferStream=class(THRBufferEOB) | 
|---|
| 96 | private | 
|---|
| 97 | FEOB:Boolean; | 
|---|
| 98 | FStream:TStream; | 
|---|
| 99 | FStreamSize:Longint; | 
|---|
| 100 | FFirstPosInBuffer:LongInt; | 
|---|
| 101 | protected | 
|---|
| 102 | function GetEOB:Boolean; override; | 
|---|
| 103 | function GetItems(Index:LongInt):Char; override; | 
|---|
| 104 | procedure SetItems(Index:LongInt;Value:Char); override; | 
|---|
| 105 | procedure SetStream(Value:TStream); | 
|---|
| 106 | public | 
|---|
| 107 | constructor Create; override; | 
|---|
| 108 | destructor Destroy; override; | 
|---|
| 109 | property Stream:TStream read FStream write SetStream; | 
|---|
| 110 | end; | 
|---|
| 111 |  | 
|---|
| 112 | {A buffer containing a list of smaller buffers in one piece of contiguous memory.} | 
|---|
| 113 | THRBufferList=class(THRBuffer) | 
|---|
| 114 | private | 
|---|
| 115 | function GetItemPos(const Index:Integer):Integer; | 
|---|
| 116 | function GetCount:Integer; | 
|---|
| 117 | function GetItemSize(Index:Integer):Integer; | 
|---|
| 118 | procedure SetItemSize(Index:Integer;Value:Integer); | 
|---|
| 119 | function GetItemBuffer(Index:Integer):PChar; | 
|---|
| 120 | public | 
|---|
| 121 | constructor Create; override; | 
|---|
| 122 | destructor Destroy; override; | 
|---|
| 123 | procedure Add(const Index,ItemSize:Integer); | 
|---|
| 124 | procedure Delete(const Index:Integer); | 
|---|
| 125 | property Count:Integer read GetCount; | 
|---|
| 126 | property ItemSize[Index:Integer]:Integer read GetItemSize write SetItemSize; | 
|---|
| 127 | property ItemBuffer[Index:Integer]:PChar read GetItemBuffer; | 
|---|
| 128 | end; | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | implementation | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | { T H R B u f f e r } | 
|---|
| 135 |  | 
|---|
| 136 | constructor THRBuffer.Create; | 
|---|
| 137 | begin | 
|---|
| 138 | FBuffer:=nil; | 
|---|
| 139 | FSize:=0; | 
|---|
| 140 | end; | 
|---|
| 141 |  | 
|---|
| 142 | destructor THRBuffer.Destroy; | 
|---|
| 143 | begin | 
|---|
| 144 | FreeBuffer; | 
|---|
| 145 | inherited Destroy; | 
|---|
| 146 | end; | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|
| 149 | procedure THRBuffer.SetSize(Value:LongInt); | 
|---|
| 150 | begin | 
|---|
| 151 | if FBuffer=nil | 
|---|
| 152 | then | 
|---|
| 153 | CreateBuffer(Value) | 
|---|
| 154 | else | 
|---|
| 155 | if Value>0 | 
|---|
| 156 | then | 
|---|
| 157 | ResizeBuffer(Value) | 
|---|
| 158 | else | 
|---|
| 159 | FreeBuffer; | 
|---|
| 160 | end; | 
|---|
| 161 |  | 
|---|
| 162 |  | 
|---|
| 163 | function THRBuffer.GetItems(Index:LongInt):Char; | 
|---|
| 164 | begin | 
|---|
| 165 | Result:=#0; | 
|---|
| 166 | end; | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 | procedure THRBuffer.SetItems(Index:LongInt;Value:Char); | 
|---|
| 170 | begin | 
|---|
| 171 | end; | 
|---|
| 172 |  | 
|---|
| 173 |  | 
|---|
| 174 | procedure THRBuffer.CreateBuffer(const Size:LongInt); | 
|---|
| 175 | begin | 
|---|
| 176 | if FBuffer=nil | 
|---|
| 177 | then | 
|---|
| 178 | begin | 
|---|
| 179 | FSize:=Size; | 
|---|
| 180 | GetMem(FBuffer,FSize+1); | 
|---|
| 181 | {Null terminate end of buffer.} | 
|---|
| 182 | FBuffer[FSize]:=#0; | 
|---|
| 183 | end; | 
|---|
| 184 | end; | 
|---|
| 185 |  | 
|---|
| 186 |  | 
|---|
| 187 | procedure THRBuffer.ResizeBuffer(const Size:LongInt); | 
|---|
| 188 | var | 
|---|
| 189 | New:PChar; | 
|---|
| 190 | MoveSize:LongInt; | 
|---|
| 191 | begin | 
|---|
| 192 | if FBuffer<>nil | 
|---|
| 193 | then | 
|---|
| 194 | begin | 
|---|
| 195 | GetMem(New,Size+1); | 
|---|
| 196 | if FSize>Size then MoveSize:=Size else MoveSize:=FSize; | 
|---|
| 197 | Move(FBuffer[0],New[0],MoveSize); | 
|---|
| 198 | FreeMem(FBuffer,FSize+1); | 
|---|
| 199 | FBuffer:=New; | 
|---|
| 200 | FSize:=Size; | 
|---|
| 201 | FBuffer[FSize]:=#0; | 
|---|
| 202 | end; | 
|---|
| 203 | end; | 
|---|
| 204 |  | 
|---|
| 205 |  | 
|---|
| 206 | procedure THRBuffer.FreeBuffer; | 
|---|
| 207 | begin | 
|---|
| 208 | if FBuffer<>nil | 
|---|
| 209 | then | 
|---|
| 210 | begin | 
|---|
| 211 | FreeMem(FBuffer,FSize+1); | 
|---|
| 212 | FBuffer:=nil; | 
|---|
| 213 | FSize:=0; | 
|---|
| 214 | end; | 
|---|
| 215 | end; | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 | { T H R B u f f e r E O B } | 
|---|
| 219 |  | 
|---|
| 220 | function THRBufferEOB.GetEOB:Boolean; | 
|---|
| 221 | begin | 
|---|
| 222 | Result:=True; | 
|---|
| 223 | end; | 
|---|
| 224 |  | 
|---|
| 225 |  | 
|---|
| 226 | { T H R B u f f e r C h a r } | 
|---|
| 227 |  | 
|---|
| 228 | constructor THRBufferChar.Create; | 
|---|
| 229 | begin | 
|---|
| 230 | inherited Create; | 
|---|
| 231 | FEOB:=False; | 
|---|
| 232 | end; | 
|---|
| 233 |  | 
|---|
| 234 |  | 
|---|
| 235 | destructor THRBufferChar.Destroy; | 
|---|
| 236 | begin | 
|---|
| 237 | inherited Destroy; | 
|---|
| 238 | end; | 
|---|
| 239 |  | 
|---|
| 240 |  | 
|---|
| 241 | function THRBufferChar.GetEOB:Boolean; | 
|---|
| 242 | begin | 
|---|
| 243 | Result:=FEOB; | 
|---|
| 244 | end; | 
|---|
| 245 |  | 
|---|
| 246 |  | 
|---|
| 247 | function THRBufferChar.GetItems(Index:LongInt):Char; | 
|---|
| 248 | begin | 
|---|
| 249 | if Index<FSize | 
|---|
| 250 | then | 
|---|
| 251 | begin | 
|---|
| 252 | Result:=FBuffer[Index]; | 
|---|
| 253 | FEOB:=False; | 
|---|
| 254 | end | 
|---|
| 255 | else | 
|---|
| 256 | begin | 
|---|
| 257 | Result:=#0; | 
|---|
| 258 | FEOB:=True; | 
|---|
| 259 | end; | 
|---|
| 260 | end; | 
|---|
| 261 |  | 
|---|
| 262 |  | 
|---|
| 263 | procedure THRBufferChar.SetItems(Index:LongInt;Value:Char); | 
|---|
| 264 | begin | 
|---|
| 265 | if Index<FSize | 
|---|
| 266 | then | 
|---|
| 267 | begin | 
|---|
| 268 | FBuffer[Index]:=Value; | 
|---|
| 269 | FEOB:=False; | 
|---|
| 270 | end | 
|---|
| 271 | else | 
|---|
| 272 | begin | 
|---|
| 273 | FEOB:=True; | 
|---|
| 274 | end; | 
|---|
| 275 | end; | 
|---|
| 276 |  | 
|---|
| 277 |  | 
|---|
| 278 | function THRBufferChar.Read:Char; | 
|---|
| 279 | begin | 
|---|
| 280 | if FPosition<FSize | 
|---|
| 281 | then | 
|---|
| 282 | begin | 
|---|
| 283 | Result:=FBuffer[FPosition]; | 
|---|
| 284 | Inc(FPosition); | 
|---|
| 285 | FEOB:=False; | 
|---|
| 286 | end | 
|---|
| 287 | else | 
|---|
| 288 | begin | 
|---|
| 289 | Result:=#0; | 
|---|
| 290 | FEOB:=True; | 
|---|
| 291 | end; | 
|---|
| 292 | end; | 
|---|
| 293 |  | 
|---|
| 294 |  | 
|---|
| 295 | procedure THRBufferChar.Write(const Value:Char); | 
|---|
| 296 | begin | 
|---|
| 297 | if FPosition<FSize | 
|---|
| 298 | then | 
|---|
| 299 | begin | 
|---|
| 300 | FBuffer[FPosition]:=Value; | 
|---|
| 301 | Inc(FPosition); | 
|---|
| 302 | FEOB:=False; | 
|---|
| 303 | end | 
|---|
| 304 | else | 
|---|
| 305 | begin | 
|---|
| 306 | FEOB:=True; | 
|---|
| 307 | end; | 
|---|
| 308 | end; | 
|---|
| 309 |  | 
|---|
| 310 |  | 
|---|
| 311 | procedure THRBufferChar.WritePChar(const Str:PChar); | 
|---|
| 312 | var | 
|---|
| 313 | i:Integer; | 
|---|
| 314 | begin | 
|---|
| 315 | for i:=0 to StrLen(Str)-1 do Write(Str[i]); | 
|---|
| 316 | end; | 
|---|
| 317 |  | 
|---|
| 318 |  | 
|---|
| 319 | procedure THRBufferChar.WriteString(const Str:String); | 
|---|
| 320 | var | 
|---|
| 321 | i:Integer; | 
|---|
| 322 | begin | 
|---|
| 323 | for i:=1 to Length(Str) do Write(Str[i]); | 
|---|
| 324 | end; | 
|---|
| 325 |  | 
|---|
| 326 |  | 
|---|
| 327 | function THRBufferChar.GetAsPChar:PChar; | 
|---|
| 328 | begin | 
|---|
| 329 | Result:=FBuffer; | 
|---|
| 330 | end; | 
|---|
| 331 |  | 
|---|
| 332 |  | 
|---|
| 333 | procedure THRBufferChar.SetAsPChar(Value:PChar); | 
|---|
| 334 | var | 
|---|
| 335 | L:Integer; | 
|---|
| 336 | begin | 
|---|
| 337 | L:=StrLen(Value); | 
|---|
| 338 | if L<=FSize | 
|---|
| 339 | then | 
|---|
| 340 | begin | 
|---|
| 341 | {Copies from value buffer to FBuffer.} | 
|---|
| 342 | StrMove(FBuffer,Value,L); | 
|---|
| 343 | FEOB:=False; | 
|---|
| 344 | end | 
|---|
| 345 | else | 
|---|
| 346 | begin | 
|---|
| 347 | FEOB:=True; | 
|---|
| 348 | end; | 
|---|
| 349 | end; | 
|---|
| 350 |  | 
|---|
| 351 |  | 
|---|
| 352 | function THRBufferChar.GetAsString:string; | 
|---|
| 353 | begin | 
|---|
| 354 | Result:=''; | 
|---|
| 355 | end; | 
|---|
| 356 |  | 
|---|
| 357 |  | 
|---|
| 358 | procedure THRBufferChar.SetAsString(Value:string); | 
|---|
| 359 | begin | 
|---|
| 360 | end; | 
|---|
| 361 |  | 
|---|
| 362 |  | 
|---|
| 363 |  | 
|---|
| 364 |  | 
|---|
| 365 | { T H R B u f f e r S t r e a m } | 
|---|
| 366 |  | 
|---|
| 367 |  | 
|---|
| 368 | constructor THRBufferStream.Create; | 
|---|
| 369 | begin | 
|---|
| 370 | inherited Create; | 
|---|
| 371 | FStream:=nil; | 
|---|
| 372 | FFirstPosInBuffer:=-1; | 
|---|
| 373 | end; | 
|---|
| 374 |  | 
|---|
| 375 |  | 
|---|
| 376 | destructor THRBufferStream.Destroy; | 
|---|
| 377 | begin | 
|---|
| 378 | inherited Destroy; | 
|---|
| 379 | end; | 
|---|
| 380 |  | 
|---|
| 381 |  | 
|---|
| 382 | procedure THRBufferStream.SetStream(Value:TStream); | 
|---|
| 383 | begin | 
|---|
| 384 | if Value<>FStream | 
|---|
| 385 | then | 
|---|
| 386 | begin | 
|---|
| 387 | FStream:=Value; | 
|---|
| 388 | FStreamSize:=FStream.Size; | 
|---|
| 389 | FFirstPosInBuffer:=-1; | 
|---|
| 390 | end; | 
|---|
| 391 | end; | 
|---|
| 392 |  | 
|---|
| 393 |  | 
|---|
| 394 | function THRBufferStream.GetEOB:Boolean; | 
|---|
| 395 | begin | 
|---|
| 396 | Result:=FEOB; | 
|---|
| 397 | end; | 
|---|
| 398 |  | 
|---|
| 399 |  | 
|---|
| 400 | function THRBufferStream.GetItems(Index:LongInt):Char; | 
|---|
| 401 | begin | 
|---|
| 402 | if Index<FStreamSize | 
|---|
| 403 | then | 
|---|
| 404 | begin | 
|---|
| 405 | if (Index>=FFirstPosInBuffer+FSize) or | 
|---|
| 406 | (Index<FFirstPosInBuffer) or | 
|---|
| 407 | (FFirstPosInBuffer=-1) | 
|---|
| 408 | then | 
|---|
| 409 | begin | 
|---|
| 410 | {Read next block from stream into buffer.} | 
|---|
| 411 | FStream.Position:=Index; | 
|---|
| 412 | FStream.Read(FBuffer[0],FSize); | 
|---|
| 413 | FFirstPosInBuffer:=Index; | 
|---|
| 414 | end; | 
|---|
| 415 | {Read from buffer} | 
|---|
| 416 | Result:=FBuffer[Index-FFirstPosInBuffer]; | 
|---|
| 417 | FEOB:=False; | 
|---|
| 418 | end | 
|---|
| 419 | else | 
|---|
| 420 | begin | 
|---|
| 421 | {EOB} | 
|---|
| 422 | Result:=#0; | 
|---|
| 423 | FEOB:=True; | 
|---|
| 424 | end; | 
|---|
| 425 | end; | 
|---|
| 426 |  | 
|---|
| 427 |  | 
|---|
| 428 | procedure THRBufferStream.SetItems(Index:LongInt;Value:Char); | 
|---|
| 429 | begin | 
|---|
| 430 | end; | 
|---|
| 431 |  | 
|---|
| 432 |  | 
|---|
| 433 | { T H R B u f f e r L i s t } | 
|---|
| 434 |  | 
|---|
| 435 | type | 
|---|
| 436 | PHRInteger=^Integer; | 
|---|
| 437 |  | 
|---|
| 438 |  | 
|---|
| 439 | constructor THRBufferList.Create; | 
|---|
| 440 | begin | 
|---|
| 441 | inherited Create; | 
|---|
| 442 | {Set count to zero.} | 
|---|
| 443 | Size:=SizeOf(Integer); | 
|---|
| 444 | PHRInteger(Buffer)^:=0; | 
|---|
| 445 | end; | 
|---|
| 446 |  | 
|---|
| 447 |  | 
|---|
| 448 | destructor THRBufferList.Destroy; | 
|---|
| 449 | begin | 
|---|
| 450 | inherited Destroy; | 
|---|
| 451 | end; | 
|---|
| 452 |  | 
|---|
| 453 |  | 
|---|
| 454 | function THRBufferList.GetItemPos(const Index:Integer):Integer; | 
|---|
| 455 | var | 
|---|
| 456 | PosIndex:Integer; | 
|---|
| 457 | Pos:Integer; | 
|---|
| 458 | PosItemSize:Integer; | 
|---|
| 459 | begin | 
|---|
| 460 | {Check for out of bounds index.} | 
|---|
| 461 | Assert(Index<PHRInteger(Buffer)^,'Index out of bounds'); | 
|---|
| 462 | {Step past count.} | 
|---|
| 463 | Pos:=SizeOf(Integer); | 
|---|
| 464 | {Loop thought items.} | 
|---|
| 465 | PosIndex:=0; | 
|---|
| 466 | while PosIndex<Index do | 
|---|
| 467 | begin | 
|---|
| 468 | {Get item size.} | 
|---|
| 469 | PosItemSize:=PHRInteger(Buffer+Pos)^; | 
|---|
| 470 | {Step over item.} | 
|---|
| 471 | Pos:=Pos+SizeOf(Integer)+PosItemSize; | 
|---|
| 472 | Inc(PosIndex); | 
|---|
| 473 | end; | 
|---|
| 474 | Result:=Pos; | 
|---|
| 475 | end; | 
|---|
| 476 |  | 
|---|
| 477 |  | 
|---|
| 478 | function THRBufferList.GetCount:Integer; | 
|---|
| 479 | begin | 
|---|
| 480 | Result:=PHRInteger(Buffer)^; | 
|---|
| 481 | end; | 
|---|
| 482 |  | 
|---|
| 483 |  | 
|---|
| 484 | function THRBufferList.GetItemSize(Index:Integer):Integer; | 
|---|
| 485 | begin | 
|---|
| 486 | Result:=PHRInteger(Buffer+GetItemPos(Index))^; | 
|---|
| 487 | end; | 
|---|
| 488 |  | 
|---|
| 489 |  | 
|---|
| 490 | procedure THRBufferList.SetItemSize(Index:Integer;Value:Integer); | 
|---|
| 491 | var | 
|---|
| 492 | Pos:Integer; | 
|---|
| 493 | ItemSize:Integer; | 
|---|
| 494 | Diff:Integer; | 
|---|
| 495 | OldSize:Integer; | 
|---|
| 496 | S,D:PChar; | 
|---|
| 497 | C:Integer; | 
|---|
| 498 | begin | 
|---|
| 499 | Pos:=GetItemPos(Index); | 
|---|
| 500 |  | 
|---|
| 501 | {Calc diff is size.} | 
|---|
| 502 | ItemSize:=PHRInteger(Buffer+Pos)^; | 
|---|
| 503 | Diff:=Value-ItemSize; | 
|---|
| 504 |  | 
|---|
| 505 | {No change in size.} | 
|---|
| 506 | if Diff=0 then Exit; | 
|---|
| 507 |  | 
|---|
| 508 | if Diff<0 | 
|---|
| 509 | then | 
|---|
| 510 | begin | 
|---|
| 511 | {Shrink buffer} | 
|---|
| 512 | {Move items > index down buffer.} | 
|---|
| 513 | S:=Buffer+Pos+SizeOf(Integer)+ItemSize; | 
|---|
| 514 | D:=S+Diff; | 
|---|
| 515 | C:=Size-(Pos+SizeOf(Integer)+ItemSize); | 
|---|
| 516 | Move(S[0],D[0],C); | 
|---|
| 517 | {Dec buffer size} | 
|---|
| 518 | Size:=Size+Diff; | 
|---|
| 519 | end | 
|---|
| 520 | else | 
|---|
| 521 | begin | 
|---|
| 522 | {Grow buffer} | 
|---|
| 523 | OldSize:=Size; | 
|---|
| 524 | {Inc buffer size} | 
|---|
| 525 | Size:=Size+Diff; | 
|---|
| 526 | {Move items > index up buffer.} | 
|---|
| 527 | S:=Buffer+Pos+SizeOf(Integer)+ItemSize; | 
|---|
| 528 | D:=S+Diff; | 
|---|
| 529 | C:=OldSize-(Pos+SizeOf(Integer)+ItemSize); | 
|---|
| 530 | Move(S[0],D[0],C); | 
|---|
| 531 | end; | 
|---|
| 532 |  | 
|---|
| 533 | {Set items new size.} | 
|---|
| 534 | PHRInteger(Buffer+Pos)^:=Value; | 
|---|
| 535 | end; | 
|---|
| 536 |  | 
|---|
| 537 |  | 
|---|
| 538 | function THRBufferList.GetItemBuffer(Index:Integer):PChar; | 
|---|
| 539 | begin | 
|---|
| 540 | Result:=Buffer+GetItemPos(Index)+SizeOf(Integer); | 
|---|
| 541 | end; | 
|---|
| 542 |  | 
|---|
| 543 |  | 
|---|
| 544 | procedure THRBufferList.Add(const Index,ItemSize:Integer); | 
|---|
| 545 | var | 
|---|
| 546 | PosIndex:Integer; | 
|---|
| 547 | Pos:Integer; | 
|---|
| 548 | PosItemSize:Integer; | 
|---|
| 549 | OldSize:Integer; | 
|---|
| 550 | S,D:PChar; | 
|---|
| 551 | C:Integer; | 
|---|
| 552 | begin | 
|---|
| 553 | {Step past count.} | 
|---|
| 554 | Pos:=SizeOf(Integer); | 
|---|
| 555 |  | 
|---|
| 556 | {Step thought list until up to index or end list.} | 
|---|
| 557 | PosIndex:=0; | 
|---|
| 558 | while (PosIndex<Index)and(PosIndex<=PHRInteger(Buffer)^-1) do | 
|---|
| 559 | begin | 
|---|
| 560 | {Get item size.} | 
|---|
| 561 | PosItemSize:=PHRInteger(Buffer+Pos)^; | 
|---|
| 562 | {Step over item.} | 
|---|
| 563 | Pos:=Pos+SizeOf(Integer)+PosItemSize; | 
|---|
| 564 | Inc(PosIndex); | 
|---|
| 565 | end; | 
|---|
| 566 |  | 
|---|
| 567 | {Pad list with empty items up to index.} | 
|---|
| 568 | while (PosIndex<Index) do | 
|---|
| 569 | begin | 
|---|
| 570 | {Add item.} | 
|---|
| 571 | Size:=Size+SizeOf(Integer); | 
|---|
| 572 | {Set size of item to zero.} | 
|---|
| 573 | PHRInteger(Buffer+Pos)^:=0; | 
|---|
| 574 | {Inc count} | 
|---|
| 575 | Inc(PHRInteger(Buffer)^); | 
|---|
| 576 | {Step over item.} | 
|---|
| 577 | Pos:=Pos+SizeOf(Integer); | 
|---|
| 578 | Inc(PosIndex); | 
|---|
| 579 | end; | 
|---|
| 580 |  | 
|---|
| 581 | {Resize buffer to accomodate new item.} | 
|---|
| 582 | OldSize:=Size; | 
|---|
| 583 | Size:=Size+SizeOf(Integer)+ItemSize; | 
|---|
| 584 |  | 
|---|
| 585 | {Push any items > index up buffer.} | 
|---|
| 586 | if PosIndex<=PHRInteger(Buffer)^-1 | 
|---|
| 587 | then | 
|---|
| 588 | begin | 
|---|
| 589 | S:=Buffer+Pos; | 
|---|
| 590 | D:=Buffer+Pos+SizeOf(Integer)+ItemSize; | 
|---|
| 591 | C:=OldSize-Pos; | 
|---|
| 592 | Move(S[0],D[0],C); | 
|---|
| 593 | end; | 
|---|
| 594 |  | 
|---|
| 595 | {Set size of item.} | 
|---|
| 596 | PHRInteger(Buffer+Pos)^:=ItemSize; | 
|---|
| 597 | {Inc count.} | 
|---|
| 598 | Inc(PHRInteger(Buffer)^); | 
|---|
| 599 | end; | 
|---|
| 600 |  | 
|---|
| 601 |  | 
|---|
| 602 | procedure THRBufferList.Delete(const Index:Integer); | 
|---|
| 603 | begin | 
|---|
| 604 | // find index | 
|---|
| 605 | // get size | 
|---|
| 606 | // move everthing > index down by sizeof(Integer) + index[size] | 
|---|
| 607 | // dec buffer size by sizeof(Integer) + index[size] | 
|---|
| 608 | // dec count | 
|---|
| 609 | end; | 
|---|
| 610 |  | 
|---|
| 611 |  | 
|---|
| 612 | end. | 
|---|