Page 1 of 1

Fast Load & Save of Large Plain Text File

Posted: Sun Dec 20, 2009 11:21 pm
by Randy Walker
Code snippet below says all that needs to be said. Credit others where credit is due.
I'm not looking for any credit. I'm also not up on all the revision improvements since
4.0 so this may not even be necessary to handle large text files, but works for me 8)

Code: Select all

; Foundation for Plain Text Editor - Primary credits to reference link below
; Able to load and save plain text files exceeding native 64k byte limit
; reference> http://forums.purebasic.com/english/viewtopic.php?p=65739#65739

; ADJUST SOURCE AND DESTINATION FILES HERE BEFORE YOU EXECUTE
MyInputFile$ = "C:\apath\intest.txt"
MyOutputFile$ = "C:\apath\outtest.txt"

Enumeration
  #Window_5
  #HelpText_5
EndEnumeration

fontVerd7.l = LoadFont(#PB_Default,"Verdana",7)
fontVerd8.l = LoadFont(#PB_Default,"Verdana",8)
fontVerd9.l = LoadFont(#PB_Default,"Verdana",9)
fontVerd9I.l = LoadFont(#PB_Default,"Verdana",9,#PB_Font_Italic)
fontVerd10.l = LoadFont(#PB_Default,"Verdana",10)
fontVerd11.l = LoadFont(#PB_Default,"Verdana",11)
fontAri8.l = LoadFont(#PB_Default,"Arial",8) 
fontAri8B.l = LoadFont(#PB_Default,"Arial",8,#PB_Font_Bold)
fontAri9.l = LoadFont(#PB_Default,"Arial",9)
fontAri9B.l = LoadFont(#PB_Default,"Arial",9,#PB_Font_Bold)
fontAri10.l = LoadFont(#PB_Default,"Arial",10)
fontAri10B.l = LoadFont(#PB_Default,"Arial",10,#PB_Font_Bold)
fontAri11.l = LoadFont(#PB_Default,"Arial",11)
fontAri11B.l = LoadFont(#PB_Default,"Arial",11,#PB_Font_Bold)
fontAri12B.l = LoadFont(#PB_Default,"Arial",12,#PB_Font_Bold)
fontCur8.l = LoadFont(#PB_Default,"Courier",8)
fontCur10B.l = LoadFont(#PB_Default,"Courier",10,#PB_Font_Bold)
fontCur11.l = LoadFont(#PB_Default,"Courier",11)
fontCur11B.l = LoadFont(#PB_Default,"Courier",11,#PB_Font_Bold)
fontCur12.l = LoadFont(#PB_Default,"Courier",12)
fontCur12B.l = LoadFont(#PB_Default,"Courier",12, #PB_Font_Bold)
fontSan10.l = LoadFont(#PB_Default,"MS Sans Serif",10)
fontSrpt18.l = LoadFont(#PB_Default,"Script",18)

Procedure.l PutText(FileOut$,sizeOut.l,outHndl.l)
  *MemoryID = AllocateMemory(sizeOut+1) ; allocating a memory block + terminating 0 byte
  Count.l=0
  If *MemoryID ; << Else no need to FreeMemory() later.
    Count.l=sizeOut
    Result = SendMessage_(outHndl,#WM_GETTEXT,sizeOut+1,*MemoryID)
    If CreateFile(0, FileOut$)        ; we create a new text file...
      WriteData(0, *MemoryID,sizeOut) ; write the entire memory block into the file
      CloseFile(0)                    ; close file to secure written data
    Else
      Debug "Can't create the file!"
    EndIf
    FreeMemory(*MemoryID)
  EndIf
  ProcedureReturn Count
EndProcedure

Procedure.l GetText(FileIn.s,sizeIn.l,inHndl.l)
  *MemoryID = AllocateMemory(sizeIn)
  Count.l=0
  If *MemoryID ; << Else no need to FreeMemory() later.
    Result = SendMessage_(inHndl,#WM_GETTEXT,sizeOut+1,*MemoryID)
    ; Using CreateFile_ API because it allows SHARE_READ attribute.
    Fhandle=CreateFile_(@FileIn,#GENERIC_READ,#FILE_SHARE_READ,#Null,#OPEN_EXISTING,#FILE_ATTRIBUTE_NORMAL,#Null )
    Result.l = ReadFile_(Fhandle,*MemoryID,sizeIn,@Count,0)
    Result = SendMessage_(inHndl,#WM_SETTEXT,#Null,*MemoryID)
    FreeMemory(*MemoryID)
    CloseHandle_(Fhandle)
  EndIf
  ProcedureReturn Count ;captured during ReadFile_ API call
EndProcedure

; CREATE WINDOW AND ILLUSTRATE SAMPLE CODE
HWND5 = OpenWindow(#Window_5,10,10,640,480,"EditorGadget",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
If HWND5
  SetGadgetFont(#PB_Default,FontID(fontSan10.l))
  HelpText5 = EditorGadget (#HelpText_5,3,10,635,460)
  SendMessage_(HelpText5, #EM_SETTARGETDEVICE, #Null, 0) ; 0=wrap , 1up=linewidth , $FFFFFF(effectively)=wrapoff
  SetGadgetColor(#HelpText_5, #PB_Gadget_BackColor, $9B9BFF)
  MessageRequester("Ready.", "Set...")
  
  ; Read text file into editor gadget
  MyTime.l = GetTickCount_()
  fileBytes = FileSize(MyInputFile$)
  If fileBytes > 0
    GetText(MyInputFile$,fileBytes,HelpText5)
    MyTime = GetTickCount_() - MyTime
    MessageRequester("Read done.", Str(fileBytes) + " bytes Loaded in " + StrD(MyTime / 1000) + " Seconds.")
    
  Else
    MessageRequester("Say what?", "Nothing to do in 0 seconds flat.")
    End
  EndIf
  
  ; Write gadget text out to file
  MyTime.l = GetTickCount_()
  ttlChr = SendMessage_(HelpText5, #WM_GETTEXTLENGTH, 0, 0)
  If ttlChr > 0
    PutText(MyOutputFile$,ttlChr.l,HelpText5)
    MyTime = GetTickCount_() - MyTime
    MessageRequester("Write done", "Total text written was " + Str(ttlChr) + " bytes in " + StrD(MyTime / 1000) + " Seconds.")
    
  Else
    MessageRequester("Say what?", "Nothing to do in 0 seconds flat.")
  EndIf
  
  Repeat :  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Large Plain Text Editor - Fast Load & Save

Posted: Mon Dec 21, 2009 5:19 pm
by Rook Zimbabwe
in 4.4... it turns pink and flashes a message window about nothing to do. Also you have reused vairable names and it complains constantly! :)

Trying in 4.3

Nope same issues on lines 62 and 96... you already used this variable name. When I add 1 to them the program closes.


If I ; out the END on line 89 it looks like it runs but it is a simple edit box. No menus and no way to select fonts etc. I think it is looking to open those files in the front and this is also why it dies.

Good start though... add a few features like font selection and a FILE menu and you got a start of something! 8)

Re: Large Plain Text Editor - Fast Load & Save

Posted: Tue Dec 22, 2009 12:34 am
by Randy Walker
Rook Zimbabwe wrote:in 4.4... it turns pink and flashes a message window about nothing to do.
It sounds like you did not follow instructions on line 5 where it implies you must adjust lines 6 and 7. You must give a legitimate source path and filename on line 6 so it can actually populate the edit box. Do this and I think you will see more than a blank window. :wink:
Also you have reused vairable names and it complains constantly! :)

Trying in 4.3

Nope same issues on lines 62 and 96... you already used this variable name. When I add 1 to them the program closes.
I wrote the code in version 4.2 where there are no such complaints. Sorry I don't have time to stay current with each new release. You will just have to adjust. :shock: Also possible you copied the code before I noticed and fixed a couple typos. Try taking a new copy and don't forget line 6 :P
If I ; out the END on line 89 it looks like it runs but it is a simple edit box. No menus and no way to select fonts etc. I think it is looking to open those files in the front and this is also why it dies.
I did submit the sample code as a ''snippet'' -- not as a fully developed application. The only intention was to illustrate a method to load and save large amounts of text (>64KB) into and out of an edit box.
Good start though... add a few features like font selection and a FILE menu and you got a start of something! 8)
Please note on line 1 where it says ''foundation'' -- meaning no extras provided. It is for you to add menus and features to suit your interest. :idea: Again, the only purpose for the snippet is to help others looking for a way to manage the 64KB obstical they might encounter when trying to load or save large plain text files. :wink:

Re: Large Plain Text Editor - Fast Load & Save

Posted: Sun Dec 27, 2009 11:43 am
by SFSxOI
@Rook Zimbabwe

Corrections for PB 4.4;

In the procedure:

Code: Select all

GetText(FileIn.s,sizeIn.l,inHndl.l)

This line on line 62:
Result.l = ReadFile_(Fhandle,*MemoryID,sizeIn,@Count,0)

remove the type .l changing Result.l  to just Result making the line:

Result = ReadFile_(Fhandle,*MemoryID,sizeIn,@Count,0)

At the bottom of the code in this on line 96:

Code: Select all

PutText(MyOutputFile$,ttlChr.l,HelpText5)

remove the type from ttlChr.l making it ttlChr making the line:

PutText(MyOutputFile$,ttlChr,HelpText5)
and a depreciated item is used "And CreateGadgetList(WindowID(0))" so just comment out or remove that.

It works then, but adds a "›s¥ý" after the block of text it writes to the output file. The gadget background is pepto-bismol pink.

It gives the message box with the "nothing to do message" when the in and out files contents match already.

Where does the "Editor" part come in at? All this is doing for me is reading a file and writing the same contents back to another file. "The only intention was to illustrate a method to load and save large amounts of text (>64KB) into and out of an edit box" < it suceeds in doing that but there is no editing capability involved. Maybe a topic title of "Large Plain Text - Fast Load & Save" would be more descriptive?

Re: Large Plain Text Editor - Fast Load & Save

Posted: Sun Dec 27, 2009 8:52 pm
by Rook Zimbabwe
It does just look like a big edit box on a window...

Re: Large Plain Text Editor - Fast Load & Save

Posted: Mon Dec 28, 2009 11:56 pm
by Randy Walker
SFSxOI wrote:"The only intention was to illustrate a method to load and save large amounts of text (>64KB) into and out of an edit box" < it suceeds in doing that but there is no editing capability involved. Maybe a topic title of "Large Plain Text - Fast Load & Save" would be more descriptive?
Point taken so I changed the subject line to "Fast Load & Save of Large Plain Text File", although in my particular application I was specifically looking to load and save a large file to an Editor Gadget. That is where I was running into issues and the reasoning for my keyword selection when I created the subject line. I find it extremely useful myself when I am searching for a solution and find a subject line that actually summerizes the entry, rather than something ambiguous like: File Problem, or Can You Help Me?... Not very helpful to others trying to find solutions to a similar problem.

Reason for the pink background, my edit box is normally white background read-only for normal users and switches to full edit mode for admin content-updates, at which time the file is locked on the server and background turns dark pink as a conspicuous reminder to the administrator that the file is locked so they don't forget, walk off and leave all other administrators blocked. I wanted to go with blood red :twisted: , but finding a suitable contrasting font color that didn't give me eye strain was too bothersome so I lightened it up a bit :)

Re: Large Plain Text Editor - Fast Load & Save

Posted: Tue Dec 29, 2009 12:04 am
by Randy Walker
Rook Zimbabwe wrote:in 4.4... it turns pink and flashes a message window about nothing to do. Also you have reused vairable names and it complains constantly! :)

Trying in 4.3

Nope same issues on lines 62 and 96... you already used this variable name. When I add 1 to them the program closes
What can I say? Works fine for me. Maybe try upgrading your 4.4 and 4.3 to 4.0 :D

Re: Fast Load & Save of Large Plain Text File

Posted: Tue Dec 29, 2009 9:03 pm
by utopiomania
This text editor uses a blitz-krieg-ish fast load and save routine too.