ReadByte (and others) return code

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
miskox
User
User
Posts: 95
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

ReadByte (and others) return code

Post by miskox »

As I already mentioned some time ago:

according to the manual ReadByte returns the read byte if successful and zero if not successfull. But we all know that zero is a valid value for a byte. So are there any plans to correct this?

How can a program distinguish between byte value of 0 (success) and error (return code also 0)?

Maybe a return variable should be more than one byte long so a return value of 256 (or more) means error. And then logical operater bitwise AND 0xFF is done to keep only the required bits.

The same goes for other Read commands.

Thanks.
Saso
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: ReadByte (and others) return code

Post by Mijikai »

-1
If you need that information use ReadData().
It will just add more overhead to the other functions which is not needed.
Optional just check the file pointer...
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: ReadByte (and others) return code

Post by NicTheQuick »

You could create your own version of ReadByte and similar by using Modules and and Wrapper like so:

Code: Select all

DeclareModule FileWrapper
	Macro ReadByte(hFile)
		FileWrapperImpl::_ReadByte(hFile)
	EndMacro
EndDeclareModule

Module FileWrapper
EndModule

DeclareModule FileWrapperImpl
	Declare _ReadByte(hFile.i)
EndDeclareModule
Module FileWrapperImpl
	Procedure.i _ReadByte(hFile.i)
		Protected b.b
		
		If Not IsFile(hFile)
			ProcedureReturn 257 ; file not initialized
		ElseIf Not ReadData(hFile, @b, SizeOf(Byte)) = SizeOf(Byte)
			ProcedureReturn 256; byte could not be read
		EndIf
		ProcedureReturn b
	EndProcedure
EndModule

; Calls the original ReadByte
;ReadByte(1)

; From now on only the overwritten version of ReadByte is used
UseModule FileWrapper

; Calls the new ReadByte
Debug ReadByte(1)
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
miskox
User
User
Posts: 95
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Re: ReadByte (and others) return code

Post by miskox »

Thank you all for the hints etc.. but I think that this problem should be addressed in PB itself.

Saso
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: ReadByte (and others) return code

Post by Mijikai »

Another option:

Code: Select all

EnableExplicit

Macro ReadByte(_hfile_,_buffer_);reads one byte - returns #True/#False
  Bool(ReadData(_hfile_,_buffer_,1) = 1)
EndMacro

Procedure.i Main()
  Protected hfile.i
  Protected first_byte.b
  hfile = ReadFile(#PB_Any,ProgramFilename())
  If hfile
    If ReadByte(hfile,@first_byte)
      Debug "0x" + Hex(first_byte,#PB_Byte)
    EndIf
    CloseFile(hfile)
  EndIf
  ProcedureReturn #Null
EndProcedure

Main()

End
Bitblazer
Enthusiast
Enthusiast
Posts: 733
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: ReadByte (and others) return code

Post by Bitblazer »

miskox wrote: Fri Aug 13, 2021 8:21 pm Thank you all for the hints etc.. but I think that this problem should be addressed in PB itself.
+1
webpage - discord chat links -> purebasic GPT4All
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ReadByte (and others) return code

Post by STARGÅTE »

When the documentation writes "Returns the read byte or zero if there was an error.", it just means: in case of any error, the return value is defined as 0, and is not undetermined. The return value cannot be used for error explanation. Of cause not, because the return value range is the full type range. Mixing error numbers and functional values it not a good idea and finally impossible for ReadQuad or ReadString.

For an advanced error investigation, PB would need something like LastError or in case of the file library: LastFileError(File). But at the moment PB has no special error codes in the file library.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: ReadByte (and others) return code

Post by BarryG »

I think this was discussed before, but why can't it just return -1 for an error instead of 0? When I look at all bytes of a file with a hex editor, they all show a value of 0-255 ($0-$FF), so if hex editors show and support that range, then why can't PureBasic use -1 for an error?
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: ReadByte (and others) return code

Post by Demivec »

BarryG wrote: Sat Aug 14, 2021 1:12 am I think this was discussed before, but why can't it just return -1 for an error instead of 0? When I look at all bytes of a file with a hex editor, they all show a value of 0-255 ($0-$FF), so if hex editors show and support that range, then why can't PureBasic use -1 for an error?
To refresh your memory:

Link to similar discussion in the General Discussion Forum: ReadByte/ReadAsciiCharacter return value on error.
Link to a resulting Feature Request: File Library: add GetLastFileError(file) command.

In the last thread link you asked the same question, it was answered by kerzur, and you then deleted your question because he answered it.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: ReadByte (and others) return code

Post by BarryG »

Yeah, my memory isn't as good as it used to be. Thanks for the link.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: ReadByte (and others) return code

Post by Mijikai »

Instead of reinventing the wheel - what is wrong with ReadData()?
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: ReadByte (and others) return code

Post by BarryG »

I'm losing my mind.
Last edited by BarryG on Sat Aug 14, 2021 11:34 am, edited 1 time in total.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: ReadByte (and others) return code

Post by Mijikai »

BarryG wrote: Sat Aug 14, 2021 10:42 am It's the same problem as ReadByte(): ReadData() will also return 0 for a read error, but the read byte could be 0 (not an error).
No it doesnt:
Returns the number of bytes actually read from the file. If there is an error, the return value is zero
Therefore the return value can be used.

Here is the POC:

Code: Select all

EnableExplicit

;override ReadByte() with our Macro

Macro ReadByte(_hfile_,_buffer_);reads one byte - returns #True on succes and #False if an error occured
  Bool(ReadData(_hfile_,_buffer_,1) = 1)
EndMacro

Procedure.i Main()
  Protected hfile.i
  Protected first_byte.b
  hfile = ReadFile(#PB_Any,ProgramFilename())
  If hfile
    ;poc: set the filepointer to the end to generate an error!
    FileSeek(hfile,Lof(hfile))
    ;lets try reading a byte :>
    If ReadByte(hfile,@first_byte)
      Debug "0x" + Hex(first_byte,#PB_Byte)
    Else
      Debug "byte could not be read!"
    EndIf
    CloseFile(hfile)
  EndIf
  ProcedureReturn #Null
EndProcedure

Main()

End
Post Reply