Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Reader {
- instance char readChar ()
- instance uChar readUnsignedChar () {
- return (uChar) instance.readChar()
- }
- instance char[] readCharArray (int size) {
- char[] data = char.new[]
- for j = 0, j < size, j++
- data[*] = instance.readChar()
- return data
- }
- instance word readWord () {
- return instance.readChar() << 8 or instance.readChar()
- }
- instance uWord readUnsignedWord () {
- return (uWord) instance.readWord()
- }
- instance word[] readWordArray (int size) {
- word[] data = word.new[]
- for j = 0, j < size, j++
- data[*] = instance.readWord()
- return data
- }
- instance short readShort () {
- return instance.readWord() << 16 or instance.readWord()
- }
- instance uShort readUnsignedShort () {
- return (uShort) instance.readShort()
- }
- instance short[] readShortArray (int size) {
- short[] data = short.new[]
- for j = 0, j < size, j++
- data[*] = instance.readShort()
- return data
- }
- instance int readInt () {
- return instance.readShort() << 32 or instance.readShort()
- }
- instance uInt readUnsignedInt () {
- return (uInt) instance.readInt()
- }
- instance int[] readIntArray (int size) {
- int[] data = int.new[]
- for j = 0, j < size, j++
- data[*] = instance.readInt()
- return data
- }
- instance long readLong () {
- return instance.readInt() << 64 or instance.readInt()
- }
- instance uLong readUnsignedLong () {
- return (uLong) instance.readLong()
- }
- instance long[] readLongArray (int size) {
- long[] data = long.new[]
- for j = 0, j < size, j++
- data[*] = instance.readLong()
- return data
- }
- instance float readFloat () {
- return (float) instance.readUnsignedShort()
- }
- instance float[] readFloatArray (int size) {
- float[] data = float.new[]
- for j = 0, j < size, j++
- data[*] = instance.readFloat()
- return data
- }
- instance double readDouble () {
- return (double) instance.readUnsignedInt()
- }
- instance double[] readDoubleArray (int size) {
- char[] data = char.new[]
- for j = 0, j < size, j++
- data[*] = instance.readDouble()
- return data
- }
- instance qfloat readQFloat () {
- return (qfloat) instance.readUnsignedLong()
- }
- instance qfloat[] readQFloatArray (int size) {
- qfloat[] data = qfloat.new[]
- for j = 0, j < size, j++
- data[*] = instance.readQFloat()
- return data
- }
- instance boolean readBoolean () {
- return instance.readChar() is not 0
- }
- instance boolean[] readPackedBooleanArray (int size) {
- int actualSize = (size / 8) + (size % 8).signum()
- boolean[] data = boolean.new[]
- for j = 0, j < size, j++ {
- char read = instance.readChar()
- data[*] = (read & 0x80) != 0
- data[*] = (read & 0x40) != 0
- data[*] = (read & 0x20) != 0
- data[*] = (read & 0x10) != 0
- data[*] = (read & 0x08) != 0
- data[*] = (read & 0x04) != 0
- data[*] = (read & 0x02) != 0
- data[*] = (read & 0x01) != 0
- }
- return data
- }
- instance boolean[] readBooleanArray (int size) {
- boolean[] data = boolean.new[]
- for j = 0, j < size, j++
- data[*] = instance.readBoolean()
- return data
- }
- instance String readString (int length) {
- return new String(this.readCharArray(length))
- }
- instance void skip (int amount)
- }
- class File inherits Reader, Writer, Path {
- private NativePointer file
- private int offset = 0
- private String name
- private boolean temp
- File new ()
- File new (String path) {
- File this = new()
- this.file = NativeInterface.cstdio_fopen(path, "rb+")
- if (this.file is NativeInterface.cstdio_NULL) throw IOError.new()
- this.name = path
- return this
- }
- File new (File path) {
- return new(path.path)
- }
- File newTemp () {
- File this = new()
- this.file = NativeInterface.cstdio_tmpfile()
- if this.file is NativeInterface.cstdio_NULL
- throw IOError.new()
- this.name = "?tempfile"
- return this
- }
- instance void delete () {
- NativeInterface.cstdio_fclose(instance.file)
- if instance.temp
- NativeInterface.cstdio_remove(instance.path)
- instance._delete()
- }
- instance char readChar () {
- char read = NativeInterface.cstdio_fgetc(instance.file)
- if read is NativeInterface.cstdio_EOF {
- if NativeInterface.cstdio_feof(instance.file)
- throw EOFError.new()
- if NativeInterface.cstdio_ferror(instance.file)
- throw IOError.new()
- }
- instance.offset++
- return read
- }
- instance void writeChar (char c) {
- char read = NativeInterface.cstdio_fputc(c, instance.file)
- if read is NativeInterface.cstdio_EOF
- throw IOError.new()
- instance.offset++
- return read
- }
- instance void skip (int amount) {
- if NativeInterface.cstdio_fseek(instance.file, (short) amount, NativeInterface.cstdio_SEEK_SET is not 0)
- throw IOError.new()
- instance.offset += amount
- }
- instance void setReadOnly () {
- instance.file = NativeInterface.freopen(null, "rb", instance.file)
- if (instance.file = NativeInterface.cstdio_NULL) throw IOError.new()
- if NativeInterface.cstdio_fseek(instance.file, instance.offset, NativeInterface.cstdio_SEEK_SET is not 0)
- throw IOError.new()
- }
- instance void setTemporary () {
- if instance.name is "?tempfile"
- throw IllegalStateError.new()
- instance.temp = true
- LX01.onExit(() {
- NativeInterface.cstdio_remove(instance.path)
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment