[SOLVED, kinda] How to QTextStream.readLine() when EOL is \r (=CR:CarriageReturn)

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
CVH
Premier Member
Posts: 4955
Joined: Wed Sep 27, 2017 4:17 pm

[SOLVED, kinda] How to QTextStream.readLine() when EOL is \r (=CR:CarriageReturn)

Post by CVH » Thu Sep 19, 2024 6:57 am

Andrew,

Received a pattern file that didn't work with QCAD.
More details in this topic

My custom tool Tile2Hatch also failed to cast the pattern dashes and or dots once.
And then I would expected a build in report or a failure on that 90% of the first bunch of definition lines where invalid.

After investigation the lines don't seem to end in \n or \r\n (NewLine or CarriageReturn + NewLine)
QTextStream.readLine() then reads the whole file with all occurring \r removed.

Searched online and this tells me that with Qt detecting End of Line is indeed hardcoded to \r\n and \n.

However, I can make it work.
Notepad++ is able to convert the EOL format of Windows (CR LF), Unix (LF) or Macintosh (CR).

Using or Windows, or Unix format the pattern is loaded.
I would have said "loaded perfectly" if the pattern was coded reasonable OK. :roll:

It is not ideal that users have to edit online sourced patterns, it is not a standalone case.

Thanks for any advice.
Regards,
Last edited by CVH on Thu Sep 19, 2024 3:32 pm, edited 1 time in total.

John Hyslop
Premier Member
Posts: 520
Joined: Mon Sep 30, 2019 6:21 am
Location: Melbourne - Australia

Re: How to QTextStream.readLine() when EOL is \r (=CR:CarriageReturn)

Post by John Hyslop » Thu Sep 19, 2024 10:09 am

CVH wrote:
Thu Sep 19, 2024 6:57 am
Andrew,

Received a pattern file that didn't work with QCAD.
More details in this topic

My custom tool Tile2Hatch also failed to cast the pattern dashes and or dots once.
And then I would expected a build in report or a failure on that 90% of the first bunch of definition lines where invalid.

After investigation the lines don't seem to end in \n or \r\n (NewLine or CarriageReturn + NewLine)
QTextStream.readLine() then reads the whole file with all occurring \r removed.

Searched online and this tells me that with Qt detecting End of Line is indeed hardcoded to \r\n and \n.

However, I can make it work.
Notepad++ is able to convert the EOL format of Windows (CR LF), Unix (LF) or Macintosh (CR).

Using or Windows, or Unix format the pattern is loaded.
I would have said "loaded perfectly" if the pattern was coded reasonable OK. :roll:

It is not ideal that users have to edit online sourced patterns, it is not a standalone case.

Thanks for any advice.
Regards,
CVH

I think Andrew has enough on his plate without having to make allowances for "obviously" poorly coded patterns.😉


John
IF IT IS TO BE IT IS UP TO ME [ Ten most powerful 2 letter words ]

CVH
Premier Member
Posts: 4955
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to QTextStream.readLine() when EOL is \r (=CR:CarriageReturn)

Post by CVH » Thu Sep 19, 2024 11:00 am

John,

It is the Macintosh EOL for text files that is the issue.
In itself it is not a false content.
For some reason Qt does not support that right out the box.

- - - - - -

If you are referring to 'poorly' meaning 'bad coded entries' in the given example pattern file, then I fully agree.
Try to set the origin of your hatch near (-6330, -1500).
That is how it should look like ... About. :lol:

The larged hurdle is the high cloning load in combination with the larger coordinate values.
Both do not seem to hinder other CAD applications, judging from the attached arguments.

Regards,
CVH

CVH
Premier Member
Posts: 4955
Joined: Wed Sep 27, 2017 4:17 pm

Re: How to QTextStream.readLine() when EOL is \r (=CR:CarriageReturn)

Post by CVH » Thu Sep 19, 2024 3:29 pm

It will remain a problem ...

At least I can catch it now and warn about a bad text data format:

Code: Select all

    file = new QFile(fileName);
    flags = new QIODevice.OpenMode(QIODevice.ReadOnly);
    if (file.open(flags)) {
        var content = file.readAll();
        var pos1 = content.indexOf("\r");
        var pos2 = content.indexOf("\n");
        if (pos1>0 && pos1+1 !== pos2) { 
            debugger;    // Warn user that EOL is probably CR
        }
    }
    file.close();
The solution is then a text editor, at least one knows what might be wrong.
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”