Loading of favorites from resource file added.

This commit is contained in:
Martin Dvorak 2014-03-29 13:19:15 +01:00
parent 0f32fe0c65
commit 105d66c75d
2 changed files with 62 additions and 43 deletions

View file

@ -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)

View file

@ -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