1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
3 // Copyright (C) ???? - INRIA - Serge STEER
4 // Copyright (C) 2012 - 2016 - Scilab Enterprises
5 // Copyright (C) 2018 - Samuel GOUGEON
7 // This file is hereby licensed under the terms of the GNU GPL v2.0,
8 // pursuant to article 5.3.4 of the CeCILL v.2.1.
9 // This file was originally licensed under the terms of the CeCILL v2.1,
10 // and continues to be available under such terms.
11 // For more information, see the COPYING file which you should have received
12 // along with this program.
14 function transorder = translatepaths(Paths, res_path)
15 // Perform translation of Matlab M-files to Scilab for all M-files found in Paths
18 // -transorder : a vector which contains the files names with a determinated order
20 // -Paths : a vector of strings (paths of Matlab M-files to translate)
21 // -res_paths : a string (results path of translated files)
23 // M2SCI kernel functions called:
28 // Get default arguments
40 // Loads libraries related to m2sci
41 if exists("m2skernellib")==0 then load("SCI/modules/m2sci/macros/kernel/lib"),end
42 if exists("m2spercentlib")==0 then load("SCI/modules/m2sci/macros/percent/lib"),end
43 if exists("m2ssci_fileslib")==0 then load("SCI/modules/m2sci/macros/sci_files/lib"),end
45 // Convert paths so that they can be used according to the platform
46 shortTMPDIR = getshortpathname(pathconvert(TMPDIR));
48 Paths = getshortpathname(pathconvert(stripblanks(Paths)));
49 res_path = getshortpathname(pathconvert(stripblanks(res_path)));
51 // Create a logfile and a whatis file
52 File = shortTMPDIR + gettext("unitfile.dat")
53 [tempfd,ierr] = file("open", File, "old");
56 load(pathconvert(TMPDIR)+gettext("unitfile.dat"))
57 file("close",whsfil_unit);
59 mdelete(pathconvert(TMPDIR)+gettext("unitfile.dat"))
61 whsfil_unit = file("open",res_path+"whatis","unknown")
62 save(pathconvert(TMPDIR)+gettext("unitfile.dat"),"whsfil_unit")
64 // Find names of files to translate
65 // mfiles is a vector which contains the names (and the paths) of files to translate
68 mfiles = [mfiles; ls(path+"*.m")];
71 // fnamvect is a vector which contains all M-files names (just the names) found in Paths
72 // filefuncallname is a list of vectors. The first component of each vector is the name of a M-file (found in the Path(s)) to translate, followed by the called functions by this M-file)
74 filefuncallname=list()
75 for k1=1:size(mfiles,1)
76 kk=strindex(mfiles(k1),sep)
77 fnamvect=[fnamvect;part(mfiles(k1),kk($)+1:length(mfiles(k1))-2)]
80 for k1=1:size(mfiles,1)
82 // disp(gettext("********************lst_funcall**********************"))
84 filefuncallname($+1)=lst_funcall(mpath,fnamvect)
87 // Determination of a translation order for the M-files found in the Paths
88 // transorder is a vector containing the M-files names which are arranged to order to respect an priority order of translation
90 for i=1:size(filefuncallname)
91 execstr(filefuncallname(i)(1)+"vect=filefuncallname(i)")
95 for i=1:size(filefuncallname)
96 execstr("transorder=translateorder(transorder,"+filefuncallname(i)(1)+"vect,overfunname)")
99 // funpath is a vector which contains the M-files names (ands their paths) to translate
101 for i=1:size(transorder,1)
102 ind=find(transorder(i)==fnamvect)
104 funpath=[funpath;mfiles(ind)]
108 // Translation is done only if M-file has changed
112 for i=1:size(funpath,1)
113 kk=strindex(funpath(i),sep)
115 fnam=part(funpath(i),kk($)+1:length(funpath(i))-2)
116 scipath=res_path+fnam+".sci"
117 scepath=res_path+fnam+".sce"
119 if newest(mpath,scipath,scepath)==1 then
120 [fd,ierr] = file("open", shortTMPDIR + fnam+".m", "old");
121 if ierr==0 & strindex(mpath,TMPDIR)==[] then
122 mfile2sci(pathconvert(TMPDIR)+fnam+".m",res_path, %t, %t)
124 mdelete(pathconvert(TMPDIR)+fnam+".m")
125 rmdir(pathconvert(TMPDIR)+pathconvert(fnam),"s")
127 mfile2sci(funpath(i),res_path, %t, %t)
130 tmp_sci_file = shortTMPDIR + "tmp_" + fnam + ".sci"
131 ierr=execstr("exec(tmp_sci_file)","errcatch");errclear();
132 if ierr==0 & strindex(mpath,TMPDIR)==[] then
135 txt=[txt;" ";mgetl(tmp_sci_file)]
137 mdelete(tmp_sci_file)
141 tmp_m2sci_file = shortTMPDIR + "tmp_m2sci_" + fnam + ".log"
142 m2scipath = res_path + "m2sci_" + fnam + ".log"
143 logtxt = [logtxt ; " " ; " " ; mgetl(m2scipath)]
146 [fd,ierr] = file("open",tmp_m2sci_file, "old");
147 if ierr==0 & strindex(mpath,TMPDIR)==[] then
148 logtxt=[logtxt;" ";mgetl(tmp_m2sci_file)]
150 mdelete(tmp_m2sci_file)
154 tmp_resume_m2sci_file = shortTMPDIR + "tmp_resume_m2sci_"+fnam+".log"
155 resumem2scipath=res_path+"resume_m2sci_"+fnam+".log"
156 if fileinfo(resumem2scipath)<>[] then
157 resumelogtxt=[resumelogtxt;" ";" ";mgetl(resumem2scipath)]
158 mdelete(resumem2scipath)
161 [fd,ierr]=file("open",tmp_resume_m2sci_file,"old");
162 if ierr==0 & strindex(mpath,TMPDIR)==[] then
163 resumelogtxt=[resumelogtxt;" ";mgetl(tmp_resume_m2sci_file)]
165 mdelete(tmp_resume_m2sci_file)
168 if isempty(logtxt) then
171 mputl(logtxt,res_path+"log");
172 if isempty(resumelogtxt) then
175 mputl(resumelogtxt,res_path+"resumelog");
179 file("close",whsfil_unit);
180 mdelete(pathconvert(TMPDIR)+gettext("unitfile.dat"))
182 // create builder.sce and loader.sce files
183 // get the directory name where the Scilab functions are written
184 if or(res_path==["./" ".\"]) then
185 res_path = pathconvert(pwd());
187 namelib = basename(pathconvert(res_path, %f)) + "lib"
191 buildertxt($+1)="path=get_absolute_file_path(""builder.sce"")"
192 buildertxt($+1)="genlib("""+namelib+""",path)"
193 builderfile = res_path+"builder.sce"
194 mputl(buildertxt,builderfile);
197 loadertxt($+1)="path=get_absolute_file_path(""loader.sce"")"
198 loadertxt($+1)="load(path+"+"""lib"")"
199 loaderfile = res_path+"loader.sce"
200 mputl(loadertxt,loaderfile);
204 // ---------------------------------------------------------------------------
206 function [transorder]=translateorder(transorder,funtxt,overfunname)
207 // PRIVATE UTILITY FUNCTION called only by translatepath() and itself (recursive)
209 // TRANSLATEORDER Recursive function
210 // Determinates a translate order of the M-files found in Paths (used by "translatepaths" function)
212 // -transorder : a vector containing the M-files names which are arranged in order to respect an priority order of translation
214 // -funtxt : a vector which contains the name of a M-file found in the Paths (its first component: funtxt(1)), and the called functions by this file (the others components : funtxt(2:$))
215 // -overfunname : a vector which contains the files names being passed like argument of "translateorder" function
217 // the file is already in the list
218 if or(transorder==funtxt(1)) then
222 if size(funtxt,"*")>1 then
223 for i=2:size(funtxt,1)
224 // the called function is already in the list
225 if find(funtxt(i)==transorder)<>[] then
227 // the called function is already passed in argument of "translateorder" function (a loop)
228 elseif find(funtxt(i)==overfunname)<>[] then
229 disp([overfunname;funtxt(i)],"loop: ")
232 // order the under level, also we call translateorder (recursive)
234 overfunname($+1)=funtxt(1)
235 execstr("[transorder]=translateorder(transorder,"+funtxt(i)+"vect,overfunname)")
238 transorder($+1)=funtxt(1)
240 // no called function by the M-file, also we put the M-file name in the transorder vector
241 transorder($+1)=funtxt(1)