Commit fe18385ffaa7e5b11db86af1e992c0fb1984609a

Authored by Jay Berkenbilt
1 parent 86f2d344

clean up windows portability code, make remaining test suite pass

git-svn-id: svn+q:///qpdf/trunk@686 71b93d88-0707-0410-a8cf-f5a4172ac649
examples/pdf-bookmarks.cc
@@ -163,14 +163,8 @@ void extract_bookmarks(QPDFObjectHandle outlines, std::vector<int>& numbers) @@ -163,14 +163,8 @@ void extract_bookmarks(QPDFObjectHandle outlines, std::vector<int>& numbers)
163 163
164 int main(int argc, char* argv[]) 164 int main(int argc, char* argv[])
165 { 165 {
166 - if ((whoami = strrchr(argv[0], '/')) == NULL)  
167 - {  
168 - whoami = argv[0];  
169 - }  
170 - else  
171 - {  
172 - ++whoami;  
173 - } 166 + whoami = QUtil::getWhoami(argv[0]);
  167 +
174 // For libtool's sake.... 168 // For libtool's sake....
175 if (strncmp(whoami, "lt-", 3) == 0) 169 if (strncmp(whoami, "lt-", 3) == 0)
176 { 170 {
examples/pdf-mod-info.cc
@@ -77,14 +77,8 @@ int main(int argc, char* argv[]) @@ -77,14 +77,8 @@ int main(int argc, char* argv[])
77 bool static_id = false; 77 bool static_id = false;
78 std::map<std::string, std::string> Keys; 78 std::map<std::string, std::string> Keys;
79 79
80 - if ((whoami = strrchr(argv[0], '/')) == NULL)  
81 - {  
82 - whoami = argv[0];  
83 - }  
84 - else  
85 - {  
86 - ++whoami;  
87 - } 80 + whoami = QUtil::getWhoami(argv[0]);
  81 +
88 // For libtool's sake.... 82 // For libtool's sake....
89 if (strncmp(whoami, "lt-", 3) == 0) 83 if (strncmp(whoami, "lt-", 3) == 0)
90 { 84 {
@@ -161,6 +155,9 @@ int main(int argc, char* argv[]) @@ -161,6 +155,9 @@ int main(int argc, char* argv[])
161 usage(); 155 usage();
162 } 156 }
163 157
  158 + std::string fl_tmp = fl_out;
  159 + fl_tmp += ".tmp";
  160 +
164 try 161 try
165 { 162 {
166 QPDF file; 163 QPDF file;
@@ -198,13 +195,21 @@ int main(int argc, char* argv[]) @@ -198,13 +195,21 @@ int main(int argc, char* argv[])
198 fileinfo.replaceKey(it->first, elt); 195 fileinfo.replaceKey(it->first, elt);
199 } 196 }
200 } 197 }
201 - std::string fl_tmp = fl_out;  
202 - fl_tmp += ".tmp";  
203 QPDFWriter w(file, fl_tmp.c_str()); 198 QPDFWriter w(file, fl_tmp.c_str());
204 w.setStreamDataMode(QPDFWriter::s_preserve); 199 w.setStreamDataMode(QPDFWriter::s_preserve);
205 w.setLinearization(true); 200 w.setLinearization(true);
206 w.setStaticID(static_id); 201 w.setStaticID(static_id);
207 w.write(); 202 w.write();
  203 + }
  204 + catch (std::exception& e)
  205 + {
  206 + std::cerr << e.what() << std::endl;
  207 + exit(2);
  208 + }
  209 +
  210 + try
  211 + {
  212 + (void) unlink(fl_out);
208 QUtil::os_wrapper("rename " + fl_tmp + " " + std::string(fl_out), 213 QUtil::os_wrapper("rename " + fl_tmp + " " + std::string(fl_out),
209 rename(fl_tmp.c_str(), fl_out)); 214 rename(fl_tmp.c_str(), fl_out));
210 } 215 }
examples/pdf-npages.cc
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 #include <stdlib.h> 3 #include <stdlib.h>
4 4
5 #include <qpdf/QPDF.hh> 5 #include <qpdf/QPDF.hh>
  6 +#include <qpdf/QUtil.hh>
6 7
7 static char const* whoami = 0; 8 static char const* whoami = 0;
8 9
@@ -15,14 +16,8 @@ void usage() @@ -15,14 +16,8 @@ void usage()
15 16
16 int main(int argc, char* argv[]) 17 int main(int argc, char* argv[])
17 { 18 {
18 - if ((whoami = strrchr(argv[0], '/')) == NULL)  
19 - {  
20 - whoami = argv[0];  
21 - }  
22 - else  
23 - {  
24 - ++whoami;  
25 - } 19 + whoami = QUtil::getWhoami(argv[0]);
  20 +
26 // For libtool's sake.... 21 // For libtool's sake....
27 if (strncmp(whoami, "lt-", 3) == 0) 22 if (strncmp(whoami, "lt-", 3) == 0)
28 { 23 {
examples/qtest/mod-info.test
@@ -18,32 +18,38 @@ cleanup(); @@ -18,32 +18,38 @@ cleanup();
18 $td->runtest("usage #1", 18 $td->runtest("usage #1",
19 {$td->COMMAND => "$prg -in target.pdf"}, 19 {$td->COMMAND => "$prg -in target.pdf"},
20 {$td->FILE => "usage.out", 20 {$td->FILE => "usage.out",
21 - $td->EXIT_STATUS => 2}); 21 + $td->EXIT_STATUS => 2},
  22 + $td->NORMALIZE_NEWLINES);
22 23
23 $td->runtest("usage #2", 24 $td->runtest("usage #2",
24 {$td->COMMAND => "$prg -key abc -val def"}, 25 {$td->COMMAND => "$prg -key abc -val def"},
25 {$td->FILE => "usage.out", 26 {$td->FILE => "usage.out",
26 - $td->EXIT_STATUS => 2}); 27 + $td->EXIT_STATUS => 2},
  28 + $td->NORMALIZE_NEWLINES);
27 29
28 $td->runtest("usage #3", 30 $td->runtest("usage #3",
29 {$td->COMMAND => "$prg -key abc -val def abc"}, 31 {$td->COMMAND => "$prg -key abc -val def abc"},
30 {$td->FILE => "usage.out", 32 {$td->FILE => "usage.out",
31 - $td->EXIT_STATUS => 2}); 33 + $td->EXIT_STATUS => 2},
  34 + $td->NORMALIZE_NEWLINES);
32 35
33 $td->runtest("usage #4", 36 $td->runtest("usage #4",
34 {$td->COMMAND => "$prg -in source1.pdf -key /date -val 01/01/01 -val 12/12/12"}, 37 {$td->COMMAND => "$prg -in source1.pdf -key /date -val 01/01/01 -val 12/12/12"},
35 {$td->FILE => "usage.out", 38 {$td->FILE => "usage.out",
36 - $td->EXIT_STATUS => 2}); 39 + $td->EXIT_STATUS => 2},
  40 + $td->NORMALIZE_NEWLINES);
37 41
38 $td->runtest("dump #1", 42 $td->runtest("dump #1",
39 {$td->COMMAND => "$prg --dump -in files/source1.pdf"}, 43 {$td->COMMAND => "$prg --dump -in files/source1.pdf"},
40 {$td->FILE => "dump.out", 44 {$td->FILE => "dump.out",
41 - $td->EXIT_STATUS => 0}); 45 + $td->EXIT_STATUS => 0},
  46 + $td->NORMALIZE_NEWLINES);
42 47
43 $td->runtest("dump #2", 48 $td->runtest("dump #2",
44 {$td->COMMAND => "$prg --dump -in files/no-info.pdf"}, 49 {$td->COMMAND => "$prg --dump -in files/no-info.pdf"},
45 {$td->STRING => "", 50 {$td->STRING => "",
46 - $td->EXIT_STATUS => 0}); 51 + $td->EXIT_STATUS => 0},
  52 + $td->NORMALIZE_NEWLINES);
47 53
48 $td->runtest("dump #3", 54 $td->runtest("dump #3",
49 {$td->COMMAND => "$prg --dump -in files/empty-info.pdf"}, 55 {$td->COMMAND => "$prg --dump -in files/empty-info.pdf"},
include/qpdf/QUtil.hh
@@ -32,8 +32,12 @@ namespace QUtil @@ -32,8 +32,12 @@ namespace QUtil
32 32
33 char* copy_string(std::string const&); 33 char* copy_string(std::string const&);
34 34
35 - // Set stdout to binary mode 35 + // Set stdin, stdout to binary mode
36 void binary_stdout(); 36 void binary_stdout();
  37 + void binary_stdin();
  38 +
  39 + // May modify argv0
  40 + char* getWhoami(char* argv0);
37 41
38 // Get the value of an environment variable in a portable fashion. 42 // Get the value of an environment variable in a portable fashion.
39 // Returns true iff the variable is defined. If `value' is 43 // Returns true iff the variable is defined. If `value' is
libqpdf/QUtil.cc
@@ -114,6 +114,41 @@ QUtil::binary_stdout() @@ -114,6 +114,41 @@ QUtil::binary_stdout()
114 #endif 114 #endif
115 } 115 }
116 116
  117 +void
  118 +QUtil::binary_stdin()
  119 +{
  120 +#ifdef _WIN32
  121 + _setmode(_fileno(stdin), _O_BINARY);
  122 +#endif
  123 +}
  124 +
  125 +char*
  126 +QUtil::getWhoami(char* argv0)
  127 +{
  128 +#ifdef _WIN32
  129 + char pathsep = '\\';
  130 +#else
  131 + char pathsep = '/';
  132 +#endif
  133 + char* whoami = 0;
  134 + if ((whoami = strrchr(argv0, pathsep)) == NULL)
  135 + {
  136 + whoami = argv0;
  137 + }
  138 + else
  139 + {
  140 + ++whoami;
  141 + }
  142 +#ifdef _WIN32
  143 + if ((strlen(whoami) > 4) &&
  144 + (strcmp(whoami + strlen(whoami) - 4, ".exe") == 0))
  145 + {
  146 + whoami[strlen(whoami) - 4] = '\0';
  147 + }
  148 +#endif
  149 + return whoami;
  150 +}
  151 +
117 bool 152 bool
118 QUtil::get_env(std::string const& var, std::string* value) 153 QUtil::get_env(std::string const& var, std::string* value)
119 { 154 {
libtests/rc4.cc
@@ -7,12 +7,6 @@ @@ -7,12 +7,6 @@
7 #include <iostream> 7 #include <iostream>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 9
10 -#ifdef _WIN32  
11 -# include <io.h>  
12 -#else  
13 -# include <unistd.h>  
14 -#endif  
15 -  
16 int main(int argc, char* argv[]) 10 int main(int argc, char* argv[])
17 { 11 {
18 if (argc != 4) 12 if (argc != 4)
qpdf/qpdf.cc
@@ -4,10 +4,6 @@ @@ -4,10 +4,6 @@
4 #include <stdlib.h> 4 #include <stdlib.h>
5 #include <fcntl.h> 5 #include <fcntl.h>
6 6
7 -#ifdef _WIN32  
8 -# include <io.h>  
9 -#endif  
10 -  
11 #include <qpdf/QUtil.hh> 7 #include <qpdf/QUtil.hh>
12 #include <qpdf/QTC.hh> 8 #include <qpdf/QTC.hh>
13 #include <qpdf/Pl_StdioFile.hh> 9 #include <qpdf/Pl_StdioFile.hh>
@@ -20,7 +16,7 @@ @@ -20,7 +16,7 @@
20 static int const EXIT_ERROR = 2; 16 static int const EXIT_ERROR = 2;
21 static int const EXIT_WARNING = 3; 17 static int const EXIT_WARNING = 3;
22 18
23 -static char* whoami = 0; 19 +static char const* whoami = 0;
24 20
25 // Note: let's not be too noisy about documenting the fact that this 21 // Note: let's not be too noisy about documenting the fact that this
26 // software purposely fails to enforce the distinction between user 22 // software purposely fails to enforce the distinction between user
@@ -434,26 +430,8 @@ parse_encrypt_options( @@ -434,26 +430,8 @@ parse_encrypt_options(
434 430
435 int main(int argc, char* argv[]) 431 int main(int argc, char* argv[])
436 { 432 {
437 -#ifdef _WIN32  
438 - char pathsep = '\\';  
439 -#else  
440 - char pathsep = '/';  
441 -#endif  
442 - if ((whoami = strrchr(argv[0], pathsep)) == NULL)  
443 - {  
444 - whoami = argv[0];  
445 - }  
446 - else  
447 - {  
448 - ++whoami;  
449 - }  
450 -#ifdef _WIN32  
451 - if ((strlen(whoami) > 4) &&  
452 - (strcmp(whoami + strlen(whoami) - 4, ".exe") == 0))  
453 - {  
454 - whoami[strlen(whoami) - 4] = '\0';  
455 - }  
456 -#endif 433 + whoami = QUtil::getWhoami(argv[0]);
  434 +
457 // For libtool's sake.... 435 // For libtool's sake....
458 if (strncmp(whoami, "lt-", 3) == 0) 436 if (strncmp(whoami, "lt-", 3) == 0)
459 { 437 {
zlib-flate/zlib-flate.cc
@@ -7,11 +7,6 @@ @@ -7,11 +7,6 @@
7 #include <iostream> 7 #include <iostream>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 -#ifdef _WIN32  
11 -# include <io.h>  
12 -#else  
13 -# include <unistd.h>  
14 -#endif  
15 10
16 static char const* whoami = 0; 11 static char const* whoami = 0;
17 12
@@ -65,6 +60,7 @@ int main(int argc, char* argv[]) @@ -65,6 +60,7 @@ int main(int argc, char* argv[])
65 } 60 }
66 61
67 QUtil::binary_stdout(); 62 QUtil::binary_stdout();
  63 + QUtil::binary_stdin();
68 Pl_StdioFile* out = new Pl_StdioFile("stdout", stdout); 64 Pl_StdioFile* out = new Pl_StdioFile("stdout", stdout);
69 Pl_Flate* flate = new Pl_Flate("flate", out, action); 65 Pl_Flate* flate = new Pl_Flate("flate", out, action);
70 66
@@ -74,7 +70,7 @@ int main(int argc, char* argv[]) @@ -74,7 +70,7 @@ int main(int argc, char* argv[])
74 bool done = false; 70 bool done = false;
75 while (! done) 71 while (! done)
76 { 72 {
77 - int len = read(0, buf, sizeof(buf)); 73 + int len = fread(buf, 1, sizeof(buf), stdin);
78 if (len <= 0) 74 if (len <= 0)
79 { 75 {
80 done = true; 76 done = true;