annotate src/name/blackcap/exifwasher/Files.kt @ 0:db63d01a23c6

JNI calls and test case (finally!) seem to work.
author David Barts <n5jrn@me.com>
date Tue, 31 Mar 2020 13:24:48 -0700
parents
children 19c381c536ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 /*
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 * For dealing with files.
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 */
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 package name.blackcap.exifwasher
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
5
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import java.io.BufferedReader
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import java.io.File
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import java.io.FileInputStream
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import java.io.InputStreamReader
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 import java.util.Properties
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
11 import java.util.logging.FileHandler
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
12 import java.util.logging.Level
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 import java.util.logging.Logger
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 import java.util.logging.SimpleFormatter
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
15
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 /* OS Type */
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
17
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 enum class OS {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 MAC, UNIX, WINDOWS, OTHER;
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 companion object {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 private val rawType = System.getProperty("os.name")?.toLowerCase()
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 val type = if (rawType == null) {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 OTHER
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 } else if (rawType.contains("win")) {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 WINDOWS
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 } else if (rawType.contains("mac")) {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 MAC
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 } else if (rawType.contains("nix") || rawType.contains("nux") || rawType.contains("aix") || rawType.contains("sunos")) {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 UNIX
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 } else {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 OTHER
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
35
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 /* joins path name components to java.io.File */
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
37
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 fun joinPath(base: String, vararg rest: String) = rest.fold(File(base), ::File)
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
39
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 /* file names */
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
41
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 private val SHORTNAME = "exifwasher"
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 private val LONGNAME = "name.blackcap." + SHORTNAME
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 private val HOME = System.getenv("HOME")
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 private val APPDATA = System.getenv("APPDATA")
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 val PF_DIR = when (OS.type) {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 OS.MAC -> joinPath(HOME, "Library", "Application Support", LONGNAME)
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 OS.WINDOWS -> joinPath(APPDATA, "Roaming", LONGNAME)
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 else -> joinPath(HOME, "." + SHORTNAME)
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 val LF_DIR = when (OS.type) {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 OS.MAC -> joinPath(HOME, "Library", "Application Support", LONGNAME)
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 OS.WINDOWS -> joinPath(APPDATA, "Local", LONGNAME)
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 else -> joinPath(HOME, "." + SHORTNAME)
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 val PROP_FILE = File(PF_DIR, SHORTNAME + ".properties")
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 val LOG_FILE = File(LF_DIR, SHORTNAME + ".log")
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
58
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 /* make some needed directories */
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
60
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 private fun File.makeIfNeeded() = if (exists()) { true } else { mkdirs() }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
62
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 /* make some usable objects */
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
64
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 val DPROPERTIES = Properties().apply {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 OS::class.java.getResourceAsStream("default.properties").use { load(it) }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
68
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 val PROPERTIES = Properties(DPROPERTIES).apply {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 PF_DIR.makeIfNeeded()
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 PROP_FILE.createNewFile()
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 BufferedReader(InputStreamReader(FileInputStream(PROP_FILE), "UTF-8")).use {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 load(it)
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
76
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 val LOGGER = run {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 LF_DIR.makeIfNeeded()
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 Logger.getLogger(LONGNAME).apply {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 addHandler(FileHandler(LOG_FILE.toString()).apply {
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 formatter = SimpleFormatter() })
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 level = Level.CONFIG
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 useParentHandlers = false
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 }
db63d01a23c6 JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 }