-
Notifications
You must be signed in to change notification settings - Fork 9
/
readconfig.c
67 lines (58 loc) · 1.54 KB
/
readconfig.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
/*
* readconfig.c
*
* Created on: Dec 2, 2017
* Author: arhimed
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "gcodestat.h"
#define MAXLINELENGTH 150 //max config line for smoothieware cannot be longer then 132 chars
int get_config_value(char *lb, char* name, double *val) {
char *p, *q;
int found;
found = -1;
p = strstr(lb, name);
if (p != NULL) {
q = strchr(lb, '#');
if ((q == NULL) || (p < q)) {
if ((p - lb) == 0) {
found = 0;
} else if (p[-1] == ' ') {
found = 0;
}
}
}
if (found == 0) {
*val = atof(p + strlen(name));
}
return (found);
}
/*
* read_config
* http://smoothieware.org/configuration-options
*/
int read_config(char * filename, print_settings_t * print_settings) {
FILE *f;
char *lb;
if (NULL == (f = fopen(filename, "r"))) {
fprintf(stderr, "Cannot open %s for read\n", filename);
return (-1);
}
if (NULL == (lb = calloc(MAXLINELENGTH, 1))) {
fprintf(stderr, "ERROR: Cannot allocate ram for line buffer\n");
fclose(f);
return (-1);
}
while (fgets(lb, MAXLINELENGTH, f)) {
get_config_value(lb, "acceleration ", &(print_settings->accel));
get_config_value(lb, "junction_deviation ", &(print_settings->jdev));
get_config_value(lb, "x_axis_max_speed ", &(print_settings->x_maxspeed));
get_config_value(lb, "y_axis_max_speed ", &(print_settings->y_maxspeed));
get_config_value(lb, "z_axis_max_speed ", &(print_settings->z_maxspeed));
}
fclose(f);
return (0);
}