bool file_valid_extension(zathura_t* zathura, const char* path) { if (zathura == NULL || path == NULL || zathura->plugins.manager == NULL) { return false; } const gchar* content_type = guess_content_type(path); if (content_type == NULL) { return false; } zathura_plugin_t* plugin = zathura_plugin_manager_get_plugin(zathura->plugins.manager, content_type); g_free((void*)content_type); return (plugin == NULL) ? false : true; }
zathura_document_t* zathura_document_open(zathura_plugin_manager_t* plugin_manager, const char* path, const char* password, zathura_error_t* error) { if (path == NULL) { return NULL; } if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) { girara_error("File '%s' does not exist", path); return NULL; } const gchar* content_type = guess_type(path); if (content_type == NULL) { girara_error("Could not determine file type."); return NULL; } /* determine real path */ long 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 = NULL; zathura_document_t* document = NULL; real_path = malloc(sizeof(char) * path_max); if (real_path == NULL) { g_free((void*)content_type); return NULL; } if (realpath(path, real_path) == NULL) { g_free((void*)content_type); free(real_path); return NULL; } zathura_plugin_t* plugin = zathura_plugin_manager_get_plugin(plugin_manager, content_type); g_free((void*)content_type); if (plugin == NULL) { girara_error("unknown file type\n"); goto error_free; } document = g_malloc0(sizeof(zathura_document_t)); document->file_path = real_path; document->password = password; document->scale = 1.0; document->plugin = plugin; document->adjust_mode = ZATHURA_ADJUST_NONE; /* open document */ zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); if (functions->document_open == NULL) { girara_error("plugin has no open function\n"); goto error_free; } zathura_error_t int_error = functions->document_open(document); if (int_error != ZATHURA_ERROR_OK) { if (error != NULL) { *error = int_error; } girara_error("could not open document\n"); goto error_free; } /* read all pages */ document->pages = calloc(document->number_of_pages, sizeof(zathura_page_t*)); if (document->pages == NULL) { goto error_free; } for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { zathura_page_t* page = zathura_page_new(document, page_id, NULL); if (page == NULL) { goto error_free; } document->pages[page_id] = page; } return document; error_free: free(real_path); if (document != NULL && document->pages != NULL) { for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { zathura_page_free(document->pages[page_id]); } free(document->pages); } g_free(document); return NULL; }