mirror of
https://github.com/davatorium/rofi.git
synced 2025-02-03 15:34:54 -05:00
Make dmenu reading very marginally faster
A slight reduction in use of realloc and avoidance of 3 or 4 strlens for a string we know the length of
This commit is contained in:
parent
0e60aaf235
commit
574bf2da82
3 changed files with 23 additions and 11 deletions
|
@ -16,7 +16,7 @@ int helper_parse_setup ( char * string, char ***output, int *length, ... );
|
|||
/**
|
||||
* Implementation of fgets with custom separator.
|
||||
*/
|
||||
char* fgets_s ( char* s, int n, FILE *iop, char sep );
|
||||
char* fgets_s ( char* s, unsigned int n, FILE *iop, char sep );
|
||||
|
||||
/**
|
||||
* @param token The string for which we want a collation key.
|
||||
|
|
|
@ -59,28 +59,40 @@ typedef struct _DmenuModePrivateData
|
|||
|
||||
static char **get_dmenu ( unsigned int *length )
|
||||
{
|
||||
char buffer[1024];
|
||||
const unsigned int buf_size = 1024;
|
||||
char buffer[buf_size];
|
||||
char **retv = NULL;
|
||||
char *buffer_end = NULL;
|
||||
unsigned int rvlength = 1;
|
||||
|
||||
*length = 0;
|
||||
|
||||
while ( fgets_s ( buffer, 1024, stdin, (char) config.separator ) != NULL ) {
|
||||
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
||||
retv[( *length )] = g_strdup ( buffer );
|
||||
retv[( *length ) + 1] = NULL;
|
||||
while ( ( buffer_end = fgets_s ( buffer, buf_size, stdin, (char) config.separator ) ) != NULL ) {
|
||||
if (rvlength < (*length + 2)) {
|
||||
rvlength *= 2;
|
||||
retv = g_realloc ( retv, ( rvlength ) * sizeof ( char* ) );
|
||||
}
|
||||
|
||||
size_t blength = buffer_end - &(buffer[0]);
|
||||
|
||||
char *copy = g_malloc( blength + 1 );
|
||||
memcpy(copy, buffer, blength);
|
||||
|
||||
// Filter out line-end.
|
||||
if ( retv[( *length )][strlen ( buffer ) - 1] == '\n' ) {
|
||||
retv[( *length )][strlen ( buffer ) - 1] = '\0';
|
||||
if ( copy[blength] == '\n' ) {
|
||||
copy[blength] = '\0';
|
||||
}
|
||||
|
||||
retv[( *length )] = copy;
|
||||
retv[( *length ) + 1] = NULL;
|
||||
|
||||
( *length )++;
|
||||
// Stop when we hit 2³¹ entries.
|
||||
if ( ( *length ) == INT_MAX ) {
|
||||
return retv;
|
||||
}
|
||||
}
|
||||
|
||||
retv = g_realloc ( retv, ( *length + 1 ) * sizeof ( char* ) );
|
||||
return retv;
|
||||
}
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ void cmd_set_arguments ( int argc, char **argv )
|
|||
/**
|
||||
* `fgets` implementation with custom separator.
|
||||
*/
|
||||
char* fgets_s ( char* s, int n, FILE *iop, char sep )
|
||||
char* fgets_s ( char* s, unsigned int n, FILE *iop, char sep )
|
||||
{
|
||||
// Map these to registers.
|
||||
register int c = EOF;
|
||||
|
@ -72,7 +72,7 @@ char* fgets_s ( char* s, int n, FILE *iop, char sep )
|
|||
*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 : s;
|
||||
return ( c == EOF && cs == s ) ? NULL : cs;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue