Mercurial > pidgin
diff src/util.c @ 9175:3e2ea5b69605
[gaim-migrate @ 9970]
W and S are now implemented for /cmds in core.
This means you can do /me with colors again.
This was probably the hardest part of cmds that was left to do. So the rest
should be fairly easy. Hopefully there's no major bugs in this. There's
some inconsist use of g_utf8_isspace vs strchr(s, ' ') I want to clean up
yet that will cause some oddness if you use a tab instead of a space as
your argument separater.
committer: Tailor Script <tailor@pidgin.im>
| author | Tim Ringenbach <marv@pidgin.im> |
|---|---|
| date | Sat, 05 Jun 2004 07:33:58 +0000 |
| parents | 456ef1f4ba19 |
| children | f0488214826f |
line wrap: on
line diff
--- a/src/util.c Fri Jun 04 04:35:30 2004 +0000 +++ b/src/util.c Sat Jun 05 07:33:58 2004 +0000 @@ -1659,6 +1659,127 @@ } +char * +gaim_markup_slice(const char *str, guint x, guint y) +{ + GString *ret; + GQueue *q; + guint z = 0; + gboolean appended = FALSE; + gunichar c; + char *tag; + + g_return_val_if_fail(x <= y, NULL); + + if (x == y) + return g_strdup(""); + + ret = g_string_new(""); + q = g_queue_new(); + + while (*str && (z < y)) { + c = g_utf8_get_char(str); + + if (c == '<') { + char *end = strchr(str, '>'); + + if (!end) { + g_string_free(ret, TRUE); + while ((tag = g_queue_pop_head(q))) + g_free(tag); + g_queue_free(q); + return NULL; + } + + if (!g_ascii_strncasecmp(str, "<img ", 5)) { + z += strlen("[Image]"); + } else if (!g_ascii_strncasecmp(str, "<br", 3)) { + z += 1; + } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) { + z += strlen("\n---\n"); + } else if (!g_ascii_strncasecmp(str, "</", 2)) { + /* pop stack */ + char *tmp; + + tmp = g_queue_pop_head(q); + if (tmp) + g_free(tmp); + /* z += 0; */ + } else { + /* push it unto the stack */ + char *tmp; + + tmp = g_strndup(str, end - str + 1); + g_queue_push_head(q, tmp); + /* z += 0; */ + } + + if (z == x && !appended) { + GList *l = q->tail; + + while (l) { + tag = l->data; + g_string_append(ret, tag); + l = l->prev; + } + appended = TRUE; + } else if (z >= x) { + g_string_append_len(ret, str, end - str + 1); + } + + str = end; + } else if (c == '&') { + char *end = strchr(str, ';'); + if (!end) { + g_string_free(ret, TRUE); + while ((tag = g_queue_pop_head(q))) + g_free(tag); + g_queue_free(q); + + return NULL; + } + + if (z >= x) + g_string_append_len(ret, str, end - str + 1); + + z++; + str = end; + } else { + if (z >= x) + g_string_append_unichar(ret, c); + z++; + } + + str = g_utf8_next_char(str); + } + + while ((tag = g_queue_pop_head(q))) { + char *name; + + name = gaim_markup_get_tag_name(tag); + g_string_append_printf(ret, "</%s>", name); + g_free(name); + g_free(tag); + } + + g_queue_free(q); + return g_string_free(ret, FALSE); +} + +char * +gaim_markup_get_tag_name(const char *tag) +{ + int i; + g_return_val_if_fail(tag != NULL, NULL); + g_return_val_if_fail(*tag == '<', NULL); + + for (i = 1; tag[i]; i++) + if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/') + break; + + return g_strndup(tag, i); +} + /************************************************************************** * Path/Filename Functions **************************************************************************/
