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
39
40
41
42
43
44
45
46
47
48
49
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <volk/volk_prefs.h>
//#if defined(_WIN32)
//#include <Windows.h>
//#endif
void get_config_path(char *path) {
const char *suffix = "/.volk/volk_config";
strcpy(path, getenv("HOME"));
strcat(path, suffix);
}
//passing by reference in C can (***********)
int load_preferences(struct volk_arch_pref **prefs) {
FILE *config_file;
char path[512], line[512], function[128], arch[32];
int n_arch_prefs = 0;
struct volk_arch_pref *t_pref;
//get the config path
get_config_path(path);
config_file = fopen(path, "r");
if(!config_file) return n_arch_prefs; //no prefs found
while(fgets(line, 512, config_file) != NULL) {
if(sscanf(line, "%s %s", function, arch) == 2 && !strncmp(function, "volk_", 5)) {
n_arch_prefs++;
}
}
//now allocate the memory required for volk_arch_prefs
(*prefs) = (struct volk_arch_pref *) malloc(n_arch_prefs * sizeof(struct volk_arch_pref));
t_pref = (*prefs);
//reset the file pointer and write the prefs into volk_arch_prefs
rewind(config_file);
while(fgets(line, 512, config_file) != NULL) {
if(sscanf(line, "%s %s", function, arch) == 2 && !strncmp(function, "volk_", 5)) {
strncpy(t_pref->name, function, 128);
strncpy(t_pref->arch, arch, 32);
t_pref++;
}
}
fclose(config_file);
return n_arch_prefs;
}
|