-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
filery Prerelease 2 is out! The main changes are:
- refactored inner structure. It will be effortless to add new platform support - partial Linux support. Linux is the first platform where Kotlin/Native support will come - appending and writing of UTF-8 text
- Loading branch information
Showing
9 changed files
with
176 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# filery — the multiplatform library for file fuckery | ||
|
||
Latest version: Prerelease 2 (0.2) | ||
|
||
WARNING: this project is currently on its early steps, is not production-ready and is welcome to contributions | ||
|
||
This project aims to provide you with a common and intuitive API for files while remaining | ||
easy to maintain and nuance-less | ||
|
||
## Starting points | ||
|
||
filery has two starting points - the `Filery` class and the `filery(path: String, block: Filery.() -> Unit)` function. | ||
The first one is familiar for JVM users, meanwhile, the second is a more idiomatic and DSL-ish way. Here are the examples: | ||
|
||
```kotlin | ||
fun main() = runBlocking { | ||
// filery function automatically closes the file in case of any error | ||
// and when work upon the file is finished | ||
filery("/home/bpavuk/fuckery.txt") { | ||
println(readLine()) // outputs "Hello, filery" | ||
} | ||
} | ||
``` | ||
|
||
Same but with `Filery` class: | ||
```kotlin | ||
fun main() = runBlocking { | ||
val file = Filery("/home/bpavuk/fuckery.txt").open() | ||
println(file.readText()) | ||
file.close() | ||
} | ||
``` | ||
|
||
Currently, filery is limited to two platforms: Linux (partially implemented) and JVM (fully implemented) | ||
|
||
You can connect the filery to your Gradle project by adding the GitHub Registry and this dependency: | ||
``` | ||
"com.bpavuk:filery:Prerelease_2" | ||
``` | ||
|
||
Have a nice file fuckery! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/linuxMain/kotlin/com/bpavuk/filery/expects/Utils.linux.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.bpavuk.filery.expects | ||
|
||
import com.bpavuk.filery.types.Path | ||
import platform.posix.F_OK | ||
import platform.posix.access | ||
|
||
public actual object Utils : IUtils { | ||
override fun exists(path: Path): Boolean = access(path.path, F_OK) != -1 | ||
} |
28 changes: 28 additions & 0 deletions
28
src/linuxMain/kotlin/com/bpavuk/filery/expects/pointers/DirectoryPointer.linux.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.bpavuk.filery.expects.pointers | ||
|
||
import com.bpavuk.filery.types.Modes | ||
import com.bpavuk.filery.types.Path | ||
|
||
public actual class DirectoryPointer actual constructor( | ||
override val path: Path, | ||
override val mode: Modes | ||
) : IDirectoryPointer { | ||
override val platformDirectoryPointer: Any | ||
get() = TODO("Not yet implemented") | ||
|
||
override suspend fun listFiles(): List<Path> { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun go(relativePath: String, mode: Modes): IDirectoryPointer { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun create(relativePath: String): IDirectoryPointer { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun delete(): Boolean { | ||
TODO("Not yet implemented") | ||
} | ||
} |
55 changes: 54 additions & 1 deletion
55
src/linuxMain/kotlin/com/bpavuk/filery/expects/pointers/FilePointer.linux.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,56 @@ | ||
package com.bpavuk.filery.expects.pointers | ||
|
||
public actual class FilePointer : IFilePointer | ||
import com.bpavuk.filery.types.Modes | ||
import com.bpavuk.filery.types.Path | ||
import kotlinx.cinterop.* | ||
import kotlinx.io.files.FileNotFoundException | ||
import platform.posix.* | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
public actual class FilePointer actual constructor( | ||
override val path: Path, | ||
override val mode: Modes | ||
) : IFilePointer { | ||
private val modeAsString: String = when (mode) { | ||
Modes.Read -> "r" | ||
Modes.Write -> "r+" | ||
Modes.ReadWrite -> "r+" | ||
} | ||
override val platformFilePointer: CFilePointer = CFilePointer( | ||
currentPointer = fopen(path.path, modeAsString) | ||
?: throw FileNotFoundException(path.path) | ||
) | ||
|
||
override fun close(): Boolean = | ||
fclose(platformFilePointer.currentPointer) == 0 | ||
|
||
override fun delete(): Boolean = remove(path.path) == 0 | ||
|
||
override suspend fun readBytes(amount: Int?): ByteArray { | ||
platformFilePointer.currentPosition = ftell(platformFilePointer.currentPointer) | ||
platformFilePointer.currentPointer = fopen(path.path, "r") ?: throw FileNotFoundException(path.path) | ||
fseek(platformFilePointer.currentPointer, 0, SEEK_END) | ||
val fileSize = ftell(platformFilePointer.currentPointer) | ||
val bytesLeft = (fileSize - platformFilePointer.currentPosition).toULong() | ||
fseek(platformFilePointer.currentPointer, platformFilePointer.currentPosition, SEEK_SET) | ||
|
||
val realAmount = amount?.toULong() ?: bytesLeft | ||
val cByteArray = nativeHeap.allocArray<ByteVar>(realAmount.toInt()) | ||
fread(cByteArray, 1.toULong(), realAmount, platformFilePointer.currentPointer) | ||
return cByteArray.readBytes(realAmount.toInt()) | ||
} | ||
|
||
override suspend fun writeBytes(bytes: ByteArray) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override suspend fun appendBytes(bytes: ByteArray) { | ||
TODO("Not yet implemented") | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
public data class CFilePointer( | ||
var currentPointer: CPointer<FILE>, | ||
var currentPosition: Long = 0 | ||
) |