Implemented basic file i/o.

This commit is contained in:
Achim D. Brucker 2018-03-06 06:37:33 +00:00
parent 928557eced
commit 31df80022e
3 changed files with 94 additions and 6 deletions

View File

@ -45,4 +45,4 @@ SCALAC_VERSION=`$ISABELLE scalac -version`
echo "Building dof_latex_converter using"
echo " * $ISABELLE_VERSION"
echo " * $SCALAC_VERSION"
$ISABELLE scalac src/dof_latex_converter.scala -d bin/dof_latex_converter.jar
$ISABELLE scalac src/*.scala -d bin/dof_latex_converter.jar

View File

@ -0,0 +1,53 @@
/**
* Copyright (c) 2018 The University of Sheffield. All rights reserved.
* 2018 The University of Paris-Sud. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package com.logicalhacking.dof.converter
import scala.language.reflectiveCalls
import scala.util.matching.Regex
import java.io.{BufferedWriter, File, FileWriter}
object IoUtils {
def using[A <: { def close(): Unit }, B](param: A)(f: A => B): B =
try {
f(param)
}
finally {
param.close()
}
def recursiveListFiles(f: File, r: Regex): Array[File] = {
val these = f.listFiles
val good = these.filter(f => r.findFirstIn(f.getName).isDefined)
good ++ these.filter(_.isDirectory).flatMap(recursiveListFiles(_,r))
}
}

View File

@ -29,9 +29,44 @@
package com.logicalhacking.dof.converter
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Empty stub for the DOF LaTeX-to-LaTeX converter.")
}
}
import java.io.{BufferedWriter, File, FileWriter}
import IoUtils._
import scala.util.matching.Regex
object DofConverter {
def convertTexLine(line:String) = {
// TODO
line
}
def convertFile(f: File) = {
val texFileName = f.getAbsolutePath()
println("ODF Converger: converting " + texFileName
+ " (Not yet fully implemented!)")
f.renameTo(new File(texFileName+".orig"))
using(io.Source.fromFile(texFileName+".orig")) {
inputFile =>
using(new BufferedWriter(new FileWriter(new File(texFileName), true))) {
outputFile =>
outputFile.write("% This file was modified by the DOV LaTex converter\n")
for (line <- inputFile.getLines) {
val outputLine = convertTexLine(line)
outputFile.write(outputLine.toUpperCase + "\n")
}
}
}
}
def main(args: Array[String]): Unit = {
val dir = if (args.length == 0) {
"."
} else {
args(0)
}
val texFiles = recursiveListFiles(new File(dir), new Regex("\\.tex$"))
for (file <- texFiles) {
convertFile(file)
}
}
}