mirror of
https://github.com/qpdf/qpdf.git
synced 2024-11-13 08:16:29 +00:00
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:
parent
bcb10021d7
commit
b36f62a326
@ -183,7 +183,21 @@ extern "C" {
|
|||||||
QPDF_ERROR_CODE qpdf_read(qpdf_data qpdf, char const* filename,
|
QPDF_ERROR_CODE qpdf_read(qpdf_data qpdf, char const* filename,
|
||||||
char const* password);
|
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. */
|
/* Return the version of the PDF file. */
|
||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
|
@ -28,7 +28,9 @@ struct _qpdf_data
|
|||||||
std::string tmp_string;
|
std::string tmp_string;
|
||||||
|
|
||||||
// Parameters for functions we call
|
// Parameters for functions we call
|
||||||
char const* filename;
|
char const* filename; // or description
|
||||||
|
char const* buffer;
|
||||||
|
unsigned long size;
|
||||||
char const* password;
|
char const* password;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -50,6 +52,13 @@ static void call_read(qpdf_data qpdf)
|
|||||||
qpdf->qpdf->processFile(qpdf->filename, qpdf->password);
|
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
|
// must set qpdf->filename
|
||||||
static void call_init_write(qpdf_data qpdf)
|
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;
|
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)
|
char const* qpdf_get_pdf_version(qpdf_data qpdf)
|
||||||
{
|
{
|
||||||
QTC::TC("qpdf", "qpdf-c called qpdf_get_pdf_version");
|
QTC::TC("qpdf", "qpdf-c called qpdf_get_pdf_version");
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
<!ENTITY mdash "—">
|
<!ENTITY mdash "—">
|
||||||
<!ENTITY ndash "–">
|
<!ENTITY ndash "–">
|
||||||
<!ENTITY nbsp " ">
|
<!ENTITY nbsp " ">
|
||||||
<!ENTITY swversion "2.2.1">
|
<!ENTITY swversion "2.2.2">
|
||||||
<!ENTITY lastreleased "October 1, 2010">
|
<!ENTITY lastreleased "October 4, 2010">
|
||||||
]>
|
]>
|
||||||
<book>
|
<book>
|
||||||
<bookinfo>
|
<bookinfo>
|
||||||
@ -2074,6 +2074,21 @@ print "\n";
|
|||||||
<filename>ChangeLog</filename> in the source distribution.
|
<filename>ChangeLog</filename> in the source distribution.
|
||||||
</para>
|
</para>
|
||||||
<variablelist>
|
<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>
|
<varlistentry>
|
||||||
<term>2.2.1: October 1, 2010</term>
|
<term>2.2.1: October 1, 2010</term>
|
||||||
<listitem>
|
<listitem>
|
||||||
|
@ -3,7 +3,9 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
static char* whoami = 0;
|
||||||
static qpdf_data qpdf = 0;
|
static qpdf_data qpdf = 0;
|
||||||
|
|
||||||
static void report_errors()
|
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,
|
static void test01(char const* infile,
|
||||||
char const* password,
|
char const* password,
|
||||||
char const* outfile,
|
char const* outfile,
|
||||||
@ -135,7 +188,10 @@ static void test06(char const* infile,
|
|||||||
char const* outfile,
|
char const* outfile,
|
||||||
char const* outfile2)
|
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_init_write(qpdf, outfile);
|
||||||
qpdf_set_static_ID(qpdf, QPDF_TRUE);
|
qpdf_set_static_ID(qpdf, QPDF_TRUE);
|
||||||
qpdf_set_object_stream_mode(qpdf, qpdf_o_generate);
|
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[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
char* whoami = 0;
|
|
||||||
char* p = 0;
|
char* p = 0;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
char const* infile = 0;
|
char const* infile = 0;
|
||||||
|
@ -186,3 +186,4 @@ QPDFObjectHandle append page contents 0
|
|||||||
QPDF_Stream getRawStreamData 0
|
QPDF_Stream getRawStreamData 0
|
||||||
QPDF_Stream getStreamData 0
|
QPDF_Stream getStreamData 0
|
||||||
QPDF_Stream expand filter abbreviation 0
|
QPDF_Stream expand filter abbreviation 0
|
||||||
|
qpdf-c called qpdf_read_memory 0
|
||||||
|
Loading…
Reference in New Issue
Block a user