Very simple editor to show local file read/write

Created a nice software using SpiderBasic ? Post you link here !
hotcore
Posts: 14
Joined: Wed Jun 29, 2016 10:18 am

Very simple editor to show local file read/write

Post by hotcore »

Code: Select all

; ------------------------------------------------------------------------------------
;  Little file editor
; ------------------------------------------------------------------------------------

title.s = "Little File Editor"
XIncludeFile "..\includes\default.sb"
Global inFile.s

Procedure FileRead(Status, Filename$, File, Size)
  If Status = #PB_Status_Loaded
    buffer.s = ""
    While Not Eof(4)
      char.c = ReadCharacter(4)
      buffer = buffer + Chr(char)
    Wend
    CloseFile(4)
    SetGadgetText(1, buffer)
  ElseIf Status = #PB_Status_Error
    Debug "Error when loading the file: " + Filename$
  EndIf
EndProcedure

Procedure FileOpen()
  If NextSelectedFile()
    inFile = SelectedFileID()
    ReadFile(4, inFile, @FileRead(), #PB_LocalFile)
  EndIf
EndProcedure

Procedure FileOpenReq()
  OpenFileRequester("*.txt", @FileOpen())
EndProcedure

Procedure FileSave()
  buffer.s = GetGadgetText(1)
  If CreateFile(5, inFile, #PB_UTF8)                ; we re-create a new text file
    For i = 1 To Len(buffer)
      WriteCharacter(5, Asc(Mid(buffer,i,1)), #PB_UTF8)  ; write a char at a time
    Next
    ExportFile(5, "text/plain")
    CloseFile(5)                      
  EndIf


EndProcedure
  
; ------------------------------------------------------------------------------------
;  Main program
; ------------------------------------------------------------------------------------
OpenWindow(0, 0, 0, 520, 590, title)

EditorGadget(1, 10, 10, 500, 500)
ButtonGadget(2, 10, 520, 220, 30, "Open a text file ..")
ButtonGadget(3, 250, 520, 220, 30, "Save as text file ..")

BindGadgetEvent(2, @FileOpenReq())
BindGadgetEvent(3, @FileSave())
Last edited by hotcore on Sat Jul 02, 2016 1:43 pm, edited 1 time in total.
User avatar
Arbrakaan
Posts: 91
Joined: Mon Feb 24, 2014 10:54 pm
Location: Geneva
Contact:

Re: Very simple editor to show local file read/write

Post by Arbrakaan »

Works great, thanks for sharing !
hotcore
Posts: 14
Joined: Wed Jun 29, 2016 10:18 am

Re: Very simple editor to show local file read/write

Post by hotcore »

I did a small update in the code to remove extraoneous variuable.
Note that writing the file effectively is a download in the browser!
Post Reply