From 105d66c75d883b8da24364c0b1e3602d09c356ae Mon Sep 17 00:00:00 2001 From: Martin Dvorak Date: Sat, 29 Mar 2014 13:19:15 +0100 Subject: [PATCH] Loading of favorites from resource file added. --- src/hstr_favorites.c | 99 +++++++++++++++++++++--------------- src/include/hstr_favorites.h | 6 +-- 2 files changed, 62 insertions(+), 43 deletions(-) diff --git a/src/hstr_favorites.c b/src/hstr_favorites.c index 83a2695..ad30018 100644 --- a/src/hstr_favorites.c +++ b/src/hstr_favorites.c @@ -19,57 +19,75 @@ void favorites_init(FavoriteItems *favorites) { favorites->items=NULL; favorites->count=0; + favorites->loaded=false; } -void favorites_load(FavoriteItems *favorites) +void favorites_get(FavoriteItems *favorites) { - // TODO fake initialization instead of .hhrc load - favorites->count=2; - favorites->items=malloc(sizeof(char *)*favorites->count); - favorites->items[0]=malloc(2); - strcpy(favorites->items[0],"a"); - favorites->items[1]=malloc(2); - strcpy(favorites->items[1],"b"); + if(!favorites->loaded) { + char *home = getenv(ENV_VAR_HOME); + char *fileName=(char*)malloc(strlen(home)+1+strlen(FILE_HH_RC)+1); + strcpy(fileName,home); + strcat(fileName,"/"); + strcat(fileName,FILE_HH_RC); - lazy loading (boolean indicator to FavoriteItems) + char *file_contents=NULL; + if(access(fileName, F_OK) != -1) { + long input_file_size; - // TODO load from file + FILE *input_file = fopen(fileName, "rb"); + fseek(input_file, 0, SEEK_END); + input_file_size = ftell(input_file); + rewind(input_file); + file_contents = malloc((input_file_size + 1) * (sizeof(char))); + if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) { + exit(EXIT_FAILURE); + } + fclose(input_file); + file_contents[input_file_size] = 0; - char *home = getenv(ENV_VAR_HOME); - char *fileName=(char*)malloc(strlen(home)+1+strlen(FILE_HH_RC)+1); - strcpy(fileName,home); - strcat(fileName,"/"); - strcat(fileName,FILE_HH_RC); + if(file_contents && strlen(file_contents)) { + favorites->count = 0; + char *p=strchr(file_contents,'\n'); + while (p!=NULL) { + favorites->count++; + p=strchr(p+1,'\n'); + } - char *file_contents=NULL; - if(access(fileName, F_OK) != -1) { - long input_file_size; - - FILE *input_file = fopen(fileName, "rb"); - fseek(input_file, 0, SEEK_END); - input_file_size = ftell(input_file); - rewind(input_file); - file_contents = malloc((input_file_size + 1) * (sizeof(char))); - if(fread(file_contents, sizeof(char), input_file_size, input_file)==-1) { - exit(EXIT_FAILURE); + favorites->items = malloc(sizeof(char*) * favorites->count); + int i = 0; + char *pb=file_contents, *pe; + pe=strchr(file_contents, '\n'); + while(pe!=NULL) { + favorites->items[i]=pb; + *pe=0; + favorites->items[i]=strdup(pb); + pb=pe+1; + pe=strchr(pb, '\n'); + i++; + } + free(file_contents); + } + } else { + // favorites file not found > favorites don't exist yet + favorites->loaded=true; + return; } - fclose(input_file); - file_contents[input_file_size] = 0; - } else { - fprintf(stderr,"\nHistory file not found: %s\n",fileName); - } - - if(file_contents) { - split & process & initilize favorites } } void favorites_add(FavoriteItems *favorites, char *newFavorite) { - favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count); - favorites->items[favorites->count-1]=newFavorite; + if(favorites->count) { + favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count); + favorites->items[favorites->count-1]=strdup(newFavorite); + favorites_choose(favorites, newFavorite); + } else { + favorites->items=malloc(sizeof(char*)); + favorites->items[0]=strdup(newFavorite); + favorites->count=1; + } - favorites_choose(favorites, newFavorite); favorites_save(favorites); } @@ -96,15 +114,16 @@ void favorites_choose(FavoriteItems *favorites, char *choice) void favorites_remove(FavoriteItems *favorites, char *almostDead) { - // TODO: keept slot you have, just change count + // TODO: keep slot you have, just change count favorites_save(favorites); } void favorites_save(FavoriteItems *favorites) { - create/update history file - + if(favorites->count) { + // TODO shrink file and rewrite it (can be shorter) + } } void favorites_destroy(FavoriteItems *favorites) diff --git a/src/include/hstr_favorites.h b/src/include/hstr_favorites.h index 45d4e91..5a91a86 100644 --- a/src/include/hstr_favorites.h +++ b/src/include/hstr_favorites.h @@ -15,19 +15,19 @@ #define ENV_VAR_USER "USER" #define ENV_VAR_HOME "HOME" -#define FILE_HH_RC ".hhrc" +#define FILE_HH_RC ".hh_favorites" typedef struct { char **items; unsigned count; + bool loaded; } FavoriteItems; void favorites_init(FavoriteItems *favorites); -void favorites_load(FavoriteItems *favorites); +void favorites_get(FavoriteItems *favorites); void favorites_add(FavoriteItems *favorites, char *favorite); void favorites_choose(FavoriteItems *favorites, char *choice); void favorites_remove(FavoriteItems *favorites, char *almostDead); -void favorites_save(FavoriteItems *favorites); void favorites_destroy(FavoriteItems *favorites); #endif