How to check if a .txt file uses line endings for Windows, Unix, or Mac?
Posted: Fri Feb 23, 2024 1:09 am
Heya,
Based on ChatGPT, I came up with some code to check if the line endings of a text file are Windows, Unix, or Mac.
Will this ReadByte command work with UTF-8 for letters/symbols that use two bytes?
Thanks!
Based on ChatGPT, I came up with some code to check if the line endings of a text file are Windows, Unix, or Mac.
Will this ReadByte command work with UTF-8 for letters/symbols that use two bytes?
Thanks!
Code: Select all
Procedure words_missing_in_master_wordlist_import_and_process_pre_count_words_in_file(file$)
; Refresh the gadgets since it froze their refreshment
GadgetsRefresh()
Debug "file$:"+file$
; Load the .txt file
ReadFile(1,file$)
ReadStringFormat(1)
location_of_file.q=Loc(1)
; Check the line ending, Windows, Unix, Mac
file_ending$=""
Repeat
t$=Chr(ReadByte(1))
If t$=#CR$
If Chr(ReadByte(1))=#LF$ : t$+#LF$ : EndIf
EndIf
If t$=#CRLF$
Debug "End of line: Windows (CR+LF)"
file_ending$=#CRLF$
ElseIf t$=#LF$
Debug "End of line: Unix (LF)"
file_ending$=#LF$
ElseIf t$=#CR$
Debug "End of line: Mac (CR)"
file_ending$=#CR$
EndIf
Until Eof(1) Or file_ending$<>""
FileSeek(1,location_of_file.q)
t$=ReadString(1,#PB_UTF8|#PB_File_IgnoreEOL)
CloseFile(1)
; Convert to Unix
; ConvertStringToUnix(t$)
; Count number of words
counter=CountString(t$,file_ending$)
If Right(t$,Len(file_ending$))<>file_ending$ : counter+1 : EndIf
Debug "counter:"+Str(counter)
; Return the number of words
ProcedureReturn counter
EndProcedure