From: Tuukka Lehtonen Date: Tue, 14 Nov 2017 06:51:05 +0000 (+0200) Subject: Added StringIO.readContentsWithCharset X-Git-Tag: v1.31.0~42 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F18%2F1218%2F1;p=simantics%2Fplatform.git Added StringIO.readContentsWithCharset refs #7615 Change-Id: I8dd435df65cb8273d2be28db47cd3fef8ed60d7d --- diff --git a/bundles/org.simantics.scl.runtime/scl/StringIO.md b/bundles/org.simantics.scl.runtime/scl/StringIO.md index 7013c2ebb..013b94cab 100644 --- a/bundles/org.simantics.scl.runtime/scl/StringIO.md +++ b/bundles/org.simantics.scl.runtime/scl/StringIO.md @@ -1,5 +1,15 @@ # StringIO -# Undocumented entities +This module contains file I/O related functions that read and +write contents of textual files into/from String instances. -::undocumented[] \ No newline at end of file +## Reading + +::value[readLines] +::value[readLinesWithCharset] +::value[readContentsWithCharset] + +## Writing + +::value[writeLines] +::value[appendLine] diff --git a/bundles/org.simantics.scl.runtime/scl/StringIO.scl b/bundles/org.simantics.scl.runtime/scl/StringIO.scl index 1971afc83..a6bf0bffa 100644 --- a/bundles/org.simantics.scl.runtime/scl/StringIO.scl +++ b/bundles/org.simantics.scl.runtime/scl/StringIO.scl @@ -1,13 +1,25 @@ importJava "org.simantics.scl.runtime.procedure.StringIO" where - "Reads all lines of the file whose name is given as a parameter" + """ + Reads all lines of the file whose name is given as a parameter. + The file contents are expected to be UTF8 encoded. + """ readLines :: String -> [String] "`readLinesWithCharset \"charset\" \"file\"` reads all lines of the file whose name is given as a parameter using the given charset." readLinesWithCharset :: String -> String -> [String] - "Creates a new file with the given file name whose contents are the given lines." + "`readContentsWithCharset \"charset\" \"file\"` reads all lines of the file whose name is given as a parameter using the given charset." + readContentsWithCharset :: String -> String -> String + + """ + Creates a new file with the given file name whose contents are the given lines. + The written file will be UTF8 encoded. + """ writeLines :: String -> [String] -> () - "`appendLine fileName line` appends `line` to the file with name `fileName`" + """ + `appendLine fileName line` appends `line` to the file with name `fileName` + The appended content will be UTF8 encoded. + """ appendLine :: String -> String -> () diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java index a3a25933e..82d3a9f32 100644 --- a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java @@ -27,7 +27,16 @@ public class StringIO { throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to " + charset + "."); } } - + + public static String readContentsWithCharset(String charset, String fileName) throws IOException { + try { + Charset cs = Charset.forName(charset); + return new String(Files.readAllBytes(Paths.get(fileName)), cs); + } catch(MalformedInputException e) { + throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to " + charset + "."); + } + } + public static void writeLines(String fileName, List lines) throws IOException { BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8); for(String line : lines) { @@ -44,4 +53,5 @@ public class StringIO { writer.write("\n"); writer.close(); } + }