1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-18 13:54:36 -05:00

Use getdelim instead of custom fgets function.

This commit is contained in:
Qball Cow 2015-11-12 14:15:33 +01:00
parent 880f8731be
commit c7dcb4821a
3 changed files with 9 additions and 40 deletions

View file

@ -59,27 +59,22 @@ typedef struct _DmenuModePrivateData
static char **get_dmenu ( unsigned int *length ) static char **get_dmenu ( unsigned int *length )
{ {
const unsigned int buf_size = 1024; char **retv = NULL;
char buffer[buf_size]; unsigned int rvlength = 1;
char **retv = NULL;
char *buffer_end = NULL;
unsigned int rvlength = 1;
*length = 0; *length = 0;
gchar *data = NULL;
while ( ( buffer_end = fgets_s ( buffer, buf_size, stdin, (char) config.separator ) ) != NULL ) { size_t data_l = 0;
while ( ( getdelim ( &data, &data_l, config.separator, stdin ) > 0 ) ) {
if ( rvlength < ( *length + 2 ) ) { if ( rvlength < ( *length + 2 ) ) {
rvlength *= 2; rvlength *= 2;
retv = g_realloc ( retv, ( rvlength ) * sizeof ( char* ) ); retv = g_realloc ( retv, ( rvlength ) * sizeof ( char* ) );
} }
size_t blength = buffer_end - &( buffer[0] ); retv[( *length )] = data; //copy;
char *copy = g_malloc0 ( blength + 1 );
memcpy ( copy, buffer, blength );
retv[( *length )] = copy;
retv[( *length ) + 1] = NULL; retv[( *length ) + 1] = NULL;
data = NULL;
data_l = 0;
( *length )++; ( *length )++;
// Stop when we hit 2³¹ entries. // Stop when we hit 2³¹ entries.

View file

@ -55,32 +55,6 @@ void cmd_set_arguments ( int argc, char **argv )
stored_argv = argv; stored_argv = argv;
} }
/**
* `fgets` implementation with custom separator.
*/
char* fgets_s ( char* s, unsigned int n, FILE *iop, char sep )
{
// Map these to registers.
register int c = EOF;
register char* cs;
cs = s;
// read until EOF or buffer is full.
while ( --n > 0 && ( c = getc ( iop ) ) != EOF ) {
// put the input char into the current pointer position, then increment it
// if a newline entered, break
if ( ( *cs++ = c ) == sep ) {
// Whipe separator
cs[-1] = '\0';
break;
}
}
// Always, 0 terminate the buffer.
*cs = '\0';
// if last read was end of file and current index is start, we are done:
// Return NULL.
return ( c == EOF && cs == s ) ? NULL : cs;
}
/** /**
* @param info To Match information on. * @param info To Match information on.
* @param res The string being generated. * @param res The string being generated.

View file

@ -749,7 +749,7 @@ static void menu_refilter ( MenuState *state )
* If number of threads > 1 and there are enough (> 20000) items, spawn threads. * If number of threads > 1 and there are enough (> 20000) items, spawn threads.
* For large lists with 8 threads I see a factor three speedup of the whole function. * For large lists with 8 threads I see a factor three speedup of the whole function.
*/ */
unsigned int nt = MAX( 1, MIN ( state->num_lines / 1000, config.threads )); unsigned int nt = MAX ( 1, MIN ( state->num_lines / 1000, config.threads ) );
thread_state states[nt]; thread_state states[nt];
GThread *threads[nt]; GThread *threads[nt];
unsigned int steps = ( state->num_lines + nt ) / nt; unsigned int steps = ( state->num_lines + nt ) / nt;