diff --git a/include/igloo/ro.h b/include/igloo/ro.h index 92b112e..6d4cc25 100644 --- a/include/igloo/ro.h +++ b/include/igloo/ro.h @@ -131,12 +131,34 @@ typedef igloo_ro_t (*igloo_ro_convert_t)(igloo_ro_t self, const igloo_ro_type_t */ typedef igloo_ro_t (*igloo_ro_get_interface_t)(igloo_ro_t self, const igloo_ro_type_t *type, const char *name, igloo_ro_t associated); +/* Type used to store flags for stringify operation. + */ +#ifdef IGLOO_CTC_HAVE_STDINT_H +typedef uint_least32_t igloo_ro_sy_t; +#else +typedef unsigned long int igloo_ro_sy_t; +#endif +/* No stringify flags set. Usefull for variable initialization. */ +#define igloo_RO_SY_NONE ((igloo_ro_sy_t)0x0000) +/* Stringify using defaults. */ +#define igloo_RO_SY_DEFAULT ((igloo_ro_sy_t)0x1000) +/* Stringify the object itself, do not touch the content. + * When set together with igloo_RO_SY_CONTENT the libigloo will select the mode. + */ +#define igloo_RO_SY_OBJECT ((igloo_ro_sy_t)0x0001) +/* Stringify the object's content. + * When set together with igloo_RO_SY_OBJECT the libigloo will select the mode. + */ +#define igloo_RO_SY_CONTENT ((igloo_ro_sy_t)0x0002) + /* Type used for callback called when the object needs to be converted to a string. * * This is used mostly for debugging or preseting the object to the user. * The callback is not expected to return a string that can be used to reconstruct the object. + * + * igloo_RO_SY_OBJECT is always set when this is called. */ -typedef char * (*igloo_ro_stringify_t)(igloo_ro_t self); +typedef char * (*igloo_ro_stringify_t)(igloo_ro_t self, igloo_ro_sy_t flags); /* Type used as a result of a compare between objects. */ @@ -350,10 +372,13 @@ igloo_ro_t igloo_ro_get_interface(igloo_ro_t self, const igloo_ro_type_t *type, * Parameters: * self * The object to convert to a string. + * flags + * Flags used to select options to the conversion. + * Should normally be igloo_RO_SY_DEFAULT. * Returns: * A string as allocated using malloc(3). The caller must call free(3). */ -char * igloo_ro_stringify(igloo_ro_t self); +char * igloo_ro_stringify(igloo_ro_t self, igloo_ro_sy_t flags); /* Compare two objects. * diff --git a/src/buffer.c b/src/buffer.c index 55679b4..2d71302 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -31,7 +31,7 @@ struct igloo_buffer_tag { }; static void __free(igloo_ro_t self); -static char * __stringify(igloo_ro_t self); +static char * __stringify(igloo_ro_t self, igloo_ro_sy_t flags); igloo_RO_PUBLIC_TYPE(igloo_buffer_t, igloo_RO_TYPEDECL_FREE(__free), @@ -141,7 +141,7 @@ int igloo_buffer_get_string(igloo_buffer_t *buffer, const char **string) return 0; } -static char * __stringify(igloo_ro_t self) +static char * __stringify(igloo_ro_t self, igloo_ro_sy_t flags) { igloo_buffer_t *buffer = igloo_RO_TO_TYPE(self, igloo_buffer_t); const char *ret; diff --git a/src/ro.c b/src/ro.c index 4bffd4d..9ce9198 100644 --- a/src/ro.c +++ b/src/ro.c @@ -392,26 +392,37 @@ igloo_ro_t igloo_ro_get_interface(igloo_ro_t self, const igloo_ro_type_t *type, return ret; } -char * igloo_ro_stringify(igloo_ro_t self) +char * igloo_ro_stringify(igloo_ro_t self, igloo_ro_sy_t flags) { igloo_ro_base_t *base = igloo_RO__GETBASE(self); char *ret = NULL; - if (!base) - return strdup("{igloo_RO_NULL}"); + if (flags & igloo_RO_SY_DEFAULT) + flags |= igloo_RO_SY_OBJECT|igloo_RO_SY_CONTENT; + + + if (!base) { + if (flags & igloo_RO_SY_OBJECT) { + return strdup("{igloo_RO_NULL}"); + } else { + return NULL; + } + } igloo_thread_mutex_lock(&(base->lock)); if (!base->refc) { - int len; - char buf; + if (flags & igloo_RO_SY_OBJECT) { + int len; + char buf; #define STRINGIFY_FORMAT_WEAK "{%s@%p, weak}", base->type->type_name, base - len = snprintf(&buf, 1, STRINGIFY_FORMAT_WEAK); - if (len > 2) { - /* We add 2 bytes just to make sure no buggy interpretation of \0 inclusion could bite us. */ - ret = calloc(1, len + 2); - if (ret) { - snprintf(ret, len + 1, STRINGIFY_FORMAT_WEAK); + len = snprintf(&buf, 1, STRINGIFY_FORMAT_WEAK); + if (len > 2) { + /* We add 2 bytes just to make sure no buggy interpretation of \0 inclusion could bite us. */ + ret = calloc(1, len + 2); + if (ret) { + snprintf(ret, len + 1, STRINGIFY_FORMAT_WEAK); + } } } @@ -419,19 +430,21 @@ char * igloo_ro_stringify(igloo_ro_t self) return ret; } - if (base->type->type_stringifycb) { - ret = base->type->type_stringifycb(self); + if (base->type->type_stringifycb && (flags & igloo_RO_SY_CONTENT)) { + ret = base->type->type_stringifycb(self, flags); } else { - int len; - char buf; + if (flags & igloo_RO_SY_OBJECT) { + int len; + char buf; #define STRINGIFY_FORMAT_FULL "{%s@%p, strong, name=\"%s\", associated=%p}", base->type->type_name, base, base->name, igloo_RO__GETBASE(base->associated) - len = snprintf(&buf, 1, STRINGIFY_FORMAT_FULL); - if (len > 2) { - /* We add 2 bytes just to make sure no buggy interpretation of \0 inclusion could bite us. */ - ret = calloc(1, len + 2); - if (ret) { - snprintf(ret, len + 1, STRINGIFY_FORMAT_FULL); + len = snprintf(&buf, 1, STRINGIFY_FORMAT_FULL); + if (len > 2) { + /* We add 2 bytes just to make sure no buggy interpretation of \0 inclusion could bite us. */ + ret = calloc(1, len + 2); + if (ret) { + snprintf(ret, len + 1, STRINGIFY_FORMAT_FULL); + } } } }