From 0ab1147543467cdb8ab93c53d468339a36694191 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Wed, 7 Oct 2015 22:13:55 +0200 Subject: [PATCH] Factor out SyncTeX string parsing Signed-off-by: Sebastian Ramacher --- zathura/main.c | 24 ++++++++---------------- zathura/synctex.c | 29 +++++++++++++++++++++++++++++ zathura/synctex.h | 3 +++ 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/zathura/main.c b/zathura/main.c index b9aecdd..7bc0f6d 100644 --- a/zathura/main.c +++ b/zathura/main.c @@ -13,6 +13,7 @@ #include "zathura.h" #include "utils.h" #include "dbus-interface.h" +#include "synctex.h" /* main function */ int @@ -113,27 +114,18 @@ main(int argc, char* argv[]) return -1; } - char** split_fwd = g_strsplit(synctex_fwd, ":", 0); - if (split_fwd == NULL || split_fwd[0] == NULL || split_fwd[1] == NULL || - split_fwd[2] == NULL || split_fwd[3] != NULL) { + int line = 0; + int column = 0; + char* input_file = NULL; + if (synctex_parse_input(synctex_fwd, &input_file, &line, &column) == false) { girara_error("Failed to parse argument to --synctex-forward."); g_free(real_path); - g_strfreev(split_fwd); return -1; } - int line = MIN(INT_MAX, g_ascii_strtoll(split_fwd[0], NULL, 10)); - int column = MIN(INT_MAX, g_ascii_strtoll(split_fwd[1], NULL, 10)); - /* SyncTeX starts indexing at 1, but we use 0 */ - if (line > 0) { - --line; - } - if (column > 0) { - --column; - } - - const bool ret = zathura_dbus_synctex_position(real_path, split_fwd[2], line, column, synctex_pid); - g_strfreev(split_fwd); + const int ret = zathura_dbus_synctex_position(real_path, input_file, line, column, synctex_pid); + g_free(input_file); + g_free(real_path); if (ret == false) { girara_error("Could not find open instance for '%s' or got no usable data from synctex.", real_path); diff --git a/zathura/synctex.c b/zathura/synctex.c index 0486539..4ee059d 100644 --- a/zathura/synctex.c +++ b/zathura/synctex.c @@ -192,3 +192,32 @@ synctex_rectangles_from_position(const char* filename, const char* input_file, return hitlist; } +bool +synctex_parse_input(const char* synctex, char** input_file, int* line, + int* column) +{ + if (synctex == NULL || input_file == NULL || line == NULL || column == NULL) { + return false; + } + + char** split_fwd = g_strsplit(synctex, ":", 0); + if (split_fwd == NULL || split_fwd[0] == NULL || split_fwd[1] == NULL || + split_fwd[2] == NULL || split_fwd[3] != NULL) { + g_strfreev(split_fwd); + return false; + } + + *line = MIN(INT_MAX, g_ascii_strtoll(split_fwd[0], NULL, 10)); + *column = MIN(INT_MAX, g_ascii_strtoll(split_fwd[1], NULL, 10)); + /* SyncTeX starts indexing at 1, but we use 0 */ + if (*line > 0) { + --*line; + } + if (*column > 0) { + --*column; + } + *input_file = g_strdup(split_fwd[2]); + + g_strfreev(split_fwd); + return true; +} diff --git a/zathura/synctex.h b/zathura/synctex.h index 99d91ed..fdeaca1 100644 --- a/zathura/synctex.h +++ b/zathura/synctex.h @@ -15,6 +15,9 @@ bool synctex_get_input_line_column(const char* filename, unsigned int page, void synctex_edit(const char* editor, zathura_page_t* page, int x, int y); +bool synctex_parse_input(const char* synctex, char** input_file, int* line, + int* column); + girara_list_t* synctex_rectangles_from_position(const char* filename, const char* input_file, int line, int column, unsigned int* page, girara_list_t** secondary_rects);