Page 1 of 1

Write EditorGadget contents to a text file

Posted: Fri Aug 04, 2006 11:31 pm
by Straker
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).

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

Posted: Sat Aug 05, 2006 12:42 am
by Hroudtwolf
This way is very slowly.
Better use this way (second post)....
http://www.purebasic.fr/english/viewtop ... ght=stream

Posted: Sat Aug 05, 2006 1:03 am
by Straker
Yes, but perhaps you missed my Linux reference in the above post. Besides, I was not trying to capture all of the RTF escape characters just the Text.

Thanks.

Posted: Thu Aug 10, 2006 6:23 pm
by acidburn
ummm another append

Code: Select all

;================
;AcidBurn 2006 PB v4
;================
Procedure AppendStr(FileN.s,Str.s) 
  FResult.l = OpenFile(#PB_Any, FileN.s)
  If FResult.l  
      length = Lof(fresult.l) 
      FileSeek(FResult.l,length)
      WriteString(FResult.l,Str.s)
      CloseFile(FResult.l)   
  EndIf  
EndProcedure

AppendStr("C:\Text.txt","testing 123456")

Posted: Fri Aug 11, 2006 1:17 pm
by Poltergeist
Why not do something like this. Reading every single line out of and editorgadget is quite slow...

pb4.0 code (not tested, btw)

Code: Select all

Procedure.l WriteEditorToFile(pEditorGadget.l, 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.

  if CountGadgetItems(pEditorGadget.l)=0
    procedurereturn 0
  endif

  ; find end of file
  if pAppend.b
    if Openfile(0,pFileName.s)=0
       procedurereturn -1
    else
       fileseek(0,lof(0))
       writestringn(0,"") ; append extra CR+LF to existing file
    endif
  else
    if createfile(0,pFileName.s)=0
      procedurereturn -1
    endif
  endif

  writestring(0,GetGadgetText(pEditorGadget.l))
  closefile(0)

  Procedurereturn CountGadgetItems(pEditorGadget.l) 
end procedure