Home > wafo > misc > geth1line.m

geth1line

PURPOSE ^

Extracts the first comment line (the H1 line) of a m-file

SYNOPSIS ^

H1 = geth1line(filename,format,N)

DESCRIPTION ^

 GETH1LINE Extracts the first comment line (the H1 line) of a m-file 
   
    CALL:  H1 = geth1line(filename,format,N); 
   
       H1       = string containing the H1 line of the file 
       filename = string containing the name of the file 
       format   = 0 No formatting of H1 (default) 
                  1 Format H1  
       N        = Number of characters for filename (default 14) 
   
   GETH1LINE assumes the H1LINE is formatted as: 
    
   <Filename> <One line comment> 
    
   GETH1LINE is used by MKCONTENTS to simply generate a formatted 
   contents file for a directory with m-files. 
    
   Examples: 
      geth1line('geth1line',1) 
      geth1line('geth1line',0) 
      geth1line('geth1line',1,7) 
   
  See also  mkcontents

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

001 function H1 = geth1line(filename,format,N) 
002 %GETH1LINE Extracts the first comment line (the H1 line) of a m-file 
003 %  
004 %   CALL:  H1 = geth1line(filename,format,N); 
005 %  
006 %      H1       = string containing the H1 line of the file 
007 %      filename = string containing the name of the file 
008 %      format   = 0 No formatting of H1 (default) 
009 %                 1 Format H1  
010 %      N        = Number of characters for filename (default 14) 
011 %  
012 %  GETH1LINE assumes the H1LINE is formatted as: 
013 %   
014 %  <Filename> <One line comment> 
015 %   
016 %  GETH1LINE is used by MKCONTENTS to simply generate a formatted 
017 %  contents file for a directory with m-files. 
018 %   
019 %  Examples: 
020 %     geth1line('geth1line',1) 
021 %     geth1line('geth1line',0) 
022 %     geth1line('geth1line',1,7) 
023 %  
024 % See also  mkcontents 
025    
026    
027   % Tested on: Matlab 5.3 
028 % History:  
029 % revised pab 30.10.2003 
030 % changed name from h1line to geth1line  
031 % -improved the robustness of the code   
032 % - added ddeblank   
033 % revised pab 07.12.2000 
034 %  - added examples 
035 %  - added N to input 
036 %  - cleaned up some code by using strtok 
037 %  - removed newline characters from H1 
038 % Revised pab 27.01.2000 
039 %  - improved while loop for searching percent signs   
040 % Revised by pab 08.11.1999 
041 %   - added frmt 
042 % Revised by pab 08.10.1999 
043 %   - updated documentation,  
044 %   - changed name from findline to h1line 
045 %   - added the possibility to specify the complete file name 
046 %   - improved formatting 
047 % by jr 27.09.1999 
048  
049 %filename 
050 if (nargin<2|isempty(format)),  
051   format = 0;  
052 end 
053 if (nargin<3|isempty(N)),     
054   N    = 14;  
055 end 
056  
057 %filename 
058 H1 = ''; 
059 filename = strtok(filename,'.'); % strip the .m extension 
060  
061 fid      = fopen([filename, '.m'],'r'); 
062 if fid==-1, 
063   disp([ 'Unable to open file ', filename ]) 
064   return 
065 end 
066  
067 foundSourceCode = 0; 
068 while ((~foundSourceCode) & (feof(fid)==0)),  
069   tline = ddeblank(fgetl(fid)); 
070   if (length(tline) > 1), % Allow that some lines may be empty or only  
071               % contain a percent sign 
072     k = findstr(tline,'%'); %LOOK for percent sign anywhere in the line 
073     foundSourceCode = ~isempty(k); 
074   end   
075 end 
076 fclose(fid); 
077  
078 if (~any(isletter(tline)) | length(tline)<2|... 
079     isempty(k) |~strcmp(tline(k(1)),'%')),  
080   warning(['No help header is found in '  filename]) 
081   return 
082 end 
083 [fn,rr] = strtok(tline(k(1)+1:end)); 
084 if strcmpi(fn,filename) 
085   rr  = ddeblank(rr);  % strip leading & trailing blanks  
086 else 
087   warning(['Wrong H1LINE-style: Filename differ from first token in' ... 
088        ' H1LINE.']) 
089   fn = filename; 
090   rr = ddeblank(tline(k(1)+1:end)); 
091 end 
092 Nbl     = max(N - length(filename) ,1); % # of blanks 
093 if isempty(rr) 
094    rr =  ' '; 
095 end 
096 switch format 
097   case 0, % No formatting of H1 line 
098     H1 = [ fn blanks(Nbl)  rr]; % alternative 
099   otherwise      %  Format H1 line   
100     % make sure func. name  is in lower case letters 
101     H1 = [ lower(fn), blanks(Nbl) , '- ', upper(rr(1)), rr(2:end) ]; 
102 end 
103 return 
104  
105 function s1 = ddeblank(s) 
106 %DDEBLANK Double deblank. Removes both leading and trailing blanks of string. 
107 % 
108 %  CALL: s1 = ddeblank(s); 
109 % 
110 %   s1,s = strings or cellarray of strings 
111 % 
112 % DDEBLANK removes leading and trailing blanks and null characters from 
113 % the string S.  A null character is one that has an absolute value of 0. 
114  
115 % If S is a cellarray of strings it works on every element of S. 
116 % 
117 % Example: 
118 %  s  = '    Testing testing 1-2-3   ' 
119 %  s1 = ddeblank(s) 
120 % 
121 % See also  deblank, dewhite, ddwhite 
122  
123 % History: 
124 % revised pab 30.10.2003 
125 %  -   
126 % revised pab, 07.12.2000 
127 % - renamed from strim to ddeblank 
128 % - made it faster. 
129 % - added the possibility that s is a cellarray 
130 % - updated the documentation 
131 % - added example 
132 % 
133 %   Author:      Peter J. Acklam 
134 %   Time-stamp:  2000-07-17 02:05:04 
135 %   E-mail:      jacklam@math.uio.no 
136 %   WWW URL:     http://www.math.uio.no/~jacklam 
137 % Date 11-08-98 
138 % Written by Stefan Baunack. Please send any bug 
139 % reports or other comments to: s.baunack@ifw-dresden.de. 
140  
141 error(nargchk(1,1,nargin)); 
142  
143 if isempty(s),  
144   s1=s;  
145   return;  
146 end 
147 if ~(ischar(s)|iscell(s)) 
148   error('Input must be a string or cellarray of strings.') 
149 end 
150  
151 if iscell(s), 
152   s1=cell(size(s)); 
153   for ix=1:prod(size(s)), 
154     s1{ix} = strim1(s{ix}); 
155   end 
156 else, % ischar 
157   s1  = strim1(s); 
158 end 
159  
160 return 
161  
162 function s1 = strim1(s); 
163 % STRIM1 Core implementation of strim 
164  
165 if ~(ischar(s)), 
166   error('Input must be a string.') 
167 end 
168 [r,c] = find(~isspace(s) & s ~= 0); 
169  
170 if isempty(c)  
171   s1 = ''; 
172 elseif size(s, 1) == 1 
173   s1 = s(min(c):max(c)); 
174 else 
175   s1 = s(:,min(c):max(c)); 
176 end 
177 return 
178

Mathematical Statistics
Centre for Mathematical Sciences
Lund University with Lund Institute of Technology

Comments or corrections to the WAFO group


Generated on Thu 06-Oct-2005 02:21:16 for WAFO by m2html © 2003