bfcd070258cafd5aadf45fff8caa889b4f331a1a
1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include "support.h"
22 #include "charset.h"
23 #include "config.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <string.h>
29 #ifndef HAVE_STRCASESTR
30 const char *
31 strcasestr(const char *haystack, const char *needle)
32 {
33 char *haystack2 = g_utf8_strdown(haystack, -1);
34 char *needle2 = g_utf8_strdown(needle, -1);
35 char *result;
37 assert(haystack != NULL);
38 assert(needle != NULL);
40 result = strstr(haystack2, needle2);
41 g_free(haystack2);
42 g_free(needle2);
44 return haystack + (result - haystack2);
45 }
46 #endif /* HAVE_STRCASESTR */
48 // FIXME: utf-8 length
49 char *
50 strscroll(char *str, char *separator, int width, scroll_state_t *st)
51 {
52 gchar *tmp, *buf;
53 gsize len, size;
55 assert(str != NULL);
56 assert(separator != NULL);
57 assert(st != NULL);
59 if( st->offset==0 ) {
60 st->offset++;
61 return g_strdup(str);
62 }
64 /* create a buffer containing the string and the separator */
65 size = strlen(str)+strlen(separator)+1;
66 tmp = g_malloc(size);
67 g_strlcpy(tmp, str, size);
68 g_strlcat(tmp, separator, size);
69 len = utf8_width(tmp);
71 if (st->offset >= len)
72 st->offset = 0;
74 /* create the new scrolled string */
75 size = width+1;
76 if (g_utf8_validate(tmp, -1, NULL) ) {
77 int ulen;
78 buf = g_malloc(size*6);// max length of utf8 char is 6
79 g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size);
80 if( (ulen = g_utf8_strlen(buf, -1)) < width )
81 g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
82 } else {
83 buf = g_malloc(size);
84 g_strlcpy(buf, tmp+st->offset, size);
85 if (strlen(buf) < (size_t)width)
86 g_strlcat(buf, tmp, size);
87 }
88 if( time(NULL)-st->t >= 1 ) {
89 st->t = time(NULL);
90 st->offset++;
91 }
92 g_free(tmp);
93 return buf;
94 }