]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added StringIO.readContentsWithCharset 18/1218/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 14 Nov 2017 06:51:05 +0000 (08:51 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 14 Nov 2017 06:51:05 +0000 (08:51 +0200)
refs #7615

Change-Id: I8dd435df65cb8273d2be28db47cd3fef8ed60d7d

bundles/org.simantics.scl.runtime/scl/StringIO.md
bundles/org.simantics.scl.runtime/scl/StringIO.scl
bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/procedure/StringIO.java

index 7013c2ebbb8e149a6a5f823ced8c9b151a33ee98..013b94cab3e08fe2d59ef9f9cbde142edffcabcd 100644 (file)
@@ -1,5 +1,15 @@
 # StringIO
 
 # 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]
index 1971afc83ad3e259dedc1b3db42f4d7daba9cf3a..a6bf0bffaf68fc347ec100e4117424bdb4fb9b40 100644 (file)
@@ -1,13 +1,25 @@
 
 importJava "org.simantics.scl.runtime.procedure.StringIO" where
 
 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 -> <Proc> [String]
     
     "`readLinesWithCharset \"charset\" \"file\"` reads all lines of the file whose name is given as a parameter using the given charset."
     readLinesWithCharset :: String -> String -> <Proc> [String]
     
     readLines :: String -> <Proc> [String]
     
     "`readLinesWithCharset \"charset\" \"file\"` reads all lines of the file whose name is given as a parameter using the given charset."
     readLinesWithCharset :: String -> String -> <Proc> [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 -> <Proc> 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] -> <Proc> ()
     
     writeLines :: String -> [String] -> <Proc> ()
     
-    "`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 -> <Proc> ()
     appendLine :: String -> String -> <Proc> ()
index a3a25933e71898af83616b42624641a26b3c7e10..82d3a9f326f772843c086a2bc43d281f9e45c3f2 100644 (file)
@@ -27,7 +27,16 @@ public class StringIO {
             throw new RuntimeException("Encoding of the file '" + fileName + "' does not conform to " + charset + ".");
         }
     }
             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<String> lines) throws IOException {
         BufferedWriter writer = Files.newBufferedWriter(Paths.get(fileName), UTF8);
         for(String line : lines) {
     public static void writeLines(String fileName, List<String> 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();
     }
         writer.write("\n");
         writer.close();
     }
+
 }
 }