Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Another implementation of the "readfile" idea in GAWK (using call_any)

33 views
Skip to first unread message

Kenny McCormack

unread,
Mar 27, 2015, 8:52:47 AM3/27/15
to
Here's a version I just threw together that uses the (as yet still
unpublished) "call_any" extension library.

Just though y'all might find it interesting...

--- Cut Here ---
# readfile1.awk
# This is the gist of it; TODO: add (better) error checking...
@load "call_any"
function readfile1(fn, fd,fs,buff) {
if ((fd = call_any("isi","open",fn,0)) == -1) return "### ERROR ###"
fs = call_any("iiii","lseek",fd,0,2)
# print "fn:",fn,"fd:",fd,"fs:",fs
buff = sprintf("%*s",fs,"")
call_any("iisii","pread",fd,buff,fs,0)
call_any("ii","close",fd)
return buff
}
--- Cut Here ---

Note also, a possible TODO is to add the following line right before the
final "return":

sub(/\n$/,"",buff)

The idea is that I almost always use readfile1() like this:

split(readfile1("someFile"),A,"\n")

which results in an extra empty field at the end. In fact, an idiom that I
now find myself using is:

delete A[split(readfile("someFile"),A,"\n")]

But, of course, having the function itself remove the extra newline is
better.

Anyway, enjoy!

--
They say compassion is a virtue, but I don't have the time!

- David Byrne -

Andrew Schorr

unread,
Mar 27, 2015, 4:12:42 PM3/27/15
to
On Friday, March 27, 2015 at 8:52:47 AM UTC-4, Kenny McCormack wrote:
> Here's a version I just threw together that uses the (as yet still
> unpublished) "call_any" extension library.
>
> Just though y'all might find it interesting...

This would be much more interesting if you were to publish the "call_any" extension. We welcome contributions to gawkextlib.

> Anyway, enjoy!

It is impossible to enjoy this code without access to the call_any extension. :-)

Regards,
Andy

Kenny McCormack

unread,
Mar 27, 2015, 4:31:23 PM3/27/15
to
In article <5e540377-c48f-49b2...@googlegroups.com>,
Yup. It is a teaser...

I guess I'm a little "camera shy" about releasing my code, but it (this
particular extension) has served me well for over a decade now.

--
The last time a Republican cared about you, you were a fetus.
0 new messages