File: genlib.sci

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (156 lines) | stat: -rw-r--r-- 4,277 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
function genlib(nam,path,force,verbose,names)
// get all .sci files in the specified directory
// Copyright Inria/Enpc 
  
  if exists('force','local')==0 then force = %f,end
  if exists('verbose','local')==0 then verbose = %f,end
  
  //
  W=who('get');
  np=predef();
  predefined=or(W($-np+1:$)==nam);
  
  updatelib=%f //true if updating an already defined library
  //check if a library with the same name exists
  oldlib=[];old_path=[];old_names=[];
  if exists(nam)==1 then 
    execstr('oldlib='+nam)
    if typeof(oldlib)=='library' then
      //yes, get its path and function list
      old_names=string(oldlib);clear oldlib
      old_path=old_names(1);old_names(1)=[];
    end
  end
  if exists('path','local')==0&old_path<>[] then
    path=old_path
    updatelib=%t
  end
  // convert path according to MSDOS flag 
  // without env variable substitution
  path1 = pathconvert(path,%t,%f); 
  // with env subsitution 
  path = pathconvert(path,%t,%t); 
  if exists('names','local')==0 then 
    // list the sci files 
    if part(getos(),1:6)=='CYGWIN' then
      // ls *.sci requires less resources than ls path/*.sci
      files=path+listfiles('*.sci',%f)
    else
       files=listfiles(path+'*.sci',%f)
    end
    if files==[] | files== "" then 
      error('I cannot find any sci files in '+path);
      return ;
    end
    names = basename(files,%f);
  else
    files = path+names
    names=strsubst(names,'.sci','')
  end
  
  names_changed=%t
  if updatelib then
    if and(sort(names)==sort(old_names)) then names_changed=%f,end
  end

  modified=%f

  if force == %t then 
    modified = %t;
    for i=1:size(files,'*')  // loop on .sci files
      scif = files(i); 
      if verbose then 
	write(%io(2),' '+names(i) + '.sci compilation forced');
      end
      // getf sci file and save functions it defines as a .bin file
      getsave(scif);
    end  
  else
     for i=1:size(files,'*')  // loop on .sci files
       scif = files(i); 
       binf = strsubst(scif,'.sci','.bin')
       binf_info = fileinfo(binf); 
       recompile = %f ; 
       if binf_info == [] then 
	 recompile = %t ;
       else 
	  scif_info = fileinfo(scif); 
	  if ( scif_info(6) > binf_info(6)) then 
	    recompile = %t ; 
	  end
       end
       if recompile == %t then 
	 if verbose then 
	   write(%io(2),'Processing file '+names(i) + '.sci');
	 end
	 // getf sci file and save functions it defines as a .bin file
	 getsave(scif);
	 modified = %t;
       end  
     end
  end
  
  if modified then 
    if verbose then write(%io(2),'Regenerate names and lib');end
    if names_changed
      mputl(names,path+'names') // write 'names' file in directory 
    end
    // create library
    execstr(nam+'=lib('''+path1+''')')
    //save it
    if execstr('save('''+path1+'lib'''+','+nam+')','errcatch')<>0 then
      error(path+'lib file cannot be created')
    end
  else
     execstr(nam+'=lib('''+path1+''')')
  end
  if names_changed
    if ~predefined then
      execstr(nam+'=resume('+nam+')')
    else
      write(%io(2),[ '   Library file '+path1+'lib'+ ' has been updated, '
		     '   but cannot be loaded into Scilab,'
		     '   because '+nam+' is a protected variable.']);
    end
  end
endfunction


function getsave(fl)
// utility function 
// performs a getf on file fl 

  prot=funcprot();funcprot(0)
  nold=size(who('get'),'*')

//  ierr=execstr('getf(fl)','errcatch') // get functions defined in file 'fl'
  ierr=exec(fl,'errcatch',-1)
  if ierr<>0 then
  	clear ierr
    mprintf('  Warning: Error in file '+fl+' :\n    ""'+lasterror()+'""\n"+...
	    '  file ignored')
  else
    clear ierr
    // lookfor names of the functions defined in file 'fl'
    new=who('get')
    new=new(1:(size(new,'*')-nold-1))
    // create output file name
    fl=strsubst(fl,'.sci','.bin')
    // save all functions in the output file
    [u,ierr]=mopen(fl,'wb')
    if ierr<>0 then 
      clear ierr
      nf=length(fl)
      if nf>40 then fl='...'+part(fl,nf-40:nf),end
      error('Impossible to open file '+fl+' for writing, ')
    end
    clear ierr
    if new<>[] then 
      execstr('save(u,'+strcat(new($:-1:1),',')+')'); 
    else 
      mprintf('  function ""'+fl+'"" does not contain any functions');
    end
    mclose(u)
  end
  funcprot(prot)
endfunction