Mercurial > cgi-bin > hgweb.cgi > ClipMan
diff src/name/blackcap/clipman/Files.kt @ 0:be282c48010a
Incomplete; checking it in as a backup.
author | David Barts <n5jrn@me.com> |
---|---|
date | Tue, 14 Jan 2020 14:07:19 -0800 |
parents | |
children | 33fbe3a78d84 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/name/blackcap/clipman/Files.kt Tue Jan 14 14:07:19 2020 -0800 @@ -0,0 +1,81 @@ +/* + * For dealing with files. + * BUG: does not ensure directories exist + */ +package name.blackcap.clipman + +import java.io.BufferedReader +import java.io.File +import java.io.FileReader +import java.util.Properties +import java.util.logging.FileHandler +import java.util.logging.Level +import java.util.logging.Logger +import java.util.logging.SimpleFormatter + +/* OS Type */ + +enum class OS { + MAC, UNIX, WINDOWS, OTHER; + companion object { + private val rawType = System.getProperty("os.name")?.toLowerCase() + val type = if (rawType == null) { + OTHER + } else if (rawType.contains("win")) { + WINDOWS + } else if (rawType.contains("mac")) { + MAC + } else if (rawType.contains("nix") || rawType.contains("nux") || rawType.contains("aix") || rawType.contains("sunos")) { + UNIX + } else { + OTHER + } + } +} + +/* joins path name components to java.io.File */ + +fun joinPath(base: String, vararg rest: String) = rest.fold(File(base), ::File) + +/* file names */ + +private val SHORTNAME = "clipman" +private val LONGNAME = "name.blackcap." + SHORTNAME +private val HOME = System.getenv("HOME") +private val APPDATA = System.getenv("APPDATA") +private val PF_DIR = when (OS.type) { + OS.MAC -> joinPath(HOME, "Library", "Application Support", LONGNAME) + OS.WINDOWS -> joinPath(APPDATA, "Roaming", LONGNAME) + else -> joinPath(HOME, "." + SHORTNAME) +} +private val LF_DIR = when (OS.type) { + OS.MAC -> joinPath(HOME, "Library", "Application Support", LONGNAME) + OS.WINDOWS -> joinPath(APPDATA, "Local", LONGNAME) + else -> joinPath(HOME, "." + SHORTNAME) +} +val PROP_FILE = File(PF_DIR, SHORTNAME + ".properties") +val LOG_FILE = File(LF_DIR, SHORTNAME + ".log") + +/* make some needed directories */ + +private fun File.makeIfNeeded() = if (exists()) { true } else { mkdirs() } + +/* make some usable objects */ + +val PROPERTIES = run { + PF_DIR.makeIfNeeded() + PROP_FILE.createNewFile() + Properties().apply { + BufferedReader(FileReader(PROP_FILE)).use { load(it) } + } +} + +val LOGGER = run { + LF_DIR.makeIfNeeded() + Logger.getLogger(LONGNAME).apply { + addHandler(FileHandler(LOG_FILE.toString()).apply { + formatter = SimpleFormatter() }) + level = Level.CONFIG + useParentHandlers = false + } +}