DEADSOFTWARE

wadcvt can convert images to pngs
[d2df-sdl.git] / src / shared / utils.pas
index 235dee4a95627e1a39f8335aa81ade78f6648f1f..eab26fb9e07e53fefc6d68377b6e713866c8d94c 100644 (file)
@@ -20,6 +20,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 +38,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 +155,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 +395,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.