* Trampoline functions for the ItemKeyFun callbacks.
*/
-#include <assert.h>
#include <ItemKeyFun.h>
/**
*/
unsigned long ItemKeyFun_hashcode(void *this,void *key) {
ItemKeyFun *type = (ItemKeyFun*) this;
- assert( type );
- return type->hashcode? type->hashcode( type, key ) : (unsigned long) key;
+ return (type && type->hashcode)? type->hashcode( type, key )
+ : (unsigned long) key;
}
/**
}
}
+/**
+ * Write a value in hexadecimal form, with "0x" followed by digits, if
+ * it fits within the given limit. Returns characters written, which
+ * is either 0 or all.
+ */
+static int ItemKeyFun_puthexlong(char *p,unsigned long x,int limit) {
+ int n = 3;
+ if ( x ) {
+ unsigned long y = x;
+ char *q = p;
+ for ( ; y; y >>= 4, q++ );
+ n = q - p + 2;
+ if ( n >= limit ) {
+ return 0;
+ }
+ *(p++) = '0';
+ *(p++) = 'x';
+ for ( q++; x; x >>= 4, q-- ) {
+ *q = x & 0xf;
+ }
+ p += n - 2;
+ } else {
+ if ( 3 >= limit ) {
+ return 0;
+ }
+ *(p++) = '0';
+ *(p++) = 'x';
+ *(p++) = '0';
+ }
+ *(p++) = 0;
+ return n;
+}
+
/**
* \brief Trampoline function for the ItemKeyFun.tostring callback.
*/
if( type && type->tostring ) {
return type->tostring( type, item, buffer, limit );
}
+ if ( limit < 14 ) {
+ return 0;
+ }
void *key = ItemKeyFun_itemkey( this, item );
- int n = snprintf( buffer, limit, "{%p/%p@%p}", type, key, item );
+ // Portable snprintf
+ void *code[] = { type, key, item };
ItemKeyFun_releasekey( this, key );
+ char *tail = "/@}";
+ char *p = buffer;
+ int i, k, n = 2;
+ if ( n >= limit ) {
+ return 0;
+ }
+ *(p++) = '{';
+ for ( k = 0; k < 3; k++ ) {
+ i = ItemKeyFun_puthexlong( p, (unsigned long) code[ k ], limit - n );
+ n += i + 1;
+ if ( i == 0 || n >= limit ) {
+ return 0;
+ }
+ *(p++) = tail[k];
+ }
+ *(p++) = 0;
return n;
}