📄 xmlutils.c
字号:
const char *local_prop_value = xmlGetProp(src, local_prop_name); set_xml_property_copy(target, local_prop_name, local_prop_value); prop_iter = prop_iter->next; }#else xmlCopyPropList(target, src->properties);#endif } FNOUT();}char * dump_xml(xmlNodePtr msg){ FNIN(); FNRET(dump_xml_node(msg, FALSE));}voidxml_message_debug(xmlNodePtr msg, const char *text){ char *msg_buffer; FNIN(); if(msg == NULL) { CRM_DEBUG3("%s: %s", text==NULL?"<null>":text,"<null>"); FNOUT(); } msg_buffer = dump_xml_node(msg, FALSE); CRM_DEBUG3("%s: %s", text==NULL?"<null>":text, msg_buffer==NULL?"<null>":msg_buffer); cl_free(msg_buffer); FNOUT();}char * dump_xml_node(xmlNodePtr msg, gboolean whole_doc){ int lpc = 0; int msg_size = -1; xmlChar *xml_message = NULL; xmlBufferPtr xml_buffer; FNIN(); if (msg == NULL) FNRET(NULL); xmlInitParser(); if (whole_doc) { if (msg->doc == NULL) { xmlDocPtr foo = xmlNewDoc("1.0"); xmlDocSetRootElement(foo, msg); xmlSetTreeDoc(msg,foo); } xmlDocDumpMemory(msg->doc, &xml_message, &msg_size); } else {#ifdef XML_TRACE CRM_DEBUG2("mem used by xml: %d", xmlMemUsed());#endif xmlMemoryDump (); xml_buffer = xmlBufferCreate(); msg_size = xmlNodeDump(xml_buffer, msg->doc, msg, 0, 0); xml_message = (xmlChar*)cl_strdup(xmlBufferContent(xml_buffer)); xmlBufferFree(xml_buffer); if (!xml_message) { cl_log(LOG_ERR, "memory allocation failed in dump_xml_node()"); } } /* HA wont send messages with newlines in them. */ for(; xml_message != NULL && lpc < msg_size; lpc++) if (xml_message[lpc] == '\n') xml_message[lpc] = ' '; FNRET((char*)xml_message); }xmlNodePtradd_node_copy(xmlNodePtr new_parent, xmlNodePtr xml_node){ xmlNodePtr node_copy = NULL; FNIN(); if(xml_node != NULL && new_parent != NULL) { node_copy = copy_xml_node_recursive(xml_node); xmlAddChild(new_parent, node_copy); } else if(xml_node == NULL) { cl_log(LOG_ERR, "Could not add copy of NULL node"); } else { cl_log(LOG_ERR, "Could not add copy of node to NULL parent"); } FNRET(node_copy);}xmlAttrPtrset_xml_property_copy(xmlNodePtr node, const xmlChar *name, const xmlChar *value){ const char *parent_name = NULL; const char *local_name = NULL; const char *local_value = NULL; xmlAttrPtr ret_value = NULL; FNIN(); if(node != NULL) { parent_name = node->name; } #ifdef XML_TRACE CRM_DEBUG4("[%s] Setting %s to %s", parent_name, name, value);#endif if (name == NULL || strlen(name) <= 0) { ret_value = NULL; } else if(node == NULL) { ret_value = NULL; } else if (value == NULL || strlen(value) <= 0) { ret_value = NULL; xmlUnsetProp(node, local_name); } else { local_value = cl_strdup(value); local_name = cl_strdup(name); ret_value = xmlSetProp(node, local_name, local_value); } FNRET(ret_value);}xmlNodePtrcreate_xml_node(xmlNodePtr parent, const char *name){ const char *local_name = NULL; const char *parent_name = NULL; xmlNodePtr ret_value = NULL; FNIN(); if (name == NULL || strlen(name) < 1) { ret_value = NULL; } else { local_name = cl_strdup(name); if(parent == NULL) ret_value = xmlNewNode(NULL, local_name); else { parent_name = parent->name; ret_value = xmlNewChild(parent, NULL, local_name, NULL); } }#ifdef XML_TRACE CRM_DEBUG3("Created node [%s [%s]]", parent_name, local_name);#endif FNRET(ret_value);}voidunlink_xml_node(xmlNodePtr node){ xmlUnlinkNode(node); /* this helps us with frees and really should be being done by * the library call */ node->doc = NULL;}voidfree_xml(xmlNodePtr a_node){ FNIN(); if (a_node == NULL) ; /* nothing to do */ else if (a_node->doc != NULL) xmlFreeDoc(a_node->doc); else { /* make sure the node is unlinked first */ xmlUnlinkNode(a_node);#if 0 /* set a new doc, wont delete without one? */ xmlDocPtr foo = xmlNewDoc("1.0"); xmlDocSetRootElement(foo, a_node); xmlSetTreeDoc(a_node,foo); xmlFreeDoc(foo);#else xmlFreeNode(a_node);#endif } FNOUT();}voidset_node_tstamp(xmlNodePtr a_node){ char *since_epoch = (char*)cl_malloc(128*(sizeof(char))); FNIN(); sprintf(since_epoch, "%ld", (unsigned long)time(NULL)); set_xml_property_copy(a_node, XML_ATTR_TSTAMP, since_epoch); cl_free(since_epoch);}xmlNodePtrcopy_xml_node_recursive(xmlNodePtr src_node){#if XML_TRACE const char *local_name = NULL; xmlNodePtr local_node = NULL, node_iter = NULL, local_child = NULL; xmlAttrPtr prop_iter = NULL; FNIN(); if(src_node != NULL && src_node->name != NULL) { local_node = create_xml_node(NULL, src_node->name); prop_iter = src_node->properties; while(prop_iter != NULL) { const char *local_prop_name = prop_iter->name; const char *local_prop_value = xmlGetProp(src_node, local_prop_name); set_xml_property_copy(local_node, local_prop_name, local_prop_value); prop_iter = prop_iter->next; } node_iter = src_node->children; while(node_iter != NULL) { local_child = copy_xml_node_recursive(node_iter); if(local_child != NULL) { xmlAddChild(local_node, local_child); CRM_DEBUG3("Copied node [%s [%s]", local_name, local_child->name); } node_iter = node_iter->next; } CRM_DEBUG2("Returning [%s]", local_node->name); FNRET(local_node); } CRM_DEBUG("Returning null"); FNRET(NULL);#else return xmlCopyNode(src_node, 1);#endif}xmlNodePtrstring2xml(const char *input){ char ch = 0; int lpc = 0, input_len = strlen(input); gboolean more = TRUE; gboolean inTag = FALSE; xmlNodePtr xml_object = NULL; const char *the_xml; xmlDocPtr doc; xmlBufferPtr xml_buffer = xmlBufferCreate(); for(lpc = 0; (lpc < input_len) && more; lpc++) { ch = input[lpc]; switch(ch) { case EOF: case 0: ch = 0; more = FALSE; xmlBufferAdd(xml_buffer, &ch, 1); break; case '>': case '<': inTag = TRUE; if(ch == '>') inTag = FALSE; xmlBufferAdd(xml_buffer, &ch, 1); break; case '\n': case '\t': case ' ': ch = ' '; if(inTag) { xmlBufferAdd(xml_buffer, &ch, 1); } break; default: xmlBufferAdd(xml_buffer, &ch, 1); break; } } the_xml = xmlBufferContent(xml_buffer); doc = xmlParseMemory(the_xml, strlen(the_xml)); if (doc == NULL) { cl_log(LOG_ERR, "Malformed XML [xml=%s]", the_xml); return NULL; } xml_object = xmlDocGetRootElement(doc); xml_message_debug(xml_object, "Created fragment"); return xml_object;}xmlNodePtrfile2xml(FILE *input){ char ch = 0; gboolean more = TRUE; gboolean inTag = FALSE; xmlNodePtr xml_object = NULL; xmlBufferPtr xml_buffer = xmlBufferCreate(); const char *the_xml; xmlDocPtr doc; if(input == NULL) { cl_log(LOG_ERR, "File pointer was NULL"); return NULL; } while (more) { ch = fgetc(input);/* cl_log(LOG_DEBUG, "Got [%c]", ch); */ switch(ch) { case EOF: case 0: ch = 0; more = FALSE; xmlBufferAdd(xml_buffer, &ch, 1); break; case '>': case '<': inTag = TRUE; if(ch == '>') inTag = FALSE; xmlBufferAdd(xml_buffer, &ch, 1); break; case '\n': case '\t': case ' ': ch = ' '; if(inTag) { xmlBufferAdd(xml_buffer, &ch, 1); } break; default: xmlBufferAdd(xml_buffer, &ch, 1); break; } } the_xml = xmlBufferContent(xml_buffer); doc = xmlParseMemory(the_xml, strlen(the_xml)); if (doc == NULL) { cl_log(LOG_ERR, "Malformed XML [xml=%s]", the_xml); return NULL; } xml_object = xmlDocGetRootElement(doc); xml_message_debug(xml_object, "Created fragment"); return xml_object;}voiddump_array(int log_level, const char *message, const char **array, int depth){ int j; if(message != NULL) { cl_log(log_level, "%s", message); } cl_log(log_level, "Contents of the array:"); if(array == NULL || array[0] == NULL || depth == 0) { cl_log(log_level, "\t<empty>"); } for (j=0; j < depth && array[j] != NULL; j++) { if (array[j] == NULL) break; cl_log(log_level, "\t--> (%s).", array[j]); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -