testing parsing a simple conf file

xkbcommon
Alessandro Mauri 4 years ago
parent 038e312941
commit 1fb0a4c6fa
  1. 3
      .gitignore
  2. 13
      template.conf
  3. 11
      tests/makefile
  4. 42
      tests/parse.c

3
.gitignore vendored

@ -2,3 +2,6 @@
*.swp
*.o
hkd*
tests/parse
tests/ioctl
tests/evtest

@ -0,0 +1,13 @@
# This is a comment
# these are valid entries
keymap = it
layout = qwerty
I dont care about whitespaces = true
# will be translated to Idontcareaboutwhitespaces=true
or tabs = true
# is: ortabs=true
# I can define strings
identifier = "keyboard 0"

@ -0,0 +1,11 @@
CC = gcc
CFLAGS = -Wall -Werror -pedantic -O2
parse: parse.c
ioctl: ioctl.c
evtest: evtest.c
clean:
rm *.o parse ioctl 2> /dev/null

@ -0,0 +1,42 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char const *argv[])
{
if (argc < 2)
return 1;
FILE *fd;
fd = fopen(argv[1], "r");
if (!fd)
return -1;
for (;;) {
char *line = NULL;
size_t linelen = 0;
if (getline(&line, &linelen, fd) == -1)
break;
if (linelen < 2)
continue;
printf("%s\n", line);
// remove white spaces
for (size_t i = 0; i < linelen; i++) {
if (isblank(line[i]))
memmove(&line[i], &line[i + 1], linelen - i);
}
if (line[0] == '#')
continue;
printf("%s\n", line);
char *token = NULL;
token = strtok(line, "=");
if (token)
printf("%s\n", token);
token = strtok(NULL, "=");
if (token)
printf("%s\n", token);
free(line);
}
return 0;
}
Loading…
Cancel
Save