1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/ports/SkXMLParser_tinyxml.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#include "SkXMLParser.h" 1.14 +#include "SkStream.h" 1.15 +#include "SkTemplates.h" 1.16 +#include "tinyxml.h" 1.17 + 1.18 +static void walk_elem(SkXMLParser* parser, const TiXmlElement* elem) 1.19 +{ 1.20 + //printf("walk_elem(%s) ", elem->Value()); 1.21 + 1.22 + parser->startElement(elem->Value()); 1.23 + 1.24 + const TiXmlAttribute* attr = elem->FirstAttribute(); 1.25 + while (attr) 1.26 + { 1.27 + //printf("walk_elem_attr(%s=\"%s\") ", attr->Name(), attr->Value()); 1.28 + 1.29 + parser->addAttribute(attr->Name(), attr->Value()); 1.30 + attr = attr->Next(); 1.31 + } 1.32 + //printf("\n"); 1.33 + 1.34 + const TiXmlNode* node = elem->FirstChild(); 1.35 + while (node) 1.36 + { 1.37 + if (node->ToElement()) 1.38 + walk_elem(parser, node->ToElement()); 1.39 + else if (node->ToText()) 1.40 + parser->text(node->Value(), strlen(node->Value())); 1.41 + node = node->NextSibling(); 1.42 + } 1.43 + 1.44 + parser->endElement(elem->Value()); 1.45 +} 1.46 + 1.47 +static bool load_buf(SkXMLParser* parser, const char buf[]) 1.48 +{ 1.49 + TiXmlDocument doc; 1.50 + 1.51 + (void)doc.Parse(buf); 1.52 + if (doc.Error()) 1.53 + { 1.54 + printf("tinyxml error: <%s> row[%d] col[%d]\n", doc.ErrorDesc(), doc.ErrorRow(), doc.ErrorCol()); 1.55 + return false; 1.56 + } 1.57 + 1.58 + walk_elem(parser, doc.RootElement()); 1.59 + return true; 1.60 +} 1.61 + 1.62 +bool SkXMLParser::parse(SkStream& stream) 1.63 +{ 1.64 + size_t size = stream.getLength(); 1.65 + 1.66 + SkAutoMalloc buffer(size + 1); 1.67 + char* buf = (char*)buffer.get(); 1.68 + 1.69 + stream.read(buf, size); 1.70 + buf[size] = 0; 1.71 + 1.72 + return load_buf(this, buf); 1.73 +} 1.74 + 1.75 +bool SkXMLParser::parse(const char doc[], size_t len) 1.76 +{ 1.77 + SkAutoMalloc buffer(len + 1); 1.78 + char* buf = (char*)buffer.get(); 1.79 + 1.80 + memcpy(buf, doc, len); 1.81 + buf[len] = 0; 1.82 + 1.83 + return load_buf(this, buf); 1.84 +} 1.85 + 1.86 +void SkXMLParser::GetNativeErrorString(int error, SkString* str) 1.87 +{ 1.88 + if (str) 1.89 + str->set("GetNativeErrorString not implemented for TinyXml"); 1.90 +}