1 | #include <iostream>
|
---|
2 | #include <fstream>
|
---|
3 | #include <iomanip>
|
---|
4 | using namespace std;
|
---|
5 |
|
---|
6 | #include "mywrapper.h"
|
---|
7 |
|
---|
8 | bool extract_bundled_file( const char[], int, char* );
|
---|
9 |
|
---|
10 | int main(int argc, char** argv) {
|
---|
11 |
|
---|
12 | string tempdir = (string)argv[0] + ".tmp"; //temporary directory where we will store extracted files
|
---|
13 | string jarfile = tempdir + "/greenstone3.jar"; //where we will store the jar file
|
---|
14 | bool succeeded = false;
|
---|
15 |
|
---|
16 | //create the temp folder
|
---|
17 | cout << "Creating temp directory" << endl;
|
---|
18 | succeeded = ( 0 == system( ("mkdir " + tempdir).c_str() ) );
|
---|
19 | if ( !succeeded ) {
|
---|
20 | cout << "Failed to write to create the temp directory '" << tempdir << "'" << endl;
|
---|
21 | cout << "Check that it does not already exist and try again" << endl;
|
---|
22 | cout << "Exiting" << endl;
|
---|
23 | return 1;
|
---|
24 | }
|
---|
25 |
|
---|
26 | //extract files
|
---|
27 | cout << "Extracting JAR file..." << endl;
|
---|
28 | succeeded = extract_bundled_file( greenstone3jar, sizeof(greenstone3jar), (char*)jarfile.c_str() );
|
---|
29 | if ( !succeeded ) {
|
---|
30 | cout << "failed" << endl;
|
---|
31 | cout << "Failed to extract the JAR file to '" << jarfile << "'" << endl;
|
---|
32 | cout << "The JAR file may have been bundled with this executable incorrectly" << endl;
|
---|
33 | cout << "Or this program may not have sufficient file permissions to write it to disk" << endl;
|
---|
34 | cout << "Exiting" << endl;
|
---|
35 | return 1;
|
---|
36 | }
|
---|
37 |
|
---|
38 | //run the jar
|
---|
39 | cout << "Running the jar" << endl;
|
---|
40 | system( ("java -jar " + jarfile).c_str() );
|
---|
41 |
|
---|
42 | //delete the temp files
|
---|
43 | cout << "Deleting the temp directory" << endl;
|
---|
44 | system( ("rm -rf " + tempdir).c_str() );
|
---|
45 | system( "rm ant.install.log" );
|
---|
46 |
|
---|
47 |
|
---|
48 | return 0;
|
---|
49 |
|
---|
50 | }
|
---|
51 |
|
---|
52 | bool extract_bundled_file( const char data[], int size, char* filename ) {
|
---|
53 |
|
---|
54 | //delete the file if it exists
|
---|
55 | remove( filename );
|
---|
56 |
|
---|
57 | //open the file
|
---|
58 | fstream binary_file( filename, ios::out|ios::binary );
|
---|
59 | if ( !binary_file.good() ) {
|
---|
60 | binary_file.close();
|
---|
61 | return false;
|
---|
62 | }
|
---|
63 |
|
---|
64 | //write the file
|
---|
65 | binary_file.write( data, size );
|
---|
66 | if ( !binary_file.good() ) {
|
---|
67 | binary_file.close();
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 |
|
---|
71 | //close the file
|
---|
72 | binary_file.close();
|
---|
73 | if ( !binary_file.good() ) {
|
---|
74 | binary_file.close();
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 |
|
---|
78 | return true;
|
---|
79 |
|
---|
80 | }
|
---|