* Bugs 16567 16586 fixed: mfile2sci() supports block %{..%}. Appended comments improved"
[scilab.git] / scilab / modules / m2sci / macros / kernel / multi_fun_file.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
3 //
4 // Copyright (C) 2012 - 2016 - Scilab Enterprises
5 //
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
12
13 function bval=multi_fun_file(fil,res_path,Recmode,only_double,verbose_mode,prettyprintoutput)
14     // This function converts M-Files containing more than one functio
15     // Inputs are the same as mfile2sci()
16     // Outputs :
17     // - bval: boolean value, %t if file contains more than on function, %f else
18
19     sciparam();
20
21     // File name
22     k=strindex(fil,".")
23     if k<>[]
24         ke=k($)-1
25         base_name=part(fil,1:ke)
26     else
27         ke=length(fil)
28         base_name=fil
29     end
30     // File path
31     k=strindex(fil,"/")
32     if k==[] then
33         file_path="./"
34     else
35         file_path=part(fil,1:k($))
36         base_name=part(base_name,k($)+1:ke)
37     end
38
39     txt=mgetl(fil);
40
41     kf=grep(txt,["function[","function "])
42
43     if isempty(kf) then
44         // Batch file
45         bval=%f
46     elseif size(kf,"*")==1 then
47         // Only one function defined
48         bval=%f
49     else
50         funcdecl=[]
51         for kk=kf
52             ind=strindex(txt(kk),["function[";"function "])
53             if isacomment(txt(kk))==0 & ~isinstring(txt(kk),ind) & part(stripblanks(txt(kk),%T),1:8)=="function"  then // function prototype
54                 funcdecl=[funcdecl kk]
55             end
56         end
57
58         if isempty(funcdecl) then
59             // "function" only exists in comments and strings
60             bval=%f
61             return
62         elseif size(funcdecl,"*")==1 then
63             bval=%f
64             return
65         end
66
67         // Verify if the directory exists
68         dirnam = ls(pathconvert(TMPDIR)+base_name);
69
70         sep = filesep();
71
72         if or(dirnam<>"") then
73             rmdir(pathconvert(TMPDIR)+base_name,"s")
74         end
75         mkdir(pathconvert(TMPDIR),base_name)
76         write(%io(2),msprintf(gettext(" -- File %s contains more than one function -- "),fil));
77
78         bval= %t
79
80         // First split file into as many files as function declared
81         funcdecl=[funcdecl size(txt,"*")+1]
82
83         tmpfiles=[]
84         for k=1:size(funcdecl,"*")-1
85             functxt=txt(funcdecl(k):funcdecl(k+1)-1)
86             str=  strindex(txt(funcdecl(k)),"(")
87             if str==[] then
88                 funcname=stripblanks(part(txt(funcdecl(k)),strindex(txt(funcdecl(k)),["function[","function "])+8:length(txt(funcdecl(k)))))
89             else
90                 funcname=stripblanks(part(txt(funcdecl(k)),strindex(txt(funcdecl(k)),["function[","function "])+8:str(1)-1))
91             end
92
93             keq=strindex(funcname,"=")
94             if ~isempty(keq) then
95                 funcname=stripblanks(part(funcname,keq+1:length(funcname)))
96             end
97             tmpfiles=[tmpfiles;funcname]
98             mputl(functxt,pathconvert(TMPDIR)+base_name+sep+tmpfiles($)+".m");
99         end
100
101         write(%io(2),msprintf(gettext(" -- Each function converted separately: %s -- "),strcat(tmpfiles," ")));
102         write(%io(2),msprintf(gettext(" -- Temporary files put in: %s -- "),pathconvert(TMPDIR)));
103
104         // Conversion of each file
105
106         for k=1:size(tmpfiles,"*")
107             txt=mgetl(pathconvert(TMPDIR)+base_name+sep+tmpfiles(k)+".m")
108             //mfile2sci(pathconvert(TMPDIR)+tmpfiles(k)+".m",res_path,Recmode,only_double,verbose_mode,prettyprintoutput)
109         end
110
111         translatepaths(pathconvert(TMPDIR)+base_name,pathconvert(TMPDIR)+base_name)
112
113         txt=[]
114         if isfile(pathconvert(TMPDIR)+base_name+sep+"log") then
115             txt=mgetl(pathconvert(TMPDIR)+base_name+sep+"log")
116         end
117         mputl(txt,res_path+"log");
118         if isfile(pathconvert(TMPDIR)+base_name+sep+"resumelog") then
119             txt=mgetl(pathconvert(TMPDIR)+base_name+sep+"resumelog")
120         end
121         mputl(txt,res_path+"resumelog");
122
123         // Catenation of all .sci files to have only one output file
124         txt=[]
125         for k=1:size(tmpfiles,"*")
126             txt=[txt ;"";mgetl(pathconvert(TMPDIR)+base_name+sep+tmpfiles(k)+".sci")]
127         end
128
129         // Delete useless .sci files
130         //for k=1:size(tmpfiles,"*")
131         //mdelete(res_path+tmpfiles(k)+".sci")
132         //end
133
134         mputl(txt,res_path+base_name+".sci");
135
136         // Catenation of all .log files to have only one output file
137         //if exists("logfile")==0 then
138         //txt=[]
139         //for k=1:size(tmpfiles,"*")
140         //txt=[txt ; mgetl(pathconvert(TMPDIR)+base_name+sep+"m2sci_"+tmpfiles(k)+".log")]
141         //end
142
143         // Delete useless .log files
144         //for k=1:size(tmpfiles,"*")
145         //mdelete(pathconvert(TMPDIR)+base_name+sep+"m2sci_"+tmpfiles(k)+".log")
146         //end
147
148         //mputl(txt,res_path+"m2sci_"+base_name+".log");
149         //end
150
151         // Catenation of all resume.log files to have only one output file
152         //if exists("resume_logfile")==0 then
153         //txt=[]
154         //for k=1:size(tmpfiles,"*")
155         //txt=[txt ; mgetl(res_path+"m2sci_"+tmpfiles(k)+"_resume.log")]
156         //end
157
158         // Delete useless _resume.log files
159         //for k=1:size(tmpfiles,"*")
160         //mdelete(res_path+"m2sci_"+tmpfiles(k)+"_resume.log")
161         //end
162
163         //mputl(txt,res_path+"m2sci_"+base_name+"_resume.log");
164         //end
165
166         // Delete useless .m files
167         //for k=1:size(tmpfiles,"*")
168         //mdelete(pathconvert(TMPDIR)+tmpfiles(k)+".m")
169         //end
170
171         rmdir(pathconvert(TMPDIR)+base_name,"s")
172         for k=1:size(tmpfiles,"*")
173             mdelete(pathconvert(TMPDIR)+tmpfiles(k)+".tree")
174         end
175     end
176
177 endfunction