diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2013-09-24 16:39:32 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2013-09-24 16:39:32 +0300 |
commit | 7234d4a6c1ef8763ab6ac25619f8a225260d60b8 (patch) | |
tree | 86f67ae6ab3aef4e8837afb2acd3a9ab0fe99c84 /doc/gawk.texi | |
parent | 8aa14c5f3cf78f90b589785a9ffe5f7f02050b37 (diff) | |
parent | 08e8087fc3b1b9839e464ee436e8b24a45b024aa (diff) | |
download | egawk-7234d4a6c1ef8763ab6ac25619f8a225260d60b8.tar.gz egawk-7234d4a6c1ef8763ab6ac25619f8a225260d60b8.tar.bz2 egawk-7234d4a6c1ef8763ab6ac25619f8a225260d60b8.zip |
Merge branch 'gawk-4.1-stable'
Diffstat (limited to 'doc/gawk.texi')
-rw-r--r-- | doc/gawk.texi | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/doc/gawk.texi b/doc/gawk.texi index 2b67a6ca..1c642255 100644 --- a/doc/gawk.texi +++ b/doc/gawk.texi @@ -608,6 +608,8 @@ particular records in a file and perform operations upon them. * Join Function:: A function to join an array into a string. * Getlocaltime Function:: A function to get formatted times. +* Readfile Function:: A function to read an entire file at + once. * Data File Management:: Functions for managing command-line data files. * Filetrans Function:: A function for handling data file @@ -19082,6 +19084,7 @@ programming use. vice versa. * Join Function:: A function to join an array into a string. * Getlocaltime Function:: A function to get formatted times. +* Readfile Function:: A function to read an entire file at once. @end menu @node Strtonum Function @@ -19706,6 +19709,81 @@ A more general design for the @code{getlocaltime()} function would have allowed the user to supply an optional timestamp value to use instead of the current time. +@node Readfile Function +@subsection Reading A Whole File At Once + +Often, it is convenient to have the entire contents of a file available +in memory as a single string. A straightforward but naive way to +do that might be as follows: + +@example +function readfile(file, tmp, contents) +@{ + if ((getline tmp < file) < 0) + return + + contents = tmp + while (getline tmp < file) > 0) + contents = contents RT tmp + + close(file) + return contents +@} +@end example + +This function reads from @code{file} one record at a time, building +up the full contents of the file in the local variable @code{contents}. +It works, but is not necessarily efficient. + +The following function, based on a suggestion by Denis Shirokov, +reads the entire contents of the named file in one shot: + +@cindex @code{readfile()} user-defined function +@example +@c file eg/lib/readfile.awk +# readfile.awk --- read an entire file at once +@c endfile +@ignore +@c file eg/lib/readfile.awk +# +# Original idea by Denis Shirokov, cosmogen@@gmail.com, April 2013 +# +@c endfile +@end ignore +@c file eg/lib/readfile.awk + +function readfile(file, tmp, save_rs) +@{ + save_rs = RS + RS = "^$" + getline tmp < file + close(file) + RS = save_rs + + return tmp +@} +@c endfile +@end example + +It works by setting @code{RS} to @samp{^$}, a regular expression that +will never match if the file has contents. @command{gawk} reads data from +the file into @code{tmp} attempting to match @code{RS}. The match fails +after each read, but fails quickly, such that @command{gawk} fills +@code{tmp} with the entire contents of the file. +(@xref{Records}, for information on @code{RT} and @code{RS}.) + +In the case that @code{file} is empty, the return value is the null +string. Thus calling code may use something like: + +@example +contents = readfile("/some/path") +if (length(contents) == 0) + # file was empty @dots{} +@end example + +This tests the result to see if it is empty or not. An equivalent +test would be @samp{contents == ""}. + @node Data File Management @section Data File Management |