Page 1 of 1
Accessing a txt file resource
Posted: 2 Oct 2018 14:29
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?
Thanks in advance
dave
Re: Accessing a txt file resource
Posted: 2 Oct 2018 20:19
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).
Re: Accessing a txt file resource
Posted: 3 Oct 2018 14:33
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).
Re: Accessing a txt file resource
Posted: 3 Oct 2018 20:09
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
Re: Accessing a txt file resource
Posted: 3 Oct 2018 20:17
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
Re: Accessing a txt file resource
Posted: 3 Oct 2018 22:49
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
. Anyways, we decided that
#stringinclude would be a better feature
.
Re: Accessing a txt file resource
Posted: 6 Oct 2018 21:30
by daveplummermd
looking forward to it. Good addition.
Re: Accessing a txt file resource
Posted: 7 Oct 2018 3:17
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.