[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Pipes
From: |
Sather User |
Subject: |
Pipes |
Date: |
Fri, 16 Dec 2011 18:48:41 +1030 (CST) |
Hello, all. In too much of a tizz to look in since my last super
roughie. Sorry about it. I quickly abandoned libtiff. I decided that
re-implementation in Sather directly was a better approach than
digesting the libtiff documentation. For example several TIFF tag
values are rationals and Sather has a rational type (RAT) but C does
not so libtiff does a workaround and you never seen the numerator and
denominator of an XResolution, for example, only a floating point value.
To hastily move on, here is an addition you can make to
Library/IO/file.sa. You can put it anywhere, but that's what I did.
I needed to look up the date from Sather, you see, to put the DateTime
tag into TIFF files, and I had to do that by running the shell
command, date, and reading the response.
This, I guess, is another roughie. It works, that's all I know.
-------------------------------------------------------------------
partial class FILE_PIPE is
-- Things common to both
readonly attr fp:EXT_OB; -- The FILE pointer readonly attr
get_char:CHAR pre ~void(fp) is
return RUNTIME::fgetc(fp);
end;
get_str:STR is
-- A string containing the characters up to the next newline.
return get_line.str;
end;
get_line:FSTR is
-- A string buffer containing the characters up to the next newline.
res::=#FSTR;
loop
c::=get_char;
if eof then return res end;
-- Next 2 lines reversed mtw Sep 2011
until!(c='\n');
res:=res.push(c);
end;
return res;
end;
get(s:STR):FLIST{STR} is
p::=open_for_read(s);
if p.error then raise "oops" end;
ans::=#FLIST{STR};
loop
t::=p.get_str;
while!(~p.eof);
ans:=ans.push(t)
end;
p.close;
return ans
end;
end;
class PIPE is
-- Not < $OSTREAM because no plus...
-- MTW Oct 2011
-- The stdin/stdout of FILE maybe should be here.
--
-- Example:
-- class MAIN is
-- main is
-- p::=PIPE::open_for_read("date +'%Y:%m:%d %H:%M:%S'");
-- s:STR:=p.get_str;
-- p.close;
-- #OUT+s+"\n"; -- prints formatted date
-- end
-- end
--
include FILE_PIPE;
open_for_read(command:STR):SAME is
-- Open pipe for reading.
r::=new;
r.fp:=RUNTIME::popen(command,"r");
return r
end;
close pre ~void(fp) is
-- Close the pipe corresponding to self.
RUNTIME::pclose(fp)
end;
error:BOOL is
-- true is an error has occurred with this file. Cleared by "clear".
return void(fp) or RUNTIME::ferror(fp)
end;
eof:BOOL pre ~void(fp) is
-- true if EOF has been encountered. Cleared by "clear".
return RUNTIME::feof(fp)
end;
end;
class FILE < $OSTREAM is ... etc
-------------------------------------------------------------
Library/System/runtime.sa:
external class RUNTIME is
...
popen(nm:STR,tp:STR):EXT_OB;
pclose(fp:EXT_OB);
...
---------------------------------------------------------------------
Hope I haven't missed anything there. You can see from the commented
example that it's easy as pie, just one of the loose ends left undone
in ICSI Sather.
Merry Christmas to all.
--Mike
--
Michael Talbot-Wilson