/** * ezXML - XML Parsing C Library * Home: http://ezxml.sourceforge.net/ * Author: Aaron Voisine * * DESCRIPTION * =========== * This simple function allows to indent XML structures, * in order to beautify XML code. * Simple usage: * ezxml_pretty(xml); * char * buffer = ezxml_toxml(xml); * printf("%s\n", buffer); * * LICENCE * ======= * Copyright 2004-2006 Aaron Voisine * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #define EZXML_INDENT 2 ezxml_t ezxml_pretty(ezxml_t xml) { int i = 0, j, level = 0; ezxml_t x; char *s; if (! xml || strspn(xml->txt, " \t\r\n") != strlen(xml->txt)) return xml; if (xml->child) { for (x = xml; x; x = x->parent) level += EZXML_INDENT; // how deep? for (x = xml->child; x; x = x->ordered) { i += level + 1; x->off = i; } s = (char *)malloc(i + level + 2); for (j = 0; j <= i; j += level + 1) { s[j] = '\n'; memset(s + j + 1, ' ', level); } s[j - EZXML_INDENT] = '\0'; ezxml_set_flag(ezxml_set_txt(xml, s), EZXML_TXTM); ezxml_pretty(xml->child); } else ezxml_set_txt(xml, ""); return ezxml_pretty(xml->ordered); }