Commit 540ef351 authored by Bas Lijnse's avatar Bas Lijnse

Merge branch 'csv' into 'master'

add createCSVFileWith/createUtf8CSVFileWith

See merge request !362
parents 21fc8a3e b70741d7
Pipeline #34864 passed with stage
in 6 minutes and 46 seconds
......@@ -38,6 +38,35 @@ importCSVDocumentWith :: !Char !Char !Char !Document -> Task [[String]]
* @return The exported content as a document
*/
createCSVFile :: !String ![[String]] -> Task Document
/**
* Export a list of rows of fields to a comma separated vector (CSV) document.
*
* @param Separator: The field separator
* @param Quote character: The string quote character
* @param Escape character: The escape character
* @param File name: A name of the created CSV file
* @param Cells: The content to export as a list of rows of lists of fields
*
* @return The exported content as a document
*/
createCSVFileWith :: !Char !Char !Char !String ![[String]] -> Task Document
/**
* Export a list of rows of fields to a comma separated vector (CSV) document encoded in UTF-8.
* The file starts with a UTF-8 byte order mask.
* This is to make sure the Excel correctly detect the encoding when importing the file.
*
* @param Separator: The field separator
* @param Quote character: The string quote character
* @param Escape character: The escape character
* @param File name: A name of the created CSV file
* @param Cells: The content to export as a list of rows of lists of fields
*
* @return The exported content as a document
*/
createUtf8CSVFileWith :: !Char !Char !Char !String ![[String]] -> Task Document
/**
* Export a list of rows of fields to a comma separated vector (CSV) file on the server's filesystem.
*
......
implementation module iTasks.Extensions.CSVFile
import StdBool, StdList, System.FilePath, Text, Text.CSV, System.File, Data.Error
import iTasks, iTasks.Extensions.TextFile
import iTasks.Internal.IWorld, iTasks.Internal.Task, iTasks.Internal.TaskState, iTasks.Internal.TaskStore
importCSVFile :: !FilePath -> Task [[String]]
......@@ -27,17 +28,35 @@ where
# (filename,iworld) = documentLocation documentId iworld
= fileTaskRead taskId filename (readCSVFileWith delimitChar quoteChar escapeChar) iworld
createCSVFile :: !String ![[String]] -> Task Document
createCSVFile filename content = mkInstantTask eval
createCSVFile filename content =
withTemporaryDirectory
(\dir -> let tmpFilePath = dir </> filename in exportCSVFile filename content >-| importDocument tmpFilePath)
createCSVFileWith :: !Char !Char !Char !String ![[String]] -> Task Document
createCSVFileWith delimitChar quoteChar escapeChar filename content =
withTemporaryDirectory
( \dir ->
let tmpFilePath = dir </> filename in
exportCSVFileWith delimitChar quoteChar escapeChar filename content >-| importDocument tmpFilePath
)
createUtf8CSVFileWith :: !Char !Char !Char !String ![[String]] -> Task Document
createUtf8CSVFileWith delimitChar quoteChar escapeChar filename content =
withTemporaryDirectory
( \dir ->
let tmpFilePath = dir </> filename in
exportCSVFileWith delimitChar quoteChar escapeChar filename content >-|
importTextFile tmpFilePath >>- \fileContent ->
mkInstantTask (createDoc fileContent)
)
where
eval taskId iworld=:{current={taskTime}}
# csv = join "\n" (map (join ",") content)
# (mbDoc,iworld) = createDocument filename "text/csv" csv iworld
createDoc fileContent _ iworld
# (mbDoc, iworld) = createDocument filename "text/csv" ("\xEF\xBB\xBF" +++ fileContent) iworld
= case mbDoc of
Ok doc = (Ok doc, iworld)
Error e = (Error (dynamic e,toString e),iworld)
Ok doc -> (Ok doc, iworld)
Error e -> (Error (dynamic e, toString e), iworld)
exportCSVFile :: !FilePath ![[String]] -> Task [[String]]
exportCSVFile filename content = mkInstantTask eval
where
......
implementation module iTasks.Extensions.TextFile
import StdBool, StdList, StdFile, StdArray, System.FilePath, Text, System.File, Data.Error, StdString
import StdBool, StdList, StdFile, StdArray, System.FilePath, Text, System.File, Data.Error, StdString, Data.Func
import iTasks.Internal.IWorld, iTasks.Internal.Task, iTasks.Internal.TaskState, iTasks.Internal.TaskStore
CHUNK_SIZE :== 1048576 // 1M
......@@ -37,14 +37,15 @@ fileTaskRead taskId filename f iworld=:{IWorld|current={taskTime},world}
# (ok,world) = fclose file world
| not ok = (closeException filename,{IWorld|iworld & world = world})
= (Ok res, {IWorld|iworld & world = world})
readAll file
# (chunk,file) = freads file CHUNK_SIZE
| size chunk < CHUNK_SIZE
= (chunk,file)
| otherwise
# (rest,file) = readAll file
= (chunk +++ rest,file)
readAll :: !*File -> (!String, !*File)
readAll file = readAll` [] file
where
readAll` :: ![String] !*File -> (!String, !*File)
readAll` acc file
# (chunk,file) = freads file CHUNK_SIZE
| size chunk < CHUNK_SIZE = (concat $ reverse [chunk: acc], file)
| otherwise = readAll` [chunk: acc] file
writeAll content file
= fwrites content file
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment