From 53359d6fb34d24c091e7999d2db4913454a976df Mon Sep 17 00:00:00 2001 From: erickson Date: Mon, 6 Jun 2005 17:52:07 +0000 Subject: [PATCH] C fieldmapper stuff git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@329 9efc2488-bf62-4759-914b-345cdb29e865 --- src/utils/Makefile | 14 ++- src/utils/fieldmapper-c.pl | 229 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+), 3 deletions(-) create mode 100755 src/utils/fieldmapper-c.pl diff --git a/src/utils/Makefile b/src/utils/Makefile index 12e8ce9..e168156 100644 --- a/src/utils/Makefile +++ b/src/utils/Makefile @@ -2,15 +2,23 @@ CC = gcc LIB_DIR = ../../lib CC_OPTS = -fPIC -Wall -O2 -I ../../include EXE_LD_OPTS = -L $(LIB_DIR) +INCLUDE = ../../include +FM_HEADER = $(INCLUDE)/opensrf/fieldmapper.h +FM_SOURCE = fieldmapper.c -all: util +all: util fieldmapper util: md5 md5: md5.c $(CC) -c $(CC_OPTS) md5.c -o md5.o $(CC) -shared -W1 md5.o -o $(LIB_DIR)/libmd5.so - /bin/rm -f md5.o + +fieldmapper: fieldmapper-c.pl + perl fieldmapper-c.pl $(FM_HEADER) $(FM_SOURCE) + $(CC) -c $(CC_OPTS) fieldmapper.c -o fieldmapper.o + $(CC) -shared -W1 fieldmapper.o -o $(LIB_DIR)/libopensrf_fm.so clean: - /bin/rm -f $(LIB_DIR)/libmd5.so + /bin/rm -f md5.o + /bin/rm -f $(FM_SOURCE) $(FM_HEADER) diff --git a/src/utils/fieldmapper-c.pl b/src/utils/fieldmapper-c.pl new file mode 100755 index 0000000..a362f41 --- /dev/null +++ b/src/utils/fieldmapper-c.pl @@ -0,0 +1,229 @@ +#!/usr/bin/perl +use strict; use warnings; +use Data::Dumper; +use OpenILS::Utils::Fieldmapper; + + +if(!$ARGV[1]) { + print "usage: $0 \n"; + exit; +} + +warn "Generating fieldmapper-c code...\n"; + +#open(HEADER, ">fieldmapper.h"); +#open(SOURCE, ">fieldmapper.c"); + +print $ARGV[0] . "\n"; +print $ARGV[1] . "\n"; + +open(HEADER, ">$ARGV[0]"); +open(SOURCE, ">$ARGV[1]"); + +my $map = $Fieldmapper::fieldmap; + +print HEADER <classname. */ +fm_obj* fm_factory(object*); + +/* builds a new fieldmapper object by class name */ +fm_obj* fm_factory_string(char*); + +void fm_free(fm_obj* fm); +C + +print SOURCE <array); + free(fm); +} +C + +my $factory_string = <classname) return NULL; + fm_obj* fm = NULL; + +C + +my $fstring_string = <{$object}->{hint}; + my $func_header_string = ""; + my $func_code_string = ""; + my $constructor_string = ""; + + $factory_string .= <classname,"$short_name")) + fm = (fm_obj*) fm_new_$short_name(obj); +CODE + + $fstring_string .= <set_class(obj, "$short_name"); +CODE + + my $header_string = <array) return NULL; + growing_buffer* buf = buffer_init(256); + buffer_fadd(buf, "%s => {\\n", fm->classname); + object* o; +CODE + + $constructor_string .= <array = new_object(NULL); + fm->array->is_array = 1; + fm->array->set_class(fm->array, "$short_name"); + + } else { + + /* we don't want invalid objects */ + if (!obj_array->classname) return NULL; + if(strcmp(obj_array->classname, "$short_name")) return NULL; + + char* s = obj_array->to_json(obj_array); + fm->array = json_parse_string(s); + free(s); + } + + fm->classname = fm->array->classname; + fm->to_string = &fm_${short_name}_to_string; +CODE + + for my $field (keys %{$map->{$object}->{fields}}) { + my $position = $map->{$object}->{fields}->{$field}->{position}; + + $to_string .= <array->get_index(fm->array, $position); + if(o) { + char* str = o->to_json(o); + buffer_fadd(buf, " $field\\t: %s\\n", str); + free(str); + }; +CODE + + $fstring_string .= <is_null = 1; + obj->add_index(obj, $position, tmp); +CODE + + $header_string .= <set_$field = &fm_${short_name}_set_$field; + fm->get_$field = &fm_${short_name}_get_$field; +CODE + + $func_header_string .= <array) return NULL; + return fm->array->get_index(fm->array, $position); + } + + void fm_${short_name}_set_$field(fm_$short_name* fm, object* obj) { + if(!fm || !fm->array || !obj) return; + fm->array->add_index(fm->array, $position, obj); + } +CODE + + } + + $fstring_string .= <