Commit f5fabb01 authored by Moritz Lipp's avatar Moritz Lipp
Browse files

Determine realpath of path

parent cd7fda37
/* See LICENSE file for license and copyright information */
#define _BSD_SOURCE
#define _XOPEN_SOURCE 500
// TODO: Implement realpath
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "document.h"
#include "../utils.h"
......@@ -32,13 +37,33 @@ zathura_document_open(const char* path, const char* password)
return NULL;
}
/* determine real path */
size_t path_max;
#ifdef PATH_MAX
path_max = PATH_MAX;
#else
path_max = pathconf(path,_PC_PATH_MAX);
if(path_max <= 0)
path_max = 4096;
#endif
char* real_path = malloc(sizeof(char) * path_max);
if(!real_path) {
return NULL;
}
if(!realpath(path, real_path)) {
free(real_path);
return NULL;
}
zathura_document_t* document = malloc(sizeof(zathura_document_t));
if(!document) {
free(real_path);
return NULL;
}
document->file_path = path;
document->file_path = real_path;
document->password = password;
document->current_page_number = 0;
document->number_of_pages = 0;
......@@ -64,6 +89,11 @@ zathura_document_open(const char* path, const char* password)
if(zathura_document_plugins[i].open_function) {
if(zathura_document_plugins[i].open_function(document)) {
return document;
} else {
fprintf(stderr, "error: could not open file\n");
free(real_path);
free(document);
return NULL;
}
}
}
......@@ -71,6 +101,9 @@ zathura_document_open(const char* path, const char* password)
fprintf(stderr, "error: unknown file type\n");
free(real_path);
free(document);
return NULL;
}
......@@ -83,12 +116,21 @@ zathura_document_free(zathura_document_t* document)
if(!document->functions.document_free) {
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
if(document->file_path) {
free(document->file_path);
}
free(document);
return true;
}
bool r = document->functions.document_free(document);
if(document->file_path) {
free(document->file_path);
}
free(document);
return r;
......
......@@ -70,7 +70,7 @@ typedef struct zathura_page_s
struct zathura_document_s
{
const char* file_path;
char* file_path;
const char* password;
unsigned int current_page_number;
unsigned int number_of_pages;
......
......@@ -21,13 +21,47 @@ pdf_document_open(zathura_document_t* document)
document->functions.page_render = pdf_page_render;
document->functions.page_free = pdf_page_free;
document->data = malloc(sizeof(pdf_document_t));
if(!document->data) {
return false;
}
/* format path */
GError* error = NULL;
char* file_uri = g_filename_to_uri(document->file_path, NULL, &error);
if(!file_uri) {
fprintf(stderr, "error: could not open file: %s\n", error->message);
g_error_free(error);
free(document->data);
return false;
}
pdf_document_t* pdf_document = (pdf_document_t*) document->data;
pdf_document->document = poppler_document_new_from_file(file_uri, document->password, &error);
if(!pdf_document->document) {
fprintf(stderr, "error: could not open file: %s\n", error->message);
g_error_free(error);
free(document->data);
return false;
}
return true;
}
bool
pdf_document_free(zathura_document_t* document)
{
return false;
if(!document) {
return false;
}
if(document->data) {
free(document->data);
}
return true;
}
zathura_list_t*
......
......@@ -4,9 +4,15 @@
#define PDF_H
#include <stdbool.h>
#include <poppler.h>
#include "../document.h"
typedef struct pdf_document_s
{
PopplerDocument *document;
} pdf_document_t;
bool pdf_document_open(zathura_document_t* document);
bool pdf_document_free(zathura_document_t* document);
zathura_list_t* pdf_document_index_generate(zathura_document_t* document);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment