Embedded text file as a "Resource"

Just starting out? Need help? Post your questions and find answers here.
PB2004
User
User
Posts: 33
Joined: Sat Aug 11, 2007 6:16 pm
Location: Frisco, US

Embedded text file as a "Resource"

Post by PB2004 »

I'm a noob from a coddled DBPro background who would like to know if i can embed a simple text file in my PB executable as readable data instead of making 30,000 data statements or going to compiler resourcing (yet). This would be a large text file with chr(13) chr(10) lines of varying length that will be used to populate a list structure. I need to be able to read in each line so that I can subsequently parse, and populate the list.

I've looked at many relevant posts but the examples usually center on embedding images or reading a few strings. If I attempt the oft- suggested IncludeBinary, I am not presently able to read line by line. If I use IncludeFile, PB treats it as code and of course fails.

Here is some flawed example code. Any and all suggestions are appreciated, even if "just use compiler resourcing" or totally different approaches. I could pad the file strings to a standard width if it helps. The parsing will not be a problem, I just need to be able to extract Unicode text per line.

The text file I want to embed, "textfile.txt":

Code: Select all

hey
Pure Basic
is
amazing
and wonderful to know,
don't
you think?
And a test block:

Code: Select all

For Index = 0 To ?DataEnd - ?DataStart
  Debug Hex(PeekA(?DataStart + Index))
Next Index

DataSection
  DataStart:
  IncludeBinary "textfile.txt"
  DataEnd:
EndDataSection
Thanks for looking!
User avatar
Demivec
Addict
Addict
Posts: 4283
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Embedded text file as a "Resource"

Post by Demivec »

It depends on the encoding of your text data. Is it UTF-16 or UTF-8 (or ASCII)?

If it is UTF-16 you can try this:

Code: Select all

For Index = 0 To ?DataEnd - ?DataStart Step SizeOf(Character)
  Debug Hex(PeekC(?DataStart + Index))
Next Index

DataSection
  DataStart:
  IncludeBinary "textfile.txt"
  DataEnd:
EndDataSection
Another point is you should not have a BOM at the beginning of the text file, since you already know how it is encoded. If you still want to put one there then you will have to skip over it when you are parsing the data.
Last edited by Demivec on Thu Dec 01, 2016 3:48 am, edited 1 time in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5032
Joined: Sun Apr 12, 2009 6:27 am

Re: Embedded text file as a "Resource"

Post by RASHAD »

Hi
Edit 2:Modified
If the file is very large you can divide it to many segments

Code: Select all

Length = ?DataEnd-?DataStart
Text$ = PeekS(?DataStart,Length,#PB_UTF8)
nLines = CountString(Text$,Chr(10))+1
Dim _Lines.s(nLines)

For k = 1 To nLines
  _Lines(k) = StringField(Text$, k, Chr(10))
  Debug Str(k)
  Debug _Lines(k)
Next

DataSection
  DataStart:
  IncludeBinary "textfile.txt"
  DataEnd:
EndDataSection
Egypt my love
PB2004
User
User
Posts: 33
Joined: Sat Aug 11, 2007 6:16 pm
Location: Frisco, US

Re: Embedded text file as a "Resource"

Post by PB2004 »

Thanks Demivec. In spite of my readings, I'm easily confused on file formats but if I open in Notepad and do a Save As, "ANSI" is highlighted and the other encodings listed are Unicode, Unicode Big Endian, and UTF-8. If I open in Notepad++, UTF8 encoding is selected and there is no "convert to UTF-16" option so I'm not entirely sure. I created the raw data in Excel and pasted into Notepad++ to produce the original file. The two-character hex I get when I run your code is not useful and would require additional work to combine into strings to populate the list structure.

Rashad, your snippet works great for my purposes and I'll try it on the large file. I was surprised that only a chr(10) delimiter worked but fwiw I get the same result if I use a chr(13)+chr(10) delimiter.

Appreciate your inputs!!
Post Reply