PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Wrong function return for file procedures?  (Read 524 times)

Bumblebee

  • Little Newbie
  • *
  • Posts: 5
Wrong function return for file procedures?
« on: January 19, 2020, 11:29:05 AM »

If my understanding is correct, the Afx file procedures listed below should return true upon each successful file operation.
As it stands, variable y only returns true for AfxMakeDir and AfxKill. The other functions return false even though their disk operations were successful.
What am I doing wrong?

Relevant code excerpt:

Code: [Select]
dim shared as CWSTR c,d,rm(),m
dim shared y as Boolean
dim shared g as Integer

select case g
  case 1 : y = AfxMakeDir (d)
  case 2 : y = AfxFileCopy (c,d)
  case 3 : y = AfxName (c,d)
  case 4 : y = AfxKill (c)
  case 5 : y = AfxRemoveDir (d)
end select
if y = false then print "Error ^"

Edit: I forgot to test AfxRemoveDir... it also returns true.
Here is the output from a test of all five functions:

;
  Mirror backup  01-19-2020  12:20:57
  --------------------------------------------------
  C:\Decoy > E:\Decoy
  --------------------------------------------------
  Create folder  E:\Decoy\hello [true]
         Update  E:\Decoy\Software\fb\backup.bas [false]
         Update  E:\Decoy\Software\fb\backup.exe [false]
   Empty folder  E:\Decoy\download
  File moved to  E:\Decoy\hello\Aquarius.jpg [false]
           Copy  C:\Decoy\hello\Athena.jpg [false]
  File moved to  E:\Decoy\hello\bartlett.jpg [false]
  File moved to  E:\Decoy\hello\crow.jpg [false]
  File moved to  E:\Decoy\hello\crows in cemetary.jpg [false]
  File moved to  E:\Decoy\hello\current.alm [false]
  File moved to  E:\Decoy\hello\kayla-maurais-Vsllwfgk4yw-unsplash.jpg [false]
  File moved to  E:\Decoy\hello\misc.txt [false]
           Copy  C:\Decoy\hello\abc.gif [false]
           Copy  C:\Decoy\Software\fb\crow.exe [false]
         Delete  E:\Decoy\download\abc.gif [true]
   Erase folder  E:\Decoy\download [true]
»\(║_║)/»                   »\(║_║)/»

 13998 files processed. Backup complete.
« Last Edit: January 19, 2020, 12:29:08 PM by Bumblebee »
Logged

JosÚ Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3229
Re: Wrong function return for file procedures?
« Reply #1 on: January 19, 2020, 02:32:36 PM »

Both AfxFileCopy, AfxKill and AfxName are unicode replacements for FreeBasic FileCopy and Name functions, and return the same values that the functions that they replace.

For functions that return true instead of false, use AfxCopyFile, AfxDeleteFile and AfxRenameFile.

As stated in the documentation:

AfxMkDir is an unicode replacement for Free Basic's MkDir and returns 0 on success, or -1 on failure.
AfxKill is an unicode replacement for Free Basic's Kill and returns 0 on success, or -1 on failure.
AfxName is an unicode replacement for Free Basic's Name and returns 0 on success, or non-zero on failure.
AfxRmDir is an unicode replacement for Free Basic's RmDir and returns 0 on success, or -1 on failure.


Bumblebee

  • Little Newbie
  • *
  • Posts: 5
Re: Wrong function return for file procedures?
« Reply #2 on: January 19, 2020, 03:37:02 PM »

dim shared y as integer

;
  Mirror backup  01-19-2020  15:17:18
  --------------------------------------------------
  C:\Decoy > E:\Decoy
  --------------------------------------------------
  Create folder  E:\Decoy\download [-1]
         Update  E:\Decoy\Software\fb\backup.bas [ 0 ]
         Update  E:\Decoy\Software\fb\backup.exe [ 0 ]
   Empty folder  E:\Decoy\hello
  File moved to  E:\Decoy\download\Aquarius.jpg [ 0 ]
  File moved to  E:\Decoy\download\Athena.jpg [ 0 ]
  File moved to  E:\Decoy\download\bartlett.jpg [ 0 ]
  File moved to  E:\Decoy\download\crow.jpg [ 0 ]
  File moved to  E:\Decoy\download\crows in cemetary.jpg [ 0 ]
  File moved to  E:\Decoy\download\current.alm [ 0 ]
  File moved to  E:\Decoy\download\fantasy map.map [ 0 ]
  File moved to  E:\Decoy\download\Fool's Proof.jpg [ 0 ]
  File moved to  E:\Decoy\download\Gwhendolyn.gif [ 0 ]
  File moved to  E:\Decoy\download\Helena.png [ 0 ]
  File moved to  E:\Decoy\download\kayla-maurais-Vsllwfgk4yw-unsplash.jpg [ 0 ]
  File moved to  E:\Decoy\download\misc.txt [ 0 ]
  File moved to  E:\Decoy\download\Stereo Songweaver.jpg [ 0 ]
  File moved to  E:\Decoy\download\Summer Of The Fire Ship.jpg [ 0 ]
  File moved to  E:\Decoy\download\tcovrc.jpg [ 0 ]
  File moved to  E:\Decoy\download\abc.gif [ 0 ]
   Erase folder  E:\Decoy\hello [-1]

»\(║_║)/»                   »\(║_║)/»

 13982 files processed. Backup complete.


AfxName returns 0 on success
AfxMakeDir/AfxMkDir returns -1 on success
AfxRemoveDir/AfxRmDir returns -1 on success

Whether I use a boolean or integer variable, the returned value for some of the functions is incorrect.
As it stands now, the documentation is partially incorrect.

I can work around this, but why aren't the return codes consistent?
« Last Edit: January 19, 2020, 04:05:55 PM by Bumblebee »
Logged

JosÚ Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3229
Re: Wrong function return for file procedures?
« Reply #3 on: January 19, 2020, 04:17:06 PM »

They don't return incorrect results. There are some functions, named like the FB ansi functions but with the Afx prefix,  that return 0 on success for compatibility with the intrinsic FB functions, and there are others with different names that return True on success.

AfxName returns 0 on success for compatibility with FB's NAME function, but AfxRenameFile returns a boolean True or False.

Bumblebee

  • Little Newbie
  • *
  • Posts: 5
Re: Wrong function return for file procedures?
« Reply #4 on: January 19, 2020, 04:37:51 PM »

Ideally, I would select unicode replacements that return boolean True.
Do all these functions support unicode?
Logged

JosÚ Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3229
Re: Wrong function return for file procedures?
« Reply #5 on: January 19, 2020, 09:33:40 PM »

> Do all these functions support unicode?

Indeed. Everything in my framework is unicode aware.

Bumblebee

  • Little Newbie
  • *
  • Posts: 5
Re: Wrong function return for file procedures?
« Reply #6 on: January 21, 2020, 07:59:32 AM »

> Do all these functions support unicode?

Indeed. Everything in my framework is unicode aware.
Excellent. Thanks for your help.

To reiterate, AfxMkDir returns -1 on success and AfxRmDir returns -1 on success, contrary to what the documentation says. Their return codes are boolean.
Logged

JosÚ Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3229
Re: Wrong function return for file procedures?
« Reply #7 on: January 21, 2020, 06:42:01 PM »

Thanks. I'm going to revise them. The problem was that I wrote them before the boolean data type was implemented in FreeBsic. Previously, the current functions that return a boolean returned a long and a value of 0 or 1. After changing the return value from long to boolean, it returns 0 or -1, and NOT -1 returns -2 instead of 0.

JosÚ Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3229
Re: Wrong function return for file procedures?
« Reply #8 on: January 22, 2020, 11:17:04 PM »

Modified AfxChDir, AfxMkDir, AfxRmDir and AfxName to return 0 on success or -1 on failure.

Bumblebee

  • Little Newbie
  • *
  • Posts: 5
Re: Wrong function return for file procedures?
« Reply #9 on: February 17, 2020, 06:03:01 AM »

To confirm, are the following functions identical, other than the way they are spelled?
AfxMakeDir / AfxCreateDirectory
AfxRenameFile / AfxMoveFile
AfxRemoveDir / AfxRemoveDirectory
Logged

JosÚ Roca

  • Moderator
  • Guru Member
  • *****
  • Posts: 3229
Re: Wrong function return for file procedures?
« Reply #10 on: February 17, 2020, 08:12:18 AM »

Yes.