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

we dont have a file to string function? #789

Open
UnixJunkie opened this issue Sep 6, 2017 · 6 comments
Open

we dont have a file to string function? #789

UnixJunkie opened this issue Sep 6, 2017 · 6 comments
Assignees

Comments

@UnixJunkie
Copy link
Member

val BatFile.as_string: BatFile.t -> string

would be pretty handy to me for small files.
I mean, this function would read the whole file and return the corresponding string.

@UnixJunkie
Copy link
Member Author

my current hack:

(* return whole file content as a string *)
let as_string fn =
  let buff = Buffer.create 80 in
  Utls.with_in_file fn (fun input ->
      try
        Buffer.add_channel buff input (1024 * 1024);
        failwith "BatFile.as_string: file is too big"
      with End_of_file -> () (* OK, we read the whole file *)
    );
  Buffer.contents buff

@UnixJunkie UnixJunkie self-assigned this Sep 11, 2017
@gasche
Copy link
Member

gasche commented Sep 12, 2017

Note that BatFile.with_file_in path BatIO.read_all is a shorter way to do this.

@UnixJunkie
Copy link
Member Author

I want to avoid BatIO. I had performance problems with it in the past.

@c-cube
Copy link
Member

c-cube commented Sep 12, 2017

Comment is cheap, but I would suggest that batteries 3, with a proper IO module that is compatible with the stdlib, would be the only good solution to this. With containers it's the same one-liner as @gasche mentioned, except it's efficient.

@gasche
Copy link
Member

gasche commented Sep 12, 2017

(No disagreement here.)

@UnixJunkie
Copy link
Member Author

UnixJunkie commented Sep 12, 2017

batteries 3 makes me think about GNU Hurd ... :)
we are too low on manpower to do big things like this

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

No branches or pull requests

3 participants