Skip to content

Commit

Permalink
Add function for appending to file
Browse files Browse the repository at this point in the history
  • Loading branch information
phischu committed Feb 6, 2025
1 parent 0c11360 commit 5265982
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions libraries/common/io/filesystem.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ def writeFile(path: String, contents: String): Unit / Exception[IOError] = {
go()
}

/// Appends the (utf8 encoded) string `contents` to the specified file.
def appendFile(path: String, contents: String): Unit / Exception[IOError] = {
val file = openForAppending(path);
with on[IOError].finalize { close(file) }

val chunkSize = 1048576 // 1MB
val buffer = contents.fromString
var offset = 0;

def go(): Unit = {
val n = write(file, buffer, offset, min(buffer.size - offset, chunkSize), -1)
offset = offset + n
if (offset < buffer.size) { go() }
}

go()
}

/// An abstract interface applications can program against.
///
Expand Down

0 comments on commit 5265982

Please sign in to comment.