From: <svn...@op...> - 2009-09-17 17:02:24
|
Author: dgollub Date: Thu Sep 17 19:02:06 2009 New Revision: 5783 URL: http://www.opensync.org/changeset/5783 Log: Add OSyncError** struct to: * osync_xmlformat_sort * osync_xmlformat_assemble To make those interface more future safe refs #1087 Modified: trunk/opensync/xmlformat/opensync_xmlformat.c trunk/opensync/xmlformat/opensync_xmlformat.h trunk/tests/capabilities-tests/check_xmlformat.c trunk/wrapper/opensync-merger.i Modified: trunk/opensync/xmlformat/opensync_xmlformat.c ============================================================================== --- trunk/opensync/xmlformat/opensync_xmlformat.c Thu Sep 17 18:39:09 2009 (r5782) +++ trunk/opensync/xmlformat/opensync_xmlformat.c Thu Sep 17 19:02:06 2009 (r5783) @@ -242,17 +242,17 @@ return NULL; } -osync_bool osync_xmlformat_assemble(OSyncXMLFormat *xmlformat, char **buffer, unsigned int *size) +osync_bool osync_xmlformat_assemble(OSyncXMLFormat *xmlformat, char **buffer, unsigned int *size, OSyncError **error) { osync_assert(xmlformat); osync_assert(buffer); osync_assert(size); xmlDocDumpFormatMemoryEnc(xmlformat->doc, (xmlChar **)buffer, (int *)size, NULL, 1); - return TRUE; + return TRUE; } -void osync_xmlformat_sort(OSyncXMLFormat *xmlformat) +osync_bool osync_xmlformat_sort(OSyncXMLFormat *xmlformat, OSyncError **error) { int index; OSyncXMLField *cur; @@ -266,7 +266,9 @@ goto end; } - list = g_malloc0(sizeof(OSyncXMLField *) * xmlformat->child_count); + list = osync_try_malloc0(sizeof(OSyncXMLField *) * xmlformat->child_count, error); + if (!list) + goto error; index = 0; cur = osync_xmlformat_get_first_field(xmlformat); @@ -301,6 +303,11 @@ end: xmlformat->sorted = TRUE; osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + +error: + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; } osync_bool osync_xmlformat_is_sorted(OSyncXMLFormat *xmlformat) @@ -344,19 +351,24 @@ osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, source, destination); - osync_xmlformat_assemble(source, &buffer, &size); + if (!osync_xmlformat_assemble(source, &buffer, &size, error)) + goto error; + *destination = osync_xmlformat_parse(buffer, size, error); - if (!(*destination)) { - osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); - return FALSE; - } + if (!(*destination)) + goto error; - if (source->sorted) (*destination)->sorted = TRUE; + if (source->sorted) + (*destination)->sorted = TRUE; g_free(buffer); osync_trace(TRACE_EXIT, "%s", __func__); return TRUE; + +error: + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; } unsigned int osync_xmlformat_size() Modified: trunk/opensync/xmlformat/opensync_xmlformat.h ============================================================================== --- trunk/opensync/xmlformat/opensync_xmlformat.h Thu Sep 17 18:39:09 2009 (r5782) +++ trunk/opensync/xmlformat/opensync_xmlformat.h Thu Sep 17 19:02:06 2009 (r5783) @@ -97,9 +97,10 @@ * @param buffer The pointer to the buffer which will hold the xml document. It is up * to the caller to free this buffer. * @param size The pointer to the buffer which will hold the size of the xml document - * @return Always returns TRUE. + * @param error The error which will hold the info in case of an error + * @return TRUE on success, FALSE on any error */ -OSYNC_EXPORT osync_bool osync_xmlformat_assemble(OSyncXMLFormat *xmlformat, char **buffer, unsigned int *size); +OSYNC_EXPORT osync_bool osync_xmlformat_assemble(OSyncXMLFormat *xmlformat, char **buffer, unsigned int *size, OSyncError **error); /** * @brief Sort all xmlfields of the xmlformat. @@ -108,8 +109,11 @@ * The recommended approach is to assemble the xmlformat in a sorted way instead. * * @param xmlformat The pointer to the xmlformat object + * @param error The error which will hold the info in case of an error + * @return TRUE on success, FALSE on any error + * */ -OSYNC_EXPORT void osync_xmlformat_sort(OSyncXMLFormat *xmlformat); +OSYNC_EXPORT osync_bool osync_xmlformat_sort(OSyncXMLFormat *xmlformat, OSyncError **error); /** * @brief Check if all xmlfields of an xmlformat are sorted. Modified: trunk/tests/capabilities-tests/check_xmlformat.c ============================================================================== --- trunk/tests/capabilities-tests/check_xmlformat.c Thu Sep 17 18:39:09 2009 (r5782) +++ trunk/tests/capabilities-tests/check_xmlformat.c Thu Sep 17 19:02:06 2009 (r5783) @@ -57,7 +57,8 @@ fail_unless(xmlformat != NULL, NULL); fail_unless(error == NULL, NULL); - osync_xmlformat_sort(xmlformat); + osync_xmlformat_sort(xmlformat, &error); + fail_unless(error == NULL, NULL); osync_xmlformat_unref(xmlformat); @@ -82,7 +83,9 @@ fail_unless(osync_xmlformat_is_sorted(xmlformat) == FALSE, NULL); - osync_xmlformat_sort(xmlformat); + osync_xmlformat_sort(xmlformat, &error); + fail_unless(error == NULL, NULL); + fail_unless(osync_xmlformat_is_sorted(xmlformat) == TRUE, NULL); @@ -108,7 +111,10 @@ fail_unless(xmlformat != NULL, NULL); fail_unless(error == NULL, NULL); g_free(buffer); - osync_xmlformat_sort(xmlformat); + osync_xmlformat_sort(xmlformat, &error); + fail_unless(error == NULL, NULL); + + OSyncXMLFieldList *xmlfieldlist = osync_xmlformat_search_field(xmlformat, "Name", &error, NULL); fail_unless(xmlfieldlist != NULL, NULL); Modified: trunk/wrapper/opensync-merger.i ============================================================================== --- trunk/wrapper/opensync-merger.i Thu Sep 17 18:39:09 2009 (r5782) +++ trunk/wrapper/opensync-merger.i Thu Sep 17 19:02:06 2009 (r5783) @@ -113,18 +113,21 @@ PyObject *assemble() { char *buf; unsigned int size; - osync_bool ret = osync_xmlformat_assemble(self, &buf, &size); - if (!ret) { - wrapper_exception("osync_xmlformat_assemble failed\n"); + Error *err = NULL; + osync_xmlformat_assemble(self, &buf, &size, &err); + if (raise_exception_on_error(err)) return NULL; - } + PyObject *obj = PyString_FromStringAndSize(buf, size); free(buf); return obj; } void sort() { - osync_xmlformat_sort(self); + Error *err = NULL; + osync_xmlformat_sort(self, &err); + if (raise_exception_on_error(err)) + return; } %pythoncode %{ |