/*---------------------------------------------------------------------------
  Copyright 2020-2021, Microsoft Research, Daan Leijen.

  This is free software; you can redistribute it and/or modify it under the
  terms of the Apache License, Version 2.0. A copy of the License can be
  found in the LICENSE file at the root of this distribution.
---------------------------------------------------------------------------*/

/* File operations.
*/
module std/os/filestd/os/file

import std/os/pathstd/os/path
import std/os/dirstd/os/dir

extern import
  c file "file-inline.c"
  js file "file-inline.js"

// Read a text file synchronously (using UTF8 encoding)
pub fun read-text-filestd/os/file/read-text-file: (path : path) -> <exn,fsys> string( pathpath: path : pathstd/os/path/path: V )result: -> <exn,fsys> string : <std/core/types/total: Efsysstd/core/fsys: X,exnstd/core/exn/exn: (E, V) -> V> stringstd/core/types/string: V
  match read-text-file-errstd/os/file/read-text-file-err: (path : string) -> <fsys,exn> error<string>(pathpath: path.stringstd/os/path/string: (p : path) -> <fsys,exn> string)
    Errorstd/core/exn/Error: forall<a> (exception : exception) -> error<a>(exnexn: exception)  -> throw-exnstd/core/exn/throw-exn: (exn : exception) -> <exn,fsys> string(exnexn: exception.prependstd/os/file/prepend: (exn : exception, pre : string) -> <exn,fsys> exception("unable to read text file "literal: string
count= 25
++std/core/types/(++): (x : string, y : string) -> <exn,fsys> string pathpath: path.showstd/os/path/show: (p : path) -> <exn,fsys> string)) Okstd/core/exn/Ok: forall<a> (result : a) -> error<a>(contentcontent: string) -> contentcontent: string
// Write a text file synchronously (using UTF8 encoding) pub fun write-text-filestd/os/file/write-text-file: (path : path, content : string, create-dir : ? bool) -> <exn,fsys> ()( pathpath: path : pathstd/os/path/path: V, contentcontent: string : stringstd/core/types/string: V, create-dircreate-dir: ? bool : boolstd/core/types/bool: V = Truestd/core/types/True: bool )result: -> <exn,fsys> () : <std/core/types/total: Efsysstd/core/fsys: X,exnstd/core/exn/exn: (E, V) -> V> (std/core/types/unit: V)std/core/types/unit: V if create-dircreate-dir: bool then ensure-dirstd/os/dir/ensure-dir: (dir : path) -> <exn,fsys> ()(pathpath: path.nobasestd/os/path/nobase: (p : path) -> <exn,fsys> path)std/core/types/Unit: () match(write-text-file-errstd/os/file/write-text-file-err: (path : string, content : string) -> <fsys,exn> error<()>(pathpath: path.stringstd/os/path/string: (p : path) -> <fsys,exn> string,contentcontent: string)) Errorstd/core/exn/Error: forall<a> (exception : exception) -> error<a>(exnexn: exception) -> throw-exnstd/core/exn/throw-exn: (exn : exception) -> <exn,fsys> ()(exnexn: exception.prependstd/os/file/prepend: (exn : exception, pre : string) -> <exn,fsys> exception("unable to write text file "literal: string
count= 26
++std/core/types/(++): (x : string, y : string) -> <exn,fsys> string pathpath: path.showstd/os/path/show: (p : path) -> <exn,fsys> string)) _ -> (std/core/types/Unit: ()
)std/core/types/Unit: () fun prependstd/os/file/prepend: (exn : exception, pre : string) -> exception( exnexn: exception : exceptionstd/core/exn/exception: V, prepre: string : stringstd/core/types/string: V )result: -> total exception : exceptionstd/core/exn/exception: V Exceptionstd/core/exn/Exception: (message : string, info : exception-info) -> exception(prepre: string ++std/core/types/(++): (x : string, y : string) -> string ": "literal: string
count= 2
++std/core/types/(++): (x : string, y : string) -> string exnexn: exception.messagestd/core/exn/exception/message: (exception : exception) -> string, exnexn: exception.infostd/core/exn/exception/info: (exception : exception) -> exception-info
) extern read-text-file-errstd/os/file/read-text-file-err: (path : string) -> fsys error<string>( pathpath: string : stringstd/core/types/string: V ) : fsysstd/core/fsys: X errorstd/core/exn/error: V -> V<stringstd/core/types/string: V> c "kk_os_read_text_file_error" js "_read_text_file_error" //cs inline "System.IO.File.ReadAllText(#1,System.Text.Encoding.UTF8)" extern write-text-file-errstd/os/file/write-text-file-err: (path : string, content : string) -> fsys error<()>( pathpath: string : stringstd/core/types/string: V, contentcontent: string : stringstd/core/types/string: V ) : fsysstd/core/fsys: X errorstd/core/exn/error: V -> V<(std/core/types/unit: V)std/core/types/unit: V> c "kk_os_write_text_file_error" js "_write_text_file_error" //cs inline "System.IO.File.WriteAllText(#1,#2,System.Text.Encoding.UTF8)"