-
Notifications
You must be signed in to change notification settings - Fork 2
/
mapping.c
100 lines (82 loc) · 2.65 KB
/
mapping.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
/*
irmplircd -- zeroconf LIRC daemon that reads IRMP events from the USB IR Remote Receiver
http://www.mikrocontroller.net/articles/USB_IR_Remote_Receiver
Copyright (C) 2011-2012 Dirk E. Wagner
based on:
inputlircd -- zeroconf LIRC daemon that reads from /dev/input/event devices
Copyright (C) 2006 Guus Sliepen <[email protected]>
This program is free software; you can redistribute it and/or modify it
under the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Standard headers */
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sysexits.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
#include <ctype.h>
#include "debug.h"
#include "hashmap.h"
#include "mapping.h"
bool parse_translation_table(const char *path, map_t mymap) {
FILE *table;
char *line = NULL;
size_t line_size = 0;
char key[KEY_MAX_LENGTH];
char value[KEY_MAX_LENGTH];
int error = 0;
int len;
key[0] = value[0] = 0;
if(!path)
return false;
table = fopen(path, "r");
if(!table) {
fprintf(stderr, "Could not open translation table %s: %s\n", path, strerror(errno));
return false;
}
while(getline(&line, &line_size, table) >= 0) {
// Skip empty lines and lines starting with "#"
if (strcspn(line, "\n\r#") == 0)
continue;
len = sscanf(line, "%99s %99[^\n]", key, value);
if(len != 2) {
syslog(LOG_ERR, "line ignored: %s\n", line);
continue;
}
key[KEY_MAX_LENGTH-1] = '\0';
value[KEY_MAX_LENGTH-1] = '\0';
if(strlen(key) < 1 || strlen(value) < 1)
continue;
DBG ("parse_translation_table: key = %s, value = %s\n", key, value);
map_entry_t *map_entry = malloc(sizeof(map_entry_t));
snprintf(map_entry->key, KEY_MAX_LENGTH, "%s", key);
snprintf(map_entry->value, KEY_MAX_LENGTH, "%s", value);
error = hashmap_put(mymap, map_entry->key, map_entry);
if(error) {
fprintf(stderr, "hashmap_put failure: %d\n", error);
fclose(table);
free(line);
return false;
}
}
fclose(table);
free(line);
return true;
}