From 9b7a05755b0c8d612f32540fe9dd9b3b7496d29a Mon Sep 17 00:00:00 2001 From: Carsten Haitzler Date: Mon, 20 Dec 2010 06:05:09 +0000 Subject: [PATCH] move mkup to/from text calls to elm_util.c in preparation for some patches. SVN revision: 55649 --- legacy/elementary/src/lib/Makefile.am | 1 + legacy/elementary/src/lib/elm_priv.h | 3 + legacy/elementary/src/lib/elm_util.c | 190 ++++++++++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 legacy/elementary/src/lib/elm_util.c diff --git a/legacy/elementary/src/lib/Makefile.am b/legacy/elementary/src/lib/Makefile.am index e84f9e9b94..ab9e24a6ac 100644 --- a/legacy/elementary/src/lib/Makefile.am +++ b/legacy/elementary/src/lib/Makefile.am @@ -38,6 +38,7 @@ libelementary_la_SOURCES = \ elm_priv.h \ \ elm_main.c \ +elm_util.c \ elm_theme.c \ elm_module.c \ elm_store.c \ diff --git a/legacy/elementary/src/lib/elm_priv.h b/legacy/elementary/src/lib/elm_priv.h index ce8f17d0f4..332e74e95b 100644 --- a/legacy/elementary/src/lib/elm_priv.h +++ b/legacy/elementary/src/lib/elm_priv.h @@ -190,6 +190,9 @@ Eina_Bool _elm_dangerous_call_check(const char *call); Evas_Object *_elm_scroller_edje_object_get(Evas_Object *obj); +char *_elm_util_mkup_to_text(const char *mkup); +char *_elm_util_text_to_mkup(const char *text); + extern char *_elm_appname; extern Elm_Config *_elm_config; extern const char *_elm_data_dir; diff --git a/legacy/elementary/src/lib/elm_util.c b/legacy/elementary/src/lib/elm_util.c new file mode 100644 index 0000000000..eca0482b04 --- /dev/null +++ b/legacy/elementary/src/lib/elm_util.c @@ -0,0 +1,190 @@ +#ifdef HAVE_CONFIG_H +# include "elementary_config.h" +#endif +#include +#include "elm_priv.h" + +static char * +_str_append(char *str, const char *txt, int *len, int *alloc) +{ + int txt_len = strlen(txt); + + if (txt_len <= 0) return str; + if ((*len + txt_len) >= *alloc) + { + char *str2; + int alloc2; + + alloc2 = *alloc + txt_len + 128; + str2 = realloc(str, alloc2); + if (!str2) return str; + *alloc = alloc2; + str = str2; + } + strcpy(str + *len, txt); + *len += txt_len; + return str; +} + +char * +_elm_util_mkup_to_text(const char *mkup) +{ + char *str = NULL; + int str_len = 0, str_alloc = 0; + char *s, *p; + char *tag_start, *tag_end, *esc_start, *esc_end, *ts; + + if (!mkup) return NULL; + tag_start = tag_end = esc_start = esc_end = NULL; + p = (char *)mkup; + s = p; + for (;;) + { + if ((!*p) || + (tag_end) || (esc_end) || + (tag_start) || (esc_start)) + { + if (tag_end) + { + char *ttag; + + ttag = malloc(tag_end - tag_start); + if (ttag) + { + strncpy(ttag, tag_start + 1, tag_end - tag_start - 1); + ttag[tag_end - tag_start - 1] = 0; + if (!strcmp(ttag, "br")) + str = _str_append(str, "\n", &str_len, &str_alloc); + else if (!strcmp(ttag, "\n")) + str = _str_append(str, "\n", &str_len, &str_alloc); + else if (!strcmp(ttag, "\\n")) + str = _str_append(str, "\n", &str_len, &str_alloc); + else if (!strcmp(ttag, "\t")) + str = _str_append(str, "\t", &str_len, &str_alloc); + else if (!strcmp(ttag, "\\t")) + str = _str_append(str, "\t", &str_len, &str_alloc); + else if (!strcmp(ttag, "ps")) /* Unicode paragraph separator */ + str = _str_append(str, "\xE2\x80\xA9", &str_len, &str_alloc); + free(ttag); + } + tag_start = tag_end = NULL; + } + else if (esc_end) + { + ts = malloc(esc_end - esc_start + 1); + if (ts) + { + const char *esc; + strncpy(ts, esc_start, esc_end - esc_start); + ts[esc_end - esc_start] = 0; + esc = evas_textblock_escape_string_get(ts); + if (esc) + str = _str_append(str, esc, &str_len, &str_alloc); + free(ts); + } + esc_start = esc_end = NULL; + } + else if ((!*p) && (s)) + { + ts = malloc(p - s + 1); + if (ts) + { + strncpy(ts, s, p - s); + ts[p - s] = 0; + str = _str_append(str, ts, &str_len, &str_alloc); + free(ts); + } + break; + } + } + if (*p == '<') + { + if ((s) && (!esc_start)) + { + tag_start = p; + tag_end = NULL; + ts = malloc(p - s + 1); + if (ts) + { + strncpy(ts, s, p - s); + ts[p - s] = 0; + str = _str_append(str, ts, &str_len, &str_alloc); + free(ts); + } + s = NULL; + } + } + else if (*p == '>') + { + if (tag_start) + { + tag_end = p; + s = p + 1; + } + } + else if (*p == '&') + { + if ((s) && (!tag_start)) + { + esc_start = p; + esc_end = NULL; + ts = malloc(p - s + 1); + if (ts) + { + strncpy(ts, s, p - s); + ts[p - s] = 0; + str = _str_append(str, ts, &str_len, &str_alloc); + free(ts); + } + s = NULL; + } + } + else if (*p == ';') + { + if (esc_start) + { + esc_end = p; + s = p + 1; + } + } + p++; + } + return str; +} + +char * +_elm_util_text_to_mkup(const char *text) +{ + char *str = NULL; + int str_len = 0, str_alloc = 0; + int ch, pos = 0, pos2 = 0; + + if (!text) return NULL; + for (;;) + { + pos = pos2; + pos2 = evas_string_char_next_get((char *)(text), pos2, &ch); + if ((ch <= 0) || (pos2 <= 0)) break; + if (ch == '\n') + str = _str_append(str, "
", &str_len, &str_alloc); + else if (ch == '\t') + str = _str_append(str, "<\t>", &str_len, &str_alloc); + else if (ch == '<') + str = _str_append(str, "<", &str_len, &str_alloc); + else if (ch == '>') + str = _str_append(str, ">", &str_len, &str_alloc); + else if (ch == '&') + str = _str_append(str, "&", &str_len, &str_alloc); + else if (ch == 0x2029) /* PS */ + str = _str_append(str, "", &str_len, &str_alloc); + else + { + char tstr[16]; + + strncpy(tstr, text + pos, pos2 - pos); + tstr[pos2 - pos] = 0; + str = _str_append(str, tstr, &str_len, &str_alloc); + } + } + return str; +}