From 53c75f6f3e519d1069457b5ff2e435321fba8e9c Mon Sep 17 00:00:00 2001 From: leitner Date: Thu, 31 Mar 2005 11:57:15 +0000 Subject: [PATCH] add trivial mysql2ldif program --- Makefile | 2 +- ldif.h | 2 +- mysql2ldif.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 mysql2ldif.c diff --git a/Makefile b/Makefile index 1cbfa0f..6d1f196 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ DEBUG=1 all: t1 t2 parse dumpidx idx2ldif addindex bindrequest tinyldap \ tinyldap_standalone tinyldap_debug ldapclient ldapclient_str \ -md5password # t6 # t +md5password mysql2ldif # t6 # t asn1.a: fmt_asn1intpayload.o fmt_asn1length.o fmt_asn1tag.o \ fmt_asn1int.o fmt_asn1string.o fmt_asn1transparent.o scan_asn1tag.o \ diff --git a/ldif.h b/ldif.h index a67b14f..2b784cc 100644 --- a/ldif.h +++ b/ldif.h @@ -2,7 +2,7 @@ #include /* how many attributes do we allow per record? */ -#define ATTRIBS 32 +#define ATTRIBS 100 struct attribute { long name, value; diff --git a/mysql2ldif.c b/mysql2ldif.c new file mode 100644 index 0000000..670e3be --- /dev/null +++ b/mysql2ldif.c @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include +#include +#include + +#define _BSD_SOURCE +#include + +static unsigned long line; + +static void parseerror(char* message) { + char buf[FMT_ULONG]; + buf[fmt_ulong(buf,line)]=0; + die(1,"parse error in line ",buf,": ",message,"!\n"); +} + +static void nomem() { + die(1,"out of memory"); +} + +int main() { + static stralloc sa; + static array fn; /* field names */ + static char* table; + int mode=0; + int pkey; + + while (buffer_getnewline_sa(buffer_0,&sa)==1) { + ++line; + if (!stralloc_0(&sa)) nomem(); + if (stralloc_starts(&sa,"CREATE TABLE ")) { + char* tmp=sa.s+14; + unsigned int i; + mode=1; + for (i=0; isa.s+sa.len) + parseerror("expected ' ' after table name"); + tmp[i]=0; + free(table); + table=strdup(tmp); + if (!table) nomem(); + array_trunc(&fn); + continue; + } else if (mode==1 && stralloc_starts(&sa,") TYPE=")) { + mode=2; + continue; + } + if (mode==1) { + /* parsing CREATE TABLE */ + if (!stralloc_starts(&sa," ")) parseerror("expected two leading spaces"); + if (stralloc_starts(&sa," PRIMARY KEY ")) { + char* tmp=sa.s+14; + int i; + while (*tmp && *tmp!='(') ++tmp; + if (*tmp != '(') parseerror("expected '('"); + ++tmp; + for (i=0; tmp[i] && tmp[i]!=')'; ++i) ; + if (tmp[i]!=')') parseerror("expected ')'"); + tmp[i]=0; pkey=-1; + for (i=0; i