Only allocate dynamically if we add to the list

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2014-01-19 19:42:54 +01:00
parent 7fc2b365ba
commit af40bbabad

View File

@ -157,12 +157,13 @@ synctex_rectangles_from_position(const char* filename, const char* position,
} }
} }
ret = false; ret = false;
unsigned int rpage = 0; unsigned int rpage = 0;
unsigned int current_page = 0; unsigned int current_page = 0;
girara_list_t* hitlist = girara_list_new2(g_free); girara_list_t* hitlist = girara_list_new2(g_free);
girara_list_t* other_rects = girara_list_new2(g_free); girara_list_t* other_rects = girara_list_new2(g_free);
zathura_rectangle_t* rectangle = NULL; bool got_rect = false;
zathura_rectangle_t rectangle;
while (found_end == false) { while (found_end == false) {
switch (g_scanner_get_next_token(scanner)) { switch (g_scanner_get_next_token(scanner)) {
@ -184,51 +185,51 @@ synctex_rectangles_from_position(const char* filename, const char* position,
rpage = current_page; rpage = current_page;
} }
if (*page == current_page && rectangle != NULL) { if (got_rect == false) {
girara_list_append(hitlist, rectangle); continue;
rectangle = NULL; }
} else if (rectangle != NULL) { got_rect = false;
synctex_page_rect_t* page_rect = g_try_malloc0(sizeof(synctex_page_rect_t));
if (*page == current_page) {
zathura_rectangle_t* real_rect = g_try_malloc(sizeof(zathura_rectangle_t));
if (real_rect == NULL) {
continue;
}
*real_rect = rectangle;
girara_list_append(hitlist, real_rect);
} else {
synctex_page_rect_t* page_rect = g_try_malloc(sizeof(synctex_page_rect_t));
if (page_rect == NULL) { if (page_rect == NULL) {
continue; continue;
} }
page_rect->page = current_page; page_rect->page = current_page;
page_rect->rect = *rectangle; page_rect->rect = rectangle;
girara_list_append(other_rects, page_rect); girara_list_append(other_rects, page_rect);
} }
g_free(rectangle);
rectangle = g_try_malloc0(sizeof(zathura_rectangle_t));
if (rectangle == NULL) {
continue;
}
} }
break; break;
case SYNCTEX_PROP_H: case SYNCTEX_PROP_H:
if (rectangle != NULL) { rectangle.x1 = scan_float(scanner);
rectangle->x1 = scan_float(scanner); got_rect = true;
}
break; break;
case SYNCTEX_PROP_V: case SYNCTEX_PROP_V:
if (rectangle != NULL) { rectangle.y2 = scan_float(scanner);
rectangle->y2 = scan_float(scanner); got_rect = true;
}
break; break;
case SYNCTEX_PROP_WIDTH: case SYNCTEX_PROP_WIDTH:
if (rectangle != NULL) { rectangle.x2 = rectangle.x1 + scan_float(scanner);
rectangle->x2 = rectangle->x1 + scan_float(scanner); got_rect = true;
}
break; break;
case SYNCTEX_PROP_HEIGHT: case SYNCTEX_PROP_HEIGHT:
if (rectangle != NULL) { rectangle.y1 = rectangle.y2 - scan_float(scanner);
rectangle->y1 = rectangle->y2 - scan_float(scanner); got_rect = true;
}
break; break;
} }
break; break;
@ -238,16 +239,19 @@ synctex_rectangles_from_position(const char* filename, const char* position,
} }
} }
if (rectangle != NULL) { if (got_rect == true) {
if (current_page == rpage) { if (current_page == rpage) {
girara_list_append(hitlist, rectangle); zathura_rectangle_t* real_rect = g_try_malloc(sizeof(zathura_rectangle_t));
if (real_rect != NULL) {
*real_rect = rectangle;
girara_list_append(hitlist, real_rect);
}
} else { } else {
synctex_page_rect_t* page_rect = g_try_malloc0(sizeof(synctex_page_rect_t)); synctex_page_rect_t* page_rect = g_try_malloc(sizeof(synctex_page_rect_t));
if (page_rect != NULL) { if (page_rect != NULL) {
page_rect->page = current_page; page_rect->page = current_page;
page_rect->rect = *rectangle; page_rect->rect = rectangle;
girara_list_append(other_rects, page_rect); girara_list_append(other_rects, page_rect);
g_free(rectangle);
} }
} }
} }