Author Topic: Pelles compiler, Ftell, Fseek, and things that go bump in the night ...  (Read 330 times)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2330
    • View Profile
Robert graciously resurrected this 20 year old Pelles forum thread for me.

If your compiler of choice is Pelles C, I encourage you to follow this thread:

https://forum.pellesc.de/index.php?topic=221.msg39550#msg39550


airr

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Re: Pelles compiler, Ftell, Fseek, and things that go bump in the night ...
« Reply #1 on: September 02, 2024, 11:46:25 PM »
That is a very interesting thread, thanks for the link!

I do have one question:

Would it break the use of other compilers if instead of conditionally specifying binary read for Pelles only, you just did it for all compilers?

AIR.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2330
    • View Profile
Re: Pelles compiler, Ftell, Fseek, and things that go bump in the night ...
« Reply #2 on: September 03, 2024, 08:44:14 AM »
That is a very interesting thread, thanks for the link!

I do have one question:

Would it break the use of other compilers if instead of conditionally specifying binary read for Pelles only, you just did it for all compilers?

AIR.

I had not thought of doing that Armando but I expect it will work fine.

I'll do some testing. 


MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2330
    • View Profile
Re: Pelles compiler, Ftell, Fseek, and things that go bump in the night ...
« Reply #3 on: September 03, 2024, 09:30:26 AM »
I asked ChatGpt to identify any potential pitfalls that could result
from changing "r" to "rb" for reading normal lines of text, including
utf-8. The primary warning is that Windows does not automatically
transform "\r\n" to "\n" line endings in "rb" mode. 

That won't be a problem because BCX has never relied on Windows for that
translation, instead BCX uses the crt function "strcspn" to detect those
line endings and BCX itself handles termination, so very little to change.

In fact what I have changed and I'm testing now, is simply changing the
"r" file mode to "rb" when using:  OPEN "filename" FOR INPUT as #1

That means instead of needing to change our habits to use:

    OPEN "filename" FOR BINARY READ as #1

we continue to use

    OPEN "filename" FOR INPUT as #1

as we always have.

This also means that "FOR INPUT" and "FOR BINARY READ" are treated identically by BCX.

I like this solution a lot.




« Last Edit: September 03, 2024, 10:54:36 AM by MrBcx »

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2330
    • View Profile
Re: Pelles compiler, Ftell, Fseek, and things that go bump in the night ...
« Reply #4 on: September 03, 2024, 01:36:32 PM »
I made the change ( mode "r" to mode "rb" ) in OPEN "filename" FOR INPUT AS #1
types of statements.  I also successfully completed running my standard regression tests on
6 versions of BCX that were compiled with MSVC, Clang, Mingw, Pelles, Embarcadero, and LccWin32.

I've added the following note to the Bcx Revisions.txt file:

Kevin Diggins  : It was confirmed that Pelles treatment of the _ftelli64 and _fseeki64 can
                 cause file i/o errors. The BCX commands and functions that depend on those
                 functions are: SEEK, FTELL, LOOKAHEAD$, RECORD, and LONGESTLINE. I learned
                 that changing statements like: OPEN "filename" for INPUT AS #1  to use the
                 fopen "rb" mode instead of "r" mode would prevent the file i/o errors.
                 BCX now emits the "rb" mode for all compilers when translating statements
                 like: OPEN "filename" for INPUT AS #1   (fully tested, works like a charm)


« Last Edit: September 03, 2024, 01:39:17 PM by MrBcx »

airr

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Resurrecting this for a question:

For SEEK would it be possible to add an optional flag?

Right now, the origin flag is hard coded in the translated code to SEEK_SET, meaning that the offset is based on the beginning of the file.

It would be useful to be able to pass the SEEK_CUR flag, in order to move forward x number of bytes from the current position.

I'm coding a tool that makes heavy use of seek, and had to drop down to using fseek directly in order to pass the SEEK_CUR flag.

With SEEK as it currently stands, in order to jump to a location 64 bytes forward or backward, for example, I have to first call FTELL and then use that output along with my desired offset to jump to the location I want.

AIR.


MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2330
    • View Profile
Resurrecting this for a question:

For SEEK would it be possible to add an optional flag?

Right now, the origin flag is hard coded in the translated code to SEEK_SET, meaning that the offset is based on the beginning of the file.

It would be useful to be able to pass the SEEK_CUR flag, in order to move forward x number of bytes from the current position.

I'm coding a tool that makes heavy use of seek, and had to drop down to using fseek directly in order to pass the SEEK_CUR flag.

With SEEK as it currently stands, in order to jump to a location 64 bytes forward or backward, for example, I have to first call FTELL and then use that output along with my desired offset to jump to the location I want.

AIR.

Sounds easy enough ... I'll take a look and will include the change in 8.1.7, barring any surprises.

 
[UPDATE]   The following show my mods.  These were tested and found to be working as it should.

Code: [Select]


FUNCTION Emit_SeekProcs(sWord$, FuncRetnFlag AS PINT)
    UNREFERENCED_PARAMETER(FuncRetnFlag)
    UNREFERENCED_PARAMETER(sWord$)

    REMOVE "#" FROM Stk$[2]
    IF ISNULL(Stk$[2]) THEN CALL Abort ("Bad SEEK Argument")
    '***************************************************************************
    ' Example:  SEEK FP1, 8192 [, SEEK_CUR ]               ' MrBcx 817 - Added
    '***************************************************************************
    DIM Origin$                                            ' MrBcx 817 - Added
    SELECT CASE UCASE$(Stk$[Ndx])                          ' MrBcx 817 - Added
    CASE "SEEK_CUR", "SEEK_END", "SEEK_SET"                ' MrBcx 817 - Added
        Origin$ = UCASE$(Stk$[Ndx])                        ' MrBcx 817 - Added
        DECR Ndx, 2                   ' Adjust the stack   ' MrBcx 817 - Added
    CASE ELSE                                              ' MrBcx 817 - Added
        Origin$ = "SEEK_SET"                               ' MrBcx 817 - Added
    END SELECT                                             ' MrBcx 817 - Added
    '***************************************************************************
    IF DataType(Stk$[2])= vt_NUMBER THEN
        Stk$[2]= "FP" + Stk$[2]
    END IF

    DIM i
    IF CheckLocal(Stk$[2], ADDRESSOF(i)) = vt_UNKNOWN THEN
        CALL AddGlobal(Stk$[2], vt_FILEPTR)
    END IF

    FPRINT Outfile, Scoot$, "_fseeki64("; ' Suppress CRLF
    Stk$[++Ndx] = "," + Origin$ + ");"                     ' MrBcx 817 - Modified

    CALL WriteCleanTokens(2, Ndx)
    FUNCTION = 0
END FUNCTION  ' Emit_SeekProcs

« Last Edit: October 06, 2024, 09:25:07 AM by MrBcx »

airr

  • Full Member
  • ***
  • Posts: 242
    • View Profile
Thanks, MrB!

AIR.