Ticket #1205: opensync_xmlformat.c.2.diff

File opensync_xmlformat.c.2.diff, 5.0 KB (added by scriptor, 2 years ago)

New version of opensync_xmlformat.c.diff, as of Feb, 7th 2010.

  • opensync_xmlformat.c

    old new OSyncXMLFieldList *osync_xmlformat_searc 
    247247 
    248248osync_bool osync_xmlformat_assemble(OSyncXMLFormat *xmlformat, char **buffer, unsigned int *size, OSyncError **error) 
    249249{ 
     250        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, (void *)xmlformat, (void *)buffer, (void *)size, (void *)error); 
     251 
    250252        osync_assert(xmlformat); 
    251253        osync_assert(buffer); 
    252254        osync_assert(size); 
    253255         
    254256        xmlDocDumpFormatMemoryEnc(xmlformat->doc, (xmlChar **)buffer, (int *)size, NULL, 1); 
     257 
     258        osync_trace(TRACE_EXIT, "%s", __func__); 
    255259        return TRUE; 
    256260} 
    257261 
    258262osync_bool osync_xmlformat_sort(OSyncXMLFormat *xmlformat, OSyncError **error) 
    259263{ 
    260         int index; 
    261         OSyncXMLField *cur; 
    262         void **list = NULL; 
     264        int index = 0, final_index = 0; 
     265        OSyncXMLField *cur = NULL, *final_cur = NULL; 
     266        void **sorted_list = NULL; 
     267        int new_child_count = 0; 
    263268 
    264269        osync_trace(TRACE_ENTRY, "%s(%p)", __func__, xmlformat); 
    265270        osync_assert(xmlformat); 
    osync_bool osync_xmlformat_sort(OSyncXML 
    268273                osync_trace(TRACE_INTERNAL, "child_count <= 1 - no need to sort"); 
    269274                goto end; 
    270275        } 
     276 
     277        new_child_count = xmlformat->child_count; 
    271278         
    272         list = osync_try_malloc0(sizeof(OSyncXMLField *) * xmlformat->child_count, error); 
    273         if (!list) 
     279        sorted_list = osync_try_malloc0(sizeof(OSyncXMLField *) * xmlformat->child_count, error); 
     280        if (!sorted_list) 
    274281                goto error; 
    275          
     282 
     283        // Fill sorted_list with the elements in an unsorted way         
    276284        index = 0; 
    277285        cur = osync_xmlformat_get_first_field(xmlformat); 
    278286        for(; cur != NULL; cur = osync_xmlfield_get_next(cur)) { 
    279                 list[index] = cur; 
     287                sorted_list[index] = cur; 
    280288                index++; 
    281289                xmlUnlinkNode(cur->node); 
    282290        } 
    283291         
    284         qsort(list, xmlformat->child_count, sizeof(OSyncXMLField *), osync_xmlfield_compare_stdlib); 
     292        // Bring sorted_list in a sorted state 
     293        qsort(sorted_list, xmlformat->child_count, sizeof(OSyncXMLField *), osync_xmlfield_compare_stdlib); 
    285294         
    286         /** bring the xmlformat and the xmldoc in a consistent state */ 
    287         xmlformat->first_child = ((OSyncXMLField *)list[0])->node->_private; 
    288         xmlformat->last_child = ((OSyncXMLField *)list[xmlformat->child_count-1])->node->_private; 
    289  
    290         for(index = 0; index < xmlformat->child_count; index++) { 
    291                 cur = (OSyncXMLField *)list[index]; 
    292                 xmlAddChild(xmlDocGetRootElement(xmlformat->doc), cur->node); 
    293                          
    294                 if(index < xmlformat->child_count-1) 
    295                         cur->next = (OSyncXMLField *)list[index+1]; 
    296                 else 
    297                         cur->next = NULL; 
     295         
     296        // Read from sorted_list and eliminate those elements,  
     297        // that have the same node pointer as the preceding element. 
     298        // This can happen because of the way xmlAddChild() works. 
     299        for (index = 0, final_index = 0, final_cur = (OSyncXMLField *)sorted_list[0];  
     300                   index < xmlformat->child_count;  
     301                   index++)  
     302        { 
     303                cur = (OSyncXMLField *)sorted_list[index]; 
     304                final_cur = (OSyncXMLField *)sorted_list[final_index]; 
     305 
     306 
     307                cur->node = xmlAddChild(xmlDocGetRootElement(xmlformat->doc), cur->node); 
     308                if (cur->node == NULL) { 
     309                        osync_error_set(error, OSYNC_ERROR_GENERIC, "xmlAddChild() has failed."); 
     310 
     311                        goto error; 
     312                }        
     313         
     314 
     315                // Eliminate those elements that have the same node pointer as 
     316                // their preceding element.  Only relevant for index > 0. 
     317                if (index) { 
     318                        if (((OSyncXMLField *)sorted_list[index])->node == ((OSyncXMLField *)sorted_list[index - 1])->node) { 
     319 
     320                                // Now we have one child less 
     321                                new_child_count--; 
     322 
     323                                // Just in case, a further loop wouldn't be performed, any more... 
     324                                final_cur->next = NULL; 
     325                                ((OSyncXMLField *)sorted_list[index - 1])->next = NULL; 
     326 
     327                                // Proceed in for-loop 
     328                                continue; 
     329                        } 
     330                } 
     331 
     332                // Set parent, child and node and sorted 
     333                // As long as index == final_index, these assignments are redundant. 
     334                // As soon as final_index lags behind index, these assignments move 
     335                // in effect each element to positions with lower indexes. 
     336                final_cur->parent = ((OSyncXMLField *)sorted_list[index])->parent; 
     337                final_cur->child = ((OSyncXMLField *)sorted_list[index])->child; 
     338                final_cur->node = ((OSyncXMLField *)sorted_list[index])->node; 
     339                final_cur->sorted = ((OSyncXMLField *)sorted_list[index])->sorted; 
     340 
     341                // Set prev and next 
     342                if (final_index < new_child_count - 1) { 
     343                        final_cur->next = (OSyncXMLField *)sorted_list[final_index + 1]; 
     344                        final_cur->next->prev = final_cur; 
     345                } else { 
     346                        final_cur->next = NULL; 
     347                } 
    298348                 
    299                 if(index) 
    300                         cur->prev = (OSyncXMLField *)list[index-1]; 
    301                 else 
    302                         cur->prev = NULL; 
     349                if (final_index) { 
     350                        final_cur->prev = (OSyncXMLField *)sorted_list[final_index - 1]; 
     351                        final_cur->prev->next = final_cur; 
     352                } else { 
     353                        final_cur->prev = NULL; 
     354                } 
     355 
     356                // Increment final_index, when an element has been added, only.   
     357                final_index++; 
    303358        } 
    304         g_free(list); 
     359 
     360 
     361        /** bring the xmlformat and the xmldoc in a consistent state */ 
     362        xmlformat->first_child = ((OSyncXMLField *)sorted_list[0])->node->_private; 
     363        xmlformat->last_child = ((OSyncXMLField *)sorted_list[new_child_count - 1])->node->_private; 
     364        xmlformat->child_count = final_index; 
     365 
     366        g_free(sorted_list); 
    305367 
    306368 end:    
    307369        xmlformat->sorted = TRUE;