Small cleanup and fixes.

* Use g_strfreev
    * Use g_ascii*strto(u)ll
This commit is contained in:
Dave Davenport 2014-08-21 19:03:21 +02:00
parent 1a7819275a
commit 63e5f4e940
3 changed files with 96 additions and 108 deletions

View File

@ -144,7 +144,7 @@ void history_set ( const char *filename, const char *entry )
list[length] = g_malloc ( sizeof ( _element ) );
// Copy name
if ( list[length] != NULL ) {
strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
g_strlcpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0';
// set # hits
list[length]->index = 1;

View File

@ -70,7 +70,6 @@
#define LINE_MARGIN 3
#ifdef HAVE_I3_IPC_H
#define I3_SOCKET_PATH_PROP "I3_SOCKET_PATH"
// This setting is no longer user configurable, but partial to this file:
int config_i3_mode = 0;
// Path to HAVE_I3_IPC_H socket.
@ -164,19 +163,6 @@ static char **tokenize ( const char *input )
return retv;
}
static inline void tokenize_free ( char **ip )
{
if ( ip == NULL ) {
return;
}
// Free with g_free.
for ( int i = 0; ip[i] != NULL; i++ ) {
g_free ( ip[i] );
}
g_free ( ip );
}
#ifdef HAVE_I3_IPC_H
// Focus window on HAVE_I3_IPC_H window manager.
static void focus_window_i3 ( const char *socket_path, int id )
@ -815,33 +801,33 @@ int window_match ( char **tokens, __attribute__( ( unused ) ) const char *input,
for ( int j = 0; match && tokens[j]; j++ ) {
int test = 0;
if ( !test && c->title[0] != '\0' ) {
char *sml = g_utf8_casefold ( c->title, -1 );
char *key = g_utf8_collate_key ( sml, -1 );
if ( !test && c->title[0] != '\0' ) {
test = ( strstr ( key, tokens[j] ) != NULL );
}
g_free ( sml ); g_free ( key );
}
sml = g_utf8_casefold ( c->class, -1 );
key = g_utf8_collate_key ( sml, -1 );
if ( !test && c->class[0] != '\0' ) {
char *sml = g_utf8_casefold ( c->class, -1 );
char *key = g_utf8_collate_key ( sml, -1 );
test = ( strstr ( key, tokens[j] ) != NULL );
}
g_free ( sml ); g_free ( key );
}
sml = g_utf8_casefold ( c->role, -1 );
key = g_utf8_collate_key ( sml, -1 );
if ( !test && c->role[0] != '\0' ) {
char *sml = g_utf8_casefold ( c->role, -1 );
char *key = g_utf8_collate_key ( sml, -1 );
test = ( strstr ( key, tokens[j] ) != NULL );
}
g_free ( sml ); g_free ( key );
}
sml = g_utf8_casefold ( c->name, -1 );
key = g_utf8_collate_key ( sml, -1 );
if ( !test && c->name[0] != '\0' ) {
char *sml = g_utf8_casefold ( c->name, -1 );
char *key = g_utf8_collate_key ( sml, -1 );
test = ( strstr ( key, tokens[j] ) != NULL );
}
g_free ( sml ); g_free ( key );
}
if ( test == 0 ) {
match = 0;
@ -861,21 +847,10 @@ static int lev_sort ( const void *p1, const void *p2, void *arg )
return distances[*a] - distances[*b];
}
static int levenshtein ( const char *s, const char *t )
static int dist ( const char *s, const char *t, int *d, int ls, int lt, int i, int j )
{
int ls = strlen ( s ), lt = strlen ( t );
int d[ls + 1][lt + 1];
for ( int i = 0; i <= ls; i++ ) {
for ( int j = 0; j <= lt; j++ ) {
d[i][j] = -1;
}
}
int dist ( int i, int j )
{
if ( d[i][j] >= 0 ) {
return d[i][j];
if ( d[i * ( lt + 1 ) + j] >= 0 ) {
return d[i * ( lt + 1 ) + j];
}
int x;
@ -886,23 +861,32 @@ static int levenshtein ( const char *s, const char *t )
x = ls - i;
}
else if ( s[i] == t[j] ) {
x = dist ( i + 1, j + 1 );
x = dist ( s, t, d, ls, lt, i + 1, j + 1 );
}
else {
x = dist ( i + 1, j + 1 );
x = dist ( s, t, d, ls, lt, i + 1, j + 1 );
int y;
if ( ( y = dist ( i, j + 1 ) ) < x ) {
if ( ( y = dist ( s, t, d, ls, lt, i, j + 1 ) ) < x ) {
x = y;
}
if ( ( y = dist ( i + 1, j ) ) < x ) {
if ( ( y = dist ( s, t, d, ls, lt, i + 1, j ) ) < x ) {
x = y;
}
x++;
}
return d[i][j] = x;
return d[i * ( lt + 1 ) + j] = x;
}
static int levenshtein ( const char *s, const char *t )
{
int ls = strlen ( s ), lt = strlen ( t );
int d[( ls + 1 ) * ( lt + 1 )];
for ( int i = 0; i < ( ( ls + 1 ) * ( lt + 1 ) ); i++ ) {
d[i] = -1;
}
return dist ( 0, 0 );
return dist ( s, t, d, ls, lt, 0, 0 );
}
void window_set_opacity ( Display *display, Window box, unsigned int opacity )
@ -1201,7 +1185,7 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom
// Cleanup + bookkeeping.
filtered_lines = j;
tokenize_free ( tokens );
g_strfreev ( tokens );
}
else{
for ( i = 0; i < num_lines; i++ ) {
@ -1638,12 +1622,7 @@ SwitcherMode run_switcher_window ( char **input, void *data )
}
}
for ( i = 0; i < lines; i++ ) {
g_free ( list[i] );
}
g_free ( list );
g_strfreev ( list );
winlist_free ( ids );
}

View File

@ -39,7 +39,8 @@ typedef enum
{
xrm_String = 0,
xrm_Number = 1,
xrm_Boolean = 2
xrm_SNumber = 2,
xrm_Boolean = 3
} XrmOptionType;
typedef struct
@ -49,6 +50,7 @@ typedef struct
union
{
unsigned int * num;
int * snum;
char ** str;
};
char *mem;
@ -61,7 +63,7 @@ static XrmOption xrmOptions[] = {
{ xrm_String, "switchers", { .str = &config.switchers }, NULL },
{ xrm_Number, "opacity", { .num = &config.window_opacity }, NULL },
{ xrm_Number, "width", { .num = &config.menu_width }, NULL },
{ xrm_SNumber, "width", { .snum = &config.menu_width }, NULL },
{ xrm_Number, "lines", { .num = &config.menu_lines }, NULL },
{ xrm_Number, "columns", { .num = &config.menu_columns }, NULL },
@ -89,8 +91,8 @@ static XrmOption xrmOptions[] = {
{ xrm_Number, "location", { .num = &config.location }, NULL },
{ xrm_Number, "padding", { .num = &config.padding }, NULL },
{ xrm_Number, "yoffset", { .num = &config.y_offset }, NULL },
{ xrm_Number, "xoffset", { .num = &config.x_offset }, NULL },
{ xrm_SNumber, "yoffset", { .snum = &config.y_offset }, NULL },
{ xrm_SNumber, "xoffset", { .snum = &config.x_offset }, NULL },
{ xrm_Boolean, "fixed-num-lines", { .num = &config.fixed_num_lines }, NULL },
{ xrm_Boolean, "hmode", { .num = &config.hmode }, NULL },
@ -142,10 +144,13 @@ void parse_xresource_options ( Display *display )
xrmOptions[i].mem = ( *xrmOptions[i].str );
}
else if ( xrmOptions[i].type == xrm_Number ) {
*xrmOptions[i].num = strtol ( xrmValue.addr, NULL, 10 );
*xrmOptions[i].num = (unsigned int) g_ascii_strtoll ( xrmValue.addr, NULL, 10 );
}
else if ( xrmOptions[i].type == xrm_SNumber ) {
*xrmOptions[i].num = (int) g_ascii_strtoull ( xrmValue.addr, NULL, 10 );
}
else if ( xrmOptions[i].type == xrm_Boolean ) {
if ( xrmValue.size > 0 && strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 ) {
if ( xrmValue.size > 0 && g_ascii_strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 ) {
*xrmOptions[i].num = TRUE;
}
else{
@ -182,11 +187,15 @@ void xresource_dump ( void )
}
}
printf ( "%s.%s: ", namePrefix, xrmOptions[i].name );
printf ( "%s.%s: %*s", namePrefix, xrmOptions[i].name, (int) ( 16 - strlen ( xrmOptions[i].name ) ),
"" );
switch ( xrmOptions[i].type )
{
case xrm_Number:
printf ( "%i", *xrmOptions[i].num );
printf ( "%u", *xrmOptions[i].num );
break;
case xrm_SNumber:
printf ( "%i", *xrmOptions[i].snum );
break;
case xrm_String:
printf ( "%s", *xrmOptions[i].str );