~jscott/pdfpartition

6900e933964585a6ba824ac4330992cbb5ef78fd — John Scott 5 months ago 02928be master
Use poppler_document_new_from_gfile() instead.

Apparently file:// URIs are not portable.
2 files changed, 14 insertions(+), 46 deletions(-)

M main.c
M meson.build
M main.c => main.c +12 -44
@@ 4,6 4,7 @@
#include <cairo-pdf.h>
#include <errno.h>
#include <fcntl.h>
#include <gio/gio.h>
#include <glib.h>
#include <locale.h>
#include <poppler.h>


@@ 14,31 15,6 @@
#include <sys/stat.h>
#include <unistd.h>

static int asprintf(char *str[restrict static 1], const char fmt[restrict static 1], ...) {
	FILE *const memstream = open_memstream(str, &(size_t){0});
	if(!memstream) {
		return -1;
	}

	va_list list;
	va_start(list, fmt);
	int n = vfprintf(memstream, fmt, list);
	va_end(list);
	if(n < 0) {
		const int save = errno;
		if(fclose(memstream) != EOF) {
			free(*str);
		}
		errno = save;
		return n;
	}

	if(fclose(memstream) == EOF) {
		return -1;
	}
	return n;
}

G_GNUC_ALLOC_SIZE2(2, 3) G_GNUC_WARN_UNUSED_RESULT
static void *reallocarray(void *p, size_t m, size_t n) {
	if(n && m > SIZE_MAX / n) {


@@ 75,25 51,18 @@ int main(int argc, char *argv[static argc+1]) {
		exit(EXIT_FAILURE);
	}


	char *urione, *uritwo;
	if(asprintf(&urione, "file://%s", argv[0]) < 0) {
		perror("Failed to construct URI");
		exit(EXIT_FAILURE);
	}
	if(asprintf(&uritwo, "file://%s", argv[1]) < 0) {
		perror("Failed to construct URI");
		goto endurione;
	}
	GFile *fileone, *filetwo;
	fileone = g_file_new_for_path(argv[0]);
	filetwo = g_file_new_for_path(argv[1]);

	GError *err = NULL;
	PopplerDocument *const docone = poppler_document_new_from_file(urione, NULL, &err);
	PopplerDocument *const docone = poppler_document_new_from_gfile(fileone, NULL, NULL, &err);
	if(!docone) {
		fprintf(stderr, "Failed to read %s: %s\n", argv[0], err->message);
		g_error_free(err);
		goto enduritwo;
		goto endfiletwo;
	}
	PopplerDocument *const doctwo = poppler_document_new_from_file(uritwo, NULL, &err);
	PopplerDocument *const doctwo = poppler_document_new_from_gfile(filetwo, NULL, NULL, &err);
	if(!doctwo) {
		fprintf(stderr, "Failed to read %s: %s\n", argv[1], err->message);
		g_error_free(err);


@@ 175,8 144,8 @@ int main(int argc, char *argv[static argc+1]) {
	free(maindocpages);
	g_object_unref(doctwo);
	g_object_unref(docone);
	free(uritwo);
	free(urione);
	g_object_unref(filetwo);
	g_object_unref(fileone);
	exit(EXIT_SUCCESS);

endcontext:


@@ 191,9 160,8 @@ enddoctwo:
	g_object_unref(doctwo);
enddocone:
	g_object_unref(docone);
enduritwo:
	free(uritwo);
endurione:
	free(urione);
endfiletwo:
	g_object_unref(filetwo);
	g_object_unref(fileone);
	exit(EXIT_FAILURE);
}

M meson.build => meson.build +2 -2
@@ 16,7 16,6 @@ endif

posix = ['-D_POSIX_C_SOURCE=200809L']
assert(cc.has_function('getopt', args: posix, prefix: '#include <unistd.h>'))
assert(cc.has_function('open_memstream', args: posix, prefix: '#include <stdio.h>'))

cairo = dependency('cairo-pdf')
if not cc.compiles('''


@@ 28,6 27,7 @@ if not cc.compiles('''
	error('Cairo PDF support is required')
endif

gio = dependency('gio-2.0')
glib = dependency('glib-2.0')
poppler = dependency('poppler-glib')



@@ 39,4 39,4 @@ if not cc.compiles('''
	error('support for Poppler Cairo output is required')
endif

executable('pdfpartition', 'main.c', install: true, dependencies: [cairo, glib, poppler])
executable('pdfpartition', 'main.c', install: true, dependencies: [cairo, gio, glib, poppler])