Author Topic: Binary Functions  (Read 1033 times)

0 Members and 1 Guest are viewing this topic.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Binary Functions
« on: March 22, 2009, 10:19:21 am »
Script Name: Binary Functions
Script Description Binary file reading and writing
Author: DorkeyDear
Compile Test: Passed
Core Version: 2.6.3
Hosted by: Soldat Central - http://soldatcentral.com/

Full Description:
This is a set of functions to read and write binary data. Note that this does not actually stream the data from the file. When fOpen is first called, it collects all data from it. And when writing, it only writes when fFlush or fClose is called.

Functions:
function fOpen(const Filename: string): integer;
procedure fSaveAs(const Handle: word; const Filename: string);
procedure fClose(const Handle: integer);
procedure fSeekBeginning(const Handle, Position: word);
procedure fSeekRelative(const Handle, Position: integer);
function fReadChar(const Handle: word): char;
function fReadUInt8(const Handle: word): byte;
function fReadSInt8(const Handle: word): shortint;
function fReadUInt16(const Handle: word): word;
function fReadSInt16(const Handle: word): smallint;
function fReadUInt32(const Handle: word): cardinal;
function fReadSInt32(const Handle: word): integer;
function fReadAryChar(const Handle, Length: word): array of char;
function fReadAryUInt8(const Handle, Length: word): array of byte;
function fReadBool(const Handle: word): boolean;
function fReadString(const Handle: word): string;
procedure fWriteChar(const Handle: word; const Value: char);
procedure fWriteUInt8(const Handle: word; const Value: byte);
procedure fWriteSInt8(const Handle: word; const Value: shortint);
procedure fWriteUInt16(const Handle, Value: word);
procedure fWriteSInt16(const Handle: word; const Value: smallint);
procedure fWriteUInt32(const Handle: word; const Value: cardinal);
procedure fWriteSInt32(const Handle: word; const Value: integer);
procedure fWriteBool(const Handle: word; const Value: boolean);
procedure fWriteString(const Handle: word; const Value: string);

Functions fRead<type> and fWrite<type> types are named slightly different than they are in pascal. Here are the different ones:
UInt8 = byte
SInt8 = shortint
UInt16 = word
SInt16 = smallint
UInt32 = cardinal / longword
SInt32 = integer / longint
AryByte = array of byte
AryChar = array of char
Bool = boolean

Also please note that ReadFile sometimes adds extra linebreaks at the end of the file, so that may cause for incorrect data when trying to access it.
Seeking is in bytes, not bits.
One boolean is one byte.

An example has been included at the bottom under ActivateServer modifying the file 'test' in the main directory.





(Size 1.7 KB)
- http://soldatcentral.com/index.php?page=script&f=105 -


** Script hosted by Soldat Central! Please visit the author's script page and Rate this script **