Error loading @FILE preprocessor

Discuss any general programming issues here

Error loading @FILE preprocessor

Postby Figgy78 » Wed Nov 29, 2017 1: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.
Figgy78
 
Posts: 21
Joined: Wed Oct 11, 2017 9:13 am

Re: Error loading @FILE preprocessor

Postby Tipsi » Wed Nov 29, 2017 2: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
Tipsi
 
Posts: 6
Joined: Sun Feb 14, 2010 7:06 pm

Re: Error loading @FILE preprocessor

Postby Figgy78 » Wed Nov 29, 2017 5: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.
Figgy78
 
Posts: 21
Joined: Wed Oct 11, 2017 9:13 am

Re: Error loading @FILE preprocessor

Postby SamuraiCrow » Wed Nov 29, 2017 7:20 pm

Try reading this post.
SamuraiCrow
 
Posts: 101
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Error loading @FILE preprocessor

Postby Figgy78 » Thu Nov 30, 2017 10: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!
Figgy78
 
Posts: 21
Joined: Wed Oct 11, 2017 9:13 am

Re: Error loading @FILE preprocessor

Postby airsoftsoftwair » Thu Nov 30, 2017 11: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))
User avatar
airsoftsoftwair
 
Posts: 2233
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany

Re: Error loading @FILE preprocessor

Postby Figgy78 » Fri Dec 01, 2017 3: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: 21
Joined: Wed Oct 11, 2017 9:13 am

Re: Error loading @FILE preprocessor

Postby Figgy78 » Mon Dec 04, 2017 10: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))
Figgy78
 
Posts: 21
Joined: Wed Oct 11, 2017 9:13 am

Re: Error loading @FILE preprocessor

Postby airsoftsoftwair » Thu Dec 07, 2017 6: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 :)
User avatar
airsoftsoftwair
 
Posts: 2233
Joined: Fri Feb 12, 2010 3:33 pm
Location: Germany

Re: Error loading @FILE preprocessor

Postby Figgy78 » Fri Dec 08, 2017 6:37 pm

That's great news.. Changing the world one step at a time ;)
Figgy78
 
Posts: 21
Joined: Wed Oct 11, 2017 9:13 am


Return to General programming

Who is online

Users browsing this forum: No registered users and 3 guests