PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/mscan.m

http://matdatac.googlecode.com/
MATLAB | 224 lines | 123 code | 25 blank | 76 comment | 26 complexity | 948ffbc3723105714d218b16f783313d MD5 | raw file
Possible License(s): GPL-2.0
  1. function mscan(name,varargin)
  2. % MSCAN Version 1.0 Copyright (C) Peter Rydesäter 1998-12-13 -
  3. %
  4. % This makes your m-files look better and helps
  5. % you find non matching for,if,while,try,else
  6. % ,end . . .
  7. %
  8. % Tested with matlab 5.x
  9. %
  10. % Syntax:
  11. %
  12. % mscan name option
  13. % mscan(name,option)
  14. %
  15. % name is filename with/without path and can also
  16. % hold wildcard (*) to work on multiple files. If an
  17. % optional string argument with an 'r' in is added
  18. % it will work on ALL sub directories.
  19. %
  20. % Updates m-files, replaces tabs with spaces and
  21. % indents for block structures. Also fixes end of
  22. % line: windows (^M ) <--> unix
  23. % It works with wildcards and can
  24. %
  25. % Use with care. Backup your m-files first!
  26. %
  27. %
  28. %
  29. % Example 1:
  30. % mscan /home/mrxx/matlab/*.m r
  31. % OR
  32. % mscan('/home/mrxx/matlab/*.m','r')
  33. %
  34. % Converts all *.m files in the path and sub
  35. % derectories.( Optional r.)
  36. %
  37. % Example 2:
  38. % mscan('test*.m');
  39. % OR
  40. % mscan test*.m
  41. %
  42. % Converts all m-files beginning with 'test' in
  43. % current dir. No sub directories.
  44. %
  45. %
  46. % This program is free software; you can redistribute it and/or
  47. % modify it under the terms of the GNU General Public License
  48. % as published by the Free Software Foundation; either version 2
  49. % of the License, or (at your option) any later version.
  50. %
  51. % This program is distributed in the hope that it will be useful,
  52. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  53. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  54. % GNU General Public License for more details.
  55. % You should have received a copy of the GNU General Public License
  56. % along with this program; if not, write to the Free Software
  57. % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  58. % 02111-1307, USA.
  59. %
  60. % Please send me an e-mail if you use this:
  61. % Peter.Rydesater@ite.mh.se
  62. % Mitthögskolan
  63. % Östersund
  64. % Sweden
  65. %
  66. disp('mscan.m - V0.1 Prettify .m files')
  67. subdir=0;
  68. inter=1;
  69. tbsize=1; % controls number of spaces when indenting (use 1 for tabbing...)
  70. casesub=1; % controls back-tab for case (use 1 when tabbing)
  71. level=0;
  72. lastwasfun=0;
  73. for a=1:length(varargin),
  74. if strcmp(upper(varargin{a}),'R'),
  75. subdir=1;
  76. elseif strcmp(upper(varargin{a}),'NI'),
  77. inter=0;
  78. end
  79. end
  80. if inter,
  81. r=input('Have you backed up your *.m files or some other safety routine? (y/n)','s');
  82. if strncmp(r,'y',1)==0,
  83. disp('Then you better do that first!');
  84. return;
  85. end
  86. disp('Processing following files:');
  87. if strncmp(findrevend(name),'m.',2)==0,
  88. disp('Not matlab *.m file');
  89. return;
  90. end
  91. end
  92. if subdir,
  93. [dirname,filename,extname]=fileparts(name);
  94. dirnames=dir(dirname);
  95. for a=1:length(dirnames),
  96. if strncmp(dirnames(a).name,'.',1), % Not .* directorys
  97. elseif dirnames(a).isdir, % Recursive call if it's a directory
  98. mscan(fullfile(dirnames(a).name,[filename extname]),'R','NI');
  99. end
  100. end
  101. end
  102. % Operators for start of move in...
  103. start_op=strvcat('if','else','elseif','function','for',...
  104. 'switch','otherwise','while','try','catch');
  105. % Operators for end of move in...
  106. stop_op=strvcat('end','else','elseif','otherwise','catch','end;');
  107. % Reversed ordered letters for operators ending move in in the end of line.
  108. end_op_at_end=strvcat('dne');
  109. % Converts a tab to a space
  110. asciiconv=char([1:255]);
  111. asciiconv(9)=' ';
  112. names=dir(name);
  113. for a=1:length(names),
  114. disp(names(a).name);
  115. file=fopen(names(a).name,'r');
  116. line=1;
  117. while(1),
  118. buff=fgetl(file);
  119. if ~isstr(buff), break, end %here is a coment at the end
  120. filebuff{line}=pr_str_trim(asciiconv(double(buff)));
  121. line=line+1;
  122. end
  123. fclose(file);
  124. level=0;
  125. file=fopen(names(a).name,'w');
  126. for line=1:length(filebuff),
  127. if length(filebuff)<=line & length(filebuff{line})==0,
  128. fprintf(file,'\n');
  129. else
  130. [x,y]=strtok(filebuff{line},' ()');
  131. x=lower(pr_str_trim(x));
  132. if strcmp('function',x),
  133. level=0;
  134. lastwasfun=1;
  135. end
  136. if strmatch(x,stop_op,'exact') & level>0 & length(x)>0,
  137. level=level-1;
  138. end
  139. if strncmp(filebuff{line},'%%',2) | ...
  140. (strncmp(filebuff{line},'%',1) & lastwasfun)
  141. fprintf(file,'%s\n',filebuff{line});
  142. else
  143. fprintf(file,'%s%s\n',blanks_t(level*tbsize-casesub*strcmp('case',x)),filebuff{line});
  144. if strcmp('function',x)
  145. lastwasfun=1;
  146. elseif length(x)==0
  147. lastwasfun=lastwasfun; % Do nothing empty line.
  148. else
  149. lastwasfun=0;
  150. end
  151. end
  152. if strmatch(x,start_op,'exact') & length(x)>0,
  153. level=level+1;
  154. end
  155. x=findrevend(filebuff{line});
  156. if strncmp(x,'...',3),
  157. fprintf(file,blanks_t(ceil(tbsize*1.5)));
  158. end
  159. [x,y]=strtok(x,' ,;');
  160. if strmatch(x,end_op_at_end,'exact') & level>0 & length(x)>0 & length(deblank(y))>2,
  161. level=level-1;
  162. end
  163. end
  164. end
  165. clear line
  166. clear filebuff
  167. fclose(file);
  168. end
  169. return;
  170. function ret=findrevend(str)
  171. % Picks out end of line before comment in reverse order with blanks
  172. % in beginning and end removed.
  173. deblank(str);
  174. isstr=0;
  175. for a=1:length(str),
  176. if str(a)==char(39),
  177. isstr= ~isstr;
  178. elseif str(a)=='%' & isstr==0,
  179. a=a-1;
  180. break;
  181. end
  182. end
  183. ret=deblank(str(a:-1:1));
  184. return;
  185. function ret=pr_str_trim(str)
  186. % ret=pr_str_trim(str) Removes blanks in both beginninge and end of line.
  187. %
  188. % Peter Rydesäter 99-04-06
  189. %
  190. str=deblank(str(end:-1:1));
  191. ret=deblank(str(end:-1:1));
  192. return;
  193. function [out] = blanks_t(n)
  194. % replaces blanks with tabs. Use with tbsize and casesub = 1
  195. %
  196. out=[];
  197. for i=1:n
  198. out=[out '\t']; % just concatenate the blanks...
  199. end;