WhatsMyLine

' Code to recognize file endings.  Windows/DOS, Linux/HTML/Mac OSX, or Max OS9 (and earlier)
' Written in Liberty BASIC by Harmon V.
' Released as public domain, Jan 2008
Function WhatsMyLineType$(path$, file$)
    ' open file, read entire file into a string variable, close file
    open path$+file$ for input as #fin
    text$ = input$(#fin, lof(#fin))
    close #fin
    if len(text$)>40000 then text$ = left$(text$, 40000) ' 40K ought to be enough.  :-)
 
    ' count number of CRs,LFs and pairs.
    winEnds = HowMany(text$, chr$(13)+chr$(10))
    nixEnds = HowMany(text$, chr$(10)) - winEnds
    macEnds = HowMany(text$, chr$(13)) - winEnds
 
    ' max number determines filetype
    winner = Max(winEnds,macEnds)
    winner = Max(winner, nixEnds)
    select case winner
        case winEnds : ender$ = chr$(13)+chr$(10) ' Windows or DOS
        case nixEnds : ender$ = chr$(10)  ' HTML, Linux or Mac OSX
        case macEnds : ender$ = chr$(13)  ' Mac OS 9 or earlier
        case else : ender$ = ""  ' random file or one-line ASCII file
    end select
    WhatsMyLineType$ = ender$
End Function  ' WhatsMyLineType
 
' count the number of times search$ occurs in source$
Function HowMany(source$, search$)
    n = 0 : pos = 0
    do
    pos = instr(source$, search$, pos+1)
    if pos>0 then n = n + 1
    loop until pos=0
    HowMany = n
End Function  ' HowMany