Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Text mode file operations #51

Open
sjakobi opened this issue Nov 18, 2019 · 2 comments
Open

Text mode file operations #51

sjakobi opened this issue Nov 18, 2019 · 2 comments

Comments

@sjakobi
Copy link

sjakobi commented Nov 18, 2019

UnliftIO.IO.File currently offers only binary mode file operations.

It would be nice if text mode operations were offered too, to simplify writing text files in a cross-platform way: On Windows, text mode writes change LF line endings to CRLF. See openBinaryFile:

On Windows, reading a file in text mode (which is the default) will translate CRLF to LF, and writing will translate LF to CRLF. This is usually what you want with text files. With binary files this is undesirable; also, as usual under Microsoft operating systems, text mode treats control-Z as EOF. Binary mode turns off all special treatment of end-of-line and end-of-file characters. (See also hSetBinaryMode.)

@sjakobi
Copy link
Author

sjakobi commented Nov 18, 2019

Hmm, if I understand hSetBinaryMode correctly, I can already use it to switch to text mode with hSetBinaryMode False.

So writeFileAtomic could be implemented as

writeFileAtomic fp bytes =
  liftIO $ withBinaryFileAtomic fp WriteMode $ \h -> do
    hSetBinaryMode h False
    B.hPut h bytes

@snoyberg
Copy link
Member

Yes, that would be my recommended approach. I don't have a strong opinion, but I'm weakly against adding more helper functions to the API due to expanding things too much.

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

No branches or pull requests

2 participants