-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_ini.c
136 lines (118 loc) · 4.75 KB
/
parse_ini.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // For isspace and tolower functions
#include "parse_ini.h"
#include "t31-c-utils.h"
char* trim_whitespace(char* str) {
char *end;
// Trim leading space
while(isspace((unsigned char)*str)) str++;
if(*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
// Write new null terminator character
end[1] = '\0';
return str;
}
void ini_parse_line(char *section, char *key, char *value) {
// Handle sensor section
if (strcmp(section, "sensor") == 0) {
if (strcmp(key, "name") == 0) {
strncpy(config.name, value, MAX_STR-1);
} else if (strcmp(key, "width") == 0) {
config.width = atoi(value);
} else if (strcmp(key, "height") == 0) {
config.height = atoi(value);
} else if (strcmp(key, "i2c_addr") == 0) {
config.i2c_addr = strtol(value, NULL, 16);
}
}
// Handle channel sections
else if (strcmp(section, "channel0") == 0 || strcmp(section, "channel1") == 0) {
int channelIndex = section[7] - '0';
if (strcmp(key, "width") == 0) {
config.channels[channelIndex].width = atoi(value);
} else if (strcmp(key, "height") == 0) {
config.channels[channelIndex].height = atoi(value);
} else if (strcmp(key, "frnum") == 0) {
config.channels[channelIndex].frnum = atoi(value);
} else if (strcmp(key, "frden") == 0) {
config.channels[channelIndex].frden = atoi(value);
}
}
// Existing logic for qrcode and natural sections
else if (strcmp(section, "qrcode") == 0 || strcmp(section, "natural") == 0) {
ImageProfile *profile = (strcmp(section, "qrcode") == 0) ? &config.qrcode : &config.natural;
if (strcmp(key, "aecomp") == 0) {
profile->aecomp = atoi(value);
} else if (strcmp(key, "contrast") == 0) {
profile->contrast = atoi(value);
} else if (strcmp(key, "brightness") == 0) {
profile->brightness = atoi(value);
} else if (strcmp(key, "sharpness") == 0) {
profile->sharpness = atoi(value);
} else if (strcmp(key, "drc") == 0) {
profile->drc = atoi(value);
}
}
}
int parse_ini_file(const char *file_path) {
FILE *file = fopen(file_path, "r");
if (!file) {
perror("Unable to open the file");
return -1;
}
char line[256];
char section[MAX_STR] = {0};
while (fgets(line, sizeof(line), file)) {
char *trimmedLine = trim_whitespace(line);
if (trimmedLine[0] == ';' || trimmedLine[0] == '#' || trimmedLine[0] == '\n') {
continue; // Skip comments and empty lines
}
if (trimmedLine[0] == '[') {
sscanf(trimmedLine, "[%[^]]]", section); // Get section name
section[strcspn(section, "\r\n")] = 0; // Remove newline characters
continue;
}
char key[MAX_STR], value[MAX_STR];
if (sscanf(trimmedLine, "%[^=]=%s", key, value) == 2) {
char *trimmedKey = trim_whitespace(key);
char *trimmedValue = trim_whitespace(value);
ini_parse_line(section, trimmedKey, trimmedValue);
}
}
fclose(file);
return 0;
}
void print_config(const SensorConfig *cfg) {
printf("Sensor Name: %s\n", cfg->name);
printf("Sensor i2c_addr: 0x%X\n", cfg->i2c_addr);
printf("Sensor Width: %d\n", cfg->width);
printf("Sensor Height: %d\n", cfg->height);
for (int i = 0; i < MAX_CHANNELS; ++i) {
printf("Channel %d Width: %d\n", i, cfg->channels[i].width);
printf("Channel %d Height: %d\n", i, cfg->channels[i].height);
printf("Channel %d Frame Rate Num: %d\n", i, cfg->channels[i].frnum);
printf("Channel %d Frame Rate Den: %d\n", i, cfg->channels[i].frden);
}
// Add qrcode and natural profile printing
printf("QR Code Profile AEComp: %d\n", cfg->qrcode.aecomp);
printf("QR Code Profile Contrast: %d\n", cfg->qrcode.contrast);
printf("QR Code Profile Brightness: %d\n", cfg->qrcode.brightness);
printf("QR Code Profile Sharpness: %d\n", cfg->qrcode.sharpness);
printf("QR Code Profile DRC: %d\n", cfg->qrcode.drc);
printf("Natural Profile AEComp: %d\n", cfg->natural.aecomp);
printf("Natural Profile Contrast: %d\n", cfg->natural.contrast);
printf("Natural Profile Brightness: %d\n", cfg->natural.brightness);
printf("Natural Profile Sharpness: %d\n", cfg->natural.sharpness);
printf("Natural Profile DRC: %d\n", cfg->natural.drc);
}
/*int main() {
if (parse_ini_file("config.ini") == 0) {
print_config(&config);
}
return 0;
}*/