-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.c
38 lines (35 loc) · 1.03 KB
/
files.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "files.h"
#include <stdio.h>//fopen, fclose, fseek, fread, ftell, fscanf, fprintf
#include <stdlib.h>//malloc, free, exit
#include <string.h>//strlen, strtok
void saveTheme(int theme){
FILE *fp = fopen("../data/.theme.txt", "w");
fprintf(fp, "%d", theme);
fclose(fp);
}
int loadTheme(void){
int theme = 0;
FILE *fp = fopen("../data/.theme.txt", "r");
fscanf(fp, "%d", &theme);
fclose(fp);
if(theme > 4) return 4;
else if (theme < 0) return 0;
else return theme;
}
void loadTagsFromFile(struct FileTags *file_tags){
FILE *fp = fopen("../data/tags.txt", "r");
fseek(fp, 0L, SEEK_END);
long int char_count = ftell(fp);
file_tags->tags_data = malloc(char_count + 1);
fseek(fp, 0L, SEEK_SET);
fread(file_tags->tags_data, 1, char_count, fp);
file_tags->tags_data[char_count] = '\0';
fclose(fp);
file_tags->tags_count = 0;
char *token = strtok(file_tags->tags_data, "\n");
for(int i = 0; token; i++){
file_tags->tags_count++;
file_tags->tag_ptrs[i] = token;
token = strtok(NULL, "\n");
}
}