comparison 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
comparison
equal deleted inserted replaced
9174:a839ef5d2f34 9175:3e2ea5b69605
1657 } 1657 }
1658 return unescaped; 1658 return unescaped;
1659 1659
1660 } 1660 }
1661 1661
1662 char *
1663 gaim_markup_slice(const char *str, guint x, guint y)
1664 {
1665 GString *ret;
1666 GQueue *q;
1667 guint z = 0;
1668 gboolean appended = FALSE;
1669 gunichar c;
1670 char *tag;
1671
1672 g_return_val_if_fail(x <= y, NULL);
1673
1674 if (x == y)
1675 return g_strdup("");
1676
1677 ret = g_string_new("");
1678 q = g_queue_new();
1679
1680 while (*str && (z < y)) {
1681 c = g_utf8_get_char(str);
1682
1683 if (c == '<') {
1684 char *end = strchr(str, '>');
1685
1686 if (!end) {
1687 g_string_free(ret, TRUE);
1688 while ((tag = g_queue_pop_head(q)))
1689 g_free(tag);
1690 g_queue_free(q);
1691 return NULL;
1692 }
1693
1694 if (!g_ascii_strncasecmp(str, "<img ", 5)) {
1695 z += strlen("[Image]");
1696 } else if (!g_ascii_strncasecmp(str, "<br", 3)) {
1697 z += 1;
1698 } else if (!g_ascii_strncasecmp(str, "<hr>", 4)) {
1699 z += strlen("\n---\n");
1700 } else if (!g_ascii_strncasecmp(str, "</", 2)) {
1701 /* pop stack */
1702 char *tmp;
1703
1704 tmp = g_queue_pop_head(q);
1705 if (tmp)
1706 g_free(tmp);
1707 /* z += 0; */
1708 } else {
1709 /* push it unto the stack */
1710 char *tmp;
1711
1712 tmp = g_strndup(str, end - str + 1);
1713 g_queue_push_head(q, tmp);
1714 /* z += 0; */
1715 }
1716
1717 if (z == x && !appended) {
1718 GList *l = q->tail;
1719
1720 while (l) {
1721 tag = l->data;
1722 g_string_append(ret, tag);
1723 l = l->prev;
1724 }
1725 appended = TRUE;
1726 } else if (z >= x) {
1727 g_string_append_len(ret, str, end - str + 1);
1728 }
1729
1730 str = end;
1731 } else if (c == '&') {
1732 char *end = strchr(str, ';');
1733 if (!end) {
1734 g_string_free(ret, TRUE);
1735 while ((tag = g_queue_pop_head(q)))
1736 g_free(tag);
1737 g_queue_free(q);
1738
1739 return NULL;
1740 }
1741
1742 if (z >= x)
1743 g_string_append_len(ret, str, end - str + 1);
1744
1745 z++;
1746 str = end;
1747 } else {
1748 if (z >= x)
1749 g_string_append_unichar(ret, c);
1750 z++;
1751 }
1752
1753 str = g_utf8_next_char(str);
1754 }
1755
1756 while ((tag = g_queue_pop_head(q))) {
1757 char *name;
1758
1759 name = gaim_markup_get_tag_name(tag);
1760 g_string_append_printf(ret, "</%s>", name);
1761 g_free(name);
1762 g_free(tag);
1763 }
1764
1765 g_queue_free(q);
1766 return g_string_free(ret, FALSE);
1767 }
1768
1769 char *
1770 gaim_markup_get_tag_name(const char *tag)
1771 {
1772 int i;
1773 g_return_val_if_fail(tag != NULL, NULL);
1774 g_return_val_if_fail(*tag == '<', NULL);
1775
1776 for (i = 1; tag[i]; i++)
1777 if (tag[i] == '>' || tag[i] == ' ' || tag[i] == '/')
1778 break;
1779
1780 return g_strndup(tag, i);
1781 }
1782
1662 /************************************************************************** 1783 /**************************************************************************
1663 * Path/Filename Functions 1784 * Path/Filename Functions
1664 **************************************************************************/ 1785 **************************************************************************/
1665 const char * 1786 const char *
1666 gaim_home_dir(void) 1787 gaim_home_dir(void)