[DCPM] some example code:

Neil Williams linux at codehelp.co.uk
Fri Jul 1 15:36:45 PDT 2005


This illustrates the problem of quoting and escaping. I'll be changing this to 
use $a <<MARKER;
...
MARKER
sometime over the weekend which will remove all the \\ and \$ nonsense.

Several simple substitutions will then remain.

sub makefile {
$Makefile = "lib_LTLIBRARIES = lib" . $object . "qof.la

AM_CFLAGS = \\
  -I.. -I../.. \\
  -I../popt \\
  \${QOF_CFLAGS} \\
  \${GLIB_CFLAGS}

lib" . $object . "qof_la_LIBADD= \\
	\$(GLIB_LIBS)	\\
	\$(QOF_LIBS)

lib" . $object . "qof_la_SOURCES =  \\
        " . $object . ".c

bin_PROGRAMS=\\
        qof-" . $object . "

qof_" . $object . "_SOURCES=\\
        qof-main.c

qof_" . $object . "_LDADD =               \\
        lib" . $object . "qof.la \\
	\${QOF_LIBS} \\
        \$(GLIB_LIBS) \\
        \$(POPT_LIBS) \\
        -lltdl

";
return $Makefile;
}

1;

Two other files require this kind of addition:
foreach $object (@objects) {
	$qofmainH .= "#include \"" . $object . ".h\"\n";
}

or:

foreach $object (@objects) {
	$qofmainC .= "\t" . ucfirst($object) . "_Register();\n";
}

From this kind of XML:

<?xml version="1.0"?>
<qof-qsf xmlns="http://qof.sourceforge.net/">
  <book count="1">
    <book-guid>7b3cc0cae1068b7170e71c2fde0338ea</book-guid>
    <object type="gncInvoice" count="1">
      <string type="id">000066</string>
      <string type="billing_id">345</string>
      <string type="notes"/>
      <guid type="guid">fc93ba81b854e6f3d42504c1550ee6d3</guid>
      <guid type="terms">be0f11d164df506e5ce50e147548159b</guid>
      <guid type="account">7d7ada08cf42fd87473b8fe506a468b6</guid>
      <guid type="posted_txn">84668e91fadf48b0207d073efc456111</guid>
      <boolean type="active">true</boolean>
      <date type="date_posted">2004-03-20T00:00:00Z</date>
      <date type="date_opened">2004-03-22T00:00:00Z</date>
    </object>
  </book>
</qof-qsf>

I know the Perl itself can be cleaned up. The important element is that I want 
to be able to update the final Makefile.am more easily. In this case, it 
should be solved with the $a = <<EOF; syntax as I can then copy and paste the 
required Makefile rules where needed.

The parameters are generated using:
sub string_function {
	my $e;
	foreach $s (@_) {
		$s =~ s/[ |-]/_/g;
		$e .= "static char*\n" . "get" . ucfirst($s) . "(";
		$e .= $object . "* obj)\n";
		$e .= "{\n\tg_return_val_if_fail (obj != NULL, NULL);\n";
		$e .= "\treturn obj->$s;\n}\n\n";
		$e .= "static void\n" . "set" . ucfirst($s) . "(" . $object;
		$e .= "* obj, const char* string)\n";
		$e .= "{\n\tg_return_if_fail (obj != NULL);\n";
		$e .= "\tobj->$s = g_strdup(string);\n}\n\n";
	}
	return $e;
}

The parser uses:
	if($_[1] eq 'string')   { push (@{$strings[$i]}, @_[3]); }

The output routine uses:

foreach $object (@objects)
{
	print (QOF string_function(@{$strings[$ii]}));
}

to produce:

static char*
getId(gncInvoice* obj)
{
	g_return_val_if_fail (obj != NULL, NULL);
	return obj->id;
}

static void
setId(gncInvoice* obj, const char* string)
{
	g_return_if_fail (obj != NULL);
	obj->id = g_strdup(string);
}

The objects are provided with their create functions using:

sub create_object {
	$object = @_[0];
	my $m = $object . "_create";
	my $u = "QOF_" . uc($object) . "_NAME";
	my $e = "
#include <stdlib.h>
#include \"$object.h\"

$object*
$m (QofBook *book)\n{
	$object  *obj;
	QofCollection *coll;
	GList *all;

	obj = g_new0 ($object, 1);
	qof_instance_init (&obj->inst, $u, book);
	coll = qof_book_get_collection (book, $u);
	all = qof_collection_get_data (coll);\n\tall = g_list_prepend (all, obj);
	qof_collection_set_data (coll, all);\n\treturn obj;\n}\n
";
return $e;
}

HTH

-- 

Neil Williams
=============
http://www.data-freedom.org/
http://www.nosoftwarepatents.com/
http://www.linux.codehelp.co.uk/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/devoncornwall-pm/attachments/20050701/58898338/attachment.bin


More information about the Devoncornwall-pm mailing list