1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * os_process_test.cpp -- test harness of os_process classes
|
---|
4 | * Copyright (C) 2010 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | *********************************************************************/
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 | #include "os_process_windows.h"
|
---|
29 | #include "os_process_unix.h"
|
---|
30 |
|
---|
31 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
32 | # include <ospace/std/iostream>
|
---|
33 | #elif defined(GSDL_USE_IOS_H)
|
---|
34 | # include <iostream.h>
|
---|
35 | #else
|
---|
36 | # include <iostream>
|
---|
37 | using namespace std;
|
---|
38 | #endif
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | void test_uni_write_pipe()
|
---|
44 | {
|
---|
45 | #ifdef __WIN32__
|
---|
46 | char* prog_name = "c:\\cygwin\\bin\\tr.exe";
|
---|
47 | #else
|
---|
48 | char* prog_name = "tr";
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | char* argv[] = { "tr", "[A-Z]", "[a-z]", NULL };
|
---|
52 |
|
---|
53 | cout << "<hr>" << endl;
|
---|
54 | cout << "<p>Unidirectional Write Test<br>" << endl;
|
---|
55 | cout << " Converts uppercase letters to lowercase<br>" << endl;
|
---|
56 | cout << " These are then sent to whereever the stdout of the child process goes" << endl;
|
---|
57 | cout << "<p>" << endl;
|
---|
58 |
|
---|
59 | #ifdef __WIN32__
|
---|
60 | osprocesswindows* process = new osprocesswindows(uniWrite,prog_name,argv);
|
---|
61 | #else
|
---|
62 | osprocessunix* process = new osprocessunix(uniWrite,prog_name,argv);
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | char* buffer = "TESTING uniWirite IN CAP<br>\nTesting uniWrite in Mixed Case<br>\n";
|
---|
66 | int buffer_len = strlen(buffer);
|
---|
67 |
|
---|
68 | if (!process->write(buffer,buffer_len)) {
|
---|
69 | cerr << "Error: failed to write to child process\n";
|
---|
70 | }
|
---|
71 |
|
---|
72 | process->close();
|
---|
73 |
|
---|
74 | process->wait();
|
---|
75 |
|
---|
76 | delete process;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | void test_uni_read_pipe()
|
---|
81 | {
|
---|
82 | char* prog_name = "java";
|
---|
83 | char* argv[] = { "java", NULL };
|
---|
84 |
|
---|
85 | cout << "<hr>" << endl;
|
---|
86 | cout << "<p>Unidirectional Read Test<br>" << endl;
|
---|
87 | cout << " The output of 'java' is read in by this process and then sent to stdout" << endl;
|
---|
88 | cout << "<p>" << endl;
|
---|
89 |
|
---|
90 |
|
---|
91 | // through virutal inheritence can pass object around as base class, if
|
---|
92 | // desired
|
---|
93 | #ifdef __WIN32__
|
---|
94 | osprocess* process = new osprocesswindows(uniRead,prog_name,argv);
|
---|
95 | #else
|
---|
96 | osprocess* process = new osprocessunix(uniRead,prog_name,argv);
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | const int BufferSize = 1024;
|
---|
100 | char buffer[BufferSize];
|
---|
101 |
|
---|
102 | int bytes_read = 0;
|
---|
103 | do {
|
---|
104 | bytes_read = process->read(buffer,BufferSize);
|
---|
105 | if (bytes_read>0) {
|
---|
106 | fwrite(buffer,1,bytes_read,stdout);
|
---|
107 | }
|
---|
108 | } while (bytes_read==BufferSize);
|
---|
109 |
|
---|
110 |
|
---|
111 | // deleting object will also have the effect of closing any open handles
|
---|
112 | delete process;
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 |
|
---|
118 | void test_bi_read_write_pipe()
|
---|
119 | {
|
---|
120 | #ifdef __WIN32__
|
---|
121 | char* prog_name = "c:\\cygwin\\bin\\tr.exe";
|
---|
122 | #else
|
---|
123 | char* prog_name = "tr";
|
---|
124 | #endif
|
---|
125 | char* argv[] = { "tr", "[A-Z]", "[a-z]", NULL };
|
---|
126 |
|
---|
127 | // Converts uppercase letters to lowercase
|
---|
128 | // Answer is read back and then printed to STDOUT by this process
|
---|
129 |
|
---|
130 | cout << "<hr>" << endl;
|
---|
131 | cout << "<p>Bidirectional Read/Write Test<br>" << endl;
|
---|
132 | cout << " Converts uppercase letters to lowercase<br>" << endl;
|
---|
133 | cout << " Data is sent to child process down pipe by this process<br>" << endl;
|
---|
134 | cout << " Answer is read back from child process and printed to stdout" << endl;
|
---|
135 | cout << "<p>" << endl;
|
---|
136 |
|
---|
137 | #ifdef __WIN32__
|
---|
138 | osprocesswindows* process = new osprocesswindows(biReadWrite,prog_name,argv);
|
---|
139 | #else
|
---|
140 | osprocessunix* process = new osprocessunix(biReadWrite,prog_name,argv);
|
---|
141 | #endif
|
---|
142 |
|
---|
143 | char* write_buffer = "TESTING biReadWrite IN CAP<br>\nTesting biReadWrite in Mixed Case<br>\n";
|
---|
144 | int write_buffer_len = strlen(write_buffer);
|
---|
145 |
|
---|
146 | if (!process->write(write_buffer,write_buffer_len)) {
|
---|
147 | cerr << "Error: failed to write to child process\n";
|
---|
148 | }
|
---|
149 |
|
---|
150 | const int BufferSize = 1024;
|
---|
151 | char read_buffer[BufferSize];
|
---|
152 | process->close_write_pipe();
|
---|
153 |
|
---|
154 | int bytes_read = 0;
|
---|
155 | do {
|
---|
156 | bytes_read = process->read(read_buffer,BufferSize);
|
---|
157 | if (bytes_read>0) {
|
---|
158 | fwrite(read_buffer,1,bytes_read,stdout);
|
---|
159 | }
|
---|
160 | } while (bytes_read==BufferSize);
|
---|
161 |
|
---|
162 |
|
---|
163 | process->close();
|
---|
164 |
|
---|
165 | delete process;
|
---|
166 |
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | int main()
|
---|
171 | {
|
---|
172 | printf("Content-type: text/html\n\n");
|
---|
173 |
|
---|
174 | printf( "<html>\n");
|
---|
175 | printf( " <head>\n");
|
---|
176 | printf( " <title>Testing</title>\n");
|
---|
177 | printf( " </head>\n");
|
---|
178 | printf( " <body>\n");
|
---|
179 |
|
---|
180 | test_uni_read_pipe();
|
---|
181 | test_uni_write_pipe();
|
---|
182 | test_bi_read_write_pipe();
|
---|
183 |
|
---|
184 | printf( " </body>\n");
|
---|
185 | printf( "</html>\n");
|
---|
186 |
|
---|
187 | return 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 |
|
---|