summaryrefslogtreecommitdiffstats
path: root/source/history.c
diff options
context:
space:
mode:
authorQC <qball@gmpclient.org>2014-05-22 09:33:32 +0200
committerQC <qball@gmpclient.org>2014-05-22 09:33:32 +0200
commit21a0666a6e58a373106ec104015b4fc2def62f9f (patch)
treeba6f030c9e0973e99d1012521c1dec847c2f7ae4 /source/history.c
parenta26cf2637d944dd88987f7571e65ec372e2ea7ef (diff)
Cleanups and indenting.
Diffstat (limited to 'source/history.c')
-rw-r--r--source/history.c199
1 files changed, 111 insertions, 88 deletions
diff --git a/source/history.c b/source/history.c
index 759ab3ed..0a7f46bb 100644
--- a/source/history.c
+++ b/source/history.c
@@ -33,8 +33,8 @@
#include "rofi.h"
#include "history.h"
-#define HISTORY_NAME_LENGTH 256
-#define HISTORY_MAX_ENTRIES 25
+#define HISTORY_NAME_LENGTH 256
+#define HISTORY_MAX_ENTRIES 25
typedef struct __element
{
@@ -49,40 +49,41 @@ static int __element_sort_func ( const void *ea, const void *eb )
return b->index - a->index;
}
-static void __history_write_element_list( FILE *fd, _element **list, unsigned int length)
+static void __history_write_element_list ( FILE *fd, _element **list, unsigned int length )
{
- if(list == NULL) {
+ if ( list == NULL )
+ {
return;
}
// Sort the list before writing out.
qsort ( list, length, sizeof ( _element* ), __element_sort_func );
// Set the max length of the list.
- length = (length > HISTORY_MAX_ENTRIES)? HISTORY_MAX_ENTRIES:length;
+ length = ( length > HISTORY_MAX_ENTRIES ) ? HISTORY_MAX_ENTRIES : length;
// Write out entries.
- for(unsigned int iter = 0; iter < length ;iter++)
+ for ( unsigned int iter = 0; iter < length; iter++ )
{
- fprintf(fd , "%ld %s\n", list[iter]->index, list[iter]->name);
- }
+ fprintf ( fd, "%ld %s\n", list[iter]->index, list[iter]->name );
+ }
}
static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
{
- char buffer[HISTORY_NAME_LENGTH+16];
+ char buffer[HISTORY_NAME_LENGTH + 16];
_element **retv = NULL;
- if (length == NULL)
+ if ( length == NULL )
{
return NULL;
}
*length = 0;
- if( fd == NULL)
+ if ( fd == NULL )
{
return NULL;
}
- while ( fgets (buffer, HISTORY_NAME_LENGTH+16, fd ) != NULL)
+ while ( fgets ( buffer, HISTORY_NAME_LENGTH + 16, fd ) != NULL )
{
char * start = NULL;
// Skip empty lines.
@@ -90,18 +91,18 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
{
continue;
}
- retv = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
- retv[(*length)] = malloc ( sizeof ( _element ) );
+ retv = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
+ retv[( *length )] = malloc ( sizeof ( _element ) );
// remove trailing \n
buffer[strlen ( buffer ) - 1] = '\0';
// Parse the number of times.
- retv[(*length)]->index = strtol ( buffer, &start, 10 );
- strncpy(retv[(*length)]->name, (start+1),HISTORY_NAME_LENGTH);
+ retv[( *length )]->index = strtol ( buffer, &start, 10 );
+ strncpy ( retv[( *length )]->name, ( start + 1 ), HISTORY_NAME_LENGTH );
// Force trailing '\0'
- retv[(*length)]->name[HISTORY_NAME_LENGTH-1] = '\0';
- retv[(*length) + 1] = NULL;
+ retv[( *length )]->name[HISTORY_NAME_LENGTH - 1] = '\0';
+ retv[( *length ) + 1] = NULL;
- (*length)++;
+ ( *length )++;
}
return retv;
}
@@ -109,41 +110,44 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
void history_set ( const char *filename, const char *entry )
{
- int found = 0;
+ int found = 0;
unsigned int curr = 0;
unsigned int length = 0;
- _element **list = NULL;
+ _element **list = NULL;
// Open file for reading and writing.
- FILE *fd = fopen(filename, "a+");
- if(fd == NULL)
+ FILE *fd = fopen ( filename, "a+" );
+ if ( fd == NULL )
{
- fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
- return ;
+ fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
+ return;
}
// Get list.
- list = __history_get_element_list(fd, &length);
-
- // Look if the entry exists.
- for(unsigned int iter = 0;!found && iter < length; iter++)
+ list = __history_get_element_list ( fd, &length );
+
+ // Look if the entry exists.
+ for ( unsigned int iter = 0; !found && iter < length; iter++ )
{
- if(strcmp(list[iter]->name, entry) == 0)
+ if ( strcmp ( list[iter]->name, entry ) == 0 )
{
- curr = iter;
+ curr = iter;
found = 1;
}
}
- if(found) {
+ if ( found )
+ {
// If exists, increment list index number
list[curr]->index++;
- }else{
+ }
+ else
+ {
// If not exists, add it.
// Increase list by one
- list = realloc(list,(length+2)*sizeof(_element *));
- list[length] = malloc(sizeof(_element));
+ list = realloc ( list, ( length + 2 ) * sizeof ( _element * ) );
+ list[length] = malloc ( sizeof ( _element ) );
// Copy name
- strncpy(list[length]->name, entry, HISTORY_NAME_LENGTH);
- list[length]->name[HISTORY_NAME_LENGTH-1] = '\0';
+ strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
+ list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0';
// set # hits
list[length]->index = 1;
@@ -152,111 +156,130 @@ void history_set ( const char *filename, const char *entry )
}
// Rewind.
- fseek(fd, 0L, SEEK_SET);
+ fseek ( fd, 0L, SEEK_SET );
// Clear file.
- if ( ftruncate(fileno(fd), 0) == 0)
+ if ( ftruncate ( fileno ( fd ), 0 ) == 0 )
{
// Write list.
- __history_write_element_list(fd, list, length);
- }else {
- fprintf(stderr, "Failed to truncate file: %s\n", strerror(errno));
+ __history_write_element_list ( fd, list, length );
+ }
+ else
+ {
+ fprintf ( stderr, "Failed to truncate file: %s\n", strerror ( errno ) );
}
// Free the list.
- for(unsigned int iter = 0; iter < length; iter++)
+ for ( unsigned int iter = 0; iter < length; iter++ )
+ {
+ if ( list[iter] != NULL )
+ {
+ free ( list[iter] );
+ }
+ }
+ if ( list != NULL )
{
- if(list[iter] != NULL) { free(list[iter]); }
+ free ( list );
}
- if(list != NULL) free(list);
// Close file.
- fclose(fd);
+ fclose ( fd );
}
void history_remove ( const char *filename, const char *entry )
{
- _element ** list = NULL;
- int found = 0;
- unsigned int curr = 0;
- unsigned int length = 0;
+ _element ** list = NULL;
+ int found = 0;
+ unsigned int curr = 0;
+ unsigned int length = 0;
// Open file for reading and writing.
- FILE *fd = fopen(filename, "a+");
- if(fd == NULL)
+ FILE *fd = fopen ( filename, "a+" );
+ if ( fd == NULL )
{
- fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
- return ;
+ fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
+ return;
}
// Get list.
- list = __history_get_element_list(fd, &length);
-
- // Find entry.
- for(unsigned int iter = 0;!found && iter < length; iter++)
+ list = __history_get_element_list ( fd, &length );
+
+ // Find entry.
+ for ( unsigned int iter = 0; !found && iter < length; iter++ )
{
- if(strcmp(list[iter]->name, entry) == 0)
+ if ( strcmp ( list[iter]->name, entry ) == 0 )
{
- curr = iter;
+ curr = iter;
found = 1;
}
}
// If found, remove it and write out new file.
- if(found) {
+ if ( found )
+ {
// Remove the entry.
- free(list[curr]);
+ free ( list[curr] );
// Swap last to here (if list is size 1, we just swap empty sets).
- list[curr] = list[length-1];
+ list[curr] = list[length - 1];
// Empty last.
- list[length-1] = NULL;
+ list[length - 1] = NULL;
length--;
// Rewind.
- fseek(fd, 0L, SEEK_SET);
+ fseek ( fd, 0L, SEEK_SET );
// Clear list.
- if(ftruncate(fileno(fd), 0) == 0) {
+ if ( ftruncate ( fileno ( fd ), 0 ) == 0 )
+ {
// Write list.
- __history_write_element_list(fd, list, length);
- } else {
- fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
+ __history_write_element_list ( fd, list, length );
+ }
+ else
+ {
+ fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
}
}
// Free the list.
- for(unsigned int iter = 0; iter < length; iter++)
+ for ( unsigned int iter = 0; iter < length; iter++ )
+ {
+ if ( list[iter] != NULL )
+ {
+ free ( list[iter] );
+ }
+ }
+ if ( list != NULL )
{
- if(list[iter] != NULL) { free(list[iter]); }
+ free ( list );
}
- if(list != NULL) free(list);
// Close file.
- fclose(fd);
+ fclose ( fd );
}
char ** history_get_list ( const char *filename, unsigned int *length )
{
_element **list = NULL;
- char **retv = NULL;
+ char **retv = NULL;
// Open file.
- FILE *fd = fopen(filename, "r");
- if(fd == NULL)
+ FILE *fd = fopen ( filename, "r" );
+ if ( fd == NULL )
{
- if(errno != ENOENT) {
- fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
+ if ( errno != ENOENT )
+ {
+ fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
}
return NULL;
}
// Get list.
- list = __history_get_element_list(fd, length);
+ list = __history_get_element_list ( fd, length );
- // Copy list in right format.
- if((*length) > 0 )
+ // Copy list in right format.
+ if ( ( *length ) > 0 )
{
- retv = malloc(((*length)+1)*sizeof(char *));
- for ( int iter = 0; iter < (*length); iter++)
+ retv = malloc ( ( ( *length ) + 1 ) * sizeof ( char * ) );
+ for ( int iter = 0; iter < ( *length ); iter++ )
{
- retv[iter] = strdup(list[iter]->name);
- free(list[iter]);
+ retv[iter] = strdup ( list[iter]->name );
+ free ( list[iter] );
}
- retv[(*length)] = NULL;
- free(list);
+ retv[( *length )] = NULL;
+ free ( list );
}
- fclose(fd);
+ fclose ( fd );
return retv;
}