Commit 36f9720c authored by Jurrien Stutterheim's avatar Jurrien Stutterheim

DOS -> Unix line endings

parent cfae7d9d
definition module Platform
PlatformDependant win mac :== win
IF_MACOSX macosx not_macosx :== macosx
DirSeparator:=='/'
DirSeparatorString:=="/"
application_path :: !{#Char} -> {#Char}
definition module Platform
PlatformDependant win mac :== win
IF_MACOSX macosx not_macosx :== macosx
DirSeparator:=='/'
DirSeparatorString:=="/"
application_path :: !{#Char} -> {#Char}
definition module UtilNewlinesFile
from StdClass import class ==, class toString
:: NewlineConvention
= NewlineConventionNone | NewlineConventionMac | NewlineConventionUnix | NewlineConventionDos
HostNativeNewlineConvention :== NewlineConventionUnix
instance == NewlineConvention
instance toString NewlineConvention
// read a line with any newline convention
// the file should have been opened with mode FReadData
// the line returned ends with one '\n' (except for the last line)
readAnyLine :: !*File -> (NewlineConvention, !.{#Char}, !*File)
// same as readAnyLine, but discards newline convention (compatible with freadline)
readLine file
:== (line, file`)
where
(_, line, file`)
= readAnyLine file
// write a line with a specified newline convention
// the first argument is the line
// (only the last character of the line is inspected for a newline character,
// their shouldn't be any newlines in the middle of the line)
// the second argument is the newline string
// the file should have been opened with mode FWriteData
writeAnyLine :: !{#Char} !{#Char} !*File -> *File
convertLine :: !*{#Char} -> (NewlineConvention, *{#Char})
readConvLines :: !*File -> (NewlineConvention,[String],*File)
definition module UtilNewlinesFile
from StdClass import class ==, class toString
:: NewlineConvention
= NewlineConventionNone | NewlineConventionMac | NewlineConventionUnix | NewlineConventionDos
HostNativeNewlineConvention :== NewlineConventionUnix
instance == NewlineConvention
instance toString NewlineConvention
// read a line with any newline convention
// the file should have been opened with mode FReadData
// the line returned ends with one '\n' (except for the last line)
readAnyLine :: !*File -> (NewlineConvention, !.{#Char}, !*File)
// same as readAnyLine, but discards newline convention (compatible with freadline)
readLine file
:== (line, file`)
where
(_, line, file`)
= readAnyLine file
// write a line with a specified newline convention
// the first argument is the line
// (only the last character of the line is inspected for a newline character,
// their shouldn't be any newlines in the middle of the line)
// the second argument is the newline string
// the file should have been opened with mode FWriteData
writeAnyLine :: !{#Char} !{#Char} !*File -> *File
convertLine :: !*{#Char} -> (NewlineConvention, *{#Char})
readConvLines :: !*File -> (NewlineConvention,[String],*File)
implementation module UtilNewlinesFile
import StdFile, StdInt, StdChar, StdString, StdArray, StdBool, StdClass, StdTuple, StdMisc
// simple sequence operator
(:-) infixl 0
(:-) f a
:== a f
:: NewlineConvention
= NewlineConventionNone
| NewlineConventionMac
| NewlineConventionUnix
| NewlineConventionDos
HostNativeNewlineConvention :== NewlineConventionUnix
instance == NewlineConvention where
(==) NewlineConventionNone NewlineConventionNone
= True
(==) NewlineConventionMac NewlineConventionMac
= True
(==) NewlineConventionUnix NewlineConventionUnix
= True
(==) NewlineConventionDos NewlineConventionDos
= True
(==) _ _
= False
instance toString NewlineConvention where
toString NewlineConventionNone
= ""
toString NewlineConventionMac
= "\xd"
toString NewlineConventionUnix
= "\xa"
toString NewlineConventionDos
= "\xd\xa"
// slice that returns a unique array
(%.) infixl 9 :: !.{#Char} !(!Int,!Int) -> .{#Char}
(%.) string indices
= code
{
.inline %.
.d 1 2 ii
jsr sliceAC
.o 1 0
.end
}
// this should be added to the Clean rts, so that the string doesn't have to be copied
downSize :: Int *{#Char} -> *{#Char}
downSize newSize string
= string %. (0, newSize-1)
convertLine :: !*{#Char} -> (NewlineConvention, *{#Char})
convertLine line
#! maxIndex
= size line - 1
| maxIndex >= 0
#! lastChar
= line.[maxIndex]
| lastChar == '\xa'
| maxIndex >= 1
#! lastButOneChar
= line.[maxIndex-1]
| lastButOneChar == '\xd'
= (NewlineConventionDos, {downSize maxIndex line & [maxIndex-1] = '\n'})
// otherwise
= (NewlineConventionUnix, {line & [maxIndex] = '\n'})
// otherwise
= (NewlineConventionUnix, {line & [maxIndex] = '\n'})
| lastChar == '\xd'
= (NewlineConventionMac, {line & [maxIndex] = '\n'})
// otherwise
= (NewlineConventionNone, line)
// otherwise
= (NewlineConventionNone, line)
readAnyLine :: !*File -> (NewlineConvention, !.{#Char}, !*File)
readAnyLine file
# (line, file)
= freadline file
(convention, line)
= convertLine line
= (convention, line, file)
readLine file
:== (line, file`)
where
(_, line, file`)
= readAnyLine file
writeAnyLine :: !{#Char} !{#Char} !*File -> *File
writeAnyLine line newlineString file
# maxIndex
= size line - 1
lastChar
= line.[maxIndex]
| maxIndex >= 0 && lastChar == '\n'
= file :- fwrites (line %. (0, maxIndex-1)) :- fwrites newlineString
// otherwise
= file :- fwrites line
//--
readConvLines :: !*File -> (NewlineConvention,[String],*File)
readConvLines file
# (conv,line,more,file) = readAnyLine` file
(eof, file) = fend file
| eof
| more // last line ends in a newline?
= (conv,[line,""], file)
= (conv,[line], file)
# (_,lines,file) = readConvLines file
= (conv,[line:lines], file)
//readAnyLine` :: !*File -> (NewlineConvention, !.{#Char}, !*File)
readAnyLine` file
# (line, file) = freadline file
#! maxIndex = size line - 1
| maxIndex >= 0
# lastChar = line.[maxIndex]
| lastChar == '\xa'
| maxIndex >= 1
# lastButOneChar = line.[maxIndex-1]
| lastButOneChar == '\xd'
= (NewlineConventionDos, downSize (dec maxIndex) line, True, file)
= (NewlineConventionUnix, downSize maxIndex line, True, file)
= (NewlineConventionUnix, downSize maxIndex line, True, file)
| lastChar == '\xd'
= (NewlineConventionMac, downSize maxIndex line, True, file)
= (NewlineConventionNone, line, False, file)
= (NewlineConventionNone, line, False, file)
implementation module UtilNewlinesFile
import StdFile, StdInt, StdChar, StdString, StdArray, StdBool, StdClass, StdTuple, StdMisc
// simple sequence operator
(:-) infixl 0
(:-) f a
:== a f
:: NewlineConvention
= NewlineConventionNone
| NewlineConventionMac
| NewlineConventionUnix
| NewlineConventionDos
HostNativeNewlineConvention :== NewlineConventionUnix
instance == NewlineConvention where
(==) NewlineConventionNone NewlineConventionNone
= True
(==) NewlineConventionMac NewlineConventionMac
= True
(==) NewlineConventionUnix NewlineConventionUnix
= True
(==) NewlineConventionDos NewlineConventionDos
= True
(==) _ _
= False
instance toString NewlineConvention where
toString NewlineConventionNone
= ""
toString NewlineConventionMac
= "\xd"
toString NewlineConventionUnix
= "\xa"
toString NewlineConventionDos
= "\xd\xa"
// slice that returns a unique array
(%.) infixl 9 :: !.{#Char} !(!Int,!Int) -> .{#Char}
(%.) string indices
= code
{
.inline %.
.d 1 2 ii
jsr sliceAC
.o 1 0
.end
}
// this should be added to the Clean rts, so that the string doesn't have to be copied
downSize :: Int *{#Char} -> *{#Char}
downSize newSize string
= string %. (0, newSize-1)
convertLine :: !*{#Char} -> (NewlineConvention, *{#Char})
convertLine line
#! maxIndex
= size line - 1
| maxIndex >= 0
#! lastChar
= line.[maxIndex]
| lastChar == '\xa'
| maxIndex >= 1
#! lastButOneChar
= line.[maxIndex-1]
| lastButOneChar == '\xd'
= (NewlineConventionDos, {downSize maxIndex line & [maxIndex-1] = '\n'})
// otherwise
= (NewlineConventionUnix, {line & [maxIndex] = '\n'})
// otherwise
= (NewlineConventionUnix, {line & [maxIndex] = '\n'})
| lastChar == '\xd'
= (NewlineConventionMac, {line & [maxIndex] = '\n'})
// otherwise
= (NewlineConventionNone, line)
// otherwise
= (NewlineConventionNone, line)
readAnyLine :: !*File -> (NewlineConvention, !.{#Char}, !*File)
readAnyLine file
# (line, file)
= freadline file
(convention, line)
= convertLine line
= (convention, line, file)
readLine file
:== (line, file`)
where
(_, line, file`)
= readAnyLine file
writeAnyLine :: !{#Char} !{#Char} !*File -> *File
writeAnyLine line newlineString file
# maxIndex
= size line - 1
lastChar
= line.[maxIndex]
| maxIndex >= 0 && lastChar == '\n'
= file :- fwrites (line %. (0, maxIndex-1)) :- fwrites newlineString
// otherwise
= file :- fwrites line
//--
readConvLines :: !*File -> (NewlineConvention,[String],*File)
readConvLines file
# (conv,line,more,file) = readAnyLine` file
(eof, file) = fend file
| eof
| more // last line ends in a newline?
= (conv,[line,""], file)
= (conv,[line], file)
# (_,lines,file) = readConvLines file
= (conv,[line:lines], file)
//readAnyLine` :: !*File -> (NewlineConvention, !.{#Char}, !*File)
readAnyLine` file
# (line, file) = freadline file
#! maxIndex = size line - 1
| maxIndex >= 0
# lastChar = line.[maxIndex]
| lastChar == '\xa'
| maxIndex >= 1
# lastButOneChar = line.[maxIndex-1]
| lastButOneChar == '\xd'
= (NewlineConventionDos, downSize (dec maxIndex) line, True, file)
= (NewlineConventionUnix, downSize maxIndex line, True, file)
= (NewlineConventionUnix, downSize maxIndex line, True, file)
| lastChar == '\xd'
= (NewlineConventionMac, downSize maxIndex line, True, file)
= (NewlineConventionNone, line, False, file)
= (NewlineConventionNone, line, False, file)
definition module Platform
PlatformDependant win mac :== win
IF_MACOSX macosx not_macosx :== not_macosx
DirSeparator:=='/'
DirSeparatorString:=="/"
application_path :: !{#Char} -> {#Char}
definition module Platform
PlatformDependant win mac :== win
IF_MACOSX macosx not_macosx :== not_macosx
DirSeparator:=='/'
DirSeparatorString:=="/"
application_path :: !{#Char} -> {#Char}
definition module UtilNewlinesFile
from StdClass import class ==, class toString
:: NewlineConvention
= NewlineConventionNone | NewlineConventionMac | NewlineConventionUnix | NewlineConventionDos
HostNativeNewlineConvention :== NewlineConventionUnix
instance == NewlineConvention
instance toString NewlineConvention
// read a line with any newline convention
// the file should have been opened with mode FReadData
// the line returned ends with one '\n' (except for the last line)
readAnyLine :: !*File -> (NewlineConvention, !.{#Char}, !*File)
// same as readAnyLine, but discards newline convention (compatible with freadline)
readLine file
:== (line, file`)
where
(_, line, file`)
= readAnyLine file
// write a line with a specified newline convention
// the first argument is the line
// (only the last character of the line is inspected for a newline character,
// their shouldn't be any newlines in the middle of the line)
// the second argument is the newline string
// the file should have been opened with mode FWriteData
writeAnyLine :: !{#Char} !{#Char} !*File -> *File
convertLine :: !*{#Char} -> (NewlineConvention, *{#Char})
readConvLines :: !*File -> (NewlineConvention,[String],*File)
definition module UtilNewlinesFile
from StdClass import class ==, class toString
:: NewlineConvention
= NewlineConventionNone | NewlineConventionMac | NewlineConventionUnix | NewlineConventionDos
HostNativeNewlineConvention :== NewlineConventionUnix
instance == NewlineConvention
instance toString NewlineConvention
// read a line with any newline convention
// the file should have been opened with mode FReadData
// the line returned ends with one '\n' (except for the last line)
readAnyLine :: !*File -> (NewlineConvention, !.{#Char}, !*File)
// same as readAnyLine, but discards newline convention (compatible with freadline)
readLine file
:== (line, file`)
where
(_, line, file`)
= readAnyLine file
// write a line with a specified newline convention
// the first argument is the line
// (only the last character of the line is inspected for a newline character,
// their shouldn't be any newlines in the middle of the line)
// the second argument is the newline string
// the file should have been opened with mode FWriteData
writeAnyLine :: !{#Char} !{#Char} !*File -> *File
convertLine :: !*{#Char} -> (NewlineConvention, *{#Char})
readConvLines :: !*File -> (NewlineConvention,[String],*File)
implementation module UtilNewlinesFile
import StdFile, StdInt, StdChar, StdString, StdArray, StdBool, StdClass, StdTuple, StdMisc
// simple sequence operator
(:-) infixl 0
(:-) f a
:== a f
:: NewlineConvention
= NewlineConventionNone
| NewlineConventionMac
| NewlineConventionUnix
| NewlineConventionDos
HostNativeNewlineConvention :== NewlineConventionUnix
instance == NewlineConvention where
(==) NewlineConventionNone NewlineConventionNone
= True
(==) NewlineConventionMac NewlineConventionMac
= True
(==) NewlineConventionUnix NewlineConventionUnix
= True
(==) NewlineConventionDos NewlineConventionDos
= True
(==) _ _
= False
instance toString NewlineConvention where
toString NewlineConventionNone
= ""
toString NewlineConventionMac
= "\xd"
toString NewlineConventionUnix
= "\xa"
toString NewlineConventionDos
= "\xd\xa"
// slice that returns a unique array
(%.) infixl 9 :: !.{#Char} !(!Int,!Int) -> .{#Char}
(%.) string indices
= code
{
.inline %.
.d 1 2 ii
jsr sliceAC
.o 1 0
.end
}
// this should be added to the Clean rts, so that the string doesn't have to be copied
downSize :: Int *{#Char} -> *{#Char}
downSize newSize string
= string %. (0, newSize-1)
convertLine :: !*{#Char} -> (NewlineConvention, *{#Char})
convertLine line
#! maxIndex
= size line - 1
| maxIndex >= 0
#! lastChar
= line.[maxIndex]
| lastChar == '\xa'
| maxIndex >= 1
#! lastButOneChar
= line.[maxIndex-1]
| lastButOneChar == '\xd'
= (NewlineConventionDos, {downSize maxIndex line & [maxIndex-1] = '\n'})
// otherwise
= (NewlineConventionUnix, {line & [maxIndex] = '\n'})
// otherwise
= (NewlineConventionUnix, {line & [maxIndex] = '\n'})
| lastChar == '\xd'
= (NewlineConventionMac, {line & [maxIndex] = '\n'})
// otherwise
= (NewlineConventionNone, line)
// otherwise
= (NewlineConventionNone, line)
readAnyLine :: !*File -> (NewlineConvention, !.{#Char}, !*File)
readAnyLine file
# (line, file)
= freadline file
(convention, line)
= convertLine line
= (convention, line, file)
readLine file
:== (line, file`)
where
(_, line, file`)
= readAnyLine file
writeAnyLine :: !{#Char} !{#Char} !*File -> *File
writeAnyLine line newlineString file
# maxIndex
= size line - 1
lastChar
= line.[maxIndex]
| maxIndex >= 0 && lastChar == '\n'
= file :- fwrites (line %. (0, maxIndex-1)) :- fwrites newlineString
// otherwise
= file :- fwrites line
//--
readConvLines :: !*File -> (NewlineConvention,[String],*File)
readConvLines file
# (conv,line,more,file) = readAnyLine` file
(eof, file) = fend file
| eof
| more // last line ends in a newline?
= (conv,[line,""], file)
= (conv,[line], file)
# (_,lines,file) = readConvLines file
= (conv,[line:lines], file)
//readAnyLine` :: !*File -> (NewlineConvention, !.{#Char}, !*File)
readAnyLine` file
# (line, file) = freadline file
#! maxIndex = size line - 1
| maxIndex >= 0
# lastChar = line.[maxIndex]
| lastChar == '\xa'
| maxIndex >= 1
# lastButOneChar = line.[maxIndex-1]
| lastButOneChar == '\xd'
= (NewlineConventionDos, downSize (dec maxIndex) line, True, file)
= (NewlineConventionUnix, downSize maxIndex line, True, file)
= (NewlineConventionUnix, downSize maxIndex line, True, file)
| lastChar == '\xd'
= (NewlineConventionMac, downSize maxIndex line, True, file)
= (NewlineConventionNone, line, False, file)
= (NewlineConventionNone, line, False, file)
implementation module UtilNewlinesFile
import StdFile, StdInt, StdChar, StdString, StdArray, StdBool, StdClass, StdTuple, StdMisc
// simple sequence operator
(:-) infixl 0
(:-) f a
:== a f
:: NewlineConvention
= NewlineConventionNone
| NewlineConventionMac
| NewlineConventionUnix
| NewlineConventionDos
HostNativeNewlineConvention :== NewlineConventionUnix
instance == NewlineConvention where
(==) NewlineConventionNone NewlineConventionNone
= True
(==) NewlineConventionMac NewlineConventionMac
= True
(==) NewlineConventionUnix NewlineConventionUnix
= True
(==) NewlineConventionDos NewlineConventionDos
= True
(==) _ _
= False
instance toString NewlineConvention where
toString NewlineConventionNone
= ""
toString NewlineConventionMac
= "\xd"
toString NewlineConventionUnix
= "\xa"
toString NewlineConventionDos
= "\xd\xa"
// slice that returns a unique array
(%.) infixl 9 :: !.{#Char} !(!Int,!Int) -> .{#Char}
(%.) string indices
= code
{
.inline %.
.d 1 2 ii
jsr sliceAC
.o 1 0
.end
}
// this should be added to the Clean rts, so that the string doesn't have to be copied
downSize :: Int *{#Char} -> *{#Char}
downSize newSize string
= string %. (0, newSize-1)
convertLine :: !*{#Char} -> (NewlineConvention, *{#Char})
convertLine line
#! maxIndex
= size line - 1
| maxIndex >= 0
#! lastChar
= line.[maxIndex]
| lastChar == '\xa'
| maxIndex >= 1
#! lastButOneChar
= line.[maxIndex-1]
| lastButOneChar == '\xd'
= (NewlineConventionDos, {downSize maxIndex line & [maxIndex-1] = '\n'})
// otherwise
= (NewlineConventionUnix, {line & [maxIndex] = '\n'})
// otherwise
= (NewlineConventionUnix, {line & [maxIndex] = '\n'})
| lastChar == '\xd'
= (NewlineConventionMac, {line & [maxIndex] = '\n'})
// otherwise
= (NewlineConventionNone, line)
// otherwise
= (NewlineConventionNone, line)
readAnyLine :: !*File -> (NewlineConvention, !.{#Char}, !*File)
readAnyLine file
# (line, file)
= freadline file
(convention, line)
= convertLine line
= (convention, line, file)
readLine file
:== (line, file`)
where
(_, line, file`)
= readAnyLine file