diff options
author | Dmitrij D. Czarkoff <czarkoff@gmail.com> | 2016-05-01 14:37:16 +0200 |
---|---|---|
committer | Dmitrij D. Czarkoff <czarkoff@gmail.com> | 2016-05-01 14:37:16 +0200 |
commit | aaab6d54a4cee653290d9cca59811e3f14217b7c (patch) | |
tree | d4cabbeeaca58cec9c08d7337c57546d7801846a | |
parent | 30db614d328952ea3baf547c5a5d71b6661aba92 (diff) | |
download | imv-aaab6d54a4cee653290d9cca59811e3f14217b7c.tar.gz |
Remove buf_size from struct imv_navigator
Implementation details should not leak to other parts of the code.
While at it, clean up initialization and destruction of navigator.
-rw-r--r-- | src/navigator.c | 38 | ||||
-rw-r--r-- | src/navigator.h | 3 |
2 files changed, 15 insertions, 26 deletions
diff --git a/src/navigator.c b/src/navigator.c index 40a315b..ebf69b2 100644 --- a/src/navigator.c +++ b/src/navigator.c @@ -26,53 +26,37 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. void imv_navigator_init(struct imv_navigator *nav) { - nav->buf_size = 512; - nav->paths = malloc(sizeof(char*) * nav->buf_size); - if (nav->paths == NULL) { - perror("imv_navigator_init"); - exit(1); - } - nav->mtimes = malloc(sizeof(time_t) * nav->buf_size); - if (nav->paths == NULL) { - perror("imv_navigator_init"); - free(nav->paths); - exit(1); - } - nav->num_paths = 0; - nav->cur_path = 0; + memset(nav, 0, sizeof(struct imv_navigator)); nav->last_move_direction = 1; - nav->changed = 0; - nav->wrapped = 0; } void imv_navigator_destroy(struct imv_navigator *nav) { if(nav->paths) { for(int i = 0; i < nav->num_paths; ++i) { - free(nav->paths[i]); + if(nav->paths[i] != NULL) { + free(nav->paths[i]); + } } free(nav->paths); - nav->paths = NULL; } if(nav->mtimes) { free(nav->mtimes); - nav->mtimes = NULL; } - nav->buf_size = 0; - nav->num_paths = 0; + memset(nav, 0, sizeof(struct imv_navigator)); } static void add_item(struct imv_navigator *nav, const char *path, time_t mtime) { - if(nav->buf_size == nav->num_paths) { + if(nav->num_paths % BUFFER_SIZE == 0) { char **new_paths; time_t *new_mtimes; - nav->buf_size *= 2; - new_paths = realloc(nav->paths, sizeof(char*) * nav->buf_size); - new_mtimes = realloc(nav->mtimes, sizeof(time_t) * nav->buf_size); + size_t new_size = nav->num_paths + BUFFER_SIZE; + new_paths = realloc(nav->paths, sizeof(char*) * new_size); + new_mtimes = realloc(nav->mtimes, sizeof(time_t) * new_size); if (new_paths == NULL || new_mtimes == NULL) { perror("add_item"); free(nav->paths); @@ -233,6 +217,10 @@ int imv_navigator_poll_changed(struct imv_navigator *nav, const int nopoll) return 1; } + if(nav->paths == NULL) { + return 0; + }; + if(!nopoll) { struct stat file_info; if(stat(nav->paths[nav->cur_path], &file_info) == -1) { diff --git a/src/navigator.h b/src/navigator.h index b539e8b..5933aa4 100644 --- a/src/navigator.h +++ b/src/navigator.h @@ -20,9 +20,10 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#define BUFFER_SIZE 512 + struct imv_navigator { int num_paths; - int buf_size; int cur_path; char **paths; time_t *mtimes; |