Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add std.writeFile #932

Merged
merged 1 commit into from
Feb 27, 2025
Merged

Add std.writeFile #932

merged 1 commit into from
Feb 27, 2025

Conversation

bnoordhuis
Copy link
Contributor

std.loadFile already exists and is convenient. std.writeFile replaces the more onerous std.open + std.write + std.close idiom.

std.loadFile already exists and is convenient. std.writeFile replaces
the more onerous std.open + std.write + std.close idiom.
@bnoordhuis bnoordhuis merged commit 9a902ba into quickjs-ng:master Feb 27, 2025
60 checks passed
@bnoordhuis bnoordhuis deleted the std-writefile branch February 27, 2025 14:52
Comment on lines +365 to +367
When `data` is a string, the file is opened in text mode; otherwise it is
opened in binary mode. (This distinction is only relevant on Windows.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might add that the string is encoded in UTF-8 on all systems, whereas the data from typed arrays or ArrayBuffer objects is simply written as untranslated binary data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I'm not entirely sure that's correct. It uses fwrite and on Windows that's code page-dependent, AFAIK.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I'm not entirely sure that's correct. It uses fwrite and on Windows that's code page-dependent, AFAIK.

Indeed fwrite in Windows can do this, so if the stream is configured for code page or worse Unicode conversion, fwrite would unlikely produce gibberish output, aka Mojibake.

Here are the relevant paragraphs in the Microsoft help page:

The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there's one) is incremented by the number of bytes fwrite writes. If stream is opened in text mode, each line feed is replaced with a carriage return-line feed pair. The replacement has no effect on the return value.

When stream is opened in Unicode translation mode — for example, if stream is opened by calling fopen and using a mode parameter that includes ccs=UNICODE, ccs=UTF-16LE, or ccs=UTF-8, or if the mode is changed to a Unicode translation mode by using _setmode and a mode parameter that includes _O_WTEXT, _O_U16TEXT, or _O_U8TEXTbuffer is interpreted as a pointer to an array of wchar_t that contains UTF-16 data. An attempt to write an odd number of bytes in this mode causes a parameter validation error.

You open the file in default mode (text mode would use "wt") so the stream translation mode will be the default translation mode defined by the global variable _fmode which could be _O_TEXT and trigger LF to CR/LF translation or _O_BINARY which would leave the output unchanged.

Unless I am missing some more subtle side effects, the data written should just undergo line ending translation.

My suggestion is correct for unix-like systems and somewhat helpful, albeit UTF-8 is probably expected on these systems, and a more explicit caveat for Windows could help programmers who set up the standard streams stdout or stderr to perform code page or Unicode translation.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants