Skip to content

Commit

Permalink
[FSharp] Move IO utlities to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Oct 30, 2023
1 parent 6a0f09f commit 61a94a7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 113 deletions.
1 change: 1 addition & 0 deletions src/Aardvark.Base.FSharp/Aardvark.Base.FSharp.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Compile Include="Utilities\Pickler\FsPicklerExtensions.fs" />
<Compile Include="Utilities\Pickler\AdaptivePicklers.fs" />
<Compile Include="Utilities\Threading.fs" />
<Compile Include="Utilities\IO.fs" />
<Compile Include="Utilities\Measures.fs" />
<Compile Include="Utilities\Lens.fs" />
<Compile Include="Utilities\Monoid.fs" />
Expand Down
109 changes: 109 additions & 0 deletions src/Aardvark.Base.FSharp/Utilities/IO.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
namespace Aardvark.Base

open System.IO

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module IO =
let alterFileName str f = Path.Combine (Path.GetDirectoryName str, f (Path.GetFileName str))

let createFileStream path =
if File.Exists path
then File.Delete path
new FileStream(path, FileMode.CreateNew)


[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Path =
let combine (paths : seq<string>) = Path.Combine(paths |> Seq.toArray)

let andPath first second = combine [| first; second |]

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module File =

/// <summary>
/// Creates the parent directory of the given file path, if it does not exist.
/// </summary>
/// <param name="path">The path of the file, whose parent directory is to be created.</param>
let createParentDirectory (path : string) =
let info = FileInfo(path)
if not info.Directory.Exists then
info.Directory.Create()

/// <summary>
/// Creates a new file, writes the specified string array to the file, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="lines">The lines to write to the file.</param>
let writeAllLines (path : string) (lines : string[]) =
File.WriteAllLines(path, lines)

/// <summary>
/// Creates a new file, writes the specified string array to the file, and then closes the file.
/// If the parent directory does not exist, it is created first.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="lines">The lines to write to the file.</param>
let writeAllLinesSafe (path : string) (lines : string[]) =
createParentDirectory path
File.WriteAllLines(path, lines)

/// <summary>
/// Creates a new file, writes the specified string to the file, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="text">The string to write to the file.</param>
let writeAllText (path : string) (text : string) =
File.WriteAllText(path, text)

/// <summary>
/// Creates a new file, writes the specified string to the file, and then closes the file.
/// If the parent directory does not exist, it is created first.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="text">The string to write to the file.</param>
let writeAllTextSafe (path : string) (text : string) =
createParentDirectory path
File.WriteAllText(path, text)

/// <summary>
/// Creates a new file, writes the specified byte array to the file, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="bytes">The bytes to write to the file.</param>
let writeAllBytes (path : string) (bytes : uint8[]) =
File.WriteAllBytes(path, bytes)

/// <summary>
/// Creates a new file, writes the specified byte array to the file, and then closes the file.
/// If the parent directory does not exist, it is created first.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="bytes">The bytes to write to the file.</param>
let writeAllBytesSafe (path : string) (bytes : uint8[]) =
createParentDirectory path
File.WriteAllBytes(path, bytes)

/// <summary>
/// Opens a text file, reads all lines of the file into a string array, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A string array containing all lines of the file.</returns>
let readAllLines (path : string) =
File.ReadAllLines path

/// <summary>
/// Opens a text file, reads all the text in the file into a string, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A string containing all the text in the file.</returns>
let readAllText (path : string) =
File.ReadAllText path

/// <summary>
/// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A byte array containing the contents of the file.</returns>
let readAllBytes (path : string) =
File.ReadAllBytes path
114 changes: 1 addition & 113 deletions src/Aardvark.Base.FSharp/Utilities/Interop/FSLibExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ module Caching =
let memoTable = System.Collections.Concurrent.ConcurrentDictionary<obj,obj>()
let cacheFunction (f : 'a -> 'b) (a : 'a) : 'b =
memoTable.GetOrAdd((a,f) :> obj, fun (o:obj) -> f a :> obj) |> unbox<'b>

[<AutoOpen>]
module NiceUtilities =

Expand Down Expand Up @@ -391,118 +391,6 @@ module NiceUtilities =
| (true, v) -> Some v
| _ -> None


[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module IO =
open System.IO

let alterFileName str f = Path.Combine (Path.GetDirectoryName str, f (Path.GetFileName str))

let createFileStream path =
if File.Exists path
then File.Delete path
new FileStream(path, FileMode.CreateNew)


[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Path =
open System.IO

let combine (paths : seq<string>) = Path.Combine(paths |> Seq.toArray)

let andPath first second = combine [| first; second |]

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module File =
open System.IO

/// <summary>
/// Creates the parent directory of the given file path, if it does not exist.
/// </summary>
/// <param name="path">The path of the file, whose parent directory is to be created.</param>
let createParentDirectory (path : string) =
let info = FileInfo(path)
if not info.Directory.Exists then
info.Directory.Create()

/// <summary>
/// Creates a new file, writes the specified string array to the file, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="lines">The lines to write to the file.</param>
let writeAllLines (path : string) (lines : string[]) =
File.WriteAllLines(path, lines)

/// <summary>
/// Creates a new file, writes the specified string array to the file, and then closes the file.
/// If the parent directory does not exist, it is created first.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="lines">The lines to write to the file.</param>
let writeAllLinesSafe (path : string) (lines : string[]) =
createParentDirectory path
File.WriteAllLines(path, lines)

/// <summary>
/// Creates a new file, writes the specified string to the file, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="text">The string to write to the file.</param>
let writeAllText (path : string) (text : string) =
File.WriteAllText(path, text)

/// <summary>
/// Creates a new file, writes the specified string to the file, and then closes the file.
/// If the parent directory does not exist, it is created first.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="text">The string to write to the file.</param>
let writeAllTextSafe (path : string) (text : string) =
createParentDirectory path
File.WriteAllText(path, text)

/// <summary>
/// Creates a new file, writes the specified byte array to the file, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="bytes">The bytes to write to the file.</param>
let writeAllBytes (path : string) (bytes : uint8[]) =
File.WriteAllBytes(path, bytes)

/// <summary>
/// Creates a new file, writes the specified byte array to the file, and then closes the file.
/// If the parent directory does not exist, it is created first.
/// </summary>
/// <param name="path">The file to write to.</param>
/// <param name="bytes">The bytes to write to the file.</param>
let writeAllBytesSafe (path : string) (bytes : uint8[]) =
createParentDirectory path
File.WriteAllBytes(path, bytes)

/// <summary>
/// Opens a text file, reads all lines of the file into a string array, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A string array containing all lines of the file.</returns>
let readAllLines (path : string) =
File.ReadAllLines path

/// <summary>
/// Opens a text file, reads all the text in the file into a string, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A string containing all the text in the file.</returns>
let readAllText (path : string) =
File.ReadAllText path

/// <summary>
/// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <returns>A byte array containing the contents of the file.</returns>
let readAllBytes (path : string) =
File.ReadAllBytes path

[<AutoOpen>]
module NativeUtilities =
open System.Runtime.InteropServices
Expand Down

0 comments on commit 61a94a7

Please sign in to comment.