EdFile.icl 1.82 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
1
2
implementation module EdFile

3
4
// reading and writing text files

Diederik van Arkel's avatar
Diederik van Arkel committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import StdBool, StdFile, StdArray, StdInt, StdString, StdChar, StdFunc
import UtilNewlinesFile
import EdText, StrictList
import StdError
import UtilIO
import Platform

readText :: !String !*env -> ((Error Text,NewlineConvention,Bool), !*env) | FileSystem env
readText name env
  # (openOk,file,env)	= fopen name FReadData env
  | not openOk				
	= ((Error "error while opening file",NewlineConventionNone,False), env)
  # (readOnly,env)		= FReadOnly name env
  # (conv,lines,file)	= readConvLines file
    lines				= slFromList lines
	(readError, file)	= ferror file 
	(closeOk, env)		= fclose file env
  | readError
	= ((Error "error while reading file",conv,readOnly), env)	
  | not closeOk
	= ((Error "error while closing file",conv,readOnly), env)
  | otherwise
	= ((Ok (stringsToText lines),conv,readOnly), env)

writeText :: !String !NewlineConvention !Text !*env -> (Maybe String,!*env) | FileSystem env
writeText fileName nc text env
  # (ok, file, env) 		= PlatformDependant
  								(fopen fileName FWriteData env) // get proper line-endings on PC...sigh
								(fopen fileName FWriteText env)	// get correct creatorCode on Mac...
  | not ok
    = (Just "error opening file for writing",env)
  # file					= writeLines (textToStrings text) file
	(writeError, file)		= ferror file 
    (closeOk, env)			= fclose file env
  | writeError
	= (Just "error while writing file", env)	
  | not closeOk
    = (Just "error closing file",env)
  = (Nothing,env)
where
	writeLines :: (StrictList String) *File -> *File
	writeLines SNil file 
	  = file
	writeLines (SCons string SNil) file
	  = fwrites string file
	writeLines (SCons string strings) file 
	  # file = fwrites (string +++ ncString) file
	  = writeLines strings file
	
	ncString
		| nc == NewlineConventionNone	= "\n"
		= toString nc