use strict;
use Cwd;
use File::Find;
use File::Basename;
use Text::Wrap;
use FileHandle;
use IPC::Open3;
my $in_dir = @ARGV[0];
my $out_dir = @ARGV[1];
my $tar_file = @ARGV[2];
system ("cp -pr $in_dir $out_dir") ;
my $old_pwd = cwd();
chdir($out_dir);
system ("tar xpBvzf $old_pwd/$tar_file");
chdir($old_pwd);
my @demo_files = ('demo1.c', 'demo2.c', 'demo3.c', 'demo.c', 'demo.h') ;
foreach my $fff ( @demo_files )
{
my $infile = sprintf ("%s/Demo/cs_%s", $in_dir, $fff) ;
my $outfile = sprintf ("%s/Demo/cs_%s", $out_dir, $fff) ;
printf ("%s to %s\n", $infile, $outfile) ;
if (open (OUT, ">$outfile"))
{
if (open (IN, $infile))
{
while (<IN>)
{
s/\bcsi\b/int/g;
print OUT $_;
}
close (IN);
}
close (OUT);
}
my $outfile = sprintf ("%s/Demo/cs_di_%s", $out_dir, $fff) ;
printf ("%s to %s\n", $infile, $outfile) ;
if (open (OUT, ">$outfile"))
{
if (open (IN, $infile))
{
while (<IN>)
{
s/\bcsi\b/int/g;
s/\bcs/cs_di/g ;
s/cs_di\.h/cs.h/ ;
print OUT $_;
}
close (IN);
}
close (OUT);
}
my $outfile = sprintf ("%s/Demo/cs_dl_%s", $out_dir, $fff) ;
printf ("%s to %s\n", $infile, $outfile) ;
if (open (OUT, ">$outfile"))
{
if (open (IN, $infile))
{
while (<IN>)
{
s/\bcsi\b/cs_long_t/g;
s/\bcs/cs_dl/g ;
s/cs_dl_long_t/cs_long_t/g;
s/cs_dl\.h/cs.h/ ;
print OUT $_;
}
close (IN);
}
close (OUT);
}
my $outfile = sprintf ("%s/Demo/cs_ci_%s", $out_dir, $fff) ;
printf ("%s to %s\n", $infile, $outfile) ;
if (open (OUT, ">$outfile"))
{
if (open (IN, $infile))
{
while (<IN>)
{
s/\bcsi\b/int/g;
s/\bcs/cs_ci/g ;
s/cs_ci_long_t/cs_long_t/g;
s/cs_ci\.h/cs.h/ ;
s/fabs/cabs/g;
s/\bdouble\b/cs_complex_t/g;
s/\(cs_complex_t\) /(double) /g;
s/cs_complex_t norm/double norm/;
s/cs_complex_t tic/double tic/;
s/cs_complex_t toc \(cs_complex_t/double toc (double/;
s/cs_complex_t s = tic/double s = tic/;
s/cs_complex_t tol/double tol/;
s/cs_complex_t lnz/double lnz/;
s/cs_complex_t unz/double unz/;
s/cs_complex_t cs_cumsum/double cs_cumsum/;
s/, / ;\n double / ;
print OUT $_;
}
close (IN);
}
close (OUT);
}
my $outfile = sprintf ("%s/Demo/cs_cl_%s", $out_dir, $fff) ;
printf ("%s to %s\n", $infile, $outfile) ;
if (open (OUT, ">$outfile"))
{
if (open (IN, $infile))
{
while (<IN>)
{
s/\bcsi\b/cs_long_t/g;
s/\bcs/cs_cl/g ;
s/cs_cl_long_t/cs_long_t/g;
s/cs_cl\.h/cs.h/ ;
s/fabs/cabs/g;
s/\bdouble\b/cs_complex_t/g;
s/\(cs_complex_t\) /(double) /g;
s/cs_complex_t norm/double norm/;
s/cs_complex_t tic/double tic/;
s/cs_complex_t toc \(cs_complex_t/double toc (double/;
s/cs_complex_t s = tic/double s = tic/;
s/cs_complex_t tol/double tol/;
s/cs_complex_t lnz/double lnz/;
s/cs_complex_t unz/double unz/;
s/cs_complex_t cs_cumsum/double cs_cumsum/;
s/, / ;\n double / ;
print OUT $_;
}
close (IN);
}
close (OUT);
}
}
my @src_files = ('Source/cs_add.c', 'Source/cs_amd.c', 'Source/cs_chol.c',
'Source/cs_cholsol.c', 'Source/cs_counts.c', 'Source/cs_cumsum.c',
'Source/cs_dfs.c', 'Source/cs_dmperm.c', 'Source/cs_droptol.c',
'Source/cs_dropzeros.c', 'Source/cs_dupl.c', 'Source/cs_entry.c',
'Source/cs_etree.c', 'Source/cs_fkeep.c', 'Source/cs_gaxpy.c',
'Source/cs_happly.c', 'Source/cs_ipvec.c',
'Source/cs_lsolve.c', 'Source/cs_ltsolve.c', 'Source/cs_lu.c',
'Source/cs_lusol.c', 'Source/cs_malloc.c', 'Source/cs_maxtrans.c',
'Source/cs_multiply.c', 'Source/cs_norm.c', 'Source/cs_permute.c',
'Source/cs_pinv.c', 'Source/cs_post.c',
'Source/cs_pvec.c', 'Source/cs_qr.c', 'Source/cs_qrsol.c',
'Source/cs_scatter.c', 'Source/cs_scc.c', 'Source/cs_schol.c',
'Source/cs_sqr.c', 'Source/cs_symperm.c', 'Source/cs_tdfs.c',
'Source/cs_transpose.c', 'Source/cs_compress.c',
'Source/cs_usolve.c', 'Source/cs_util.c', 'Source/cs_utsolve.c',
'Source/cs_reach.c', 'Source/cs_spsolve.c', 'Source/cs_leaf.c',
'Source/cs_ereach.c', 'Source/cs_randperm.c' ) ;
foreach my $file ( @src_files )
{
my $infile = sprintf ("%s/%s", $in_dir, $file) ;
my $outfile = sprintf ("%s/%s", $out_dir, $file) ;
my $fbase = basename($file,('.c'));
if (open(OUT,">$outfile"))
{
if (open(IN,$infile))
{
while (<IN>)
{
s/CSparse/CXSparse/g;
s/fabs/CS_ABS/g;
s/\bcsi\b/CS_INT/g;
s/\bdouble\b/CS_ENTRY/g;
s/\(CS_ENTRY\) /(double) /g;
s/\(CS_ENTRY \*\) /(double \*) /;
s/CS_ENTRY tol/double tol/;
s/CS_ENTRY \*vnz/double \*vnz/;
s/, / ;\n double / ;
if ($fbase =~ /cs_mex/)
{
s/matrix must be CS_ENTRY/matrix must be double/;
s/A->p =/A->p = (CS_INT *)/;
s/A->i =/A->i = (CS_INT *)/;
s/, A->p/, (mwIndex *) A->p/;
s/, A->i/, (mwIndex *) A->i/;
}
if ($fbase =~ /cs_add_mex/ || $fbase =~ /cs_permute_mex/)
{
s/via CS_ENTRY transpose/via double transpose/;
}
if ($fbase =~ /cs_chol/)
{
s/\(d <= 0\)/(CS_REAL (d) <= 0 || CS_IMAG (d) != 0)\n\t /;
s/lki \* lki/lki * CS_CONJ (lki)/;
s/ = lki/ = CS_CONJ (lki)/;
}
if ($fbase =~ /cs_norm/)
{
s/^CS_ENTRY cs_norm/double cs_norm/;
}
if ($fbase =~ /cs_cumsum/)
{
s/CS_ENTRY/double/;
}
if ($fbase =~ /cs_transpose/)
{
s/Ax \[p\]/(values > 0) ? CS_CONJ (Ax [p]) : Ax [p]/;
}
if ($fbase =~ /cs_symperm/)
{
s/Ax \[p\]/(i2 <= j2) ? Ax [p] : CS_CONJ (Ax [p])/;
}
if ($fbase =~ /cs_qr/)
{
s/n, sizeof \(CS_ENTRY\)/n, sizeof (double)/;
}
if ($fbase =~ /cs_happly/)
{
s/^(.*tau.*)(Vx\s*\[p\])/$1CS_CONJ ($2)/;
s/CS_ENTRY beta/double beta/;
}
if ($fbase =~ /cs_ltsolve/)
{
s/(Lx \[.*?\])(\s+[\*;])/CS_CONJ ($1)$2/;
}
if ($fbase =~ /cs_utsolve/)
{
s/(Ux \[.*?\])(\s+[\*;])/CS_CONJ ($1)$2/;
}
print OUT $_;
}
close (IN);
}
close (OUT);
}
}