Go Back   Cockos Incorporated Forums > REAPER Forums > Dstruct's Casa De Nitpicks

Reply
 
Thread Tools Display Modes
Old 03-19-2023, 04:14 AM   #1
jack461
Human being with feelings
 
jack461's Avatar
 
Join Date: Nov 2013
Location: France
Posts: 181
Default [JSFX] file_text(), file_avail() mysteries and associated curious behaviours

What?
"file_avail()" does'nt work correctly.

Long
I recently found a bug in one of my JSFX effects, which I can exemplify with this code:
Code:
desc:File Testing
@init
fhd = file_open("/Users/jjg4/text.txt");
(fhd >= 0) ? (
    tlcnt = 0; // line count
    ttext = file_text(fhd);
    tfav = file_avail(fhd); 
    tempty = 0;
    while (file_avail(fhd)) (
        file_string(fhd, #str);
        (strlen(#str) === 0) ? tempty += 1; 
        tlcnt += 1;
    );
    tfinal =  file_avail(fhd);
    file_close(fhd);
);
fhd = file_open("/Users/jjg4/text.xyz");
(fhd >= 0) ? (
    xlcnt = 0; // line count
    xtext = file_text(fhd);
    xfav = file_avail(fhd);
    xempty = 0;
    while (file_avail(fhd)) (
        file_string(fhd, #str);
        (strlen(#str) === 0) ? xempty += 1; 
        xlcnt += 1;
    );    
    xfinal =  file_avail(fhd);
    file_close(fhd);
);
sprintf(#st2, "text.txt: type:%d favail:%d lines:%d empty:%d final:%d     text.xyz: type:%d favail:%d lines:%d empty:%d final:%d",
            ttext, tfav, tlcnt, tempty, tfinal, xtext, xfav, xlcnt, xempty, xfinal);
strcpy(#dbg_desc, #st2);
This JSFX reads two files, "text.txt" and "text.xyz" which have the same contents and differ only by their names. The code opens the files, reads successive strings (there are 4 of them), closes the files, and displays some results in #dbg_desc.

The result displayed is:
Code:
"text.txt: type:1 favail:1 lines:5 empty:1 final:0     text.xyz: type:0 favail:16 lines:1048576 empty:1048572 final:16
The file "text.txt", with the ".txt" suffix, is recognized as a text file, and while the last executed "file_avail()" in the while statement returns 1 (we already are at the end of file at this moment), the end of file condition is detected only after the next "file_string()" returns an empty string.

On the other hand, the "text.xyz", due to its suffix, is not recognized as a text file (let it be so). However, it happens that the result of "file_avail()" is always 1, even when the end of file has been reached. So the "while" code loops forever, which in the case of JSFX, corresponds to the limit of "1048576", which is 2^20. This behaviour of "file_avail()" is clearly "not right", and results in the fact that we can't use this operation to manage the loop.

Suggestions:
1) correct the behaviour of "file_avail()" so it returns "0" when the end of file is reached.

2) as a linked problem, the JSFX manual indicates that "file_text(handle,istext)" has two parameters, while in fact only the form "file_text(handle)" is accepted. Since I use text files, which are really text files with different suffixes for different purposes (".jsfx", ".md", ".lua", etc. are text files !), I suggest as an extension of the "file_text()" function that "file_text(handle, 1)" could be used to indicate that the current opened file should be considered as a true text file, and that "file_text(handle, 0)" be used to indicate that the current opened file should not be handled as a text file.

Thanks for your attention !

J.Jack.
jack461 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 04:02 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.