Skip to content

Instantly share code, notes, and snippets.

@Sciss
Created July 2, 2019 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sciss/4ba53e36b21b8034d1e4442fa8ebce82 to your computer and use it in GitHub Desktop.
Save Sciss/4ba53e36b21b8034d1e4442fa8ebce82 to your computer and use it in GitHub Desktop.
val fCfg = userHome / ".config" / "rncbc.org" / "QjackCtl.conf"
assert(fCfg.isFile)
val cOld = {
import java.io._
val in = new FileInputStream(fCfg)
try {
val sz = in.available()
val arr = new Array[Byte](sz)
in.read(arr)
new String(arr, "UTF-8")
} finally {
in.close()
}
}
val linesOld = cOld.split('\n').toList
val i1 = linesOld.indexOf("[Presets]") + 1
val i2 = linesOld.indexOf("", i1)
val (linesRem, lineOff, names) = {
var i = 1
val b = List.newBuilder[String]
var r = linesOld
var mn = i2
var i3 = i2
while ({
val key = s"Preset$i="
val j = r.indexWhere(_ startsWith key, i1)
j > 0 && j < i3 && {
b += r(j).substring(key.length)
r = r.patch(j, Nil, 1)
i += 1
i3 -= 1
mn = math.min(mn, j)
true
}
}) ()
(r, mn, b.result())
}
assert(names.nonEmpty && linesRem.size + names.size == linesOld.size)
val namesS = names.sortBy(_.toUpperCase(java.util.Locale.US))
val linesNew = namesS.zipWithIndex.foldLeft(linesRem) { case (r, (name, idx)) =>
val key = s"Preset${idx + 1}=$name"
r.patch(lineOff + idx, key :: Nil, 0)
}
assert(linesNew.size == linesOld.size)
val cNew = linesNew.mkString("\n")
{
import java.io._
val out = new FileOutputStream(fCfg)
try {
val arr = cNew.getBytes("UTF-8")
out.write(arr)
} finally {
out.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment