add qpdf_read_memory to C API

git-svn-id: svn+q:///qpdf/trunk@1044 71b93d88-0707-0410-a8cf-f5a4172ac649
This commit is contained in:
Jay Berkenbilt 2010-10-04 15:24:10 +00:00
parent bcb10021d7
commit b36f62a326
5 changed files with 116 additions and 6 deletions

View File

@ -183,7 +183,21 @@ extern "C" {
QPDF_ERROR_CODE qpdf_read(qpdf_data qpdf, char const* filename,
char const* password);
/* Read functions below must be called after qpdf_read. */
/* Calling qpdf_read_memory causes processMemoryFile to be called
* in the C++ API. Otherwise, it behaves in the same way as
* qpdf_read. The description argument will be used in place of
* the file name in any error or warning messages generated by the
* library.
*/
QPDF_DLL
QPDF_ERROR_CODE qpdf_read_memory(qpdf_data qpdf,
char const* description,
char const* buffer,
unsigned long size,
char const* password);
/* Read functions below must be called after qpdf_read or
* qpdf_read_memory. */
/* Return the version of the PDF file. */
QPDF_DLL

View File

@ -28,7 +28,9 @@ struct _qpdf_data
std::string tmp_string;
// Parameters for functions we call
char const* filename;
char const* filename; // or description
char const* buffer;
unsigned long size;
char const* password;
};
@ -50,6 +52,13 @@ static void call_read(qpdf_data qpdf)
qpdf->qpdf->processFile(qpdf->filename, qpdf->password);
}
// must set qpdf->filename, qpdf->buffer, qpdf->size, and qpdf->password
static void call_read_memory(qpdf_data qpdf)
{
qpdf->qpdf->processMemoryFile(qpdf->filename, qpdf->buffer,
qpdf->size, qpdf->password);
}
// must set qpdf->filename
static void call_init_write(qpdf_data qpdf)
{
@ -244,6 +253,22 @@ QPDF_ERROR_CODE qpdf_read(qpdf_data qpdf, char const* filename,
return status;
}
QPDF_ERROR_CODE qpdf_read_memory(qpdf_data qpdf,
char const* description,
char const* buffer,
unsigned long size,
char const* password)
{
QPDF_ERROR_CODE status = QPDF_SUCCESS;
qpdf->filename = description;
qpdf->buffer = buffer;
qpdf->size = size;
qpdf->password = password;
status = trap_errors(qpdf, &call_read_memory);
QTC::TC("qpdf", "qpdf-c called qpdf_read_memory", status);
return status;
}
char const* qpdf_get_pdf_version(qpdf_data qpdf)
{
QTC::TC("qpdf", "qpdf-c called qpdf_get_pdf_version");

View File

@ -5,8 +5,8 @@
<!ENTITY mdash "&#x2014;">
<!ENTITY ndash "&#x2013;">
<!ENTITY nbsp "&#xA0;">
<!ENTITY swversion "2.2.1">
<!ENTITY lastreleased "October 1, 2010">
<!ENTITY swversion "2.2.2">
<!ENTITY lastreleased "October 4, 2010">
]>
<book>
<bookinfo>
@ -2074,6 +2074,21 @@ print "\n";
<filename>ChangeLog</filename> in the source distribution.
</para>
<variablelist>
<varlistentry>
<term>2.2.2: October 4, 2010</term>
<listitem>
<itemizedlist>
<listitem>
<para>
Add new function <function>qpdf_read_memory</function>
to the C API to call
<function>QPDF::processMemoryFile</function>. This was an
omission in qpdf 2.2.1.
</para>
</listitem>
</itemizedlist>
</listitem>
</varlistentry>
<varlistentry>
<term>2.2.1: October 1, 2010</term>
<listitem>

View File

@ -3,7 +3,9 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
static char* whoami = 0;
static qpdf_data qpdf = 0;
static void report_errors()
@ -42,6 +44,57 @@ static void report_errors()
}
}
static void read_file_into_memory(char const* filename,
char** buf, unsigned long* size)
{
char* buf_p = 0;
FILE* f = NULL;
size_t bytes_read = 0;
size_t len = 0;
f = fopen(filename, "rb");
if (f == NULL)
{
fprintf(stderr, "%s: unable to open %s: %s\n",
whoami, filename, strerror(errno));
exit(2);
}
fseek(f, 0, SEEK_END);
*size = (unsigned long) ftell(f);
fseek(f, 0, SEEK_SET);
*buf = malloc(*size);
if (*buf == NULL)
{
fprintf(stderr, "%s: unable to allocate %lu bytes\n",
whoami, *size);
exit(2);
}
buf_p = *buf;
bytes_read = 0;
len = 0;
while ((len = fread(buf_p + bytes_read, 1, *size - bytes_read, f)) > 0)
{
bytes_read += len;
}
if (bytes_read != *size)
{
if (ferror(f))
{
fprintf(stderr, "%s: failure reading file %s into memory:",
whoami, filename);
}
else
{
fprintf(stderr, "%s: premature EOF reading file %s:",
whoami, filename);
}
fprintf(stderr, " read %lu, wanted %lu\n",
(unsigned long) bytes_read, (unsigned long) size);
exit(2);
}
fclose(f);
}
static void test01(char const* infile,
char const* password,
char const* outfile,
@ -135,7 +188,10 @@ static void test06(char const* infile,
char const* outfile,
char const* outfile2)
{
qpdf_read(qpdf, infile, password);
char* buf = NULL;
unsigned long size = 0;
read_file_into_memory(infile, &buf, &size);
qpdf_read_memory(qpdf, infile, buf, size, password);
qpdf_init_write(qpdf, outfile);
qpdf_set_static_ID(qpdf, QPDF_TRUE);
qpdf_set_object_stream_mode(qpdf, qpdf_o_generate);
@ -271,7 +327,6 @@ static void test15(char const* infile,
int main(int argc, char* argv[])
{
char* whoami = 0;
char* p = 0;
int n = 0;
char const* infile = 0;

View File

@ -186,3 +186,4 @@ QPDFObjectHandle append page contents 0
QPDF_Stream getRawStreamData 0
QPDF_Stream getStreamData 0
QPDF_Stream expand filter abbreviation 0
qpdf-c called qpdf_read_memory 0