aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt
diff options
context:
space:
mode:
authorDiogenes Molinares <amolinares19@gmail.com>2020-06-10 10:35:03 +0200
committerGitHub <noreply@github.com>2020-06-10 14:05:03 +0530
commit0050deb5012d158f83f04bc6be61a52596fa99c2 (patch)
tree51417971d7b7dbcbeb3d34d688c9a8d45a9c24ca /app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt
parent7569d4027081d27a6e3d975c14cfc9bdcd879257 (diff)
downloadHardenedPass-0050deb5012d158f83f04bc6be61a52596fa99c2.tar.gz
HardenedPass-0050deb5012d158f83f04bc6be61a52596fa99c2.zip
Remove Apache Commons dependencies (#840)
Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt47
1 files changed, 47 insertions, 0 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt b/app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt
new file mode 100644
index 00000000..867ec0aa
--- /dev/null
+++ b/app/src/main/java/com/zeapo/pwdstore/utils/FileUtils.kt
@@ -0,0 +1,47 @@
+/*
+ * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package com.zeapo.pwdstore.utils
+
+import java.io.File
+
+object FileUtils {
+ @JvmStatic
+ fun listFiles(dir: File, recursive: Boolean): Collection<File> {
+ val res = ArrayList<File>()
+ val files = dir.listFiles()
+
+ if (files != null && files.isNotEmpty()) {
+
+ files.forEach { file ->
+ // Check if the file is a directory and recursive add
+ if (file.isDirectory && recursive) {
+ res.addAll(listFiles(file, recursive))
+ } else if (!file.isDirectory) {
+ res.add(file)
+ }
+ }
+ }
+ return res
+ }
+
+ @JvmStatic
+ fun getBaseName(filename: String): String {
+ // Take the file name along with its extension
+ val indexName = filename.lastIndexOf('/')
+ val nameWithExtension = filename.substring(indexName + 1)
+
+ // Find the final '.' character in the previously calculated nameWithExtension
+ val indexExt = nameWithExtension.lastIndexOf('.')
+
+ // If no '.' is found in the name, we assume this is a directory and return the previously
+ // derived nameWithExtensions as-is, otherwise we slice out a substring from the first character
+ // to the last occurrence of '.' which we found earlier.
+ return if (indexExt == -1)
+ nameWithExtension
+ else
+ nameWithExtension.substring(0, indexExt)
+ }
+}