base/src/sqwl/cms/package.scala
author Tomas Zeman <tzeman@volny.cz>
Thu, 13 Dec 2018 17:33:35 +0100
changeset 21 1fa630ed9b8a
parent 18 8cfd67425811
child 23 afcfbdeb3b88
permissions -rw-r--r--
Content: type annotations

package sqwl

import java.nio.file.Path

import scala.io.Source
import scala.util.{Failure, Success, Try}

package object cms {

  def readSourceFull(src: Source): Try[String] = {
    try {
      Success(src.getLines.mkString)
    } catch {
      case t: Throwable => Failure(t)
    } finally {
      src.close()
    }
  }

  def readFile(f: Path, enc: String = "UTF-8"): Try[String] =
    readSourceFull(Source.fromFile(f.toFile, enc))

  def readFile(f: String): String =
    readSourceFull(Source.fromFile(f, "UTF-8")) getOrElse ""

}