Reading from a file?
Xavier Oswald
xoswald at gmail.com
Wed Jul 29 23:27:07 UTC 2009
On 18:58 Wed 29 Jul , Jeremy @ Cowgar wrote:
> Hello Lisaac,
>
> I see FILE_INPUT_STREAM, but I am not seeing how to create a
> STD_FILE to pass to FILE_INPUT_STREAM. Nor, in all of Lisaac do I
> find a use of FILE_INPUT_STREAM.
As you asked me how to open or write in a file, I put an example joined to this
mail.
Greetings,
--
,''`. ** Xavier Oswald <xoswald at debian.org>
: :' : ** Research Engineer
`. `' ** GNU/LINUX Debian Developer (http://debian.org)
`- ** Isaac Project Developer (http://isaacproject.u-strasbg.fr/)
-------------- next part --------------
////////////////////////////////////////////////////////////////////////////////
// //
// Lisaac BENCHMARK code //
// //
// LSIIT - ULP - CNRS - INRIA - FRANCE //
// CeCILL v2 licence (see `licence.txt') //
// //
// http://www.IsaacOS.com //
// //
////////////////////////////////////////////////////////////////////////////////
Section Header
+ name := LI_HORIZONTAL_TEST;
- bibliography:= "http://IsaacOS.com";
- author := "Xavier Oswald (x.oswald at free.fr)";
- comment := "Lisaac vertical inheritance test.";
Section Inherit
- parent_object:OBJECT := OBJECT;
Section Private
- header:STRING_CONSTANT := "header.txt";
- nb_inheritance_tmp:STRING_CONSTANT;
- nb_inheritance:INTEGER;
- usage:STRING_CONSTANT :=
"----------------------------------------------------------------\n\
\-- Lisaac source - Vertical inheritance test --\n\
\-- Xavier Oswald (x.oswald at free.fr) --\n\
\-- http://www.IsaacOS.com --\n\
\----------------------------------------------------------------\n\
\Usage: \n\
\ li_vert_test [Options] \n\
\ \n\
\Options: \n\
\ -n <INTEGER> : number of inheritance prototype wanted \n\
\ \n\
\Example: \n\
\ $ ./li_vert_test -n 100 \n\
\ \n\
\Bug report: x.oswald at free.fr \n\
\----------------------------------------------------------------\n";
- read_options <-
( + cmd:STRING_CONSTANT;
+ j:INTEGER;
(COMMAND_LINE.upper = 0 ).if {
display_usage;
};
nb_inheritance_tmp := NULL;
j := 1;
{ j > COMMAND_LINE.upper}.until_do{
cmd := COMMAND_LINE.item j;
(cmd.item 1 = '-').if {
((cmd.count = 2) && {cmd.item 2 = 'n'}).if {
j := j+1;
( COMMAND_LINE.count > j ).if {
nb_inheritance_tmp := COMMAND_LINE.item j;
(nb_inheritance_tmp.is_integer).if {
nb_inheritance := nb_inheritance_tmp.to_integer;
} else {
display_usage;
};
}else{
display_usage;
};
}else{
display_usage;
}
}else{
display_usage;
};
j := j+1;
};
(nb_inheritance_tmp = NULL).if{
display_usage;
die_with_code exit_failure_code;
};
);
- display_usage <-
(
usage.print;
die_with_code exit_failure_code;
);
- create_header :STRING <-
( + file:STD_FILE;
+ entry:ENTRY;
+ buf:STRING;
buf := STRING.create 256;
entry := FILE_SYSTEM.get header;
(entry = NULL).if {
STD_ERROR.put_string "Error: File ";
STD_ERROR.put_string header;
STD_ERROR.put_string " does not exist !\n";
die_with_code exit_failure_code;
};
file ?= entry.open;
file.read buf size (file.size);
file.close;
buf
);
// Create/Modify a file and write buf
- save_file output_file:STRING with buf:STRING <-
( + file:STD_FILE;
+ entry:ENTRY;
entry := FILE_SYSTEM.make_file output_file;
(entry = NULL).if {
STD_ERROR.put_string "Error: File ";
STD_ERROR.put_string output_file;
STD_ERROR.put_string " was not created !\n";
die_with_code exit_failure_code;
};
file ?= entry.open;
file.write buf from (buf.lower) size (buf.count);
file.close;
);
- create_protos <-
( + i:INTEGER;
+ proto_file:STRING;
+ buf:STRING;
+ buf_header:STRING;
+ proto_name:STRING;
+ parent_proto:STRING_CONSTANT;
parent_proto := "PROTO";
proto_file := STRING.create 10; // file.li
proto_name := STRING.create 10; // protoX
buf := STRING.create 10; // file content
buf_header := create_header;
// ========================================================================
// +===========================+
// |create first proto (parent)|
// +===========================+
proto_name.append "PROTO";
proto_file.append "proto.li";
buf.append buf_header;
// Section Header (Parent prototype)
buf.append
"Section Header\n\n";
buf.append " + name := ";
buf.append proto_name;
buf.append ";\n\n";
buf.append
" - bibliography:= \"http://IsaacOS.com\"; \n\
\ - author := \"Xavier Oswald (x.oswald at free.fr)\"; \n\
\ - comment := \"Horizontal inheritance BENCHMARK.\"; \n\n";
// Section Inherit (Parent prototype)
buf.append
"Section Inherit \n\n\
\ - parent:OBJECT := OBJECT; \n\n";
// Section Private
buf.append
"Section Private\n\n\
\ - storage:FAST_ARRAY[PROTO];\n\n";
// Section Public
buf.append
"Section Public \n\n\
\ - msg_print <- \n\
\ ( \n\
\ \"proto_parent\\n\".print; \n\
\ ); \n\n\
\ - main <- \n\
\ ( + i:INTEGER; \n\
\ + proto:PROTO; \n\
\ \n\
\ proto := PROTO.clone; \n\
\ i := ";
nb_inheritance.append_in buf;
buf.append ";\n\n";
buf.append
" storage := FAST_ARRAY[PROTO].create_with_capacity i;\n";
i := nb_inheritance;
{ i > 0 }.while_do {
buf.append " storage.add_last PROTO";
i.append_in buf;
buf.append ";\n";
i := i - 1;
};
buf.append
" i:=0; \n\
\ { i < 2000000 }.while_do { \n\
\ i:=i+1; \n\
\ proto := storage.item (storage.lower);\n\
\ proto.msg_print; \n\
\ }; \n\
\ );";
save_file proto_file with buf;
// ========================================================================
// +===========================+
// |create next protos |
// +===========================+
i := nb_inheritance;
{ i > 0 }.while_do {
proto_name.copy "PROTO";
i.append_in proto_name;
proto_file.copy "proto";
i.append_in proto_file;
proto_file.append ".li";
"Generating prototype :".print;
proto_file.print;
'\n'.print;
buf.copy buf_header;
// Section Header (Parent prototype)
buf.append
"Section Header\n\n";
buf.append " + name := ";
buf.append proto_name;
buf.append ";\n\n";
buf.append
" - bibliography:= \"http://IsaacOS.com\"; \n\
\ - author := \"Xavier Oswald (x.oswald at free.fr)\"; \n\
\ - comment := \"Horizontal inheritance BENCHMARK.\"; \n\n";
// Section Inherit (Parent prototype)
buf.append
"Section Inherit \n\n\
\ + parent: ";
buf.append parent_proto;
buf.append " := ";
buf.append parent_proto;
buf.append ";\n\n";
// Section Public (method redefinition)
buf.append
"Section Public \n\n\
\ - msg_print <- \n\
\ ( \n\
\ \"current proto: ";
buf.append proto_name;
buf.append
"\\n\".print; \n\
\ ); ";
save_file proto_file with buf;
i := i-1;
}
);
Section Public
- main <-
(
read_options;
create_protos;
);
More information about the Lisaac-devel
mailing list