From: Michael Göhler Date: Sun, 30 Nov 2014 21:12:43 +0000 (+0100) Subject: coding style fixes - closes #63 X-Git-Url: https://git.danieliu.xyz/?p=smdp.git;a=commitdiff_plain;h=434d0d1b561b64b99e9d60a704b82fac6e9d4b8e coding style fixes - closes #63 --- diff --git a/AUTHORS b/AUTHORS index 79c1dd0..8ce8542 100644 --- a/AUTHORS +++ b/AUTHORS @@ -8,4 +8,5 @@ lukaslueg mattn fredjean hspak +dopsi diff --git a/include/url.h b/include/url.h index 33a270b..0426752 100644 --- a/include/url.h +++ b/include/url.h @@ -1,14 +1,42 @@ -#ifndef URL_H +#if !defined( URL_H ) #define URL_H -// init the url module -void url_init(void); +/* + * An object to store all urls of a slide. + * Copyright (C) 2014 Michael Goehler + * + * This file is part of mdp. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * function: url_init to intialize a new url object + */ + +typedef struct _url_t { + char *link_name; + char *target; + int x; + int y; + struct _url_t *next; +} url_t; -int url_add(const char * link_name, int link_name_length, const char * target, int target_length, int x, int y); +void url_init(void); +int url_add(const char *link_name, int link_name_length, const char *target, int target_length, int x, int y); char * url_get_target(int index); char * url_get_name(int index); int url_get_amount(void); void url_purge(void); void url_dump(void); -#endif // URL_H +#endif // !defined( URL_H ) diff --git a/include/viewer.h b/include/viewer.h index e095e9d..bcd022f 100644 --- a/include/viewer.h +++ b/include/viewer.h @@ -41,6 +41,7 @@ #include "common.h" #include "parser.h" #include "cstack.h" +#include "url.h" #define CP_WHITE 1 // 255 #define CP_BLUE 2 // 123 diff --git a/src/url.c b/src/url.c index 7ccfa10..e101013 100644 --- a/src/url.c +++ b/src/url.c @@ -5,137 +5,128 @@ #include "url.h" -typedef struct Url { - char * link_name; - char * target; - int x; - int y; - struct Url * next; -} Url_t; - -static void url_del_elem(Url_t * elem); -static void url_print(Url_t * u); - -static Url_t *list; +static void url_del_elem(url_t *elem); +static void url_print(url_t *u); + +static url_t *list; static int index_max; static int init_ok; void url_init(void) { - list = NULL; - index_max = 0; - init_ok = 1; + list = NULL; + index_max = 0; + init_ok = 1; } -int url_add(const char * link_name, int link_name_length, const char * target, int target_length, int x, int y) { - if (!init_ok) return -1; - - Url_t *tmp = NULL; - int i = 0; - - if (list) { - tmp = list; - while (tmp->next) { - tmp = tmp->next; - i++; - } - tmp->next = malloc(sizeof(Url_t)); - assert(tmp->next); - tmp = tmp->next; - } else { - list = malloc(sizeof(Url_t)); - tmp = list; - assert(tmp); - } - - tmp -> link_name = calloc(link_name_length, sizeof(char)); - assert(tmp->link_name); - strncpy(tmp->link_name, link_name, link_name_length); - - tmp->target = calloc(target_length, sizeof(char)); - assert(tmp->target); - strncpy(tmp->target, target, target_length); - - tmp->x = x; - tmp->y = y; - tmp->next = NULL; - - index_max++; - - return index_max-1; -} +int url_add(const char *link_name, int link_name_length, const char *target, int target_length, int x, int y) { + if (!init_ok) return -1; + + url_t *tmp = NULL; + int i = 0; + + if (list) { + tmp = list; + while (tmp->next) { + tmp = tmp->next; + i++; + } + tmp->next = malloc(sizeof(url_t)); + assert(tmp->next); + tmp = tmp->next; + } else { + list = malloc(sizeof(url_t)); + tmp = list; + assert(tmp); + } + + tmp -> link_name = calloc(link_name_length, sizeof(char)); + assert(tmp->link_name); + strncpy(tmp->link_name, link_name, link_name_length); + tmp->target = calloc(target_length, sizeof(char)); + assert(tmp->target); + strncpy(tmp->target, target, target_length); + + tmp->x = x; + tmp->y = y; + tmp->next = NULL; + + index_max++; + + return index_max-1; +} char * url_get_target(int index) { - Url_t * tmp = list; - - while (index > 0 && tmp && tmp->next) { - tmp = tmp->next; - index --; - } - - if (!index) { - return tmp->target; - } else return NULL; + url_t *tmp = list; + + while (index > 0 && tmp && tmp->next) { + tmp = tmp->next; + index--; + } + + if (!index) { + return tmp->target; + } else return NULL; } char * url_get_name(int index) { - Url_t * tmp = list; - - while (index > 0 && tmp && tmp->next) { - tmp = tmp->next; - index --; - } - - if (!index) { - return tmp->link_name; - } else return NULL; + url_t *tmp = list; + + while (index > 0 && tmp && tmp->next) { + tmp = tmp->next; + index --; + } + + if (!index) { + return tmp->link_name; + } else return NULL; } void url_purge() { - url_del_elem(list); - list = NULL; - index_max = 0; - init_ok = 0; + url_del_elem(list); + list = NULL; + index_max = 0; + init_ok = 0; } -static void url_del_elem(Url_t *elem) { - if (!elem) return; - - if (elem->next) { - url_del_elem(elem->next); - elem->next = NULL; - } - - if (elem->target) { - free(elem->target); - elem->target = NULL; - } - - if (elem->link_name) { - free(elem->link_name); - elem->link_name = NULL; - } - - free(elem); +static void url_del_elem(url_t *elem) { + if (!elem) return; + + if (elem->next) { + url_del_elem(elem->next); + elem->next = NULL; + } + + if (elem->target) { + free(elem->target); + elem->target = NULL; + } + + if (elem->link_name) { + free(elem->link_name); + elem->link_name = NULL; + } + + free(elem); } void url_dump(void) { - if (!list) return; - - Url_t *tmp = list; - - while (tmp) { - url_print(tmp); - if (tmp->next) - tmp = tmp->next; - else break; - } + if (!list) return; + + url_t *tmp = list; + + while (tmp) { + url_print(tmp); + if (tmp->next) + tmp = tmp->next; + else break; + } } -static void url_print(Url_t * u) { - printf("Url_t @ %p\n", u); +static void url_print(url_t *u) { + printf("url_t @ %p\n", u); } int url_get_amount(void) { - return index_max; + return index_max; } diff --git a/src/viewer.c b/src/viewer.c index 593d9d3..6b96ea5 100644 --- a/src/viewer.c +++ b/src/viewer.c @@ -27,7 +27,6 @@ #include // usleep #include "viewer.h" -#include "url.h" // color ramp for fading from black to color static short white_ramp[24] = { 16, 232, 233, 234, 235, 236, @@ -235,9 +234,9 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { slide = deck->slide; while(slide) { - - url_init(); - + + url_init(); + // clear windows werase(content); werase(stdscr); @@ -282,13 +281,13 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { l += (line->length / COLS) + 1; line = line->next; } - + int i, ymax; getmaxyx( content, ymax, i ); for (i = 0; i < url_get_amount(); i++) { - mvwprintw(content, ymax - url_get_amount() - 1 + i, 3, - "[%d] %s", i, url_get_target(i)); - } + mvwprintw(content, ymax - url_get_amount() - 1 + i, 3, + "[%d] %s", i, url_get_target(i)); + } // make content visible wrefresh(content); @@ -405,7 +404,7 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { // fade out if(fade) fade_out(content, trans, colors, invert); - + url_purge(); } @@ -633,41 +632,47 @@ void inline_display(WINDOW *window, const char *c, const int colors) { ((*(i - 1) == '_' || *(i - 1) == '*') && ((i - 1) == c || *(i - 2) == ' ')) || *i == '\\') { + // url in pandoc style if (*i == '[' && strchr(i, ']')) { - - if (strchr(i, ']')[1] == '(') {// url in pandoc style - i++; - // turn higlighting and underlining on - if (colors) - wattron(window, COLOR_PAIR(CP_BLUE)); - wattron(window, A_UNDERLINE); - - start_link_name = i; - - // print the content of the label - // the label is printed as is - do { - wprintw(window, "%c", *i); - i++; - } while (*i != ']'); - - length_link_name = i - 1 - start_link_name; - - i++; - i++; - - start_url = i; - - while (*i != ')') i++; - - url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0,0); - - wprintw(window, "[%d]", url_num); - // turn highlighting and undelining off - wattroff(window, A_UNDERLINE); - wattron(window, COLOR_PAIR(CP_WHITE)); - } else wprintw(window, "["); - } else switch(*i) { + if (strchr(i, ']')[1] == '(') { + i++; + + // turn higlighting and underlining on + if (colors) + wattron(window, COLOR_PAIR(CP_BLUE)); + wattron(window, A_UNDERLINE); + + start_link_name = i; + + // print the content of the label + // the label is printed as is + do { + wprintw(window, "%c", *i); + i++; + } while (*i != ']'); + + length_link_name = i - 1 - start_link_name; + + i++; + i++; + + start_url = i; + + while (*i != ')') i++; + + url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0,0); + + wprintw(window, "[%d]", url_num); + + // turn highlighting and underlining off + wattroff(window, A_UNDERLINE); + wattron(window, COLOR_PAIR(CP_WHITE)); + + } else { + wprintw(window, "["); + } + + } else switch(*i) { // enable highlight case '*': if(colors)