sc readfile failing if source file is unicode

Things you’d like to miss in the future...
Forum rules
You might want to read this if you're looking for priority service

:info: Please Include The Following Information
1) Your XYplorer Version (e.g., v28.00.0801)
2) Your Windows Version (e.g., Win 11)
3) Your Screen Scaling Percentage (e.g., 125%).

:info: Use Your Profile Or Signature
We strongly recommend adding your Windows Version and Screen Scaling Percentage to the Location field in your Profile or to your Signature. That way, you only have to type them once, and we won't have to search for that vital information.

:info: How To Attach Images
When attaching an Image, please use the Attachment tab at the bottom of your post and click "Add files".
Post Reply
tiago
Posts: 589
Joined: 14 Feb 2011 21:41

sc readfile failing if source file is unicode

Post by tiago »

It seems to be a problem when sc text or echo is applied after a file is read using numbytes and/or start parameter, which suggests sc readfile has no full unicode support.

On address bar:

Code: Select all

writefile("unicode.txt", strrepeat(x, 50), o, tu);
This WON'T reproduce the error: select the unicode.txt file and run this on AB:

Code: Select all

text readfile(<curitem>);
This will pop the error:

Code: Select all

   $offset = "50"; // amount to be read up to the end of the file

   $fsize = filesize("<curitem>");
//readfile(filename, [mode], [numbytes], [codepage], [start=1])

   $offsetend = $fsize - $offset;

   $use = readfile("<curitem>", , , , $offsetend);
   text ($use), , , , w;
//   echo $use;
Power-hungry user!!!

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: sc readfile failing if source file is unicode

Post by Stefan »

I think readfile() supports unicode.
I guess it's the [start] parameter which has problems with unicode:

Code: Select all

writefile("unicode.txt", strrepeat(x, 10), o, tu);


   //readfile(filename, [mode], [numbytes], [codepage], [start=1])
   text "off 0:" . readfile("unicode.txt", , , , );
   text "off 2:" . readfile("unicode.txt", , , ,2);
   text "off 3:" . readfile("unicode.txt", , , ,3);
   text "off 4:" . readfile("unicode.txt", , , ,4);
   text "off 5:" . readfile("unicode.txt", , , ,5);

admin
Site Admin
Posts: 67330
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: sc readfile failing if source file is unicode

Post by admin »

Correct. Fix comes.

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: sc readfile failing if source file is unicode

Post by neutrox »

I was having some problems too but had no idea why.

Then i saw this post and yes, there's something still wrong.

In my case when I simply have this piece of text on a file which is being read

Code: Select all

http​://will​terr​ell.​com/wp-​cont​ent/​uplo​ads/​2011​/11/​flyi​ng_s​quir​rels​_wil​lter​rell​.jpg​
if use a readfile with parameters i can't get the text displayed, showing only '' as a preview (see code below).

Sometimes if I append text to the wrongly previewed file, the appended section gets corrupted like this:

Code: Select all

㸾㸾㸾਍਍㸾㸾㸾㸾㸾㸾㸾㸾㸾㸾㸾㸾㸾㸾㸾㸾㸾ാഊ∊湉吠楨⁳楒敶≲椠⁳⁡潳杮戠⁹流牥捩湡栠慥祶洠瑥污戠湡⁤求捡慌敢潓楣瑥ⱹ映慥畴敲⁤湯琠敨物㈠〰‵楳瑸⁨瑳摵潩愠扬浵䴠晡
Cool: when I paste the snippet the '[/code]' section has turned '[edoc/]', and selecting the piece itself was hard. Maybe this gives a hint on codepage identification? :roll:

And this is a code I'm using which gives me no errors at all when target document is an ANSI code-based or gives me trouble with all unicode-based plus the 1st situation I described:

Code: Select all

 end(getinfo("CountSelected") < 1), "Pick a file first!";
 end(getinfo("CountSelected") > 1), "Only one!!";

 end(compare("<xyver>", '10.90.0100', 'v') < 0, "Run this @ v10.90.0100+.");

 $append = "<clipboard>";
 $sep = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";

 $offset = "100"; // amount to be read up to the end of the file

 $file = "<curitem>";

 $fsize = filesize("$file");
//readfile(filename, [mode], [numbytes], [codepage], [start=1])

 $calcoffset = $fsize - $offset;

 $initial = readfile("$file", , , , $calcoffset);

 text "------------------Ending of current file (last $offset chars):<crlf>________________________________________________________<crlf>'$initial'<crlf>___________________\\\\\\\\\\\\|||////////////___________________<crlf 2>Current separator: '$sep'<crlf 2>------------------Text to be appended:<crlf>_____________________________________<crlf 2>'$append'", , , "Preview:" , w;
 $answer = confirm("Continue?<crlf 2>Previous window was a preview of what will be appended...")?"_Y":"_N";
 if ($answer == "_N") { end 1, "User has Canceled!"; }
 elseif  ($answer == "_Y") {  }

 $sep2 = formatlist("$sep", v, "");
 writefile("$file", "<crlf 2>$sep<crlf 2>$append<crlf 2>$sep2", a);

 $fsize = filesize("$file");


 $calcoffset = $fsize - $offset;

 $final = readfile("$file", , , , $calcoffset);

 text "------------------Initial ending (last $offset chars):<crlf 2>'$initial'<crlf 2>------------------Appended (cropped, last $offset chars):<crlf 2>'$final'", , , , w;

admin
Site Admin
Posts: 67330
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: sc readfile failing if source file is unicode

Post by admin »

Three things:

1.
With unicode files, the byte count differs from the characters count. Since yesterday (v10.90.0303) Start parameter in ReadFile refers to characters in Unicode files, not to bytes.
So this logic needs revision:

Code: Select all

$calcoffset = $fsize - $offset;
2.
To append to a unicode file without creating garbage you need to specify "tu" to tell the script that this is a unicode file:

Code: Select all

writefile("$file", "<crlf 2>$sep<crlf 2>$append<crlf 2>$sep2", a, tu);
3.
And a remark: variables don't need to be quoted. This works as well:

Code: Select all

writefile($file, "<crlf 2>$sep<crlf 2>$append<crlf 2>$sep2", a, tu);

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: sc readfile failing if source file is unicode

Post by neutrox »

1. i'll check that. Actually I was already wondering what could could be wrong as the recent update broken my split files scripts.

2 and 3. thanks for the tips! :D

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: sc readfile failing if source file is unicode

Post by neutrox »

I wrote my code thinking on this:
mode
t: [default] text; auto-detects whether text can be written as ASCII or needs to be written as UNICODE
The tip
2.
To append to a unicode file without creating garbage you need to specify "tu" to tell the script that this is a unicode file:
tells me I have to to the detection myself, using script.
sc readfile does not have a return on the file being ansi or unicode.
Any advice, please?

admin
Site Admin
Posts: 67330
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: sc readfile failing if source file is unicode

Post by admin »

neutrox wrote:I wrote my code thinking on this:
mode
t: [default] text; auto-detects whether text can be written as ASCII or needs to be written as UNICODE
The tip
2.
To append to a unicode file without creating garbage you need to specify "tu" to tell the script that this is a unicode file:
tells me I have to to the detection myself, using script.
sc readfile does not have a return on the file being ansi or unicode.
Any advice, please?
You are absolutely right! Fix comes...

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: sc readfile failing if source file is unicode

Post by neutrox »

I must admit if I had the option I'd prefer the previous way of handling the start parameter as readfile now required turns the operations slow.

OTOH everything seems to be working now so I thank you!

And if you don't mind, what's the reason for the change?

admin
Site Admin
Posts: 67330
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: sc readfile failing if source file is unicode

Post by admin »

neutrox wrote:I must admit if I had the option I'd prefer the previous way of handling the start parameter as readfile now required turns the operations slow.

OTOH everything seems to be working now so I thank you!

And if you don't mind, what's the reason for the change?
http://www.xyplorer.com/xyfc/viewtopic. ... 091#p72091

Post Reply