Warning, file /education/cantor/thirdparty/discount-2.2.6-patched/xml.c was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* markdown: a C implementation of John Gruber's Markdown markup language.
0002  *
0003  * Copyright (C) 2007 David L Parsons.
0004  * The redistribution terms are provided in the COPYRIGHT file that must
0005  * be distributed with this source code.
0006  */
0007 #include <stdio.h>
0008 #include <string.h>
0009 #include <stdarg.h>
0010 #include <stdlib.h>
0011 #include <time.h>
0012 #include <ctype.h>
0013 
0014 #include "config.h"
0015 
0016 #include "cstring.h"
0017 #include "markdown.h"
0018 #include "amalloc.h"
0019 
0020 /* return the xml version of a character
0021  */
0022 static char *
0023 mkd_xmlchar(unsigned char c)
0024 {
0025     switch (c) {
0026     case '<':   return "&lt;";
0027     case '>':   return "&gt;";
0028     case '&':   return "&amp;";
0029     case '"':   return "&quot;";
0030     case '\'':  return "&apos;";
0031     default:    if ( isascii(c) || (c & 0x80) )
0032             return 0;
0033         return "";
0034     }
0035 }
0036 
0037 
0038 /* write output in XML format
0039  */
0040 int
0041 mkd_generatexml(char *p, int size, FILE *out)
0042 {
0043     unsigned char c;
0044     char *entity;
0045 
0046     while ( size-- > 0 ) {
0047     c = *p++;
0048 
0049     if ( entity = mkd_xmlchar(c) )
0050         DO_OR_DIE( fputs(entity, out) );
0051     else
0052         DO_OR_DIE( fputc(c, out) );
0053     }
0054     return 0;
0055 }
0056 
0057 
0058 /* build a xml'ed version of a string
0059  */
0060 int
0061 mkd_xml(char *p, int size, char **res)
0062 {
0063     unsigned char c;
0064     char *entity;
0065     Cstring f;
0066 
0067     CREATE(f);
0068     RESERVE(f, 100);
0069 
0070     while ( size-- > 0 ) {
0071     c = *p++;
0072     if ( entity = mkd_xmlchar(c) )
0073         Cswrite(&f, entity, strlen(entity));
0074     else
0075         Csputc(c, &f);
0076     }
0077     /* null terminate, strdup() into a free()able memory block,
0078      * and return the size of everything except the null terminator
0079      */
0080     EXPAND(f) = 0;
0081     *res = strdup(T(f));
0082     return S(f)-1;
0083 }