elementary/theme, widget, win, toolbar, thumb, toggle, util, box - removed white...
[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                          str = _str_append(str, "\n", &str_len, &str_alloc);
65                        else if (!strcmp(ttag, "\n"))
66                          str = _str_append(str, "\n", &str_len, &str_alloc);
67                        else if (!strcmp(ttag, "\\n"))
68                          str = _str_append(str, "\n", &str_len, &str_alloc);
69                        else if (!strcmp(ttag, "\t"))
70                          str = _str_append(str, "\t", &str_len, &str_alloc);
71                        else if (!strcmp(ttag, "\\t"))
72                          str = _str_append(str, "\t", &str_len, &str_alloc);
73                        else if (!strcmp(ttag, "ps")) /* Unicode paragraph separator */
74                          str = _str_append(str, "\xE2\x80\xA9", &str_len, &str_alloc);
75                        free(ttag);
76                     }
77                   tag_start = tag_end = NULL;
78                }
79              else if (esc_end)
80                {
81                   ts = malloc(esc_end - esc_start + 1);
82                   if (ts)
83                     {
84                        const char *esc;
85                        _str_ncpy(ts, esc_start, esc_end - esc_start);
86                        ts[esc_end - esc_start] = 0;
87                        esc = evas_textblock_escape_string_get(ts);
88                        if (esc)
89                          str = _str_append(str, esc, &str_len, &str_alloc);
90                        free(ts);
91                     }
92                   esc_start = esc_end = NULL;
93                }
94              else if ((!*p) && (s))
95                {
96                   ts = malloc(p - s + 1);
97                   if (ts)
98                     {
99                        _str_ncpy(ts, s, p - s);
100                        ts[p - s] = 0;
101                        str = _str_append(str, ts, &str_len, &str_alloc);
102                        free(ts);
103                     }
104                   break;
105                }
106           }
107         if (*p == '<')
108           {
109              if ((s) && (!esc_start))
110                {
111                   tag_start = p;
112                   tag_end = NULL;
113                   ts = malloc(p - s + 1);
114                   if (ts)
115                     {
116                        _str_ncpy(ts, s, p - s);
117                        ts[p - s] = 0;
118                        str = _str_append(str, ts, &str_len, &str_alloc);
119                        free(ts);
120                     }
121                   s = NULL;
122                }
123           }
124         else if (*p == '>')
125           {
126              if (tag_start)
127                {
128                   tag_end = p;
129                   s = p + 1;
130                }
131           }
132         else if (*p == '&')
133           {
134              if ((s) && (!tag_start))
135                {
136                   esc_start = p;
137                   esc_end = NULL;
138                   ts = malloc(p - s + 1);
139                   if (ts)
140                     {
141                        _str_ncpy(ts, s, p - s);
142                        ts[p - s] = 0;
143                        str = _str_append(str, ts, &str_len, &str_alloc);
144                        free(ts);
145                     }
146                   s = NULL;
147                }
148           }
149         else if (*p == ';')
150           {
151              if (esc_start)
152                {
153                   esc_end = p;
154                   s = p + 1;
155                }
156           }
157         p++;
158      }
159    return str;
160 }
161
162 char *
163 _elm_util_text_to_mkup(const char *text)
164 {
165    char *str = NULL;
166    int str_len = 0, str_alloc = 0;
167    int ch, pos = 0, pos2 = 0;
168
169    if (!text) return NULL;
170    for (;;)
171      {
172         pos = pos2;
173         pos2 = evas_string_char_next_get((char *)(text), pos2, &ch);
174         if ((ch <= 0) || (pos2 <= 0)) break;
175         if (ch == '\n')
176           str = _str_append(str, "<br>", &str_len, &str_alloc);
177         else if (ch == '\t')
178           str = _str_append(str, "<\t>", &str_len, &str_alloc);
179         else if (ch == '<')
180           str = _str_append(str, "&lt;", &str_len, &str_alloc);
181         else if (ch == '>')
182           str = _str_append(str, "&gt;", &str_len, &str_alloc);
183         else if (ch == '&')
184           str = _str_append(str, "&amp;", &str_len, &str_alloc);
185         else if (ch == 0x2029) /* PS */
186           str = _str_append(str, "<ps>", &str_len, &str_alloc);
187         else
188           {
189              char tstr[16];
190
191              _str_ncpy(tstr, text + pos, pos2 - pos);
192              tstr[pos2 - pos] = 0;
193              str = _str_append(str, tstr, &str_len, &str_alloc);
194           }
195      }
196    return str;
197 }