Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Tim Steenvoorden
clean-base
Commits
7eaf52b7
Commit
7eaf52b7
authored
Mar 07, 2016
by
Tim Steenvoorden
Browse files
add and implement Data.File to core
parent
9bb08694
Changes
3
Hide whitespace changes
Inline
Side-by-side
Sources/Clean/Core.dcl
View file @
7eaf52b7
...
...
@@ -5,7 +5,7 @@ import Data.Char
import
Data
.
Nat
import
Data
.
Int
import
Data
.
Real
//
import Data.File
import
Data
.
File
import
Data
.
Function
...
...
Sources/Data/File.dcl
View file @
7eaf52b7
system
module
Data
.
File
//TODO change `prim_` prefix to just `_` and move to internal module.
/// Basic file operations
///
/// Import this module qualified!
/// # Files
...
...
@@ -11,36 +13,37 @@ system module Data.File
/// ## Opening and Closing
prim_openFile
::
!
String
!
Int
->
(!
Bool
,!*
File
)
prim_closeFil
e
::
!*
File
->
Bool
prim_
reopen
File
::
!*
File
!
Int
->
(!
Bool
,!*
File
)
open
::
!
String
!
Int
->
(!
Bool
,!*
File
)
clos
e
::
!*
File
->
Bool
reopen
::
!*
File
!
Int
->
(!
Bool
,!*
File
)
/// ## Standard I
O
/// ##
#
Standard I
nput/Output
prim_
stdio
::
*
File
prim_
stderr
::
*
File
stdio
::
*
File
stderr
::
*
File
/// ## Seeking
prim_
position
File
::
!*
File
->
(!
Int
,!*
File
)
prim_seekFile
::
!*
File
!
Int
!
Int
->
(!
Bool
,!*
File
)
position
::
!*
File
->
(!
Int
,!*
File
)
seek
::
!*
File
!
Int
!
Int
->
(!
Bool
,!*
File
)
/// ## Test
s
/// ## Test
ing
prim_isEndOfFile
::
!*
File
->
(!
Bool
,!*
File
)
prim_
isError
File
::
!*
File
->
(!
Bool
,!*
File
)
isAtEnd
::
!*
File
->
(!
Bool
,!*
File
)
isError
::
!*
File
->
(!
Bool
,!*
File
)
/// ## Reading
prim_readCharFile
::
!*
File
->
(!
Bool
,!
Char
,!*
File
)
prim_readIntFile
::
!*
File
->
(!
Bool
,!
Int
,!*
File
)
prim_readRealFile
::!*
File
->
(!
Bool
,!
Real
,!*
File
)
prim_readStringFile
::
!*
File
!
Int
->
(!*
String
,!*
File
)
prim_readLineFile
::
!*
File
->
(!*
String
,!*
File
)
readChar
::
!*
File
->
(!
Bool
,!
Char
,!*
File
)
readInt
::
!*
File
->
(!
Bool
,!
Int
,!*
File
)
readReal
::
!*
File
->
(!
Bool
,!
Real
,!*
File
)
readString
::
!*
File
!
Int
->
(!*
String
,!*
File
)
readLine
::
!*
File
->
(!*
String
,!*
File
)
/// ## Writing
prim_
writeChar
File
::
!
Char
!*
File
->
*
File
prim_
writeInt
File
::
!
Int
!*
File
->
*
File
prim_
writeReal
File
::
!
Real
!*
File
->
*
File
prim_
writeString
File
::
!
String
!*
File
->
*
File
writeChar
::
!
Char
!*
File
->
*
File
writeInt
::
!
Int
!*
File
->
*
File
//
writeReal :: !Real !*File -> *File
writeString
::
!
String
!*
File
->
*
File
Sources/Data/File.icl
View file @
7eaf52b7
implementation
module
Data
.
File
//TODO change `prim_` prefix to just `_` and move to internal module.
import
Data
.
Function
/// ## Opening and Closing
/// Opens a file for the first time in a certain mode (read, write or append, text or data).
/// The boolean output parameter reports success or failure.
prim_openFile
::
!
String
!
Int
->
(!
Bool
,!*
File
)
prim_openFile
s
i
=
code inline {
open
::
!
String
!
Int
->
(!
Bool
,!*
File
)
open
s
i
=
code inline {
.d
1
1
i
jsr
openF
.o
0
3
b
f
...
...
@@ -15,8 +15,8 @@ prim_openFile s i = code inline {
/// Closes a file.
/// The boolean output parameter reports whether the file was successfully closed.
prim_closeFil
e
::
!*
File
->
Bool
prim_closeFil
e
f
=
code inline {
clos
e
::
!*
File
->
Bool
clos
e
f
=
code inline {
.d
0
2
f
jsr
closeF
.o
0
1
b
...
...
@@ -24,8 +24,8 @@ prim_closeFile f = code inline {
/// Re-opens an open file in a possibly different mode.
/// The boolean indicates whether the file was successfully closed before reopening.
prim_
reopen
File
::
!*
File
!
Int
->
(!
Bool
,!*
File
)
prim_
reopen
File
f
m
=
code inline {
reopen
::
!*
File
!
Int
->
(!
Bool
,!*
File
)
reopen
f
m
=
code inline {
.d
0
3
f
i
jsr
reopenF
.o
0
3
b
f
...
...
@@ -34,16 +34,16 @@ prim_reopenFile f m = code inline {
/// ## Standard IO
/// Open the 'Console' for reading and writing.
prim_
stdio
::
*
File
prim_
stdio
=
code inline {
stdio
::
*
File
stdio
=
code inline {
.d
0
0
jsr
stdioF
.o
0
2
f
}
/// Open the 'Errors' file for writing only. May be opened more than once.
prim_
stderr
::
*
File
prim_
stderr
=
code inline {
stderr
::
*
File
stderr
=
code inline {
.d
0
0
jsr
stderrF
.o
0
2
f
...
...
@@ -53,8 +53,8 @@ prim_stderr = code inline {
/// Returns the current position of the file pointer as an integer.
/// This position can be used later on for the fseek function.
prim_
position
File
::
!*
File
->
(!
Int
,!*
File
)
prim_
position
File
f
=
code inline {
position
::
!*
File
->
(!
Int
,!*
File
)
position
f
=
code inline {
.d
0
2
f
jsr
positionF
.o
0
3
i
f
...
...
@@ -62,8 +62,8 @@ prim_positionFile f = code inline {
/// Move to a different position in the file, the first integer argument is the offset,
/// the second argument is a seek mode. (see above). True is returned if successful.
prim_seekFile
::
!*
File
!
Int
!
Int
->
(!
Bool
,!*
File
)
prim_seekFile
f
p
m
=
code inline {
seek
::
!*
File
!
Int
!
Int
->
(!
Bool
,!*
File
)
seek
f
p
m
=
code inline {
.d
0
4
f
i
i
jsr
seekF
.o
0
3
b
f
...
...
@@ -72,16 +72,16 @@ prim_seekFile f p m = code inline {
/// ## Tests
/// Tests for end-of-file.
prim_isEndOfFile
::
!*
File
->
(!
Bool
,!*
File
)
prim_isEndOfFile
f
=
code inline {
isAtEnd
::
!*
File
->
(!
Bool
,!*
File
)
isAtEnd
f
=
code inline {
.d
0
2
f
jsr
endF
.o
0
3
b
f
}
/// Has an error occurred during previous file I/O operations?
prim_
isError
File
::
!*
File
->
(!
Bool
,!*
File
)
prim_
isError
File
f
=
code inline {
isError
::
!*
File
->
(!
Bool
,!*
File
)
isError
f
=
code inline {
.d
0
2
f
jsr
errorF
.o
0
3
b
f
...
...
@@ -90,8 +90,8 @@ prim_isErrorFile f = code inline {
/// ## Reading
/// Reads a character from a text file or a byte from a datafile.
prim_
readChar
File
::
!*
File
->
(!
Bool
,!
Char
,!*
File
)
prim_
readChar
File
f
=
code inline {
readChar
::
!*
File
->
(!
Bool
,!
Char
,!*
File
)
readChar
f
=
code inline {
.d
0
2
f
jsr
readFC
.o
0
4
b
c
f
...
...
@@ -100,8 +100,8 @@ prim_readCharFile f = code inline {
/// Reads an integer from a textfile by skipping spaces, tabs and newlines and
/// then reading digits, which may be preceeded by a plus or minus sign.
/// From a datafile freadi will just read four bytes (a Clean Int).
prim_
readInt
File
::
!*
File
->
(!
Bool
,!
Int
,!*
File
)
prim_
readInt
File
f
=
code inline {
readInt
::
!*
File
->
(!
Bool
,!
Int
,!*
File
)
readInt
f
=
code inline {
.d
0
2
f
jsr
readFI
.o
0
4
b
i
f
...
...
@@ -110,18 +110,18 @@ prim_readIntFile f = code inline {
/// Reads a real from a textfile by skipping spaces, tabs and newlines and then
/// reading a character representation of a real number.
/// From a datafile freadr will just read eight bytes (a Clean Real).
prim_
readReal
File
::!*
File
->
(!
Bool
,!
Real
,!*
File
)
prim_
readReal
File
f
=
code inline {
readReal
::
!*
File
->
(!
Bool
,!
Real
,!*
File
)
readReal
f
=
undefined
/*FIXME
code inline {
.d 0 2 f
jsr readFR
.o 0 5 b r f
}
}
*/
/// Reads n characters from a text or data file, which are returned as a String.
/// If the file doesn't contain n characters the file will be read to the end
/// of the file. An empty String is returned if no characters can be read.
prim_
readString
File
::
!*
File
!
Int
->
(!*
String
,!*
File
)
prim_
readString
File
f
l
=
code inline {
readString
::
!*
File
!
Int
->
(!*
String
,!*
File
)
readString
f
l
=
code inline {
.d
0
3
f
i
jsr
readFS
.o
1
2
f
...
...
@@ -129,8 +129,8 @@ prim_readStringFile f l = code inline {
/// Reads a line from a textfile. (including a newline character, except for the last
/// line) freadline cannot be used on data files.
prim_
readLine
File
::
!*
File
->
(!*
String
,!*
File
)
prim_
readLine
File
f
=
code inline {
readLine
::
!*
File
->
(!*
String
,!*
File
)
readLine
f
=
code inline {
.d
0
2
f
jsr
readLineF
.o
1
2
f
...
...
@@ -140,8 +140,8 @@ prim_readLineFile f = code inline {
/// Writes a character to a textfile.
/// To a datafile fwritec writes one byte (a Clean Char).
prim_
writeChar
File
::
!
Char
!*
File
->
*
File
prim_
writeChar
File
c
f
=
code inline {
writeChar
::
!
Char
!*
File
->
*
File
writeChar
c
f
=
code inline {
.d
0
3
c
f
jsr
writeFC
.o
0
2
f
...
...
@@ -149,8 +149,8 @@ prim_writeCharFile c f = code inline {
/// Writes an integer (its textual representation) to a text file.
/// To a datafile fwritec writes four bytes (a Clean Int).
prim_
writeInt
File
::
!
Int
!*
File
->
*
File
prim_
writeInt
File
i
f
=
code inline {
writeInt
::
!
Int
!*
File
->
*
File
writeInt
i
f
=
code inline {
.d
0
3
i
f
jsr
writeFI
.o
0
2
f
...
...
@@ -158,16 +158,16 @@ prim_writeIntFile i f = code inline {
/// Writes a real (its textual representation) to a text file.
/// To a datafile fwriter writes eight bytes (a Clean Real).
prim_
writeReal
File
::
!
Real
!*
File
->
*
File
prim_
writeReal
File
r
f
=
code inline {
writeReal
::
!
Real
!*
File
->
*
File
writeReal
r
f
=
undefined
/*FIXME
code inline {
.d 0 4 r f
jsr writeFR
.o 0 2 f
}
}
*/
/// Writes a String to a text or data file.
prim_
writeString
File
::
!
String
!*
File
->
*
File
prim_
writeString
File
s
f
=
code inline {
writeString
::
!
String
!*
File
->
*
File
writeString
s
f
=
code inline {
.d
1
2
f
jsr
writeFS
.o
0
2
f
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment