File src/hstr_favorites.c changed (mode: 100644) (index ad30018..afe316a) |
... |
... |
void favorites_init(FavoriteItems *favorites) |
22 |
22 |
favorites->loaded=false; |
favorites->loaded=false; |
23 |
23 |
} |
} |
24 |
24 |
|
|
|
25 |
|
char* favorites_get_filename() |
|
26 |
|
{ |
|
27 |
|
char *home = getenv(ENV_VAR_HOME); |
|
28 |
|
char *fileName = (char*) malloc(strlen(home) + 1 + strlen(FILE_HH_RC) + 1); |
|
29 |
|
strcpy(fileName, home); |
|
30 |
|
strcat(fileName, "/"); |
|
31 |
|
strcat(fileName, FILE_HH_RC); |
|
32 |
|
return fileName; |
|
33 |
|
} |
|
34 |
|
|
25 |
35 |
void favorites_get(FavoriteItems *favorites) |
void favorites_get(FavoriteItems *favorites) |
26 |
36 |
{ |
{ |
27 |
37 |
if(!favorites->loaded) { |
if(!favorites->loaded) { |
28 |
|
char *home = getenv(ENV_VAR_HOME); |
|
29 |
|
char *fileName=(char*)malloc(strlen(home)+1+strlen(FILE_HH_RC)+1); |
|
30 |
|
strcpy(fileName,home); |
|
31 |
|
strcat(fileName,"/"); |
|
32 |
|
strcat(fileName,FILE_HH_RC); |
|
33 |
|
|
|
|
38 |
|
char* fileName = favorites_get_filename(); |
34 |
39 |
char *file_contents=NULL; |
char *file_contents=NULL; |
35 |
40 |
if(access(fileName, F_OK) != -1) { |
if(access(fileName, F_OK) != -1) { |
36 |
41 |
long input_file_size; |
long input_file_size; |
|
... |
... |
void favorites_get(FavoriteItems *favorites) |
73 |
78 |
favorites->loaded=true; |
favorites->loaded=true; |
74 |
79 |
return; |
return; |
75 |
80 |
} |
} |
|
81 |
|
free(fileName); |
76 |
82 |
} |
} |
77 |
83 |
} |
} |
78 |
84 |
|
|
|
... |
... |
void favorites_choose(FavoriteItems *favorites, char *choice) |
114 |
120 |
|
|
115 |
121 |
void favorites_remove(FavoriteItems *favorites, char *almostDead) |
void favorites_remove(FavoriteItems *favorites, char *almostDead) |
116 |
122 |
{ |
{ |
117 |
|
// TODO: keep slot you have, just change count |
|
|
123 |
|
// TODO: keep slot you have, just change count > ? by pointer or strstr? |
118 |
124 |
|
|
119 |
125 |
favorites_save(favorites); |
favorites_save(favorites); |
120 |
126 |
} |
} |
121 |
127 |
|
|
122 |
128 |
void favorites_save(FavoriteItems *favorites) |
void favorites_save(FavoriteItems *favorites) |
123 |
129 |
{ |
{ |
|
130 |
|
char *fileName=favorites_get_filename(); |
|
131 |
|
|
124 |
132 |
if(favorites->count) { |
if(favorites->count) { |
125 |
|
// TODO shrink file and rewrite it (can be shorter) |
|
|
133 |
|
FILE *output_file = fopen(fileName, "wb"); |
|
134 |
|
rewind(output_file); |
|
135 |
|
int i; |
|
136 |
|
for(i=0; i<favorites->count; i++) { |
|
137 |
|
if(fwrite(favorites->items[i], sizeof(char), strlen(favorites->items[i]), output_file)==-1) { |
|
138 |
|
exit(EXIT_FAILURE); |
|
139 |
|
} |
|
140 |
|
if(fwrite("\n", sizeof(char), strlen("\n"), output_file)==-1) { |
|
141 |
|
exit(EXIT_FAILURE); |
|
142 |
|
} |
|
143 |
|
} |
|
144 |
|
fclose(output_file); |
|
145 |
|
} else { |
|
146 |
|
if(access(fileName, F_OK) != -1) { |
|
147 |
|
FILE *output_file = fopen(fileName, "wb"); |
|
148 |
|
fclose(output_file); |
|
149 |
|
} |
126 |
150 |
} |
} |
|
151 |
|
|
|
152 |
|
free(fileName); |
127 |
153 |
} |
} |
128 |
154 |
|
|
129 |
155 |
void favorites_destroy(FavoriteItems *favorites) |
void favorites_destroy(FavoriteItems *favorites) |