File: multi_fun_file.sci

package info (click to toggle)
scilab 5.3.3-10
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 330,656 kB
file content (174 lines) | stat: -rw-r--r-- 5,100 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
// 
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at    
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt

function bval=multi_fun_file(fil,res_path,Recmode,only_double,verbose_mode,prettyprintoutput)
// This function converts M-Files containing more than one functio
// Inputs are the same as mfile2sci()
// Outputs :
// - bval: boolean value, %t if file contains more than on function, %f else

sciparam();

// File name
k=strindex(fil,".")
if k<>[]
  ke=k($)-1
  base_name=part(fil,1:ke)
else
  ke=length(fil)
  base_name=fil
end
// File path
k=strindex(fil,"/")
if k==[] then
  file_path="./"
else
  file_path=part(fil,1:k($))
end
base_name=part(base_name,k($)+1:ke)

txt=mgetl(fil);

kf=grep(txt,["function[","function "])

if isempty(kf) then
  // Batch file
  bval=%f
elseif size(kf,"*")==1 then
  // Only one function defined
  bval=%f
else
  funcdecl=[]
  for kk=kf
    ind=strindex(txt(kk),["function[";"function "])
    if isacomment(txt(kk))==0 & ~isinstring(txt(kk),ind) & part(stripblanks(txt(kk),%T),1:8)=="function"  then // function prototype
      funcdecl=[funcdecl kk]
    end
  end
 
  if isempty(funcdecl) then
    // "function" only exists in comments and strings
    bval=%f
    return
  elseif size(funcdecl,"*")==1 then
    bval=%f
    return
  end

  // Verify if the directory exists
	dirnam = ls(pathconvert(TMPDIR)+base_name);
  
  sep = filesep();
  
  if or(dirnam<>'') then
    rmdir(pathconvert(TMPDIR)+base_name,'s')
  end
  mkdir(pathconvert(TMPDIR),base_name)
  write(%io(2),msprintf(gettext(" -- File %s contains more than one function -- "),fil));
     
  bval= %t
  
  // First split file into as many files as function declared
  funcdecl=[funcdecl size(txt,"*")+1] 
 
  tmpfiles=[]
  for k=1:size(funcdecl,"*")-1
    functxt=txt(funcdecl(k):funcdecl(k+1)-1)
    str=  strindex(txt(funcdecl(k)),"(")-1
    if str==-1 then
      funcname=stripblanks(part(txt(funcdecl(k)),strindex(txt(funcdecl(k)),["function[","function "])+8:length(txt(funcdecl(k)))))
    else
      funcname=stripblanks(part(txt(funcdecl(k)),strindex(txt(funcdecl(k)),["function[","function "])+8:str(1)))
    end
   
      keq=strindex(funcname,"=")
    if ~isempty(keq) then
      funcname=stripblanks(part(funcname,keq+1:length(funcname)))
    end
    tmpfiles=[tmpfiles;funcname]
    mputl(functxt,pathconvert(TMPDIR)+base_name+sep+tmpfiles($)+".m");
  end
  
  write(%io(2),msprintf(gettext(" -- Each function converted separately: %s -- "),strcat(tmpfiles," ")));
  write(%io(2),msprintf(gettext(" -- Temporary files put in: %s -- "),pathconvert(TMPDIR)));

  // Conversion of each file

  for k=1:size(tmpfiles,"*")
    txt=mgetl(pathconvert(TMPDIR)+base_name+sep+tmpfiles(k)+".m")
    //mfile2sci(pathconvert(TMPDIR)+tmpfiles(k)+".m",res_path,Recmode,only_double,verbose_mode,prettyprintoutput)
  end
  
  translatepaths(pathconvert(TMPDIR)+base_name,pathconvert(TMPDIR)+base_name)
  
    txt=[]
    if isfile(pathconvert(TMPDIR)+base_name+sep+'log') then
      txt=mgetl(pathconvert(TMPDIR)+base_name+sep+'log')
    end
    mputl(txt,res_path+'log'); 
    if isfile(pathconvert(TMPDIR)+base_name+sep+'resumelog') then
      txt=mgetl(pathconvert(TMPDIR)+base_name+sep+'resumelog')
    end
    mputl(txt,res_path+'resumelog'); 
  
  // Catenation of all .sci files to have only one output file
  txt=[]
  for k=1:size(tmpfiles,"*")
    txt=[txt ;'';mgetl(pathconvert(TMPDIR)+base_name+sep+tmpfiles(k)+".sci")]
  end
  
  // Delete useless .sci files
  //for k=1:size(tmpfiles,"*")
    //mdelete(res_path+tmpfiles(k)+".sci")
  //end
   
  mputl(txt,res_path+base_name+".sci");

  // Catenation of all .log files to have only one output file
  //if exists("logfile")==0 then
    //txt=[]
    //for k=1:size(tmpfiles,"*")
      //txt=[txt ; mgetl(pathconvert(TMPDIR)+base_name+sep+"m2sci_"+tmpfiles(k)+".log")]
    //end
    
    // Delete useless .log files
    //for k=1:size(tmpfiles,"*")
      //mdelete(pathconvert(TMPDIR)+base_name+sep+"m2sci_"+tmpfiles(k)+".log")
    //end
  
    //mputl(txt,res_path+"m2sci_"+base_name+".log");
  //end
  
  // Catenation of all resume.log files to have only one output file
  //if exists("resume_logfile")==0 then
    //txt=[]
    //for k=1:size(tmpfiles,"*")
      //txt=[txt ; mgetl(res_path+"m2sci_"+tmpfiles(k)+"_resume.log")]
    //end
    
    // Delete useless _resume.log files
    //for k=1:size(tmpfiles,"*")
      //mdelete(res_path+"m2sci_"+tmpfiles(k)+"_resume.log")
    //end
  
    //mputl(txt,res_path+"m2sci_"+base_name+"_resume.log");
  //end
  
  // Delete useless .m files
  //for k=1:size(tmpfiles,"*")
    //mdelete(pathconvert(TMPDIR)+tmpfiles(k)+".m")
  //end
  
  rmdir(pathconvert(TMPDIR)+base_name,'s')
  for k=1:size(tmpfiles,"*")
    mdelete(pathconvert(TMPDIR)+tmpfiles(k)+".tree")
  end
end
  
endfunction