summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Davenport <qball@gmpclient.org>2016-04-10 13:55:18 +0200
committerDave Davenport <qball@gmpclient.org>2016-04-10 13:55:18 +0200
commitb63f8e22754148e859b2ebf463353abfb9a17dbe (patch)
tree0359b3dc84fdb30c1748d2822da36468dd4affe1
parent93362d63f2446fe9d1968eccf0f119516da3bf3a (diff)
Move history to getline and remove magic limit.
-rw-r--r--source/dialogs/window.c1
-rw-r--r--source/history.c24
-rw-r--r--test/history-test.c7
3 files changed, 21 insertions, 11 deletions
diff --git a/source/dialogs/window.c b/source/dialogs/window.c
index 57684151..14e85581 100644
--- a/source/dialogs/window.c
+++ b/source/dialogs/window.c
@@ -55,7 +55,6 @@
#define CLIENTSTATE 10
#define CLIENTWINDOWTYPE 10
-#define CLIENTROLE 50
// a manageable window
typedef struct
diff --git a/source/history.c b/source/history.c
index e713b3df..612f0e66 100644
--- a/source/history.c
+++ b/source/history.c
@@ -37,13 +37,12 @@
#include "history.h"
#include "settings.h"
-#define HISTORY_NAME_LENGTH 256
#define HISTORY_MAX_ENTRIES 25
typedef struct __element
{
long int index;
- char name[HISTORY_NAME_LENGTH];
+ char *name;
}_element;
static int __element_sort_func ( const void *ea, const void *eb, void *data __attribute__( ( unused ) ) )
@@ -75,7 +74,6 @@ static void __history_write_element_list ( FILE *fd, _element **list, unsigned i
static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
{
- char buffer[HISTORY_NAME_LENGTH + 16];
_element **retv = NULL;
if ( length == NULL ) {
@@ -86,7 +84,10 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
if ( fd == NULL ) {
return NULL;
}
- while ( fgets ( buffer, HISTORY_NAME_LENGTH + 16, fd ) != NULL ) {
+ char *buffer = NULL;
+ size_t buffer_length = 0;
+ ssize_t l = 0;
+ while ( (l = getline ( &buffer, &buffer_length, fd )) > 0 ) {
char * start = NULL;
// Skip empty lines.
if ( strlen ( buffer ) == 0 ) {
@@ -100,13 +101,16 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
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 )]->name = g_strndup(start+1, l-1-(start+1-buffer));
// Force trailing '\0'
- retv[( *length )]->name[HISTORY_NAME_LENGTH - 1] = '\0';
retv[( *length ) + 1] = NULL;
( *length )++;
}
+ if ( buffer != NULL ) {
+ free ( buffer );
+ buffer = NULL;
+ }
return retv;
}
@@ -148,8 +152,7 @@ void history_set ( const char *filename, const char *entry )
list[length] = g_malloc ( sizeof ( _element ) );
// Copy name
if ( list[length] != NULL ) {
- g_strlcpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
- list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0';
+ list[length]->name = g_strdup(entry);
// set # hits
list[length]->index = 1;
@@ -172,6 +175,7 @@ void history_set ( const char *filename, const char *entry )
}
// Free the list.
for ( unsigned int iter = 0; iter < length; iter++ ) {
+ g_free ( list[iter]->name );
g_free ( list[iter] );
}
g_free ( list );
@@ -210,6 +214,7 @@ void history_remove ( const char *filename, const char *entry )
// If found, remove it and write out new file.
if ( found ) {
// Remove the entry.
+ g_free ( list[curr]->name );
g_free ( list[curr] );
// Swap last to here (if list is size 1, we just swap empty sets).
list[curr] = list[length - 1];
@@ -234,6 +239,7 @@ void history_remove ( const char *filename, const char *entry )
// Free the list.
for ( unsigned int iter = 0; iter < length; iter++ ) {
+ g_free ( list[iter]->name );
g_free ( list[iter] );
}
if ( list != NULL ) {
@@ -268,7 +274,7 @@ char ** history_get_list ( const char *filename, unsigned int *length )
if ( ( *length ) > 0 ) {
retv = g_malloc ( ( ( *length ) + 1 ) * sizeof ( char * ) );
for ( unsigned int iter = 0; iter < ( *length ); iter++ ) {
- retv[iter] = g_strdup ( list[iter]->name );
+ retv[iter] = ( list[iter]->name );
g_free ( list[iter] );
}
retv[( *length )] = NULL;
diff --git a/test/history-test.c b/test/history-test.c
index b1480ec0..75c5d7ac 100644
--- a/test/history-test.c
+++ b/test/history-test.c
@@ -33,7 +33,6 @@ static void history_test ( void )
TASSERT ( retv != NULL );
TASSERT ( length == 1 );
-
TASSERT ( strcmp ( retv[0], "aap" ) == 0 );
g_strfreev ( retv );
@@ -79,6 +78,12 @@ static void history_test ( void )
TASSERT ( retv != NULL );
TASSERT ( length == 25 );
+ for ( unsigned int in = 0; in < 24; in++ ) {
+ char *p = g_strdup_printf ( "aap%i", in+2);
+ TASSERT ( g_strcmp0(retv[in], p) == 0);
+
+ g_free ( p );
+ }
g_strfreev ( retv );