X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fshared%2Futils.pas;h=f67317832b7b01cfe5ac49a950304a03ff0ab804;hb=ed2957d16cd248859884dc5ce8be287616f5e546;hp=235dee4a95627e1a39f8335aa81ade78f6648f1f;hpb=03ec2f1d27fdcff9a5a8785806fcd8449f2537a9;p=d2df-sdl.git diff --git a/src/shared/utils.pas b/src/shared/utils.pas index 235dee4..f673178 100644 --- a/src/shared/utils.pas +++ b/src/shared/utils.pas @@ -1,4 +1,19 @@ -{$MODE DELPHI} +(* Copyright (C) DooM 2D:Forever Developers + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *) +{$INCLUDE a_modes.inc} unit utils; interface @@ -20,6 +35,7 @@ function addWadExtension (fn: AnsiString): AnsiString; function Int64ToStrComma (i: Int64): AnsiString; function UpCase1251 (ch: Char): Char; +function LoCase1251 (ch: Char): Char; // `true` if strings are equal; ignoring case for cp1251 function StrEquCI1251 (const s0, s1: AnsiString): Boolean; @@ -37,9 +53,48 @@ function findFileCI (var pathname: AnsiString; lastIsDir: Boolean=false): Boolea function openDiskFileRO (pathname: AnsiString): TStream; function createDiskFile (pathname: AnsiString): TStream; +// little endian +procedure writeInt (st: TStream; v: Byte); overload; +procedure writeInt (st: TStream; v: ShortInt); overload; +procedure writeInt (st: TStream; v: Word); overload; +procedure writeInt (st: TStream; v: SmallInt); overload; +procedure writeInt (st: TStream; v: LongWord); overload; +procedure writeInt (st: TStream; v: LongInt); overload; +procedure writeInt (st: TStream; v: Int64); overload; +procedure writeInt (st: TStream; v: UInt64); overload; + +function readByte (st: TStream): Byte; +function readShortInt (st: TStream): ShortInt; +function readWord (st: TStream): Word; +function readSmallInt (st: TStream): SmallInt; +function readLongWord (st: TStream): LongWord; +function readLongInt (st: TStream): LongInt; +function readInt64 (st: TStream): Int64; +function readUInt64 (st: TStream): UInt64; + +// big endian +procedure writeIntBE (st: TStream; v: Byte); overload; +procedure writeIntBE (st: TStream; v: ShortInt); overload; +procedure writeIntBE (st: TStream; v: Word); overload; +procedure writeIntBE (st: TStream; v: SmallInt); overload; +procedure writeIntBE (st: TStream; v: LongWord); overload; +procedure writeIntBE (st: TStream; v: LongInt); overload; +procedure writeIntBE (st: TStream; v: Int64); overload; +procedure writeIntBE (st: TStream; v: UInt64); overload; + +function readByteBE (st: TStream): Byte; +function readShortIntBE (st: TStream): ShortInt; +function readWordBE (st: TStream): Word; +function readSmallIntBE (st: TStream): SmallInt; +function readLongWordBE (st: TStream): LongWord; +function readLongIntBE (st: TStream): LongInt; +function readInt64BE (st: TStream): Int64; +function readUInt64BE (st: TStream): UInt64; + implementation + function hasWadExtension (fn: AnsiString): Boolean; begin fn := ExtractFileExt(fn); @@ -115,6 +170,30 @@ begin end; +function LoCase1251 (ch: Char): Char; +begin + if ch < #128 then + begin + if (ch >= 'A') and (ch <= 'Z') then Inc(ch, 32); + end + else + begin + if (ch >= #192) and (ch <= #223) then + begin + Inc(ch, 32); + end + else + begin + case ch of + #168, #170, #175: Inc(ch, 16); + #161, #178: Inc(ch); + end; + end; + end; + result := ch; +end; + + function StrEquCI1251 (const s0, s1: AnsiString): Boolean; var i: Integer; @@ -331,4 +410,118 @@ begin end; +procedure writeIntegerLE (st: TStream; vp: Pointer; size: Integer); +{$IFDEF ENDIAN_LITTLE} +begin + st.writeBuffer(vp^, size); +end; +{$ELSE} +var + p: PByte; +begin + p := PByte(vp)+size-1; + while size > 0 do + begin + st.writeBuffer(p^, 1); + Dec(size); + Dec(p); + end; +end; +{$ENDIF} + +procedure writeIntegerBE (st: TStream; vp: Pointer; size: Integer); +{$IFDEF ENDIAN_LITTLE} +var + p: PByte; +begin + p := PByte(vp)+size-1; + while size > 0 do + begin + st.writeBuffer(p^, 1); + Dec(size); + Dec(p); + end; +end; +{$ELSE} +begin + st.writeBuffer(vp^, size); +end; +{$ENDIF} + +procedure writeInt (st: TStream; v: Byte); overload; begin writeIntegerLE(st, @v, 1); end; +procedure writeInt (st: TStream; v: ShortInt); overload; begin writeIntegerLE(st, @v, 1); end; +procedure writeInt (st: TStream; v: Word); overload; begin writeIntegerLE(st, @v, 2); end; +procedure writeInt (st: TStream; v: SmallInt); overload; begin writeIntegerLE(st, @v, 2); end; +procedure writeInt (st: TStream; v: LongWord); overload; begin writeIntegerLE(st, @v, 4); end; +procedure writeInt (st: TStream; v: LongInt); overload; begin writeIntegerLE(st, @v, 4); end; +procedure writeInt (st: TStream; v: Int64); overload; begin writeIntegerLE(st, @v, 8); end; +procedure writeInt (st: TStream; v: UInt64); overload; begin writeIntegerLE(st, @v, 8); end; + +procedure writeIntBE (st: TStream; v: Byte); overload; begin writeIntegerBE(st, @v, 1); end; +procedure writeIntBE (st: TStream; v: ShortInt); overload; begin writeIntegerBE(st, @v, 1); end; +procedure writeIntBE (st: TStream; v: Word); overload; begin writeIntegerBE(st, @v, 2); end; +procedure writeIntBE (st: TStream; v: SmallInt); overload; begin writeIntegerBE(st, @v, 2); end; +procedure writeIntBE (st: TStream; v: LongWord); overload; begin writeIntegerBE(st, @v, 4); end; +procedure writeIntBE (st: TStream; v: LongInt); overload; begin writeIntegerBE(st, @v, 4); end; +procedure writeIntBE (st: TStream; v: Int64); overload; begin writeIntegerBE(st, @v, 8); end; +procedure writeIntBE (st: TStream; v: UInt64); overload; begin writeIntegerBE(st, @v, 8); end; + + +procedure readIntegerLE (st: TStream; vp: Pointer; size: Integer); +{$IFDEF ENDIAN_LITTLE} +begin + st.readBuffer(vp^, size); +end; +{$ELSE} +var + p: PByte; +begin + p := PByte(vp)+size-1; + while size > 0 do + begin + st.readBuffer(p^, 1); + Dec(size); + Dec(p); + end; +end; +{$ENDIF} + +procedure readIntegerBE (st: TStream; vp: Pointer; size: Integer); +{$IFDEF ENDIAN_LITTLE} +var + p: PByte; +begin + p := PByte(vp)+size-1; + while size > 0 do + begin + st.readBuffer(p^, 1); + Dec(size); + Dec(p); + end; +end; +{$ELSE} +begin + st.readBuffer(vp^, size); +end; +{$ENDIF} + +function readByte (st: TStream): Byte; begin readIntegerLE(st, @result, 1); end; +function readShortInt (st: TStream): ShortInt; begin readIntegerLE(st, @result, 1); end; +function readWord (st: TStream): Word; begin readIntegerLE(st, @result, 2); end; +function readSmallInt (st: TStream): SmallInt; begin readIntegerLE(st, @result, 2); end; +function readLongWord (st: TStream): LongWord; begin readIntegerLE(st, @result, 4); end; +function readLongInt (st: TStream): LongInt; begin readIntegerLE(st, @result, 4); end; +function readInt64 (st: TStream): Int64; begin readIntegerLE(st, @result, 8); end; +function readUInt64 (st: TStream): UInt64; begin readIntegerLE(st, @result, 8); end; + +function readByteBE (st: TStream): Byte; begin readIntegerBE(st, @result, 1); end; +function readShortIntBE (st: TStream): ShortInt; begin readIntegerBE(st, @result, 1); end; +function readWordBE (st: TStream): Word; begin readIntegerBE(st, @result, 2); end; +function readSmallIntBE (st: TStream): SmallInt; begin readIntegerBE(st, @result, 2); end; +function readLongWordBE (st: TStream): LongWord; begin readIntegerBE(st, @result, 4); end; +function readLongIntBE (st: TStream): LongInt; begin readIntegerBE(st, @result, 4); end; +function readInt64BE (st: TStream): Int64; begin readIntegerBE(st, @result, 8); end; +function readUInt64BE (st: TStream): UInt64; begin readIntegerBE(st, @result, 8); end; + + end.