aboutsummaryrefslogtreecommitdiffstats
path: root/src/support.c
blob: 6cf61137c2534c09c184a208fa1163e9beff7f9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* 
 * $Id$
 *
 * (c) 2004 by Kalle Wallin <kaw@linux.se>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "support.h"
#include "ncmpc.h"

#include <assert.h>
#include <time.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 1024

extern void screen_status_printf(const char *format, ...);

static gboolean noconvert = TRUE;

size_t
my_strlen(const char *str)
{
	assert(str != NULL);

	if (g_utf8_validate(str, -1, NULL)) {
		size_t len = g_utf8_strlen(str, -1);
		size_t width = 0;
		gunichar c;

		while (len--) {
			c = g_utf8_get_char(str);
			width += g_unichar_iswide(c) ? 2 : 1;
			str += g_unichar_to_utf8(c, NULL);
		}

		return width;
	} else
		return strlen(str);
}

char *
remove_trailing_slash(char *path)
{
	int len;

	if (path == NULL)
		return NULL;

	len = strlen(path);
	if (len > 1 && path[len - 1] == '/')
		path[len - 1] = '\0';

	return path;
}

char *
lowerstr(char *str)
{
	gsize i;
	gsize len = strlen(str);

	if (str == NULL)
		return NULL;

	i = 0;
	while (i < len && str[i]) {
		str[i] = tolower(str[i]);
		i++;
	}
	return str;
}


#ifndef HAVE_BASENAME
char *
basename(char *path)
{
	char *end;

	assert(path != NULL);

	path = remove_trailing_slash(path);
	end = path + strlen(path);

	while (end > path && *end != '/')
		end--;

	if (*end == '/' && end != path)
		return end+1;

	return path;
}
#endif /* HAVE_BASENAME */


#ifndef HAVE_STRCASESTR
char *
strcasestr(const char *haystack, const char *needle)
{
	assert(haystack != NULL);
	assert(needle != NULL);

	return strstr(lowerstr(haystack), lowerstr(needle));
}
#endif /* HAVE_STRCASESTR */

// FIXME: utf-8 length
char *
strscroll(char *str, char *separator, int width, scroll_state_t *st)
{
	gchar *tmp, *buf;
	gsize len, size;

	assert(str != NULL);
	assert(separator != NULL);
	assert(st != NULL);

	if( st->offset==0 ) {
		st->offset++;
		return g_strdup(str);
	}

	/* create a buffer containing the string and the separator */
	size = strlen(str)+strlen(separator)+1;
	tmp = g_malloc(size);
	g_strlcpy(tmp, str, size);
	g_strlcat(tmp, separator, size);
	len = my_strlen(tmp);

	if (st->offset >= len)
		st->offset = 0;

	/* create the new scrolled string */
	size = width+1;
	if (g_utf8_validate(tmp, -1, NULL) ) {
		int ulen;
		buf = g_malloc(size*6);// max length of utf8 char is 6
		g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size);
		if( (ulen = g_utf8_strlen(buf, -1)) < width )
			g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
	} else {
		buf = g_malloc(size);
		g_strlcpy(buf, tmp+st->offset, size);
		if (strlen(buf) < (size_t)width)
			g_strlcat(buf, tmp, size);
	}
	if( time(NULL)-st->t >= 1 ) {
		st->t = time(NULL);
		st->offset++;
	}
	g_free(tmp);
	return buf;
}

void
charset_init(gboolean disable)
{
  noconvert = disable;
}

char *
utf8_to_locale(const char *utf8str)
{
	gchar *str;
	gsize rb, wb;
	GError *error;

	assert(utf8str != NULL);

	if (noconvert)
		return g_strdup(utf8str);

	rb = 0; /* bytes read */
	wb = 0; /* bytes written */
	error = NULL;
	str = g_locale_from_utf8(utf8str,
				 strlen(utf8str),
				 &wb, &rb,
				 &error);
	if (error) {
		const char *charset;

		g_get_charset(&charset);
		screen_status_printf(_("Error: Unable to convert characters to %s"),
				     charset);
		D("utf8_to_locale(): %s\n", error->message);
		g_error_free(error);
		return g_strdup(utf8str);
	}

	return str;
}

char *
locale_to_utf8(const char *localestr)
{
	gchar *str;
	gsize rb, wb;
	GError *error;

	assert(localestr != NULL);

	if (noconvert)
		return g_strdup(localestr);

	rb = 0; /* bytes read */
	wb = 0; /* bytes written */
	error = NULL;
	str = g_locale_to_utf8(localestr,
			       strlen(localestr),
			       &wb, &rb,
			       &error);
	if (error) {
		screen_status_printf(_("Error: Unable to convert characters to UTF-8"));
		D("locale_to_utf8: %s\n", error->message);
		g_error_free(error);
		return g_strdup(localestr);
	}

	return str;
}