Skip to content

Instantly share code, notes, and snippets.

@josch
Created July 31, 2010 12:20
Show Gist options
  • Save josch/502123 to your computer and use it in GitHub Desktop.
Save josch/502123 to your computer and use it in GitHub Desktop.
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
Copyright (C) 2010 - Johannes 'josch' Schauer <josch@pyneo.org>
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *hexlookup = "000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
"202122232425262728292a2b2c2d2e2f"
"303132333435363738393a3b3c3d3e3f"
"404142434445464748494a4b4c4d4e4f"
"505152535455565758595a5b5c5d5e5f"
"606162636465666768696a6b6c6d6e6f"
"707172737475767778797a7b7c7d7e7f"
"808182838485868788898a8b8c8d8e8f"
"909192939495969798999a9b9c9d9e9f"
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
void fatal(char *msg, int linenum)
{
fprintf(stderr, "%s at line %d\n", msg, linenum);
exit(1);
}
void putchr(int c)
{
if (fputc(c, stdout) == EOF) {
fprintf(stderr, "cannot write output\n");
exit(2);
}
}
void putstr(char *c, int l)
{
if (!fwrite(c, l, 1, stdout)) {
fprintf(stderr, "cannot write output\n");
exit(2);
}
}
void putchrx(int c)
{
int p = ((unsigned char)c)*2;
if (fputc(hexlookup[p], stdout) == EOF || fputc(hexlookup[p+1], stdout) == EOF) {
fprintf(stderr, "cannot write output\n");
exit(2);
}
}
void putstrx(char *c, int l)
{
int i;
int p;
for (i = 0; i < l; i++, c++) {
p = ((unsigned char)*c)*2;
if (fputc(hexlookup[p], stdout) == EOF || fputc(hexlookup[p+1], stdout) == EOF) {
fprintf(stderr, "cannot write output\n");
exit(2);
}
}
}
int main(void)
{
char * line = NULL;
char * lineptr;
char * endptr;
size_t len = 0;
ssize_t read;
int linenum;
char * tablename;
int tablenamelen = 0;
char *p;
putstr("PRAGMA synchronous=OFF;\n"
"PRAGMA count_changes=OFF;\n"
"PRAGMA cache_size=2000000;\n"
"PRAGMA journal_mode = OFF;\n"
"BEGIN TRANSACTION;\n", 123);
// for each line
for (linenum = 0; (read = getline(&line, &len, stdin)) != -1; linenum++) {
lineptr = line;
if (read <= 15 || strncmp("INSERT INTO `", lineptr, 13)) {
continue;
}
lineptr += 13;
if (tablenamelen) {
if (strncmp(tablename, lineptr, tablenamelen))
fatal("expected different tablename", linenum);
} else {
if ((endptr = strchr(lineptr, '`')) == NULL)
fatal("expected tablename", linenum);
tablenamelen = endptr - lineptr;
if (tablenamelen > 255)
fatal("tablename too long", linenum);
if (tablenamelen == 0)
fatal("no tablename", linenum);
tablename = strndup(lineptr, tablenamelen);
}
lineptr += tablenamelen + 1;
if (strncmp(" VALUES (", lineptr, 9))
fatal("expected 'VALUES ('", linenum);
lineptr += 9;
// for each record
for (;;) {
putstr("INSERT INTO '", 13);
putstr(tablename, tablenamelen);
putstr("' VALUES (", 10);
// for each column
for (;;) {
if (*lineptr == '\'') {
putchr('x');
putchr('\'');
lineptr++;
// for each escape char
for (;;) {
if ((p = strpbrk(lineptr, "\\'")) == NULL)
fatal("expected '\\' or \"'\"", linenum);
if ((p-lineptr))
putstrx(lineptr, (p-lineptr));
if (*p == '\'') {
lineptr = p;
break;
} else {
switch (*(++p)) {
case 'n':
putchrx('\n'); break;
case 'r':
putchrx('\r'); break;
case '0':
putchrx('\0'); break;
case '"':
case '\\':
case '\'':
putchrx(*p); break;
default:
fatal("unknown escape character", linenum);
}
lineptr = ++p;
}
}
putchr('\'');
lineptr++;
} else {
if ((p = strpbrk(lineptr, ",)")) == NULL)
fatal("expected ',' or ')'", linenum);
putstr(lineptr, (p-lineptr));
lineptr = p;
}
if (*lineptr == ')')
break;
if (*(lineptr++) != ',')
fatal("expected ','", linenum);
putchr(',');
}
putstr(");\n", 3);
if (*(++lineptr) == ';') break;
if (*(lineptr++) != ',')
fatal("expected ','", linenum);
if (*(lineptr++) != '(')
fatal("expected '('", linenum);
}
}
putstr("COMMIT TRANSACTION;\n", 20);
if (line)
free(line);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment