⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gui_xml.c

📁 MANTIS是由科罗拉多大学开发的传感器网络嵌入式操作系统。 这是mantis的0.9.5版本的源码。
💻 C
字号:
//  This file is part of MANTIS OS, Operating System//  See http://mantis.cs.colorado.edu/////  Copyright (C) 2003-2005 University of Colorado, Boulder////  This program is free software; you can redistribute it and/or//  modify it under the terms of the mos license (see file LICENSE)#include <time.h>#include "mos.h"#include "cortex.h"#include "gui_xml.h"#include "gui_topo.h"#ifndef LIBXML_READER_ENABLED#error "You need libxml2.6+ with XML Reader enabled"#endifextern gui_topo_t *topo;extern GnomeCanvas *canvas;static void print_xml_node (xmlDocPtr doc, xmlNodePtr parent_node){   xmlNodePtr node = parent_node;   while (node != NULL) {      if (node->type == XML_ELEMENT_NODE)	 printf ("%s %s\n", node->name, xmlNodeGetContent (node));      node = node->next;   }}static void print_xml_doc (xmlDocPtr xml_doc){   gint i;   xmlChar *xpath = "//sensor_network/sensor_node";   xmlXPathObjectPtr xpath_results;   xpath_results = gui_xml_run_xpath (xml_doc, xpath);   if (xpath_results) {      xmlNodeSetPtr node_set = xpath_results->nodesetval;      for (i = 0; i < node_set->nodeNr; i++) {	 xmlNodePtr node = node_set->nodeTab[i]->xmlChildrenNode;	 printf ("%s\n", node_set->nodeTab[i]->name);	 print_xml_node (xml_doc, node);      }      xmlXPathFreeObject (xpath_results);      goto cleanup;   } else {      debug ("Could not find any sensor nodes");      goto cleanup;   }cleanup:   //xmlFreeDoc (xml_doc);   xmlCleanupParser ();}xmlDocPtr gui_xml_read_file (gchar *filename){   xmlDocPtr xml_doc = xmlParseFile (filename);   if (xml_doc == NULL) {      debug ("Couldn't parse '%s'", filename);      return NULL;   }   return xml_doc;}static void gui_xml_write_file_helper (gpointer key, gpointer data,				       gpointer user_data){   topo_node_t *node = (topo_node_t *)data;   xmlNodePtr root_node = (xmlNodePtr)user_data;   gchar buf[256];      if (g_hash_table_lookup (node->gui_topo->unconf_nodes,			    GINT_TO_POINTER (node->id)) == NULL) {      xmlNodePtr xml_node = xmlNewChild (root_node, NULL, "sensor_node", NULL);      snprintf (buf, sizeof (buf), "%d", node->id);      xmlNewChild (xml_node, NULL, "node_id", buf);      snprintf (buf, sizeof (buf), "%.2f", node->x);      xmlNewChild (xml_node, NULL, "x", buf);      snprintf (buf, sizeof (buf), "%.2f", node->y);      xmlNewChild (xml_node, NULL, "y", buf);      snprintf (buf, sizeof (buf), "%.2f", node->z);      xmlNewChild (xml_node, NULL, "z", buf);   } else {      debug("got unconf node");   }}void gui_xml_write_file (gui_topo_t *topo, gchar *filename){   xmlDocPtr doc = xmlNewDoc ("1.0");   xmlNodePtr sensor_network = xmlNewNode (NULL, "sensor_network");   xmlDocSetRootElement (doc, sensor_network);   g_hash_table_foreach (topo->nodes, gui_xml_write_file_helper, sensor_network);   //print_xml_doc (doc);   debug ("Saving XML file to '%s'", filename);   xmlSaveFormatFileEnc (filename, doc, "UTF-8", 1);   xmlFreeDoc (doc);   xmlCleanupParser ();}static void gui_xml_open_file_helper (xmlNodePtr parent_node){   xmlNodePtr node = parent_node;   gint node_id = -1;   gint x = -1;   gint y = -1;   gint z = -1;      while (node != NULL) {      if (node->type == XML_ELEMENT_NODE) {	 if (strcmp (node->name, "node_id") == 0) {	    node_id = atoi (xmlNodeGetContent (node));	 }	 if (strcmp (node->name, "x") == 0) {	    x = atoi (xmlNodeGetContent (node));	 }	 if (strcmp (node->name, "y") == 0) {	    y = atoi (xmlNodeGetContent (node));	 }	 if (strcmp (node->name, "z") == 0) {	    z = atoi (xmlNodeGetContent (node));	 }      }      node = node->next;   }   if (node_id == -1 || x == -1 || y == -1 || z == -1) {      debug ("not a complete xml file?: %d %d %d %d",	     node_id, x, y, z);   }   if (1) {      debug("need to be able to lookup nodes here");      //gui_topo_node_update (topo, node_id, -1, -1);      gui_topo_node_update_pos (topo, node_id, x, y, z);   } else {      gui_topo_node_new_pos (topo, node_id, (gdouble)x, (gdouble)y, (gdouble)z);      debug("need to add something to a treeview");   }   }void gui_xml_open_file (gui_topo_t *topo, gchar *filename){   xmlDocPtr doc = gui_xml_read_file (filename);   gint i;      /*print_xml_doc (doc);     return;*/   xmlXPathObjectPtr xpath_results;   xpath_results = gui_xml_run_xpath (doc,				      "//sensor_network/sensor_node");   if (xpath_results) {      xmlNodeSetPtr node_set = xpath_results->nodesetval;      for (i = 0; i < node_set->nodeNr; i++) {	 xmlNodePtr node = node_set->nodeTab[i]->xmlChildrenNode;	 gui_xml_open_file_helper (node);      }   } else {      debug ("Could not find any sensor nodes");   }   gnome_canvas_update_now (GNOME_CANVAS (topo->widget));         xmlFreeDoc (doc);   xmlCleanupParser ();}xmlXPathObjectPtr gui_xml_run_xpath (xmlDocPtr doc, xmlChar *xpath_request){   xmlXPathObjectPtr xpath_results;   xmlXPathContextPtr context;   context = xmlXPathNewContext (doc);   xpath_results = xmlXPathEvalExpression (xpath_request, context);   if (!xpath_results) {      debug ("XPath expression '%s' did not return anything", xpath_request);      xmlXPathFreeContext (context);      return NULL;   }   if (xmlXPathNodeSetIsEmpty (xpath_results->nodesetval)) {      debug ("No result found for XPath expr '%s'", xpath_request);      xmlXPathFreeContext (context);      return NULL;   }   xmlXPathFreeContext (context);   return xpath_results;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -