/*
package uk.ac.man.cs.lethe.internal.tools

import java.io.{ File, PrintWriter, Writer, FileOutputStream }

import scala.io.Source
import scala.util.parsing.combinator.syntactical._
import scala.util.parsing.combinator.RegexParsers

/**
 * Tools for writing and reading data from files
 */
object FileTools { 
  def writeMap(map: Map[String, Any], filename: String): Unit =
    writeMap(map, filename, { (x: Any) => x.toString } )


  def writeMap[A](map: Map[String, A], filename: String, toStringMethod: A => String): Unit = { 
    val file = new File(filename)

    val writer = new PrintWriter(new FileOutputStream(file, false))
    map.foreach { case (key, value) => 
      writer.println("--- "+key)
      writer.println()
      writer.println(toStringMethod(value))
      writer.println()
      writer.println()
    }

    writer.close()
  }

  def readMap(filename: String): DataMapping = 
    new DataMapping(filename)

  def readMap[A](filename: String, parsingMethod: String => A): Map[String, A] = { 
    readStringMap(filename).mapValues(parsingMethod)
  }


  class DataMapping(filename: String) { 
    val data = readStringMap(filename)
    
    def apply[A](key: String, parsingMethod: String => A): A = { 
      parsingMethod(data(key))
    }
  }


  private def readStringMap(filename: String): Map[String, String] = { 
    val file = new File(filename)
    val contents = Source.fromFile(new File(filename)).mkString
    var portions = contents.split("\n\n\n")

    portions.groupBy(_.lines
      .next.split(" ")(1)).mapValues(text => text.last.split("\n\n")(1))
  }

}

/**
 * Simple parsing for more complex data structures
 */
object ParsingTools  { 

  def parseObj2SetMap[T](parseFunction: (String => T))(string: String): Map[T, Set[T]] = { 
    val ep = new ExtendedParser(parseFunction)
    ep.parseDirect(ep.obj2SetMap, string) 
  }

  def parseSet2ObjMap[T](parseFunction: (String => T))(string: String): Map[Set[T], T] = { 
    val ep = new ExtendedParser(parseFunction)
    ep.parseDirect(ep.set2ObjMap, string)
  }

  private class ExtendedParser[T](parseFunction: (String => T)) extends RegexParsers { 

    def parseDirect[T](parser: Parser[T], string: String) = parseAll(parser, string) match { 
      case Success(result, _) => result
      case Failure(m, remaining) => throw new RuntimeException(m+"\n"+remaining.pos.longString)
      case Error(m, remaining) => throw new RuntimeException(m+"\n"+remaining.pos.longString)      
    }

    override def skipWhitespace = false

    def set2ObjMap: Parser[Map[Set[T], T]] = 
      "Map(" ~> repsep(set2ObjPair, ", ") <~ ")" ^^ { case pairs => pairs.toMap}

    def set2ObjPair: Parser[(Set[T], T)] = 
      objSet ~ " -> " ~ obj ^^ { case key ~ _ ~ value => key -> value }

    def obj2SetMap: Parser[Map[T, Set[T]]] = 
      "Map(" ~> repsep(obj2SetPair, ", ") <~ ")" ^^ { case pairs => pairs.toMap }
    
    def obj2SetPair: Parser[(T, Set[T])] = 
      obj ~ " -> " ~ objSet ^^ { case key ~ _ ~ set => key -> set }

    def objSet: Parser[Set[T]] =
      "Set(" ~> repsep(obj, ", ") <~ ")" ^^ { case list => list.toSet }

    def obj = 
      string ^^ { case string => parseFunction(string) }

    val string = """[^\s,\)]+"""r 
  }
}
*/
