diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2024-07-15 01:00:15 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2024-07-15 01:00:15 +0200 |
| commit | f4b3b65ab8def9dc4128a350a980afa483c97257 (patch) | |
| tree | c60fee7794c76b22a7665cedd7680a724fa1d7da /app/src/main | |
| parent | 427bb4c2ab0ce5cfb988789657c2de3761fec392 (diff) | |
Add Utils#createFileAndDirectories
Useful when creating a file in a directory with a path.
Diffstat (limited to 'app/src/main')
| -rw-r--r-- | app/src/main/java/org/the_jk/cleversync/io/Utils.kt | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/app/src/main/java/org/the_jk/cleversync/io/Utils.kt b/app/src/main/java/org/the_jk/cleversync/io/Utils.kt index 0f9a2b7..6593d04 100644 --- a/app/src/main/java/org/the_jk/cleversync/io/Utils.kt +++ b/app/src/main/java/org/the_jk/cleversync/io/Utils.kt @@ -13,6 +13,11 @@ object Utils { return current } + fun createFileAndDirectories(directory: ModifiableDirectory, vararg names: String): ModifiableFile { + val (dirNames, fileName) = resolve(*names) + return makeDirectories(directory, *dirNames).createFile(fileName) + } + fun openDirectory(directory: Directory, vararg names: String): Directory? { var current = directory names.forEach { nameWithSlashes -> @@ -22,4 +27,24 @@ object Utils { } return current } + + fun openFile(directory: Directory, vararg names: String): File? { + val (dirNames, fileName) = resolve(*names) + return openDirectory(directory, *dirNames)?.openFile(fileName) + } + + fun resolve(vararg names: String): Pair<Array<String>, String> { + val out = buildList { + names.forEach { nameWithSlashes -> + StringUtils.split(nameWithSlashes, '/', keepEmpty = false) + .forEach { name -> + add(name) + } + } + } + return if (out.isEmpty()) { emptyArray<String>() to "" } else { + val last = out.last() + out.dropLast(1).toTypedArray() to last + } + } } |
