2012-07-28 23:27:23 +00:00
|
|
|
// Copyright (c) 2005-2012 Jay Berkenbilt
|
2008-04-29 12:55:25 +00:00
|
|
|
//
|
|
|
|
// This file is part of qpdf. This software may be distributed under
|
|
|
|
// the terms of version 2 of the Artistic License which may be found
|
|
|
|
// in the source distribution. It is provided "as is" without express
|
|
|
|
// or implied warranty.
|
|
|
|
|
|
|
|
#ifndef __QUTIL_HH__
|
|
|
|
#define __QUTIL_HH__
|
|
|
|
|
2009-10-19 20:17:14 +00:00
|
|
|
#include <qpdf/DLL.h>
|
2012-06-20 15:20:57 +00:00
|
|
|
#include <qpdf/Types.h>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <string>
|
|
|
|
#include <list>
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
namespace QUtil
|
|
|
|
{
|
|
|
|
// This is a collection of useful utility functions that don't
|
|
|
|
// really go anywhere else.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2012-06-20 15:20:57 +00:00
|
|
|
std::string int_to_string(long long, int length = 0);
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string double_to_string(double, int decimal_places = 0);
|
|
|
|
|
2012-06-21 23:32:21 +00:00
|
|
|
QPDF_DLL
|
|
|
|
long long string_to_ll(char const* str);
|
|
|
|
|
2009-09-26 18:36:04 +00:00
|
|
|
// Throw std::runtime_error with a string formed by appending to
|
|
|
|
// "description: " the standard string corresponding to the
|
|
|
|
// current value of errno.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-26 18:36:04 +00:00
|
|
|
void throw_system_error(std::string const& description);
|
|
|
|
|
|
|
|
// The status argument is assumed to be the return value of a
|
|
|
|
// standard library call that sets errno when it fails. If status
|
|
|
|
// is -1, convert the current value of errno to a
|
|
|
|
// std::runtime_error that includes the standard error string.
|
|
|
|
// Otherwise, return status.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-26 18:36:04 +00:00
|
|
|
int os_wrapper(std::string const& description, int status);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2009-09-26 18:36:04 +00:00
|
|
|
// The FILE* argument is assumed to be the return of fopen. If
|
|
|
|
// null, throw std::runtime_error. Otherwise, return the FILE*
|
|
|
|
// argument.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-26 18:36:04 +00:00
|
|
|
FILE* fopen_wrapper(std::string const&, FILE*);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-06-20 15:20:57 +00:00
|
|
|
// Wrap around off_t versions of fseek and ftell if available
|
|
|
|
QPDF_DLL
|
2012-06-27 03:09:21 +00:00
|
|
|
int seek(FILE* stream, qpdf_offset_t offset, int whence);
|
2012-06-20 15:20:57 +00:00
|
|
|
QPDF_DLL
|
2012-06-27 03:09:21 +00:00
|
|
|
qpdf_offset_t tell(FILE* stream);
|
2012-06-20 15:20:57 +00:00
|
|
|
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
char* copy_string(std::string const&);
|
|
|
|
|
2009-07-15 04:26:32 +00:00
|
|
|
// Set stdin, stdout to binary mode
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-07-15 03:47:44 +00:00
|
|
|
void binary_stdout();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-07-15 04:26:32 +00:00
|
|
|
void binary_stdin();
|
2009-10-21 02:30:15 +00:00
|
|
|
// Set stdout to line buffered
|
|
|
|
QPDF_DLL
|
|
|
|
void setLineBuf(FILE*);
|
|
|
|
|
2009-07-15 04:26:32 +00:00
|
|
|
|
|
|
|
// May modify argv0
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-07-15 04:26:32 +00:00
|
|
|
char* getWhoami(char* argv0);
|
2009-07-15 03:47:44 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Get the value of an environment variable in a portable fashion.
|
|
|
|
// Returns true iff the variable is defined. If `value' is
|
|
|
|
// non-null, initializes it with the value of the variable.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
bool get_env(std::string const& var, std::string* value = 0);
|
|
|
|
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-07-12 22:52:13 +00:00
|
|
|
time_t get_current_time();
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Return a string containing the byte representation of the UTF-8
|
|
|
|
// encoding for the unicode value passed in.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string toUTF8(unsigned long uval);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // __QUTIL_HH__
|