Page 1 of 1

ReadByte (and others) return code

Posted: Thu Aug 12, 2021 6:30 pm
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

Re: ReadByte (and others) return code

Posted: Thu Aug 12, 2021 6:44 pm
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...

Re: ReadByte (and others) return code

Posted: Fri Aug 13, 2021 1:48 pm
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)

Re: ReadByte (and others) return code

Posted: Fri Aug 13, 2021 8:21 pm
by miskox
Thank you all for the hints etc.. but I think that this problem should be addressed in PB itself.

Saso

Re: ReadByte (and others) return code

Posted: Fri Aug 13, 2021 9:04 pm
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

Re: ReadByte (and others) return code

Posted: Fri Aug 13, 2021 9:12 pm
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

Re: ReadByte (and others) return code

Posted: Fri Aug 13, 2021 11:34 pm
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.

Re: ReadByte (and others) return code

Posted: Sat Aug 14, 2021 1:12 am
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?

Re: ReadByte (and others) return code

Posted: Sat Aug 14, 2021 5:09 am
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.

Re: ReadByte (and others) return code

Posted: Sat Aug 14, 2021 5:43 am
by BarryG
Yeah, my memory isn't as good as it used to be. Thanks for the link.

Re: ReadByte (and others) return code

Posted: Sat Aug 14, 2021 9:02 am
by Mijikai
Instead of reinventing the wheel - what is wrong with ReadData()?

Re: ReadByte (and others) return code

Posted: Sat Aug 14, 2021 10:42 am
by BarryG
I'm losing my mind.

Re: ReadByte (and others) return code

Posted: Sat Aug 14, 2021 11:30 am
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