]> Creatis software - CreaPhase.git/blob - octave_packages/miscellaneous-1.1.0/slurp_file.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / miscellaneous-1.1.0 / slurp_file.m
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@isr.ist.utl.pt>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn{Function File} {@var{s} = } slurp_file ( f ) 
18 ## @cindex  
19 ## slurp_file return a whole text file @var{f} as a string @var{s}.
20 ##
21 ## @var{f} : string : filename
22 ## @var{s} : string : contents of the file
23 ##
24 ## If @var{f} is not an absolute filename, and 
25 ## is not an immediately accessible file, slurp_file () 
26 ## will look for @var{f} in the path.
27 ## @end deftypefn
28
29 function s = slurp_file (f)
30
31   if (nargin != 1)
32     print_usage;
33   elseif ! ischar (f)
34     error ("f  is not a string");
35   elseif isempty (f)
36     error ("f  is empty");
37   endif
38
39   s = "";
40
41   f0 = f;
42   [st,err,msg] = stat (f);
43   if err && f(1) != "/", 
44     f = file_in_loadpath (f);
45     if isempty (f)
46       ## Could not find it anywhere. Open will fail
47       f = f0;
48       error ("slurp_file : Can't find '%s' anywhere",f0);
49     end
50   end
51
52   ## I'll even get decent error messages!
53   [status, s] = system (sprintf ("cat '%s'",f), 1);
54 endfunction