Subversion Repositories Kolibri OS

Rev

Rev 1905 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1905 Rev 3960
Line 1... Line 1...
1
/*
1
/*
2
	stringbuf: mimicking a bit of C++ to more safely handle strings
2
	stringbuf: mimicking a bit of C++ to more safely handle strings
Line 3... Line 3...
3
 
3
 
4
	copyright 2006-8 by the mpg123 project - free software under the terms of the LGPL 2.1
4
	copyright 2006-10 by the mpg123 project - free software under the terms of the LGPL 2.1
5
	see COPYING and AUTHORS files in distribution or http://mpg123.org
5
	see COPYING and AUTHORS files in distribution or http://mpg123.org
6
	initially written by Thomas Orgis
6
	initially written by Thomas Orgis
Line 7... Line 7...
7
*/
7
*/
Line 127... Line 127...
127
int attribute_align_arg mpg123_set_string(mpg123_string* sb, const char* stuff)
127
int attribute_align_arg mpg123_set_string(mpg123_string* sb, const char* stuff)
128
{
128
{
129
	sb->fill = 0;
129
	sb->fill = 0;
130
	return mpg123_add_string(sb, stuff);
130
	return mpg123_add_string(sb, stuff);
131
}
131
}
-
 
132
 
-
 
133
size_t attribute_align_arg mpg123_strlen(mpg123_string *sb, int utf8)
-
 
134
{
-
 
135
	size_t i;
-
 
136
	size_t bytelen;
-
 
137
 
-
 
138
	/* Notions of empty string. If there's only a single character, it has to be the trailing zero, and if the first is the trailing zero anyway, we got empty. */
-
 
139
	if(sb->fill < 2 || sb->p[0] == 0) return 0;
-
 
140
 
-
 
141
	/* Find the first non-null character from the back.
-
 
142
	   We already established that the first character is non-null
-
 
143
	   That at fill-2 has to be null, though. */
-
 
144
	for(i=sb->fill-2; i>0; --i)
-
 
145
	if(sb->p[i] != 0) break;
-
 
146
 
-
 
147
	/* For simple byte strings, we are done now. */
-
 
148
	bytelen = i+1;
-
 
149
 
-
 
150
	if(!utf8) return bytelen;
-
 
151
	else
-
 
152
	{
-
 
153
		/* Work out the actual count of UTF8 bytes.
-
 
154
		   This employs no particular encoding error checking. */
-
 
155
		size_t len = 0;
-
 
156
		for(i=0; i
-
 
157
		{
-
 
158
			/* Every byte that is not a continuation byte ( 0xc0 == 10xx xxxx ) stands for a character. */
-
 
159
			if((sb->p[i] & 0xc0) != 0x80) len++;
-
 
160
		}
-
 
161
		return len;
-
 
162
	}
-
 
163
}
-
 
164
 
-
 
165
int attribute_align_arg mpg123_chomp_string(mpg123_string *sb)
-
 
166
{
-
 
167
	ssize_t i;
-
 
168
	if(!sb || !sb->fill) return 0;
-
 
169
 
-
 
170
	/* Ensure that it is zero-terminated. */
-
 
171
	sb->p[sb->fill-1] = 0;
-
 
172
	for(i=sb->fill-2; i>=0; --i)
-
 
173
	{
-
 
174
		char *c = sb->p+i;
-
 
175
		/* Stop at the first proper character. */
-
 
176
		if(*c && *c != '\r' && *c != '\n') break;
-
 
177
		else *c = 0;
-
 
178
	}
-
 
179
	/* initial fill at least 1, so i at least -1,
-
 
180
	   +2 means nothing happened for fill=1 .
-
 
181
	   With i=0, we got one non-null character, fill shall be 2
-
 
182
	   to accomodate the trailing zero. */
-
 
183
	sb->fill = (size_t)i+2;
-
 
184
 
-
 
185
	return 1;
-
 
186
}