-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconf.c
More file actions
134 lines (103 loc) · 2.84 KB
/
conf.c
File metadata and controls
134 lines (103 loc) · 2.84 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include "gpio.h"
#include "conf.h"
FILE *conf_open(char *file)
{
FILE *fp;
fp = fopen(file, "r");
return fp;
}
int conf_read(FILE *fp, struct gpio_node **list)
{
char buff[75];
int bytes_read = 0;
char pinDefs = 0;
char *ptr;
while(fgets(buff, sizeof(buff), fp) != NULL){
//Removing newline
buff[strlen(buff) - 1] = '\0';
//printf("Line => %s\n", buff);
//Looking for pin mappings
if(strcmp("[Pins]", buff) == 0){
pinDefs = 1;
}
if(pinDefs == 1){
if((ptr = strchr(buff, ',')) != NULL){
int len1, len2;
*ptr = '\0';
len1 = strlen(buff);
len2 = strlen(&buff[len1 + 1]);
//buff[len2 - 1] = '\0';
//len2 = len2 - 1;
printf("%s\n", buff);
printf("%s\n", &buff[len1+1]);
printf("Len = %d\n", strlen(&buff[len1+1]));
gpio_t *gpdata = malloc(sizeof(gpio_t));
gpdata->pin = atoi(buff);
gpdata->mode = 0;
gpdata->val = 0;
//struct gpio_node *tmp;
//tmp = *list;
if((*list) != NULL){
list = &((*list)->next);
}
(*list) = (struct gpio_node *)malloc(sizeof(struct gpio_node));
//strlen returns the length of the string without the null terminator
char *str = malloc(len2 + 1);
memcpy(str, &buff[len1 + 1], len2 + 1);
(*list)->gpio = gpdata;
(*list)->cmd = str;
(*list)->next = NULL;
printf("Copy = %s\n", str);
}
}
}
}
int conf_close(FILE *fp)
{
//if(fd > 0){
fclose(fp);
//}
}
int conf_free(struct gpio_node **list)
{
struct gpio_node *tmp;
while((*list) != NULL){
tmp = *list;
list = &((*list)->next);
free(tmp);
}
}
/*void test_mem(struct gpio_node **list){
(*list) = (struct gpio_node *)malloc(sizeof(struct gpio_node));
(*list)->next = NULL;
}*/
//Added for testing purposes
/*int main(void){
struct gpio_node *list = NULL;
FILE *fp = conf_open("test.conf");
if(fp == NULL){
if(ENOENT == errno){
printf("No config file present\n");
exit(EXIT_SUCCESS);
}
}
conf_read(fp, &list);
conf_close(fp);
//test_mem(&list);
struct gpio_node *tmp;
//tmp = list;
//printf("%d\n", list);
while(list != NULL){
printf("Pin = %d\n", list->gpio->pin);
list = list->next;
}
conf_free(&list);
return 0;
}*/