Page 2 of 2

Posted: Tue Jul 27, 2004 1:39 am
by upnorth
All this stuff is a lot of work to load a 115k text file into an editor gadget IMHO.

This code will do it in a few seconds if that's fast enough for you.

As for the original question, StringGadgets are better suited for smaller amounts of text, say 32k or maybe even 64k on systems with a lot of RAM. The memory management of those gadgets is treated differently acccording to Microsoft and they should not be used for large amounts of data. EditorGadgets should be used for larger texts. In many cases you will have to set their maximum size limits as described in the posts above.

Hope this helps.

Code: Select all

Enumeration 
  #Window_0 
  #Editor_0 
EndEnumeration 

Procedure GetText(File.s)
  MyFile.l = ReadFile(#PB_Any, File)
  a.l = 0 
  Repeat
     MyData.s = ReadString() 
     AddGadgetItem(#Editor_0,a,MyData)
     a + 1
  Until Eof(MyFile) 
  CloseFile(MyFile) 
EndProcedure 

  FileName.s = "C:\temp\115k.txt" ; <---Your File Name Here
  If OpenWindow(#Window_0,10,10,640,480,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"EditorGadget") And CreateGadgetList(WindowID(0)) 
    EditorGadget (#Editor_0,10,10,620,460,#PB_Container_Raised)
    MyTime.l = GetTickCount_() 
    GetText(FileName)
    MyTime = GetTickCount_() - MyTime
    MessageRequester("Done.", "File Loaded in " + Str(MyTime / 1000) + " Seconds.") 
    Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow 
  EndIf 

Posted: Tue Jul 27, 2004 3:21 am
by fweil
upnorth,

I made a benchmark using an editor gadget, with my reference 'big text file' which makes exactly 4 670 689 bytes.

My computer is a 1,2GHz, 256MB ram running WIN2K.

The download time is about 1300 ms (GetText() time).

Rgrds

Posted: Tue Jul 27, 2004 5:16 am
by fsw
c++ has a nice iostream library... 8O

Posted: Tue Jul 27, 2004 10:57 am
by GeoTrail
upnorth, that took exactly 1 second on my machine.
AMD Athlon 2 Ghz Thoroughbred with 512 MB RAM.

Thanks alot, I think I'll go for this solution :) :D

Posted: Tue Jul 27, 2004 2:19 pm
by upnorth
Hey fweil & GeoTrail - My first example was meant to show that it could be done with native PB commands and without reallocating the PB String manipulation space. It would get pretty slow loading one line at a time if the text file gets much larger.

If speed is the most important factor, try this API example. 5214088 bytes loaded in 1016 Milliseconds on my PIII 450.

And hey fsw, thanks but no thanks, no C++ for me.

Careful, there's no error checking in this code. Have Fun! :D

Code: Select all

Enumeration 
  #Window_0 
  #Editor_0 
EndEnumeration 

#MaxFileSize = 8388608 ; 8 Megs

Procedure.l GetText(File.s)
  *MemoryID = AllocateMemory(#MaxFileSize)
  Fhandle=CreateFile_(@File,#GENERIC_READ,#FILE_SHARE_READ,#NULL,#OPEN_EXISTING,#FILE_ATTRIBUTE_NORMAL,#NULL ) 
  Count.l=0 
  Result.l = ReadFile_(FHandle,*MemoryID,#MaxFileSize,@Count,0) 
  Result = SendMessage_(GadgetID(#Editor_0),#WM_SETTEXT,#NULL,*MemoryID)  
  FreeMemory(*MemoryID) 
  CloseHandle_(Fhandle) 
  ProcedureReturn Count
EndProcedure 

FileName.s = "C:\temp\bible.txt" 
If OpenWindow(#Window_0,10,10,640,480,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"EditorGadget") And CreateGadgetList(WindowID(0)) 
  EditorGadget (#Editor_0,10,10,620,460,#PB_Container_Raised)
  MyTime.l = GetTickCount_() 
  BytesLoaded.l = GetText(FileName)
  MyTime = GetTickCount_() - MyTime
  MessageRequester("Done.", Str(BytesLoaded) + " Bytes Loaded in " + Str(MyTime) + " MilliSeconds.") 
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow 
EndIf 

Posted: Wed Jul 28, 2004 12:37 am
by GeoTrail
upnorth, that looks very interesting. I can't test it right now since I'm on a Linux machine and can't figure out how to install PB on this thing :lol:

Anyways, I'll check it out as soon as possible.