mirror of
https://git.pwmt.org/pwmt/zathura.git
synced 2025-01-30 16:44:55 +01:00
Determine realpath of path
This commit is contained in:
parent
cd7fda37cf
commit
f5fabb01ba
4 changed files with 87 additions and 5 deletions
|
@ -1,8 +1,13 @@
|
||||||
/* See LICENSE file for license and copyright information */
|
/* See LICENSE file for license and copyright information */
|
||||||
|
|
||||||
|
#define _BSD_SOURCE
|
||||||
|
#define _XOPEN_SOURCE 500
|
||||||
|
// TODO: Implement realpath
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#include "document.h"
|
#include "document.h"
|
||||||
#include "../utils.h"
|
#include "../utils.h"
|
||||||
|
@ -32,13 +37,33 @@ zathura_document_open(const char* path, const char* password)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
zathura_document_t* document = malloc(sizeof(zathura_document_t));
|
/* determine real path */
|
||||||
if(!document) {
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!realpath(path, real_path)) {
|
||||||
|
free(real_path);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
document->file_path = path;
|
zathura_document_t* document = malloc(sizeof(zathura_document_t));
|
||||||
|
if(!document) {
|
||||||
|
free(real_path);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
document->file_path = real_path;
|
||||||
document->password = password;
|
document->password = password;
|
||||||
document->current_page_number = 0;
|
document->current_page_number = 0;
|
||||||
document->number_of_pages = 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) {
|
||||||
if(zathura_document_plugins[i].open_function(document)) {
|
if(zathura_document_plugins[i].open_function(document)) {
|
||||||
return 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");
|
fprintf(stderr, "error: unknown file type\n");
|
||||||
|
|
||||||
|
free(real_path);
|
||||||
|
free(document);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,12 +116,21 @@ zathura_document_free(zathura_document_t* document)
|
||||||
|
|
||||||
if(!document->functions.document_free) {
|
if(!document->functions.document_free) {
|
||||||
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
|
fprintf(stderr, "error: %s not implemented\n", __FUNCTION__);
|
||||||
|
|
||||||
|
if(document->file_path) {
|
||||||
|
free(document->file_path);
|
||||||
|
}
|
||||||
|
|
||||||
free(document);
|
free(document);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool r = document->functions.document_free(document);
|
bool r = document->functions.document_free(document);
|
||||||
|
|
||||||
|
if(document->file_path) {
|
||||||
|
free(document->file_path);
|
||||||
|
}
|
||||||
|
|
||||||
free(document);
|
free(document);
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
|
|
|
@ -70,7 +70,7 @@ typedef struct zathura_page_s
|
||||||
|
|
||||||
struct zathura_document_s
|
struct zathura_document_s
|
||||||
{
|
{
|
||||||
const char* file_path;
|
char* file_path;
|
||||||
const char* password;
|
const char* password;
|
||||||
unsigned int current_page_number;
|
unsigned int current_page_number;
|
||||||
unsigned int number_of_pages;
|
unsigned int number_of_pages;
|
||||||
|
|
36
ft/pdf/pdf.c
36
ft/pdf/pdf.c
|
@ -21,13 +21,47 @@ pdf_document_open(zathura_document_t* document)
|
||||||
document->functions.page_render = pdf_page_render;
|
document->functions.page_render = pdf_page_render;
|
||||||
document->functions.page_free = pdf_page_free;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
pdf_document_free(zathura_document_t* document)
|
pdf_document_free(zathura_document_t* document)
|
||||||
{
|
{
|
||||||
return false;
|
if(!document) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(document->data) {
|
||||||
|
free(document->data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
zathura_list_t*
|
zathura_list_t*
|
||||||
|
|
|
@ -4,9 +4,15 @@
|
||||||
#define PDF_H
|
#define PDF_H
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <poppler.h>
|
||||||
|
|
||||||
#include "../document.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_open(zathura_document_t* document);
|
||||||
bool pdf_document_free(zathura_document_t* document);
|
bool pdf_document_free(zathura_document_t* document);
|
||||||
zathura_list_t* pdf_document_index_generate(zathura_document_t* document);
|
zathura_list_t* pdf_document_index_generate(zathura_document_t* document);
|
||||||
|
|
Loading…
Reference in a new issue