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