#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "db.h"
#include "error.h"
#include "string.h"
int main(int argc, char **argv) {
struct stat st;
struct db *db;
mpool_t *p;
char *line, *nextline, *fmt, *msg;
char *m;
char *lang;
int r;
switch(argc) {
case 1:
lang = "nl";
break;
case 2:
lang = argv[1];
break;
default:
xerror("bad argument");
}
if(fstat(STDIN_FILENO, &st) == -1)
perror_exit("fstat(stdin)");
if(!st.st_size)
xerror("input is empty");
m = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, STDIN_FILENO, 0);
if(m == MAP_FAILED)
perror_exit("mmap(stdin)");
if(m[st.st_size-1] != '\n')
xerror("last line isn't terminated");
m[st.st_size-1] = '\0';
p = mpool_new(NULL);
db = db_begin(p);
if(!db)
xerror("could not start transaction");
for(line = m; line; line = nextline) {
nextline = strchr(line, '\n');
if(nextline)
*nextline++ = '\0';
fmt = line;
while(*fmt && isblank(*fmt))
fmt++;
if(!*fmt || *fmt == '#')
continue;
msg = fmt;
while(*msg && !isblank(*msg))
msg++;
if(!*msg)
xerror("malformed line");
*msg++ = '\0';
strdespace(msg);
if(!*msg)
xerror("malformed line (empty msg)");
db_provide_format(db, lang, fmt, msg);
}
r = !db_commit(db);
mpool_destroy(p);
return r;
}