]> Creatis software - cpPlugins.git/blob - appli/bash/CreateWin32Installer.cxx
...
[cpPlugins.git] / appli / bash / CreateWin32Installer.cxx
1 /* =========================================================================
2  * @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3  * =========================================================================
4  */
5 #include <climits>
6 #include <cstdlib>
7 #include <iostream>
8 #include <memory>
9 #include <queue>
10 #include <set>
11 #include <sstream>
12 #include <stdexcept>
13 #include <string>
14 #include <sys/stat.h>
15 #include <unistd.h>
16
17 // -------------------------------------------------------------------------
18 void Split( const std::string& s, std::string& d, std::string& f )
19 {
20   size_t found = s.find_last_of( "/\\" );
21   d = s.substr( 0, found );
22   f = s.substr( found + 1 );
23 }
24
25 // -------------------------------------------------------------------------
26 inline bool Exists( const std::string& name )
27 {
28   struct stat buffer;
29   return( stat( name.c_str( ), &buffer) == 0 );
30 }
31
32 // -------------------------------------------------------------------------
33 std::string Exec( const std::string& cmd )
34 {
35   /*
36     std::array< char, 128 > buffer;
37     std::string result;
38     std::shared_ptr< FILE > pipe( popen( cmd.c_str( ), "r" ), pclose );
39     if( !pipe )
40     throw std::runtime_error( "popen( ) failed!" );
41     while( !feof( pipe.get( ) ) )
42     if( fgets( buffer.data( ), 128, pipe.get( ) ) != NULL )
43     result += buffer.data( );
44     return( result );
45   */
46   return( "" );
47 }
48
49 // -------------------------------------------------------------------------
50 int main( int argc, char* argv[] )
51 {
52   if( argc < 3 )
53   {
54     std::cerr << "Usage: " << argv[ 0 ] << " dump_tool libs" << std::endl;
55       return( 1 );
56
57   } // fi
58   std::string dump_tool;
59
60   if( !Exists( argv[ 1 ] ) )
61   {
62     std::cerr
63       << "Error: file \"" << argv[ 1 ] << "\" does not exist."
64       << std::endl;
65     return( 1 );
66
67   } // fi
68   dump_tool = realpath( argv[ 1 ], NULL );
69
70   std::set< std::string > files, dirs;
71   std::queue< std::string > q;
72   for( int i = 2; i < argc; ++i )
73   {
74     if( Exists( argv[ i ] ) )
75     {
76       std::string fname = realpath( argv[ i ], NULL );
77       std::string fname_dir, fname_file;
78       Split( fname, fname_dir, fname_file );
79       q.push( fname );
80       dirs.insert( fname_dir );
81
82     } // fi
83
84   } // rof
85
86   while( !q.empty( ) )
87   {
88     std::string e = q.front( );
89     q.pop( );
90     if( files.find( e ) == files.end( ) )
91     {
92       try
93       {
94         if( Exists( e ) )
95         {
96           std::istringstream ss(
97             Exec( dump_tool + " -x " + e + " | grep DLL\\ Name" )
98             );
99           files.insert( e );
100           std::string line;
101           while( std::getline( ss, line ) )
102           {
103             size_t p = line.find( ":" ) + 2;
104             std::set< std::string >::const_iterator d;
105             for( d = dirs.begin( ); d != dirs.end( ); ++d )
106               q.push( *d + "/" + line.substr( p ) );
107
108           } // elihw
109
110         } // fi
111       }
112       catch( std::exception& err )
113       {
114         // Just ignore
115       } // yrt
116
117     } // fi
118
119   } // elihw
120
121   for( std::string f: files )
122     std::cout << "---> " << f << std::endl;
123
124   return( 0 );
125 }
126
127 // eof - $RCSfile$