| File src/hstr_favorites.c changed (mode: 100644) (index 39788fa..e9ed6f3) |
| 25 |
25 |
|
|
| 26 |
26 |
#define FAVORITE_SEGMENT_SIZE 10 |
#define FAVORITE_SEGMENT_SIZE 10 |
| 27 |
27 |
|
|
| 28 |
|
void favorites_init(FavoriteItems *favorites) |
|
|
28 |
|
void favorites_init(FavoriteItems* favorites) |
| 29 |
29 |
{ |
{ |
| 30 |
30 |
favorites->items=NULL; |
favorites->items=NULL; |
| 31 |
31 |
favorites->count=0; |
favorites->count=0; |
| |
| ... |
... |
void favorites_show(FavoriteItems *favorites) |
| 48 |
48 |
|
|
| 49 |
49 |
char* favorites_get_filename() |
char* favorites_get_filename() |
| 50 |
50 |
{ |
{ |
| 51 |
|
char *home = getenv(ENV_VAR_HOME); |
|
| 52 |
|
char *fileName = (char*) malloc(strlen(home) + 1 + strlen(FILE_HH_FAVORITES) + 1); |
|
|
51 |
|
char* home = getenv(ENV_VAR_HOME); |
|
52 |
|
char* fileName = (char*) malloc(strlen(home) + 1 + strlen(FILE_HH_FAVORITES) + 1); |
| 53 |
53 |
strcpy(fileName, home); |
strcpy(fileName, home); |
| 54 |
54 |
strcat(fileName, "/"); |
strcat(fileName, "/"); |
| 55 |
55 |
strcat(fileName, FILE_HH_FAVORITES); |
strcat(fileName, FILE_HH_FAVORITES); |
| 56 |
56 |
return fileName; |
return fileName; |
| 57 |
57 |
} |
} |
| 58 |
58 |
|
|
| 59 |
|
void favorites_get(FavoriteItems *favorites) |
|
|
59 |
|
void favorites_get(FavoriteItems* favorites) |
| 60 |
60 |
{ |
{ |
| 61 |
61 |
if(!favorites->loaded) { |
if(!favorites->loaded) { |
| 62 |
62 |
char* fileName = favorites_get_filename(); |
char* fileName = favorites_get_filename(); |
| 63 |
|
char *fileContent=NULL; |
|
|
63 |
|
char* fileContent = NULL; |
| 64 |
64 |
if(access(fileName, F_OK) != -1) { |
if(access(fileName, F_OK) != -1) { |
| 65 |
65 |
long inputFileSize; |
long inputFileSize; |
| 66 |
66 |
|
|
| 67 |
|
FILE *inputFile = fopen(fileName, "rb"); |
|
|
67 |
|
FILE* inputFile = fopen(fileName, "rb"); |
| 68 |
68 |
fseek(inputFile, 0, SEEK_END); |
fseek(inputFile, 0, SEEK_END); |
| 69 |
69 |
inputFileSize = ftell(inputFile); |
inputFileSize = ftell(inputFile); |
| 70 |
70 |
rewind(inputFile); |
rewind(inputFile); |
| |
| ... |
... |
void favorites_get(FavoriteItems *favorites) |
| 77 |
77 |
|
|
| 78 |
78 |
if(fileContent && strlen(fileContent)) { |
if(fileContent && strlen(fileContent)) { |
| 79 |
79 |
favorites->count = 0; |
favorites->count = 0; |
| 80 |
|
char *p=strchr(fileContent,'\n'); |
|
|
80 |
|
char* p=strchr(fileContent,'\n'); |
| 81 |
81 |
while (p!=NULL) { |
while (p!=NULL) { |
| 82 |
82 |
favorites->count++; |
favorites->count++; |
| 83 |
83 |
p=strchr(p+1,'\n'); |
p=strchr(p+1,'\n'); |
| |
| ... |
... |
void favorites_get(FavoriteItems *favorites) |
| 85 |
85 |
|
|
| 86 |
86 |
favorites->items = malloc(sizeof(char*) * favorites->count); |
favorites->items = malloc(sizeof(char*) * favorites->count); |
| 87 |
87 |
favorites->count = 0; |
favorites->count = 0; |
| 88 |
|
char *pb=fileContent, *pe, *s; |
|
|
88 |
|
char* pb=fileContent, *pe, *s; |
| 89 |
89 |
pe=strchr(fileContent, '\n'); |
pe=strchr(fileContent, '\n'); |
| 90 |
90 |
while(pe!=NULL) { |
while(pe!=NULL) { |
| 91 |
91 |
*pe=0; |
*pe=0; |
| |
| ... |
... |
void favorites_get(FavoriteItems *favorites) |
| 102 |
102 |
} else { |
} else { |
| 103 |
103 |
// favorites file not found > favorites don't exist yet |
// favorites file not found > favorites don't exist yet |
| 104 |
104 |
favorites->loaded=true; |
favorites->loaded=true; |
| 105 |
|
return; |
|
| 106 |
105 |
} |
} |
| 107 |
106 |
free(fileName); |
free(fileName); |
| 108 |
107 |
} |
} |
| 109 |
108 |
} |
} |
| 110 |
109 |
|
|
| 111 |
|
void favorites_save(FavoriteItems *favorites) |
|
|
110 |
|
void favorites_save(FavoriteItems* favorites) |
| 112 |
111 |
{ |
{ |
| 113 |
|
char *fileName=favorites_get_filename(); |
|
|
112 |
|
char* fileName=favorites_get_filename(); |
| 114 |
113 |
|
|
| 115 |
114 |
if(favorites->count) { |
if(favorites->count) { |
| 116 |
|
FILE *output_file = fopen(fileName, "wb"); |
|
|
115 |
|
FILE* output_file = fopen(fileName, "wb"); |
| 117 |
116 |
rewind(output_file); |
rewind(output_file); |
| 118 |
117 |
int i; |
int i; |
| 119 |
118 |
for(i=0; i<favorites->count; i++) { |
for(i=0; i<favorites->count; i++) { |
| |
| ... |
... |
void favorites_save(FavoriteItems *favorites) |
| 134 |
133 |
free(fileName); |
free(fileName); |
| 135 |
134 |
} |
} |
| 136 |
135 |
|
|
| 137 |
|
void favorites_add(FavoriteItems *favorites, char *newFavorite) |
|
|
136 |
|
void favorites_add(FavoriteItems* favorites, char* newFavorite) |
| 138 |
137 |
{ |
{ |
| 139 |
138 |
if(favorites->count) { |
if(favorites->count) { |
| 140 |
|
favorites->items=realloc(favorites->items, sizeof(char *) * ++favorites->count); |
|
|
139 |
|
favorites->items=realloc(favorites->items, sizeof(char*) * ++favorites->count); |
| 141 |
140 |
favorites->items[favorites->count-1]=hstr_strdup(newFavorite); |
favorites->items[favorites->count-1]=hstr_strdup(newFavorite); |
| 142 |
141 |
favorites_choose(favorites, newFavorite); |
favorites_choose(favorites, newFavorite); |
| 143 |
142 |
} else { |
} else { |
| |
| ... |
... |
void favorites_add(FavoriteItems *favorites, char *newFavorite) |
| 150 |
149 |
hashset_add(favorites->set, newFavorite); |
hashset_add(favorites->set, newFavorite); |
| 151 |
150 |
} |
} |
| 152 |
151 |
|
|
| 153 |
|
void favorites_choose(FavoriteItems *favorites, char *choice) |
|
|
152 |
|
void favorites_choose(FavoriteItems* favorites, char* choice) |
| 154 |
153 |
{ |
{ |
| 155 |
154 |
if(favorites->count && choice) { |
if(favorites->count && choice) { |
| 156 |
155 |
int r; |
int r; |
| 157 |
|
char *b=NULL, *next; |
|
|
156 |
|
char* b=NULL, *next; |
| 158 |
157 |
for(r=0; r<favorites->count; r++) { |
for(r=0; r<favorites->count; r++) { |
| 159 |
158 |
if(!strcmp(favorites->items[r],choice)) { |
if(!strcmp(favorites->items[r],choice)) { |
| 160 |
159 |
favorites->items[0]=favorites->items[r]; |
favorites->items[0]=favorites->items[r]; |
| |
| ... |
... |
void favorites_choose(FavoriteItems *favorites, char *choice) |
| 171 |
170 |
} |
} |
| 172 |
171 |
} |
} |
| 173 |
172 |
|
|
| 174 |
|
bool favorites_remove(FavoriteItems *favorites, char *almostDead) |
|
|
173 |
|
bool favorites_remove(FavoriteItems* favorites, char* almostDead) |
| 175 |
174 |
{ |
{ |
| 176 |
175 |
if(favorites->count) { |
if(favorites->count) { |
| 177 |
176 |
int r, w, count; |
int r, w, count; |
| |
| ... |
... |
bool favorites_remove(FavoriteItems *favorites, char *almostDead) |
| 194 |
193 |
} |
} |
| 195 |
194 |
} |
} |
| 196 |
195 |
|
|
| 197 |
|
void favorites_destroy(FavoriteItems *favorites) |
|
|
196 |
|
void favorites_destroy(FavoriteItems* favorites) |
| 198 |
197 |
{ |
{ |
| 199 |
198 |
if(favorites) { |
if(favorites) { |
| 200 |
199 |
// TODO hashset destroys keys - no need to destroy items! |
// TODO hashset destroys keys - no need to destroy items! |