Page 1 of 1

Error loading @FILE preprocessor

Posted: Wed Nov 29, 2017 12:23 pm
by Figgy78

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.

Re: Error loading @FILE preprocessor

Posted: Wed Nov 29, 2017 1:51 pm
by Tipsi
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

Re: Error loading @FILE preprocessor

Posted: Wed Nov 29, 2017 4:11 pm
by Figgy78
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.

Re: Error loading @FILE preprocessor

Posted: Wed Nov 29, 2017 6:20 pm
by SamuraiCrow
Try reading this post.

Re: Error loading @FILE preprocessor

Posted: Thu Nov 30, 2017 9:11 am
by Figgy78
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!

Re: Error loading @FILE preprocessor

Posted: Thu Nov 30, 2017 10:17 pm
by airsoftsoftwair
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))

Re: Error loading @FILE preprocessor

Posted: Fri Dec 01, 2017 2:43 pm
by Figgy78
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.

Re: Error loading @FILE preprocessor

Posted: Mon Dec 04, 2017 9:52 am
by Figgy78
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))

Re: Error loading @FILE preprocessor

Posted: Thu Dec 07, 2017 5:52 pm
by airsoftsoftwair
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 :)

Re: Error loading @FILE preprocessor

Posted: Fri Dec 08, 2017 5:37 pm
by Figgy78
That's great news.. Changing the world one step at a time ;)