The calculation of `max_lines` takes in account the additionnal lines from URLs
[smdp.git] / src / url.c
index e101013..682bced 100644 (file)
--- a/src/url.c
+++ b/src/url.c
@@ -39,13 +39,15 @@ int url_add(const char *link_name, int link_name_length, const char *target, int
         assert(tmp);
     }
 
-    tmp -> link_name = calloc(link_name_length, sizeof(char));
+    tmp -> link_name = calloc(link_name_length+1, sizeof(char));
     assert(tmp->link_name);
     strncpy(tmp->link_name, link_name, link_name_length);
+    tmp->link_name[link_name_length] = '\0';
 
-    tmp->target = calloc(target_length, sizeof(char));
+    tmp->target = calloc(target_length+1, sizeof(char));
     assert(tmp->target);
     strncpy(tmp->target, target, target_length);
+    tmp->target[target_length] = '\0';
 
     tmp->x = x;
     tmp->y = y;
@@ -57,8 +59,12 @@ int url_add(const char *link_name, int link_name_length, const char *target, int
 }
 
 char * url_get_target(int index) {
+    if (!init_ok) return NULL;
+
     url_t *tmp = list;
 
+    if (!tmp) return NULL;
+
     while (index > 0 && tmp && tmp->next) {
         tmp = tmp->next;
         index--;
@@ -130,3 +136,23 @@ static void url_print(url_t *u) {
 int url_get_amount(void) {
     return index_max;
 }
+
+int url_count_inline(const char *line) {
+    int count = 0;
+    const char* i = line;
+
+    for (; *i; i++) {
+        if (*i == '\\') {
+            i++;
+        } else if ( *i == '[' && *(i+1) != ']') {
+            while (*i && *i != ']') i++;
+            i++;
+            if (*i == '(' && strchr(i, ')')) {
+                count ++;
+                i = strchr(i, ')') + 1;
+            }
+        }
+    }
+
+    return count;
+}