NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/util/epyt.m

Revision 495 (checked in by cbc, 12 years ago)

Initial import of Stark code.

Line 
1 function epyt(filename)
2 %EPYT   List M-file backwards.
3 %
4 %   EPYT foo.bar lists the ascii file called 'foo.bar'.
5 %   EPYT foo lists the ascii file called 'foo.m'.
6 %
7 %   If files called foo and foo.m both exist, then
8 %      EPYT foo lists the file 'foo', and
9 %      EPYT foo.m list the file 'foo.m'.
10 %
11 %   EPYT FILENAME lists the contents of the file given a full pathname
12 %   or a MATLABPATH relative partial pathname (see PARTIALPATH).
13 %
14 %   See also TYPE, WHICH.
15
16 %   Author:      Peter J. Acklam
17 %   Time-stamp:  2001-05-14 16:27:39 +0200
18 %   E-mail:      pjacklam@online.no
19 %   URL:         http://home.online.no/~pjacklam
20
21    % check number of input arguments
22    error(nargchk(1, 1, nargin));
23
24    % check file name
25    if isempty(filename) | ~ischar(filename)
26       error('Filename must be a non-empty string.');
27    end
28
29    % see if file exists
30    if ~exist(filename, 'file')
31       error([filename ': file does not exist.']);
32    end
33
34    % open file for reading
35    [fid, msg] = fopen(filename, 'rt');
36    if fid < 0
37       error([filename ': ' msg]);
38    end
39
40    % seek to end of file
41    fseek(fid, 0, 1);
42
43    cr = 13;             % carriage return
44    lf = 10;             % line feed
45
46    bufsize = 256;
47    buf = [];
48
49    % TYPE prints a newline before the first line, so we do that too
50    fprintf(1, '\n');
51
52    fpos = ftell(fid);
53    while fpos > 0
54
55       % compute position to start reading from
56       if fpos < bufsize
57          bufsize = fpos;
58          fpos = 0;
59       else
60          fpos = fpos - bufsize;
61       end
62
63       % seek to where we should start reading from
64       fseek(fid, fpos, -1);
65
66       % read a chunk of data
67       data = fread(fid, bufsize, 'uchar');
68
69       % insert data at beginning of buffer
70       buf = [data buf];
71
72       % Now convert all newline variants (CR+LF on DOS, CR on MAC, LF on
73       % UNIX) to LF.  This must be done in a way so that CR+LF never gets
74       % converted to LF+LF.  The boundary condition when a chunk of data
75       % begins with the LF in a CR+LF newline must be handled correctly.
76
77       % convert CR+LF to LF by removing all CR's that are followed by a LF
78       k = buf(1:end-1) == cr & buf(2:end) == lf;
79       buf(k) = [];
80
81       % convert CR to LF
82       k = buf == cr;
83       buf(k) = lf;
84
85       % length of buffer
86       buflen = length(buf);
87
88       % the only case when `buf' does not end in a line feed is when the
89       % file does not end in a newline
90       if buf(buflen) ~= lf
91          buf = [buf lf];
92       end
93
94       % find positions of line feeds in buffer
95       k = find(buf == lf);
96
97       if length(k) > 1
98
99          % print all lines after the first line feed char
100          for i = length(k)-1 : -1 : 1
101             lb = k(i) + 1;
102             ub = k(i+1) - 1;
103             if lb > ub
104                fprintf(1, '\n');
105             else
106                fprintf(1, '%s\n', buf(lb:ub));
107             end
108          end
109
110          % keep everything up until, and including, the first line feed char
111          buf = buf(1:k(1));
112
113       end
114
115    end
116
117    % print first line
118    if length(k)
119       fprintf(1, '%s\n', buf(1 : k(1)-1));
120    else
121       fprintf(1, '%s\n', buf);
122    end
123
124    % close the file
125    fclose(fid);
126
127    % TYPE prints a newline after the last line, so we do that too
128    fprintf(1, '\n');
Note: See TracBrowser for help on using the browser.