Discussions related to Visual Prolog
daveplummermd
VIP Member
Posts: 80
Joined: 18 Jul 2006 17:18

Accessing a txt file resource

Unread post by daveplummermd »

Guys
I have added a text file as a resource in a package.
How do I access the text contents of that file?
Image

Thanks in advance

dave
Dave Plummer
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Re: Accessing a txt file resource

Unread post by Thomas Linder Puls »

Adding a text file (and many other kinds of files) does not have any influence on the resulting program (the files will not be embedded in the program in any form).

The only difference it makes is that the file is present in the project tree, and if you double click it there it will open in an editor.

This can be handy for many reasons, you could ofor example have a to-do list, or if you have some files that will be used at run time you can manage them in the IDE, …


By the way, the image you have added looks strange (I think it is because it is not publicly accessible).
Regards Thomas Linder Puls
PDC
Martin Meyer
VIP Member
Posts: 328
Joined: 14 Nov 2002 0:01

Re: Accessing a txt file resource

Unread post by Martin Meyer »

Maybe the #bininclude compiler directive could help you. By it your text file can be included as a binary. The binary can be converted then to a string.

To demonstrate it take for example main.pro as the text file. It is coded in UTF-8 with a leading byte order mark of 3 bytes. Create a new console application project and replace the code in main.pro by

Code: Select all

implement main   constants     text8 : string8 = uncheckedConvert(string8, #bininclude(@"main.pro")).   clauses     run() :-         Str8 = string8::fromPosition(text8, 3), %remove BOM         Str = string8::fromUtf8(Str8), %convert to UTF-16         stdIO::write(Str).   end implement main   goal     console::runUtf8(main::run).
Regards Martin
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Re: Accessing a txt file resource

Unread post by Thomas Linder Puls »

Actually, the next Visual Prolog version will have a #stringinclude directive similar to #bininclude but resulting in a string.

Using #bininclude you cannot be sure that text8 is null-terminated. Therefore this code is safer:

Code: Select all

implement main   constants     text8_bin : binary = #bininclude(@"main.pro").   clauses     run() :-         Len = binary::getSize(text8_bin),         Text8 = uncheckedConvert(string8, text8_bin),         Str8 = string8::fromPosition(Text8, 3), %remove BOM         Str = string8::fromUtf8(Str8, Len - 3), % convert to UTF-16         stdio::write(Str).   end implement main
Regards Thomas Linder Puls
PDC
Martin Meyer
VIP Member
Posts: 328
Joined: 14 Nov 2002 0:01

Re: Accessing a txt file resource

Unread post by Martin Meyer »

I supposed the constant in my example is null terminated, because the Language Reference says
When creating a binary constant the compiler adds the EOS symbol immediately after this constant, which makes safe the directive usages like this:

Code: Select all

constants     text : string = uncheckedConvert(string, #bininclude("text.txt")).     % Here text.txt is a text file, which is normally not text zero terminated
Regards Martin
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Re: Accessing a txt file resource

Unread post by Thomas Linder Puls »

OK, I had forgotten that we add such a null-character, so your version is fine.

Actually, we discussed this quite recently in the team and considered adding a null-char after a #bininclude, so we must all have forgotten that such one is already added :D. Anyways, we decided that #stringinclude would be a better feature :wink:.
Regards Thomas Linder Puls
PDC
daveplummermd
VIP Member
Posts: 80
Joined: 18 Jul 2006 17:18

Re: Accessing a txt file resource

Unread post by daveplummermd »

looking forward to it. Good addition.
Dave Plummer
Harrison Pratt
VIP Member
Posts: 439
Joined: 5 Nov 2000 0:01

Re: Accessing a txt file resource

Unread post by Harrison Pratt »

I use an interface (.i) file tor embed large strings in an application. The string constant syntax7z is globally visible throughout the application. A rudimentary example is below.

Code: Select all

interface aTest     open core constants     syntax7z : string = "Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...]   <Commands>   a : Add files to archive   b : Benchmark   d : Delete files from archive ... etc. ". end interface aTest

Code: Select all

%-- somewhere in the application stdio::write("\n", syntax7z ), ... etc.
Post Reply