Embedding Text-Files into executable

Share your advanced PureBasic knowledge/code with the community.
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Embedding Text-Files into executable

Post by jamirokwai »

Dear board,

today I searched for a solution to embed Text-Files into the executable.
My search was not successfull.

Background is, I want my program to only have 1 file with everything compiled into.
I think, it is usefull for text-based-help-files and such.

And yes, I will have to re-compile the program, everytime I change the text.

Here is my approach:

Code: Select all

DataSection
  helpstring : IncludeBinary "help.txt" + Chr(0)
EndDataSection 

EditorGadget(0,0,0,100,250,#PB_Editor_ReadOnly)
mystring.s = PeekS(? helpstring)
SetGadgetText(0, mystring.s)
Sorry for double-posts, if any.

Works on PureBasic Mac 4.20 and Mac 4.30a1
Regards,
JamiroKwai
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

JamiroKwai... In the windows version of the PB IDE there is a RESOURCES tab on the compiler options window. I have to assume the same thing exists in the MAC since programming a MAC is where I learned about resources in the first place...

I don't know how to mine such resources from inside PB, but it seems to me that this is where you could include the teext file (or an RTF which would hold more formatting annd maybe improve the look of your help file!) :D

I have never used it yet.

Ahhh I found this in the documentation... it seems (for some reason) only in the windows version:
Here you can include as many Resource scripts (*.rc files) as you want. They will be compiled and included with the executable. You can use any resource editor (for example the PellesC IDE) to create such scripts.

Note: Since Resources are a specific to the Windows platform only, PB does not include a Library to manage them and they are not further documented here. See documentation on the Windows API and resources for more information.
That is strange since I learned about resources while happily chipping away in MAC using Chipmunk BASIC!

8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

What's wrong with the solution you found? <puzzled look>

Here's a function that I use myself to quickly read multiple strings from memory.

Code: Select all

Procedure.s x_peeks(addr.l,length.l=-1,flags.l=-1,terminator.s="")   ; read string from mem until a null is found or max length is reached
  Protected string.s, p.l
  Global x_retval.l, x_peeks_read.l
  ;
  ; *** read a string from memory until terminating condition is met
  ;
  ; in:     addr.l             - location in memory
  ;         terminator.s       - seperator, if you're looking for zeroes use regular peeks()
  ;         [ length.l = n ]   - max length in BYTES
  ;                    = -1    - default: ignore
  ;         [ flags.l  = n ]   - #PB_Ascii, #PB_Unicode, #PB_UTF8
  ;                    = -1    - default: autodetect unicode mode
  ; retval: .s                 - string found
  ; out:    x_retval.l         - length of string as found in memory in BYTES (only for #PB_Ascii and #PB_Unicode)
  ;         x_peeks_read.l     - as x_retval.l
  ;
  ; notes:
  ;
  ; - terminating condition can be string, a null character, or hitting maximal length
  ; - purebasic's peeks() uses chars not bytes! (in 4.02 the included helpfile is wrong)
  ; - x_peeks() uses bytes not chars!
  ; - a terminating zero in unicode mode is actually TWO zeroes, ie. $ 00 00!
  ;
  If flags = -1
    CompilerIf #PB_Compiler_Unicode
      flags = #PB_Unicode
    CompilerElse
      flags = #PB_Ascii
    CompilerEndIf
  EndIf
  ;
  If length > 0
    If flags = #PB_Unicode
      string = PeekS(addr,length/2,flags)
    Else
      string = PeekS(addr,length,flags)
    EndIf
  Else
    string = PeekS(addr,-1,flags)
  EndIf
  ;
  x_retval = StringByteLength(string,flags)
  If x_retval < length Or length = -1
    If flags = #PB_Unicode
      x_retval = x_retval+2
    Else
      x_retval = x_retval+1
    EndIf
  EndIf
  ;
  If terminator > ""
    p = FindString(string,terminator,1)
    If p > 0
      string = Left(string,p-1)
      x_retval = StringByteLength(string+terminator,flags)
    EndIf
  EndIf
  ;
  x_peeks_read = x_retval
  ProcedureReturn string
EndProcedure
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Embedding Text-Files into executable...

Post by jamirokwai »

Hopefully, Resource-files will come along with the MacOS 4.30 Intel-version.

I found the above code when trying to include some binary (png-images)
and some text-files into the binary. It works perfectly for what I needed it ;)
It will help reducing clutter on the harddisk, and also reduce the possibility
of theft. (if you need to protect your images and such...)

Really neat!

I am deeply impressed by the possibilities of PB, and it's only the beginning
of my investigations.
Regards,
JamiroKwai
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

What's wrong with the solution you found? <puzzled look>
It only exists on the Windows PB at the moment I thought! :?
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply