Minor fixes all around
This commit is contained in:
@ -11,11 +11,57 @@ import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import java.io.File
|
||||
import org.json.JSONObject
|
||||
import org.junit.jupiter.api.DisplayNameGenerator.Simple
|
||||
import java.text.DateFormat
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.prefs.Preferences
|
||||
import java.time.Instant
|
||||
|
||||
object Loader {
|
||||
data class XmlIntrinsic(val name: String, val tech: String, val retType: String, val retVar: String?, val args: List<Pair<String, String>>, val desc: String, val op: String?, val insn: List<Triple<String, String, String?>>, val cpuid: String?, val category: String)
|
||||
private val cache = Preferences.userNodeForPackage(this::class.java)
|
||||
private val intelDateTimeFormat = SimpleDateFormat("MM/dd/yyyy")
|
||||
private val dateFormat = SimpleDateFormat("dd-MMMM-yyyy")
|
||||
|
||||
data class XmlIntrinsic(val name: String, val tech: String, val retType: String, val retVar: String?,
|
||||
val args: List<Pair<String, String>>, val desc: String, val op: String?,
|
||||
val insn: List<Triple<String, String, String?>>, val cpuid: String?, val category: String
|
||||
)
|
||||
|
||||
data class Version(val major: Int, val minor: Int, val patch: Int) {
|
||||
override fun toString(): String = "$major.$minor.$patch"
|
||||
|
||||
infix fun isNewerThan(other: Version): Boolean {
|
||||
if(major > other.major) return true
|
||||
if(major == other.major && minor > other.minor) return true
|
||||
if(major == other.major && minor == other.minor && patch > other.patch) return true
|
||||
return false
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun fromString(version: String): Version {
|
||||
val parts = version.split(".")
|
||||
return Version(parts[0].toInt(), parts[1].toInt(), parts[2].toInt())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class DatedVersion(val version: Version, val releaseDate: Date, val updateDate: Date) {
|
||||
override fun toString(): String = "$version (remote released ${dateFormat.format(releaseDate)}; local updated ${dateFormat.format(updateDate)})"
|
||||
|
||||
companion object {
|
||||
fun fromPrefs(): DatedVersion {
|
||||
val v = cache.get("version", null)?.let { Version.fromString(it) } ?: Version(0, 0, 0)
|
||||
val r = cache.get("release", null)?.let { dateFormat.parse(it) } ?: Date.from(Instant.ofEpochMilli(0))
|
||||
val u = cache.get("update", null)?.let { dateFormat.parse(it) } ?: Date.from(Instant.ofEpochMilli(0))
|
||||
|
||||
return DatedVersion(v, r, u)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class XmlData(
|
||||
val version: DatedVersion,
|
||||
val types: Set<String>, val techs: Set<String>, val cpuids: Set<String>, val categories: Set<String>,
|
||||
val intrinsics: List<XmlIntrinsic>
|
||||
)
|
||||
@ -24,6 +70,12 @@ object Loader {
|
||||
|
||||
data class JsonData(val platforms: Set<String>, val data: Map<String, Map<String, Performance>>)
|
||||
|
||||
private suspend fun updateVersion(version: DatedVersion) = coroutineScope {
|
||||
cache.put("version", version.version.toString())
|
||||
cache.put("release", dateFormat.format(version.releaseDate))
|
||||
cache.put("update", dateFormat.format(version.updateDate))
|
||||
}
|
||||
|
||||
suspend fun loadXml(xmlFile: String): XmlData = coroutineScope {
|
||||
val xml = Ksoup.parseXml(File(xmlFile).readText(Charsets.UTF_8))
|
||||
|
||||
@ -35,6 +87,28 @@ object Loader {
|
||||
|
||||
val errors = mutableListOf<String>()
|
||||
|
||||
val (version, release) = xml.getElementsByTag("intrinsics_list").firstOrNull()?.let {
|
||||
val version = it.attribute("version")?.value?.let { v -> Version.fromString(v) }
|
||||
if(version == null) {
|
||||
errors += "Missing version attribute in intrinsics_list element"
|
||||
return@let null
|
||||
}
|
||||
val date = it.attribute("date")?.value?.let { d -> intelDateTimeFormat.parse(d) }
|
||||
if(date == null) {
|
||||
errors += "Missing release_date attribute in intrinsics_list element"
|
||||
return@let null
|
||||
}
|
||||
|
||||
version to date
|
||||
} ?: Version(0, 0, 0) to Date()
|
||||
|
||||
if(errors.isNotEmpty()) {
|
||||
errors.forEach { System.err.println(it) }
|
||||
throw Exception("XML file is (partially) invalid")
|
||||
}
|
||||
|
||||
val update = Date()
|
||||
|
||||
xml.getElementsByTag("intrinsic").forEachIndexed { i, it ->
|
||||
val name = it.attribute("name")?.value
|
||||
if(name == null) {
|
||||
@ -123,7 +197,10 @@ object Loader {
|
||||
throw Exception("XML file is (partially) invalid")
|
||||
}
|
||||
|
||||
XmlData(types = cppTypes, techs = techs, cpuids = cpuids, categories = categories, intrinsics = intrins)
|
||||
XmlData(version = DatedVersion(version, release, update),
|
||||
types = cppTypes, techs = techs, cpuids = cpuids, categories = categories,
|
||||
intrinsics = intrins
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun loadJson(jsonFile: String): JsonData = coroutineScope {
|
||||
@ -201,5 +278,6 @@ object Loader {
|
||||
}
|
||||
}
|
||||
}
|
||||
updateVersion(xml.version)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user