Fast Load & Save of Large Plain Text File

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 1064
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Fast Load & Save of Large Plain Text File

Post 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
Last edited by Randy Walker on Mon Dec 28, 2009 11:25 pm, edited 1 time in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Large Plain Text Editor - Fast Load & Save

Post 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)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Randy Walker
Addict
Addict
Posts: 1064
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Large Plain Text Editor - Fast Load & Save

Post 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:
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Large Plain Text Editor - Fast Load & Save

Post 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?
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Large Plain Text Editor - Fast Load & Save

Post by Rook Zimbabwe »

It does just look like a big edit box on a window...
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Randy Walker
Addict
Addict
Posts: 1064
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Large Plain Text Editor - Fast Load & Save

Post 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 :)
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1064
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Large Plain Text Editor - Fast Load & Save

Post 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
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Re: Fast Load & Save of Large Plain Text File

Post by utopiomania »

This text editor uses a blitz-krieg-ish fast load and save routine too.
Post Reply