Guys
I have added a text file as a resource in a package.
How do I access the text contents of that file?
Thanks in advance
dave
-
- VIP Member
- Posts: 81
- Joined: 18 Jul 2006 17:18
Accessing a txt file resource
Dave Plummer
- Thomas Linder Puls
- VIP Member
- Posts: 1424
- Joined: 28 Feb 2000 0:01
Re: Accessing a txt file resource
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).
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
PDC
-
- VIP Member
- Posts: 331
- Joined: 14 Nov 2002 0:01
Re: Accessing a txt file resource
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
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
- Thomas Linder Puls
- VIP Member
- Posts: 1424
- Joined: 28 Feb 2000 0:01
Re: Accessing a txt file resource
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:
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
PDC
-
- VIP Member
- Posts: 331
- Joined: 14 Nov 2002 0:01
Re: Accessing a txt file resource
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
- Thomas Linder Puls
- VIP Member
- Posts: 1424
- Joined: 28 Feb 2000 0:01
Re: Accessing a txt file resource
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 . Anyways, we decided that #stringinclude would be a better feature .
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 . Anyways, we decided that #stringinclude would be a better feature .
Regards Thomas Linder Puls
PDC
PDC
-
- VIP Member
- Posts: 81
- Joined: 18 Jul 2006 17:18
-
- VIP Member
- Posts: 452
- Joined: 5 Nov 2000 0:01
Re: Accessing a txt file resource
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.