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