[vlc-devel] [PATCH 3/5] stream_ReadLine: simplify flow and variable types
Pierre Ynard
linkfanel at yahoo.fr
Sun Sep 6 04:40:52 CEST 2020
No functional change
diff --git a/src/input/stream.c b/src/input/stream.c
index a7973b9..aacba86 100644
--- a/src/input/stream.c
+++ b/src/input/stream.c
@@ -188,7 +188,8 @@ char *vlc_stream_ReadLine( stream_t *s )
{
stream_priv_t *priv = (stream_priv_t *)s;
char *p_line = NULL;
- int i_line = 0, i_read = 0;
+ size_t i_line = 0;
+ bool b_data = false;
/* Let's fail quickly if this is a readdir access */
if( s->pf_read == NULL && s->pf_block == NULL )
@@ -198,16 +199,14 @@ char *vlc_stream_ReadLine( stream_t *s )
{
char *psz_eol;
const uint8_t *p_data;
- int i_data;
- int64_t i_pos;
+ ssize_t i_data;
/* Probe new data */
i_data = vlc_stream_Peek( s, &p_data, STREAM_PROBE_LINE );
if( i_data <= 0 ) break; /* No more data */
/* BOM detection */
- i_pos = vlc_stream_Tell( s );
- if( i_pos == 0 && i_data >= 2 )
+ if( vlc_stream_Tell( s ) == 0 && i_data >= 2 )
{
const char *psz_encoding = NULL;
@@ -246,12 +245,14 @@ char *vlc_stream_ReadLine( stream_t *s )
/* keep i_char_width boundary */
i_data = i_data - ( i_data % priv->text.char_width );
msg_Warn( s, "the read is not i_char_width compatible");
- }
- if( i_data == 0 )
- break;
+ if( i_data == 0 )
+ break;
+ }
/* Check if there is an EOL */
+ /* FIXME: this introduces bogus empty lines if the stream
+ uses <CR><LF> and <CR> falls at the end of the buffer */
if( priv->text.char_width == 1 )
{
/* UTF-8: 0A <LF> */
@@ -272,7 +273,7 @@ char *vlc_stream_ReadLine( stream_t *s )
{
if( U16_AT( p ) == eol )
{
- psz_eol = (char *)p + 1;
+ psz_eol = (char *)p;
break;
}
}
@@ -284,7 +285,7 @@ char *vlc_stream_ReadLine( stream_t *s )
{
if( U16_AT( p ) == eol )
{
- psz_eol = (char *)p + 1;
+ psz_eol = (char *)p;
break;
}
}
@@ -292,43 +293,37 @@ char *vlc_stream_ReadLine( stream_t *s )
}
if( psz_eol )
- {
- i_data = (psz_eol - (char *)p_data) + 1;
- p_line = realloc_or_free( p_line,
- i_line + i_data + priv->text.char_width ); /* add \0 */
- if( !p_line )
- goto error;
- i_data = vlc_stream_Read( s, &p_line[i_line], i_data );
- if( i_data <= 0 ) break; /* Hmmm */
- i_line += i_data - priv->text.char_width; /* skip \n */;
- i_read += i_data;
-
- /* We have our line */
- break;
- }
+ i_data = (psz_eol - (char *)p_data) + priv->text.char_width;
/* Read data (+1 for easy \0 append) */
p_line = realloc_or_free( p_line,
- i_line + STREAM_PROBE_LINE + priv->text.char_width );
+ i_line + i_data + priv->text.char_width );
if( !p_line )
goto error;
- i_data = vlc_stream_Read( s, &p_line[i_line], STREAM_PROBE_LINE );
- if( i_data <= 0 ) break; /* Hmmm */
+ i_data = vlc_stream_Read( s, &p_line[i_line], i_data );
+ if( i_data < priv->text.char_width ) break; /* Hmmm */
i_line += i_data;
- i_read += i_data;
+ b_data = true;
+
+ if( psz_eol )
+ {
+ i_line -= priv->text.char_width; /* skip \n */;
+ /* We have our line */
+ break;
+ }
- if( i_read >= STREAM_LINE_MAX )
+ if( i_line >= STREAM_LINE_MAX )
{
msg_Err( s, "line too long, returning EOF instead" );
goto error;
}
}
- if( i_read > 0 )
+ if( b_data )
{
if( priv->text.char_width > 1 )
{
- int i_new_line = 0;
+ size_t i_new_line = 0;
size_t i_in = 0, i_out = 0;
const char * p_in = NULL;
char * p_out = NULL;
@@ -340,19 +335,19 @@ char *vlc_stream_ReadLine( stream_t *s )
psz_new_line = malloc( i_new_line );
if( psz_new_line == NULL )
goto error;
- i_in = (size_t)i_line;
- i_out = (size_t)i_new_line;
+ i_in = i_line;
+ i_out = i_new_line;
p_in = p_line;
p_out = psz_new_line;
- if( vlc_iconv( priv->text.conv, &p_in, &i_in, &p_out, &i_out ) == (size_t)-1 )
+ if( vlc_iconv( priv->text.conv, &p_in, &i_in, &p_out, &i_out ) == VLC_ICONV_ERR )
{
msg_Err( s, "conversion error: %s", vlc_strerror_c( errno ) );
- msg_Dbg( s, "original: %d, in %zu, out %zu", i_line, i_in, i_out );
+ msg_Dbg( s, "original: %zu, in %zu, out %zu", i_line, i_in, i_out );
}
free( p_line );
p_line = psz_new_line;
- i_line = (size_t)i_new_line - i_out; /* does not include \0 */
+ i_line = i_new_line - i_out; /* does not include \0 */
}
/* Remove trailing LF/CR */
--
Pierre Ynard
"Une âme dans un corps, c'est comme un dessin sur une feuille de papier."
More information about the vlc-devel
mailing list