aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2009-07-15 06:26:32 +0200
committerJay Berkenbilt <ejb@ql.org>2009-07-15 06:26:32 +0200
commitfe18385ffaa7e5b11db86af1e992c0fb1984609a (patch)
tree132e8e04227971809c592a680d0dc497a2c677a4
parent86f2d3449c651674b92b1249186375ad821f9017 (diff)
downloadqpdf-fe18385ffaa7e5b11db86af1e992c0fb1984609a.tar.zst
clean up windows portability code, make remaining test suite pass
git-svn-id: svn+q:///qpdf/trunk@686 71b93d88-0707-0410-a8cf-f5a4172ac649
-rw-r--r--examples/pdf-bookmarks.cc10
-rw-r--r--examples/pdf-mod-info.cc25
-rw-r--r--examples/pdf-npages.cc11
-rw-r--r--examples/qtest/mod-info.test18
-rw-r--r--include/qpdf/QUtil.hh6
-rw-r--r--libqpdf/QUtil.cc35
-rw-r--r--libtests/rc4.cc6
-rw-r--r--qpdf/qpdf.cc28
-rw-r--r--zlib-flate/zlib-flate.cc8
9 files changed, 77 insertions, 70 deletions
diff --git a/examples/pdf-bookmarks.cc b/examples/pdf-bookmarks.cc
index 4f878211..802cdbac 100644
--- a/examples/pdf-bookmarks.cc
+++ b/examples/pdf-bookmarks.cc
@@ -163,14 +163,8 @@ void extract_bookmarks(QPDFObjectHandle outlines, std::vector<int>& numbers)
int main(int argc, char* argv[])
{
- if ((whoami = strrchr(argv[0], '/')) == NULL)
- {
- whoami = argv[0];
- }
- else
- {
- ++whoami;
- }
+ whoami = QUtil::getWhoami(argv[0]);
+
// For libtool's sake....
if (strncmp(whoami, "lt-", 3) == 0)
{
diff --git a/examples/pdf-mod-info.cc b/examples/pdf-mod-info.cc
index 7401a571..4e851316 100644
--- a/examples/pdf-mod-info.cc
+++ b/examples/pdf-mod-info.cc
@@ -77,14 +77,8 @@ int main(int argc, char* argv[])
bool static_id = false;
std::map<std::string, std::string> Keys;
- if ((whoami = strrchr(argv[0], '/')) == NULL)
- {
- whoami = argv[0];
- }
- else
- {
- ++whoami;
- }
+ whoami = QUtil::getWhoami(argv[0]);
+
// For libtool's sake....
if (strncmp(whoami, "lt-", 3) == 0)
{
@@ -161,6 +155,9 @@ int main(int argc, char* argv[])
usage();
}
+ std::string fl_tmp = fl_out;
+ fl_tmp += ".tmp";
+
try
{
QPDF file;
@@ -198,13 +195,21 @@ int main(int argc, char* argv[])
fileinfo.replaceKey(it->first, elt);
}
}
- std::string fl_tmp = fl_out;
- fl_tmp += ".tmp";
QPDFWriter w(file, fl_tmp.c_str());
w.setStreamDataMode(QPDFWriter::s_preserve);
w.setLinearization(true);
w.setStaticID(static_id);
w.write();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ exit(2);
+ }
+
+ try
+ {
+ (void) unlink(fl_out);
QUtil::os_wrapper("rename " + fl_tmp + " " + std::string(fl_out),
rename(fl_tmp.c_str(), fl_out));
}
diff --git a/examples/pdf-npages.cc b/examples/pdf-npages.cc
index b5ef6479..c62d33de 100644
--- a/examples/pdf-npages.cc
+++ b/examples/pdf-npages.cc
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <qpdf/QPDF.hh>
+#include <qpdf/QUtil.hh>
static char const* whoami = 0;
@@ -15,14 +16,8 @@ void usage()
int main(int argc, char* argv[])
{
- if ((whoami = strrchr(argv[0], '/')) == NULL)
- {
- whoami = argv[0];
- }
- else
- {
- ++whoami;
- }
+ whoami = QUtil::getWhoami(argv[0]);
+
// For libtool's sake....
if (strncmp(whoami, "lt-", 3) == 0)
{
diff --git a/examples/qtest/mod-info.test b/examples/qtest/mod-info.test
index 29a72475..90db6276 100644
--- a/examples/qtest/mod-info.test
+++ b/examples/qtest/mod-info.test
@@ -18,32 +18,38 @@ cleanup();
$td->runtest("usage #1",
{$td->COMMAND => "$prg -in target.pdf"},
{$td->FILE => "usage.out",
- $td->EXIT_STATUS => 2});
+ $td->EXIT_STATUS => 2},
+ $td->NORMALIZE_NEWLINES);
$td->runtest("usage #2",
{$td->COMMAND => "$prg -key abc -val def"},
{$td->FILE => "usage.out",
- $td->EXIT_STATUS => 2});
+ $td->EXIT_STATUS => 2},
+ $td->NORMALIZE_NEWLINES);
$td->runtest("usage #3",
{$td->COMMAND => "$prg -key abc -val def abc"},
{$td->FILE => "usage.out",
- $td->EXIT_STATUS => 2});
+ $td->EXIT_STATUS => 2},
+ $td->NORMALIZE_NEWLINES);
$td->runtest("usage #4",
{$td->COMMAND => "$prg -in source1.pdf -key /date -val 01/01/01 -val 12/12/12"},
{$td->FILE => "usage.out",
- $td->EXIT_STATUS => 2});
+ $td->EXIT_STATUS => 2},
+ $td->NORMALIZE_NEWLINES);
$td->runtest("dump #1",
{$td->COMMAND => "$prg --dump -in files/source1.pdf"},
{$td->FILE => "dump.out",
- $td->EXIT_STATUS => 0});
+ $td->EXIT_STATUS => 0},
+ $td->NORMALIZE_NEWLINES);
$td->runtest("dump #2",
{$td->COMMAND => "$prg --dump -in files/no-info.pdf"},
{$td->STRING => "",
- $td->EXIT_STATUS => 0});
+ $td->EXIT_STATUS => 0},
+ $td->NORMALIZE_NEWLINES);
$td->runtest("dump #3",
{$td->COMMAND => "$prg --dump -in files/empty-info.pdf"},
diff --git a/include/qpdf/QUtil.hh b/include/qpdf/QUtil.hh
index f5ff3323..a70db0bd 100644
--- a/include/qpdf/QUtil.hh
+++ b/include/qpdf/QUtil.hh
@@ -32,8 +32,12 @@ namespace QUtil
char* copy_string(std::string const&);
- // Set stdout to binary mode
+ // Set stdin, stdout to binary mode
void binary_stdout();
+ void binary_stdin();
+
+ // May modify argv0
+ char* getWhoami(char* argv0);
// Get the value of an environment variable in a portable fashion.
// Returns true iff the variable is defined. If `value' is
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc
index c3df35da..a000d82e 100644
--- a/libqpdf/QUtil.cc
+++ b/libqpdf/QUtil.cc
@@ -114,6 +114,41 @@ QUtil::binary_stdout()
#endif
}
+void
+QUtil::binary_stdin()
+{
+#ifdef _WIN32
+ _setmode(_fileno(stdin), _O_BINARY);
+#endif
+}
+
+char*
+QUtil::getWhoami(char* argv0)
+{
+#ifdef _WIN32
+ char pathsep = '\\';
+#else
+ char pathsep = '/';
+#endif
+ char* whoami = 0;
+ if ((whoami = strrchr(argv0, pathsep)) == NULL)
+ {
+ whoami = argv0;
+ }
+ else
+ {
+ ++whoami;
+ }
+#ifdef _WIN32
+ if ((strlen(whoami) > 4) &&
+ (strcmp(whoami + strlen(whoami) - 4, ".exe") == 0))
+ {
+ whoami[strlen(whoami) - 4] = '\0';
+ }
+#endif
+ return whoami;
+}
+
bool
QUtil::get_env(std::string const& var, std::string* value)
{
diff --git a/libtests/rc4.cc b/libtests/rc4.cc
index 34d779ad..195740ec 100644
--- a/libtests/rc4.cc
+++ b/libtests/rc4.cc
@@ -7,12 +7,6 @@
#include <iostream>
#include <stdlib.h>
-#ifdef _WIN32
-# include <io.h>
-#else
-# include <unistd.h>
-#endif
-
int main(int argc, char* argv[])
{
if (argc != 4)
diff --git a/qpdf/qpdf.cc b/qpdf/qpdf.cc
index 7bf41936..12053cbe 100644
--- a/qpdf/qpdf.cc
+++ b/qpdf/qpdf.cc
@@ -4,10 +4,6 @@
#include <stdlib.h>
#include <fcntl.h>
-#ifdef _WIN32
-# include <io.h>
-#endif
-
#include <qpdf/QUtil.hh>
#include <qpdf/QTC.hh>
#include <qpdf/Pl_StdioFile.hh>
@@ -20,7 +16,7 @@
static int const EXIT_ERROR = 2;
static int const EXIT_WARNING = 3;
-static char* whoami = 0;
+static char const* whoami = 0;
// Note: let's not be too noisy about documenting the fact that this
// software purposely fails to enforce the distinction between user
@@ -434,26 +430,8 @@ parse_encrypt_options(
int main(int argc, char* argv[])
{
-#ifdef _WIN32
- char pathsep = '\\';
-#else
- char pathsep = '/';
-#endif
- if ((whoami = strrchr(argv[0], pathsep)) == NULL)
- {
- whoami = argv[0];
- }
- else
- {
- ++whoami;
- }
-#ifdef _WIN32
- if ((strlen(whoami) > 4) &&
- (strcmp(whoami + strlen(whoami) - 4, ".exe") == 0))
- {
- whoami[strlen(whoami) - 4] = '\0';
- }
-#endif
+ whoami = QUtil::getWhoami(argv[0]);
+
// For libtool's sake....
if (strncmp(whoami, "lt-", 3) == 0)
{
diff --git a/zlib-flate/zlib-flate.cc b/zlib-flate/zlib-flate.cc
index b5ba8e5e..67cd925d 100644
--- a/zlib-flate/zlib-flate.cc
+++ b/zlib-flate/zlib-flate.cc
@@ -7,11 +7,6 @@
#include <iostream>
#include <stdlib.h>
#include <fcntl.h>
-#ifdef _WIN32
-# include <io.h>
-#else
-# include <unistd.h>
-#endif
static char const* whoami = 0;
@@ -65,6 +60,7 @@ int main(int argc, char* argv[])
}
QUtil::binary_stdout();
+ QUtil::binary_stdin();
Pl_StdioFile* out = new Pl_StdioFile("stdout", stdout);
Pl_Flate* flate = new Pl_Flate("flate", out, action);
@@ -74,7 +70,7 @@ int main(int argc, char* argv[])
bool done = false;
while (! done)
{
- int len = read(0, buf, sizeof(buf));
+ int len = fread(buf, 1, sizeof(buf), stdin);
if (len <= 0)
{
done = true;