added tostring callback
[rrq/rrqmisc.git] / vector / tupleitem.c
index ad1326735e816c5fa7989378c8e0f1fcfe81bb69..f3dfe2c70b0aff012c7b39397f3881817148bd81 100644 (file)
@@ -99,6 +99,28 @@ static void tupleitem_releasekey(void *this,void *key) {
     free( key );
 }
 
+#define OUT(X) a = X; if ( a > limit ) return 0; buffer += a; limit -= a
+
+/**
+ * This callback function writes a representation of an item into
+ * a character buffer.
+ */
+static int tupleitem_tostring(void *this,void *item,char *buffer,int limit) {
+    tupleschema *def = (tupleschema *) this;
+    tuple *t = (tuple*) item;
+    char *x = "<";
+    int a, i;
+    for ( i = 0; i < def->arity; i++ ) {
+       OUT( snprintf( buffer, limit, x ) );
+       x = ",";
+       OUT( def->columns[i]->tostring(
+                def->columns[i], (*t)[i], buffer, limit ) );
+    }
+    OUT( snprintf( buffer, limit, ">" ) );
+    return a;
+}
+
+
 // Allocate
 tuple *tuple_create(int arity,...) {
     va_list ap;
@@ -112,15 +134,18 @@ tuple *tuple_create(int arity,...) {
     return t;
 }
 
+itemkeyfun tupleschema_callbacks = {
+    .hashcode = tupleitem_hashcode,
+    .haskey = tupleitem_haskey,
+    .itemkey = tupleitem_itemkey,
+    .releasekey = tupleitem_releasekey,
+    .tostring = tupleitem_tostring
+};
+
 tupleschema *tupleschema_create(int arity,tuple *columns) {
     tupleschema *ts = (tupleschema*) malloc( sizeof( tupleschema ) );
     (*ts) = (tupleschema) {
-       .base = {
-           .hashcode = tupleitem_hashcode,
-           .haskey = tupleitem_haskey,
-           .itemkey = tupleitem_itemkey,
-           .releasekey = tupleitem_releasekey
-       },
+       .base = tupleschema_callbacks,
        .arity = arity,
        .columns = (itemkeyfun**) columns
     };
@@ -158,4 +183,3 @@ unsigned long tuple_mask(int arity,tuple *t) {
     }
     return mask;
 }
-