Write EditorGadget contents to a text file
Posted: Fri Aug 04, 2006 11:31 pm
Code updated for 5.20+
For anyone who needs it, this function writes the contents of an EditorGadget to a text file. Optionally, it can append to an existing file instead of overwriting.
The code is 3.94, and should work on Linux (but I haven't tested it).
For anyone who needs it, this function writes the contents of an EditorGadget to a text file. Optionally, it can append to an existing file instead of overwriting.
The code is 3.94, and should work on Linux (but I haven't tested it).
Code: Select all
Procedure.l WriteEditorToFile(pEditorGadget, pFileName.s, pAppend.b)
; Returns:
; 0 If the Editor contains no lines;
; the number of lines if written;
; -1 if cannot write file.
;
; Parameters:
; pEditorGadget (Long) = The editor gadget reference
; pFileName (String) = The file name to write
; pAppend (Byte) = 1 to append file, 0 to overwrite.
;
Protected.l lRetVal, lEditorLines, lListLines
Protected.i iFirstFile, iMyFile, i
Protected NewList MyLines.s()
If (FileSize(pFileName.s) > -1)
; file exists...
If (pAppend.b = 1)
iFirstFile = ReadFile(#PB_Any,pFileName.s)
If (iFirstFile)
While (Eof(iFirstFile)=0)
AddElement(MyLines.s())
MyLines.s() = ReadString(iFirstFile)
Wend
CloseFile(iFirstFile)
EndIf
EndIf
EndIf
lEditorLines.l = CountGadgetItems(pEditorGadget)
If (lEditorLines.l > 0)
For i = 1 To lEditorLines.l Step 1
AddElement(MyLines.s())
MyLines.s() = GetGadgetItemText(pEditorGadget,i - 1,0)
Next: CallDebugger
; create the file...
lListLines.l = ListSize(MyLines.s())
If (lListLines.l > 0)
iMyFile = CreateFile(#PB_Any,pFileName.s)
If (iMyFile)
ForEach MyLines.s()
WriteStringN(iMyFile, MyLines.s())
Next
CloseFile(iMyFile)
lRetVal.l = lListLines.l
Else
lRetVal.l = -1
EndIf
EndIf
EndIf
ProcedureReturn lRetVal.l
EndProcedure