Elm entry: Adjust the conversion funcs to use the new tags.
[framework/uifw/elementary.git] / src / lib / elm_util.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4 #include <Elementary.h>
5 #include "elm_priv.h"
6
7 static char *
8 _str_ncpy(char *dest, const char *src, size_t count)
9 {
10    if ((!dest) || (!src)) return NULL;
11    return strncpy(dest, src, count);
12 }
13
14 static char *
15 _str_append(char *str, const char *txt, int *len, int *alloc)
16 {
17    int txt_len = strlen(txt);
18
19    if (txt_len <= 0) return str;
20    if ((*len + txt_len) >= *alloc)
21      {
22         char *str2;
23         int alloc2;
24
25         alloc2 = *alloc + txt_len + 128;
26         str2 = realloc(str, alloc2);
27         if (!str2) return str;
28         *alloc = alloc2;
29         str = str2;
30      }
31    strcpy(str + *len, txt);
32    *len += txt_len;
33    return str;
34 }
35
36 char *
37 _elm_util_mkup_to_text(const char *mkup)
38 {
39    char *str = NULL;
40    int str_len = 0, str_alloc = 0;
41    char *s, *p;
42    char *tag_start, *tag_end, *esc_start, *esc_end, *ts;
43
44    if (!mkup) return NULL;
45    tag_start = tag_end = esc_start = esc_end = NULL;
46    p = (char *)mkup;
47    s = p;
48    for (;;)
49      {
50         if ((!*p) ||
51             (tag_end) || (esc_end) ||
52             (tag_start) || (esc_start))
53           {
54              if (tag_end)
55                {
56                   char *ttag;
57
58                   ttag = malloc(tag_end - tag_start);
59                   if (ttag)
60                     {
61                        _str_ncpy(ttag, tag_start + 1, tag_end - tag_start - 1);
62                        ttag[tag_end - tag_start - 1] = 0;
63                        if ((!strcmp(ttag, "br")) ||
64                              (!strcmp(ttag, "\n")) ||
65                              (!strcmp(ttag, "\\n")))
66                          str = _str_append(str, "\n", &str_len, &str_alloc);
67                        else if ((!strcmp(ttag, "tab")) ||
68                              (!strcmp(ttag, "\t")) ||
69                              (!strcmp(ttag, "\\t")))
70                          str = _str_append(str, "\t", &str_len, &str_alloc);
71                        else if (!strcmp(ttag, "ps")) /* Unicode paragraph separator */
72                          str = _str_append(str, "\xE2\x80\xA9", &str_len, &str_alloc);
73                        free(ttag);
74                     }
75                   tag_start = tag_end = NULL;
76                }
77              else if (esc_end)
78                {
79                   ts = malloc(esc_end - esc_start + 1);
80                   if (ts)
81                     {
82                        const char *esc;
83                        _str_ncpy(ts, esc_start, esc_end - esc_start);
84                        ts[esc_end - esc_start] = 0;
85                        esc = evas_textblock_escape_string_get(ts);
86                        if (esc)
87                          str = _str_append(str, esc, &str_len, &str_alloc);
88                        free(ts);
89                     }
90                   esc_start = esc_end = NULL;
91                }
92              else if ((!*p) && (s))
93                {
94                   ts = malloc(p - s + 1);
95                   if (ts)
96                     {
97                        _str_ncpy(ts, s, p - s);
98                        ts[p - s] = 0;
99                        str = _str_append(str, ts, &str_len, &str_alloc);
100                        free(ts);
101                     }
102                }
103
104              if (!*p) break;
105           }
106         if (*p == '<')
107           {
108              if ((s) && (!esc_start))
109                {
110                   tag_start = p;
111                   tag_end = NULL;
112                   ts = malloc(p - s + 1);
113                   if (ts)
114                     {
115                        _str_ncpy(ts, s, p - s);
116                        ts[p - s] = 0;
117                        str = _str_append(str, ts, &str_len, &str_alloc);
118                        free(ts);
119                     }
120                   s = NULL;
121                }
122           }
123         else if (*p == '>')
124           {
125              if (tag_start)
126                {
127                   tag_end = p;
128                   /* <br /> */
129                   /*  ^^^   */
130                   if ((p - mkup > 1) && (p[-1] == '/')) p--;
131                   s = p + 1;
132                }
133           }
134         else if (*p == '&')
135           {
136              if ((s) && (!tag_start))
137                {
138                   esc_start = p;
139                   esc_end = NULL;
140                   ts = malloc(p - s + 1);
141                   if (ts)
142                     {
143                        _str_ncpy(ts, s, p - s);
144                        ts[p - s] = 0;
145                        str = _str_append(str, ts, &str_len, &str_alloc);
146                        free(ts);
147                     }
148                   s = NULL;
149                }
150           }
151         else if (*p == ';')
152           {
153              if (esc_start)
154                {
155                   esc_end = p + 1;
156                   s = p + 1;
157                }
158           }
159         p++;
160      }
161    return str;
162 }
163
164 char *
165 _elm_util_text_to_mkup(const char *text)
166 {
167    char *str = NULL;
168    int str_len = 0, str_alloc = 0;
169    int ch, pos = 0, pos2 = 0;
170
171    if (!text) return NULL;
172    for (;;)
173      {
174         pos = pos2;
175         pos2 = evas_string_char_next_get((char *)(text), pos2, &ch);
176         if ((ch <= 0) || (pos2 <= 0)) break;
177         if (ch == '\n')
178           str = _str_append(str, "<br/>", &str_len, &str_alloc);
179         else if (ch == '\t')
180           str = _str_append(str, "<tab/>", &str_len, &str_alloc);
181         else if (ch == '<')
182           str = _str_append(str, "&lt;", &str_len, &str_alloc);
183         else if (ch == '>')
184           str = _str_append(str, "&gt;", &str_len, &str_alloc);
185         else if (ch == '&')
186           str = _str_append(str, "&amp;", &str_len, &str_alloc);
187         else if (ch == 0x2029) /* PS */
188           str = _str_append(str, "<ps/>", &str_len, &str_alloc);
189         else
190           {
191              char tstr[16];
192
193              _str_ncpy(tstr, text + pos, pos2 - pos);
194              tstr[pos2 - pos] = 0;
195              str = _str_append(str, tstr, &str_len, &str_alloc);
196           }
197      }
198    return str;
199 }