/* * data.cc * This file is part of him-arabic * * Copyright (C) 2007 Mohammed Sameer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ #include "data.hh" #include Data::Data(const std::string& xml) { LIBXML_TEST_VERSION; xmlDocPtr doc = xmlReadMemory(xml.c_str(), xml.size(), "noname.xml", NULL, 0); if (doc == NULL) { return; } xmlNode *root_element = xmlDocGetRootElement(doc); xmlNode *cur_node = NULL; for (cur_node = root_element; cur_node; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE) { parse_layout(cur_node->children); } } xmlFreeDoc(doc); xmlCleanupParser(); } Data::~Data() { } bool Data::operator!() { return layouts.size() == 0; } void Data::parse_layout(xmlNode *node) { xmlNode *cur_node; for (cur_node = node; cur_node; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE) { // const char *a = (char *)(xmlGetProp(cur_node, (const xmlChar *)"mode_a")); const char *b = (char *)(xmlGetProp(cur_node, (const xmlChar *)"mode_b")); if (!strcmp(b, "0")) { // Default layout. std::vector > row; parse_rows(cur_node->children, row); layouts[LAYOUT_DEFAULT] = row; } else if (strcmp(b, "0")) { std::vector > row; parse_rows(cur_node->children, row); layouts[LAYOUT_B] = row; } } } } void Data::parse_rows(xmlNode *node, std::vector >& row) { for (xmlNode *cur_node = node; cur_node; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE) { std::vector v; parse_keys(cur_node->children, v); row.push_back(v); } } } void Data::parse_keys(xmlNode *node, std::vector& v) { for (xmlNode *cur_node = node; cur_node; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE) { Key key; key.name = (char *)xmlGetProp(cur_node, (const xmlChar *)"name"); key.expand = (strcmp((const char *)xmlGetProp(cur_node, (const xmlChar *)"expand"), "1") == 0); v.push_back(key); } } } std::vector >& Data::get_layout(const LayoutType& type) throw(std::string) { std::map > >::iterator iter = layouts.find(type); if (iter == layouts.end()) { std::string error = "Can't find this layout."; throw(error); } return iter->second; }