c5ffd4b72d6309a7a0b27ea57c55dc006af7d186
[smdp.git] / src / url.c
1 #include <stdlib.h>
2 #include <assert.h>
3 #include <string.h>
4 #include <stdio.h>
5
6 #include "url.h"
7
8 static void url_del_elem(url_t *elem);
9 static void url_print(url_t *u);
10
11 static url_t *list;
12 static int index_max;
13 static int init_ok;
14
15 void url_init(void) {
16     list = NULL;
17     index_max = 0;
18     init_ok = 1;
19 }
20
21 int url_add(const char *link_name, int link_name_length, const char *target, int target_length, int x, int y) {
22     if (!init_ok) return -1;
23
24     url_t *tmp = NULL;
25     int i = 0;
26
27     if (list) {
28         tmp = list;
29         while (tmp->next) {
30             tmp = tmp->next;
31             i++;
32         }
33         tmp->next = malloc(sizeof(url_t));
34         assert(tmp->next);
35         tmp = tmp->next;
36     } else {
37         list = malloc(sizeof(url_t));
38         tmp = list;
39         assert(tmp);
40     }
41
42     tmp -> link_name = calloc(link_name_length+1, sizeof(char));
43     assert(tmp->link_name);
44     strncpy(tmp->link_name, link_name, link_name_length);
45     tmp->link_name[link_name_length] = '\0';
46
47     tmp->target = calloc(target_length+1, sizeof(char));
48     assert(tmp->target);
49     strncpy(tmp->target, target, target_length);
50     tmp->target[target_length] = '\0';
51
52     tmp->x = x;
53     tmp->y = y;
54     tmp->next = NULL;
55
56     index_max++;
57
58     return index_max-1;
59 }
60
61 char * url_get_target(int index) {
62     if (!init_ok) return NULL;
63
64     url_t *tmp = list;
65
66     if (!tmp) return NULL;
67
68     while (index > 0 && tmp && tmp->next) {
69         tmp = tmp->next;
70         index--;
71     }
72
73     if (!index) {
74         return tmp->target;
75     } else return NULL;
76 }
77
78 char * url_get_name(int index) {
79     url_t *tmp = list;
80
81     while (index > 0 && tmp && tmp->next) {
82         tmp = tmp->next;
83         index --;
84     }
85
86     if (!index) {
87         return tmp->link_name;
88     } else return NULL;
89 }
90
91 void url_purge() {
92     url_del_elem(list);
93     list = NULL;
94     index_max = 0;
95     init_ok = 0;
96 }
97
98 static void url_del_elem(url_t *elem) {
99     if (!elem) return;
100
101     if (elem->next) {
102         url_del_elem(elem->next);
103         elem->next = NULL;
104     }
105
106     if (elem->target) {
107         free(elem->target);
108         elem->target = NULL;
109     }
110
111     if (elem->link_name) {
112         free(elem->link_name);
113         elem->link_name = NULL;
114     }
115
116     free(elem);
117 }
118
119 void url_dump(void) {
120     if (!list) return;
121
122     url_t *tmp = list;
123
124     while (tmp) {
125         url_print(tmp);
126         if (tmp->next)
127             tmp = tmp->next;
128         else break;
129     }
130 }
131
132 static void url_print(url_t *u) {
133     printf("url_t @ %p\n", u);
134 }
135
136 int url_get_amount(void) {
137     return index_max;
138 }
139
140 int url_count_inline(const char *line) {
141     int count = 0;
142     const char *i = line;
143
144     for (; *i; i++) {
145         if (*i == '\\') {
146             i++;
147         } else if ( *i == '[' && *(i+1) != ']') {
148             while (*i && *i != ']') i++;
149             i++;
150             if (*i == '(' && strchr(i, ')')) {
151                 count ++;
152                 i = strchr(i, ')');
153             }
154         }
155     }
156
157     return count;
158 }
159
160 int url_len_inline(const char *text) {
161     int count = 0;
162     const char *i = text;
163
164     for (; *i; i++) {
165         if (*i == '\\') {
166             i++;
167         } else if ( *i == '[' && *(i+1) != ']') {
168             while (*i && *i != ']') i++;
169             i++;
170             if (*i == '(' && strchr(i, ')')) {
171                 while (*i && *i != ')') {
172                     count++;
173                     i++;
174                 }
175             }
176         }
177     }
178
179     return count;
180 }