Commit fe18385ffaa7e5b11db86af1e992c0fb1984609a
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
Showing
9 changed files
with
77 additions
and
70 deletions
examples/pdf-bookmarks.cc
| ... | ... | @@ -163,14 +163,8 @@ void extract_bookmarks(QPDFObjectHandle outlines, std::vector<int>& numbers) |
| 163 | 163 | |
| 164 | 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 | 168 | // For libtool's sake.... |
| 175 | 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 | 77 | bool static_id = false; |
| 78 | 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 | 82 | // For libtool's sake.... |
| 89 | 83 | if (strncmp(whoami, "lt-", 3) == 0) |
| 90 | 84 | { |
| ... | ... | @@ -161,6 +155,9 @@ int main(int argc, char* argv[]) |
| 161 | 155 | usage(); |
| 162 | 156 | } |
| 163 | 157 | |
| 158 | + std::string fl_tmp = fl_out; | |
| 159 | + fl_tmp += ".tmp"; | |
| 160 | + | |
| 164 | 161 | try |
| 165 | 162 | { |
| 166 | 163 | QPDF file; |
| ... | ... | @@ -198,13 +195,21 @@ int main(int argc, char* argv[]) |
| 198 | 195 | fileinfo.replaceKey(it->first, elt); |
| 199 | 196 | } |
| 200 | 197 | } |
| 201 | - std::string fl_tmp = fl_out; | |
| 202 | - fl_tmp += ".tmp"; | |
| 203 | 198 | QPDFWriter w(file, fl_tmp.c_str()); |
| 204 | 199 | w.setStreamDataMode(QPDFWriter::s_preserve); |
| 205 | 200 | w.setLinearization(true); |
| 206 | 201 | w.setStaticID(static_id); |
| 207 | 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 | 213 | QUtil::os_wrapper("rename " + fl_tmp + " " + std::string(fl_out), |
| 209 | 214 | rename(fl_tmp.c_str(), fl_out)); |
| 210 | 215 | } | ... | ... |
examples/pdf-npages.cc
| ... | ... | @@ -3,6 +3,7 @@ |
| 3 | 3 | #include <stdlib.h> |
| 4 | 4 | |
| 5 | 5 | #include <qpdf/QPDF.hh> |
| 6 | +#include <qpdf/QUtil.hh> | |
| 6 | 7 | |
| 7 | 8 | static char const* whoami = 0; |
| 8 | 9 | |
| ... | ... | @@ -15,14 +16,8 @@ void usage() |
| 15 | 16 | |
| 16 | 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 | 21 | // For libtool's sake.... |
| 27 | 22 | if (strncmp(whoami, "lt-", 3) == 0) |
| 28 | 23 | { | ... | ... |
examples/qtest/mod-info.test
| ... | ... | @@ -18,32 +18,38 @@ cleanup(); |
| 18 | 18 | $td->runtest("usage #1", |
| 19 | 19 | {$td->COMMAND => "$prg -in target.pdf"}, |
| 20 | 20 | {$td->FILE => "usage.out", |
| 21 | - $td->EXIT_STATUS => 2}); | |
| 21 | + $td->EXIT_STATUS => 2}, | |
| 22 | + $td->NORMALIZE_NEWLINES); | |
| 22 | 23 | |
| 23 | 24 | $td->runtest("usage #2", |
| 24 | 25 | {$td->COMMAND => "$prg -key abc -val def"}, |
| 25 | 26 | {$td->FILE => "usage.out", |
| 26 | - $td->EXIT_STATUS => 2}); | |
| 27 | + $td->EXIT_STATUS => 2}, | |
| 28 | + $td->NORMALIZE_NEWLINES); | |
| 27 | 29 | |
| 28 | 30 | $td->runtest("usage #3", |
| 29 | 31 | {$td->COMMAND => "$prg -key abc -val def abc"}, |
| 30 | 32 | {$td->FILE => "usage.out", |
| 31 | - $td->EXIT_STATUS => 2}); | |
| 33 | + $td->EXIT_STATUS => 2}, | |
| 34 | + $td->NORMALIZE_NEWLINES); | |
| 32 | 35 | |
| 33 | 36 | $td->runtest("usage #4", |
| 34 | 37 | {$td->COMMAND => "$prg -in source1.pdf -key /date -val 01/01/01 -val 12/12/12"}, |
| 35 | 38 | {$td->FILE => "usage.out", |
| 36 | - $td->EXIT_STATUS => 2}); | |
| 39 | + $td->EXIT_STATUS => 2}, | |
| 40 | + $td->NORMALIZE_NEWLINES); | |
| 37 | 41 | |
| 38 | 42 | $td->runtest("dump #1", |
| 39 | 43 | {$td->COMMAND => "$prg --dump -in files/source1.pdf"}, |
| 40 | 44 | {$td->FILE => "dump.out", |
| 41 | - $td->EXIT_STATUS => 0}); | |
| 45 | + $td->EXIT_STATUS => 0}, | |
| 46 | + $td->NORMALIZE_NEWLINES); | |
| 42 | 47 | |
| 43 | 48 | $td->runtest("dump #2", |
| 44 | 49 | {$td->COMMAND => "$prg --dump -in files/no-info.pdf"}, |
| 45 | 50 | {$td->STRING => "", |
| 46 | - $td->EXIT_STATUS => 0}); | |
| 51 | + $td->EXIT_STATUS => 0}, | |
| 52 | + $td->NORMALIZE_NEWLINES); | |
| 47 | 53 | |
| 48 | 54 | $td->runtest("dump #3", |
| 49 | 55 | {$td->COMMAND => "$prg --dump -in files/empty-info.pdf"}, | ... | ... |
include/qpdf/QUtil.hh
| ... | ... | @@ -32,8 +32,12 @@ namespace QUtil |
| 32 | 32 | |
| 33 | 33 | char* copy_string(std::string const&); |
| 34 | 34 | |
| 35 | - // Set stdout to binary mode | |
| 35 | + // Set stdin, stdout to binary mode | |
| 36 | 36 | void binary_stdout(); |
| 37 | + void binary_stdin(); | |
| 38 | + | |
| 39 | + // May modify argv0 | |
| 40 | + char* getWhoami(char* argv0); | |
| 37 | 41 | |
| 38 | 42 | // Get the value of an environment variable in a portable fashion. |
| 39 | 43 | // Returns true iff the variable is defined. If `value' is | ... | ... |
libqpdf/QUtil.cc
| ... | ... | @@ -114,6 +114,41 @@ QUtil::binary_stdout() |
| 114 | 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 | 152 | bool |
| 118 | 153 | QUtil::get_env(std::string const& var, std::string* value) |
| 119 | 154 | { | ... | ... |
libtests/rc4.cc
qpdf/qpdf.cc
| ... | ... | @@ -4,10 +4,6 @@ |
| 4 | 4 | #include <stdlib.h> |
| 5 | 5 | #include <fcntl.h> |
| 6 | 6 | |
| 7 | -#ifdef _WIN32 | |
| 8 | -# include <io.h> | |
| 9 | -#endif | |
| 10 | - | |
| 11 | 7 | #include <qpdf/QUtil.hh> |
| 12 | 8 | #include <qpdf/QTC.hh> |
| 13 | 9 | #include <qpdf/Pl_StdioFile.hh> |
| ... | ... | @@ -20,7 +16,7 @@ |
| 20 | 16 | static int const EXIT_ERROR = 2; |
| 21 | 17 | static int const EXIT_WARNING = 3; |
| 22 | 18 | |
| 23 | -static char* whoami = 0; | |
| 19 | +static char const* whoami = 0; | |
| 24 | 20 | |
| 25 | 21 | // Note: let's not be too noisy about documenting the fact that this |
| 26 | 22 | // software purposely fails to enforce the distinction between user |
| ... | ... | @@ -434,26 +430,8 @@ parse_encrypt_options( |
| 434 | 430 | |
| 435 | 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 | 435 | // For libtool's sake.... |
| 458 | 436 | if (strncmp(whoami, "lt-", 3) == 0) |
| 459 | 437 | { | ... | ... |
zlib-flate/zlib-flate.cc
| ... | ... | @@ -7,11 +7,6 @@ |
| 7 | 7 | #include <iostream> |
| 8 | 8 | #include <stdlib.h> |
| 9 | 9 | #include <fcntl.h> |
| 10 | -#ifdef _WIN32 | |
| 11 | -# include <io.h> | |
| 12 | -#else | |
| 13 | -# include <unistd.h> | |
| 14 | -#endif | |
| 15 | 10 | |
| 16 | 11 | static char const* whoami = 0; |
| 17 | 12 | |
| ... | ... | @@ -65,6 +60,7 @@ int main(int argc, char* argv[]) |
| 65 | 60 | } |
| 66 | 61 | |
| 67 | 62 | QUtil::binary_stdout(); |
| 63 | + QUtil::binary_stdin(); | |
| 68 | 64 | Pl_StdioFile* out = new Pl_StdioFile("stdout", stdout); |
| 69 | 65 | Pl_Flate* flate = new Pl_Flate("flate", out, action); |
| 70 | 66 | |
| ... | ... | @@ -74,7 +70,7 @@ int main(int argc, char* argv[]) |
| 74 | 70 | bool done = false; |
| 75 | 71 | while (! done) |
| 76 | 72 | { |
| 77 | - int len = read(0, buf, sizeof(buf)); | |
| 73 | + int len = fread(buf, 1, sizeof(buf), stdin); | |
| 78 | 74 | if (len <= 0) |
| 79 | 75 | { |
| 80 | 76 | done = true; | ... | ... |