Skip to content

Commit

Permalink
Add proc for writing a string
Browse files Browse the repository at this point in the history
  • Loading branch information
Nycto committed Mar 20, 2024
1 parent 7e9e6a9 commit cb9589b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/playdate/file.nim
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ proc write*(this: SDFile, buffer: seq[byte], length: uint): int {.raises: [IOErr
privateAccess(PlaydateFile)
if length > 0:
return playdate.file.write(this.resource, unsafeAddr(buffer[0]), length.cuint).requireValidStatus

proc write*(this: SDFile, content: string): int {.raises: [IOError], discardable} =
privateAccess(PlaydateFile)
if content.len > 0:
return playdate.file.write(this.resource, unsafeAddr(content[0]), content.len.cuint).requireValidStatus
5 changes: 5 additions & 0 deletions tests/t_files.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ proc execFilesTest*() =
var handle = playdate.file.open("test_data.txt", kFileReadData)
check(handle.readString() == "foo")

test "Writing strings to files":
block:
playdate.file.open("test_data.txt", kFileWrite).write("file content")
check(playdate.file.open("test_data.txt", kFileReadData).readString() == "file content")

test "Listing files":
createFile("list_files.txt")
check("list_files.txt" in playdate.file.listFiles("/"))
Expand Down

0 comments on commit cb9589b

Please sign in to comment.