Error loading @FILE preprocessor

Discuss any general programming issues here
Post Reply
Figgy78
Posts: 30
Joined: Wed Oct 11, 2017 9:13 am

Error loading @FILE preprocessor

Post by Figgy78 » Wed Nov 29, 2017 12:23 pm

Code: Select all

@FILE 1, "myFile.xml"
...
moai.CreateApp(FileToString(1))
This code does not seem to work for me. Any idea why?
I load and utilize files with ease using ie. @BRUSH, but with this I get:

Code: Select all

Error in line 178 (myApp.hws): Cannot read file 1 !
The filename is correct and it is located next to the .hws file.

Tipsi
Posts: 10
Joined: Sun Feb 14, 2010 6:06 pm

Re: Error loading @FILE preprocessor

Post by Tipsi » Wed Nov 29, 2017 1:51 pm

The command FileToString() works without id. The Synopsis is:

s$, len = FileToString(file$)

Write this without @FILE:

Code: Select all

moai.CreateApp(FileToString("myFile.xml"))
Griessli
Tipsi

Figgy78
Posts: 30
Joined: Wed Oct 11, 2017 9:13 am

Re: Error loading @FILE preprocessor

Post by Figgy78 » Wed Nov 29, 2017 4:11 pm

Tipsi wrote:The command FileToString() works without id.
Yes, I know it works without using the id.

My reason for using the pre processor command is to include the xml-file in the .exe file when compiling it.

SamuraiCrow
Posts: 180
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Error loading @FILE preprocessor

Post by SamuraiCrow » Wed Nov 29, 2017 6:20 pm

Try reading this post.

Figgy78
Posts: 30
Joined: Wed Oct 11, 2017 9:13 am

Re: Error loading @FILE preprocessor

Post by Figgy78 » Thu Nov 30, 2017 9:11 am

SamuraiCrow wrote:Try reading this post.
Thank you, SamuraiCrow. This is probably the right track.

I changed my code to read:

Code: Select all

;moai.CreateApp(FileToString("myFile.xml"))
@FILE 1, "myFile.xml"
moai.CreateApp(ReadString(1, FileLength(1)))
This does however give me the following error:

Code: Select all

Error in line 186 (myFile.hws): Could not read all characters from file!
Check if it is read protected!

User avatar
airsoftsoftwair
Posts: 2668
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Error loading @FILE preprocessor

Post by airsoftsoftwair » Thu Nov 30, 2017 10:17 pm

ReadString() operates in characters, FileLength() in bytes. If your file is not in UTF-8, you first need to read the raw bytes and then convert them to UTF-8 using ConvertStr(), e.g. something like this:

Code: Select all

moai.CreateApp(ConvertStr(ReadBytes(1), #ENCODING_ISO8859_1, #ENCODING_UTF8))

Figgy78
Posts: 30
Joined: Wed Oct 11, 2017 9:13 am

Re: Error loading @FILE preprocessor

Post by Figgy78 » Fri Dec 01, 2017 2:43 pm

airsoftsoftwair wrote:ReadString() operates in characters, FileLength() in bytes. If your file is not in UTF-8, you first need to read the raw bytes and then convert them to UTF-8 using ConvertStr(), e.g. something like this:

Code: Select all

moai.CreateApp(ConvertStr(ReadBytes(1), #ENCODING_ISO8859_1, #ENCODING_UTF8))
Thank you! Will try that.

Figgy78
Posts: 30
Joined: Wed Oct 11, 2017 9:13 am

Re: Error loading @FILE preprocessor

Post by Figgy78 » Mon Dec 04, 2017 9:52 am

Just a followup.

Initially I couldn't get the conversion of the xml-file to UTF-8 to work, but then I discovered that the file has a few cryptic characters at the beginning. I understand this has to do with identifying the Encoding?

Anyway. I solved this by stripping out the first three characters of the resulting string. Like this:

Code: Select all

moai.CreateApp(UnRightStr(ConvertStr(ReadBytes(1), #ENCODING_ISO8859_1, #ENCODING_UTF8), 3))

User avatar
airsoftsoftwair
Posts: 2668
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Error loading @FILE preprocessor

Post by airsoftsoftwair » Thu Dec 07, 2017 5:52 pm

Figgy78 wrote:Initially I couldn't get the conversion of the xml-file to UTF-8 to work, but then I discovered that the file has a few cryptic characters at the beginning. I understand this has to do with identifying the Encoding?
Yes, this is a 3-byte sequence known as the UTF-8 BOM. It is not necessary but can be there. Often text editors make it optional as to whether a BOM is generated or not.
Anyway. I solved this by stripping out the first three characters of the resulting string.
Actually, if your text file is already UTF-8, you don't have to do the conversion at all. Just skip the first three bytes and then use ReadString(), like this:

Code: Select all

Seek(1, 3)
moai.CreateApp(ReadString(1))
I don't like the necessity of Seek() here because I think ReadString() and ReadChr() should transparently skip the UTF-8 BOM without bothering the user with it. That's why I have now introduced the following change:

Code: Select all

- Change: ReadChr() and ReadString() automatically skip the UTF-8 BOM now whenever they operate
  in Unicode mode
This means that starting with the next update of Hollywood the Seek() won't be necessary and you can just use ReadString() and that's it :)

Figgy78
Posts: 30
Joined: Wed Oct 11, 2017 9:13 am

Re: Error loading @FILE preprocessor

Post by Figgy78 » Fri Dec 08, 2017 5:37 pm

That's great news.. Changing the world one step at a time ;)

Post Reply