It is currently Sat Apr 29, 2017 2:26 pm



Welcome
Welcome to rfobasic

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today. **You are not required to provide truthful information to any registration questions. Be whomever you wish to be.!


Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Incorrect description in BYTE.COPY
Unread postPosted: Mon Sep 05, 2016 12:15 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5487
Location: Paris, France
De Re BASIC! v01.90 wrote:
Byte.copy <file_table_nexp>,<output_file_sexp>
Copies the previously open input file represented by <file_table_nexp> to the file whose path is specified by <output_file_sexp>. The default path is "<pref base drive>/rfo-basic/data/".

If <file_table_nexp> = -1 then a run-time error will be thrown.

The input file will be completely copied to the to the output file. Both files will then be closed.

You should use Byte.copy if you are using Byte I/O for the sole purpose of copying. It is thousands (literally) of times faster than using Byte.read/Byte.write.


Byte.copy starts copy from the position where we are in the input file, not from the start of the input file.
So the sentence in red is wrong.

Use this snippet for demo:
Code:
BYTE.OPEN r, fid, "http://mougino.free.fr/index.html"
BYTE.READ.BUFFER fid, 12, e$
PRINT e$
PRINT "--------"

BYTE.COPY fid, "mougino.html"

GRABFILE r$, "mougino.html"
PRINT LEFT$(r$, 20)+"..." % 12 first chars are missing


Nicolas

_________________
- Creator of the Android BASIC! Compiler


Top
 Profile  
 
 Post subject: Re: Incorrect description in BYTE.COPY
Unread postPosted: Mon Sep 05, 2016 12:34 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5487
Location: Paris, France
This is actually a very annoying bug :(
I would prefer that BASIC! does what the manual says it does...

If I opened and read some bytes from a file, there is no way to copy it at all.
I tried to do a BYTE.POSITION.SET fid, 1 after the BYTE.READ.BUFFER, but BASIC! throws a run-time error:
Code:
Error: Mark has been invalidated.


Nicolas

_________________
- Creator of the Android BASIC! Compiler


Top
 Profile  
 
 Post subject: Re: Incorrect description in BYTE.COPY
Unread postPosted: Mon Sep 05, 2016 12:52 am 
Offline
User avatar

Joined: Sun Nov 23, 2014 8:15 am
Posts: 2185
Location: romania
what if you close and open the file again?


Top
 Profile  
 
 Post subject: Re: Incorrect description in BYTE.COPY
Unread postPosted: Mon Sep 05, 2016 1:14 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5487
Location: Paris, France
yes that's a workaround, ugly but should work, except if the resource becomes unavailable the second time of course, loss of internet connection, lag, etc.

the thing is I already wrote 60 lines of code for the GW lib auto-upgrade mechanism, and to me that's like 3 times too much lines.
but sure, I'll add a dozen more to close file, re-open, re-check if fid=-1, re-display error message in this case, else go on...

in all cases, either BASIC! or the manual needs to be fixed

_________________
- Creator of the Android BASIC! Compiler


Top
 Profile  
 
 Post subject: Re: Incorrect description in BYTE.COPY
Unread postPosted: Mon Sep 05, 2016 10:10 am 
Offline
User avatar

Joined: Sun Nov 23, 2014 8:15 am
Posts: 2185
Location: romania
the solution is a separate dedicated command for any file: FILE.COPY... as are all File.... commands (don;t mess at byte level)
maybe u can use a system command?


Top
 Profile  
 
 Post subject: Re: Incorrect description in BYTE.COPY
Unread postPosted: Mon Sep 05, 2016 10:18 am 
Offline
User avatar

Joined: Thu Nov 21, 2013 7:00 pm
Posts: 631
I ran the code with BYTE.POSITION.SET fid, 1
I did not get an error, I got
0123456789

(Basic! v1.90, Android 4.03)
Code:
BYTE.OPEN w, fid, "test.bin"
BYTE.WRITE.BUFFER fid, "0123456789"
BYTE.CLOSE fid

BYTE.OPEN r, fid, "test.bin"
BYTE.READ.BUFFER fid, 3, e$
BYTE.POSITION.SET fid, 1
BYTE.COPY fid, "test2.bin"

GRABFILE r$, "test2.bin"
PRINT r$ % prints "0123456789"

_________________
- Failure is not an option. It is bundled with the software. -


Top
 Profile  
 
 Post subject: Re: Incorrect description in BYTE.COPY
Unread postPosted: Mon Sep 05, 2016 4:59 pm 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5487
Location: Paris, France
Right, sorry, my example was misleading: BYTE.POSITION.SET works on local files but not on internet files:
Code:
BYTE.OPEN r, fid, "http://mougino.free.fr/index.html"
BYTE.READ.BUFFER fid, 3, e$
BYTE.POSITION.SET fid, 1
BYTE.COPY fid, "mougino.html"

GRABFILE r$, "mougino.html"
PRINT LEFT$(r$, 20)+"..."

[edited snippet in front post]

Nicolas

_________________
- Creator of the Android BASIC! Compiler


Top
 Profile  
 
 Post subject: Re: Incorrect description in BYTE.COPY
Unread postPosted: Mon Sep 05, 2016 5:02 pm 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5487
Location: Paris, France
gikam wrote:
the solution is a separate dedicated command for any file: FILE.COPY... as are all File.... commands (don;t mess at byte level)
maybe u can use a system command?

Disagreed. The solution to a command not operating like described in the manual shall not be to add another new command, it is to fix either command or manual.

_________________
- Creator of the Android BASIC! Compiler


Top
 Profile  
 
 Post subject: Re: Incorrect description in BYTE.COPY
Unread postPosted: Mon Sep 05, 2016 6:48 pm 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5487
Location: Paris, France
In the end I went with the BYTE.READ.BUFFER / BYTE.CLOSE / BYTE.OPEN again for GW auto-update, but I used GOTOs (although I don't particularly like spaghetti code), this saved me about 20-30 lines:
Code:
IF !IS_APK()
  gw$="http://laughton.com/basic/programs/html/GW%20(GUI-Web%20lib)/GW.bas": BYTE.OPEN r, fid, gw$
  IF fid<0 THEN gw$="http://mougino.free.fr/tmp/GW/GW.bas": BYTE.OPEN r, fid, gw$
  IF fid<0 THEN GOTO newv_skip
  BYTE.READ.BUFFER fid, 20, v$: BYTE.CLOSE fid
  v$=LEFT$(v$, IS_IN("\n", v$)-1)
  v$=TRIM$(MID$(v$, IS_IN(CHR$(34), v$)), CHR$(34))
  IF v$<=GW_VER$ THEN GOTO newv_skip
  ARRAY.LOAD newv_ar$[], "DOWNLOAD>DL", "Ignore>CONT"
  newv_pg=GW_NEW_PAGE()
  e$="A new version is available:\nGW lib "+v$+"\n\n"
  e$+="(current version: "+GW_VER$+")"
  GW_USE_THEME_CUSTO_ONCE("inline")
  newv_dlg=GW_ADD_DIALOG_MESSAGE(newv_pg, "GW LIB", e$, newv_ar$[])
  GW_USE_THEME_CUSTO_ONCE("color=b")
  newv_spin=GW_ADD_SPINNER(newv_pg, "Downloading GW lib "+v$)
  GW_RENDER(newv_pg): GW_SHOW_DIALOG_MESSAGE(newv_dlg)
  IF GW_WAIT_ACTION$()<>"DL" THEN GOTO newv_skip
  GW_SHOW_SPINNER(newv_spin)
  BYTE.OPEN r, fid, gw$
  IF fid<0 THEN GOTO newv_err
  BYTE.COPY fid, "../source/GW.tmp"
  GRABFILE e$, "../source/GW.tmp"
  IF 0=IS_IN("!-- END OF GW LIB --!", e$, -1) THEN GOTO newv_err % check integrity of the lib
  BYTE.OPEN r, fid, REPLACE$(gw$, "GW.bas", "GW_demo.bas")
  IF fid<0 THEN GOTO newv_err
  BYTE.COPY fid, "../source/GW_demo.tmp"
  GRABFILE e$, "../source/GW_demo.tmp"
  IF 0=IS_IN("!-- END OF GW DEMO --!", e$, -1) THEN GOTO newv_err % check integrity of the demo
  FILE.RENAME "../source/GW.tmp", "../source/GW.bas"
  FILE.RENAME "../source/GW_demo.tmp", "../source/GW_demo.bas"
  POPUP "GW lib and demo have\nbeen updated to "+v$
  GW_HIDE_SPINNER(): GOTO newv_skip
  newv_err:
  GW_HIDE_SPINNER()
  FILE.DELETE fid, "../source/GW.tmp"
  FILE.DELETE fid, "../source/GW_demo.tmp"
  POPUP "Error downloading\nGW lib "+v$
  newv_skip:
END IF

Nicolas

_________________
- Creator of the Android BASIC! Compiler


Top
 Profile  
 
 Post subject: Re: Incorrect description in BYTE.COPY
Unread postPosted: Sat Nov 26, 2016 10:24 am 
Offline

Joined: Wed Oct 03, 2012 9:53 am
Posts: 2795
Location: Colorado, U.S.
Thanks, Nicolas. Your post makes me wonder how we can possibly have gone so long without seeing this. Or if people have corrupt copies and don't know it.

I did not know Byte.Position.Set will not let you go back to zero for internet files. I assume that means Byte.Position.Mark does not work, or if it does it is irrelevant.

There is another case: for local files, you can't go back before the last place you marked with Byte.Position.Mark, and you can't move the mark backwards.

I don't know of any easy way to fix these things. BASIC! doesn't do true random file access. We could have the command check the file position; if non-zero try to back up, if still non-zero close and reopen the file. That's not too bad for local files, but for internet files it's quite expensive.

Or, if the file position is non-zero, the command could refuse to copy anything, so the output file would have zero-length. Is that better than making a partial copy?

I've tried to think of a way the command could tell the user about the error -- without stopping your program -- but so far nothing.

I will change the manual to describe what really happens, but I agree that we have to make BASIC! behave better. Just gotta figure out how to define "better".

- Marc


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
suspicion-preferred