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

📄 sflxmll.c

📁 短小精悍的C语言标准函数库。提供450个以上的可移植的算法和工具代码。
💻 C
📖 第 1 页 / 共 3 页
字号:
            strcat (xml_string, ">");
            for (child  = xml_first_child (item);
                 child != NULL;
                 child  = xml_next_sibling (child))
              {
                xml_string += strlen (xml_string);
                xml_save_string_item (xml_string, child);
              }
            xstrcat (xml_string, "</", item_name, ">", NULL);
          }
        else
            strcat (xml_string, "/>");
      }
    else            /*  No name => this is a value node                      */
      {
        ptr = xml_item_value (item);
        while (*ptr)
          {
            xml_string += strlen (xml_string);
            http_encode_meta (token, &ptr, LINE_MAX, FALSE);
            strcat (xml_string, token);
          }
      }
}


/*  ---------------------------------------------------------------------[<]-
    Function: xml_error

    Synopsis: Returns the last error message generated by xml_load.
    ---------------------------------------------------------------------[>]-*/

char *
xml_error (void)
{
    return (error_message);
}


/*  ---------------------------------------------------------------------[<]-
    Function: xml_seems_to_be

    Synopsis: Reads the first line of a file.  Returns XML_NOERROR if file
    is found and has a valid XML header, XML_FILEERROR if the file could not
    be opened and XML_LOADERROR if the first line is not an XML header.
    ---------------------------------------------------------------------[>]-*/

int
xml_seems_to_be (const char *path,
                 const char *filename)
{
    ASSERT (filename);
    pname = filename;
    ppath = path;

    /*  Current line w/space for EOL                                         */
    xmlline = mem_alloc (LINE_MAX + 2);

    the_next_event = looks_like_xml_event;
    xml_start_dialog ();
    mem_free (xmlline);
    return (feedback);
}


/*  ---------------------------------------------------------------------[<]-
    Function: xml_load_file

    Synopsis: Loads the contents of an XML file into a given XML tree,
    or a new XML tree if none was specified.  The XML data is not checked
    against a DTD.  Returns one of XML_NOERROR, XML_FILEERROR,
    XML_LOADERROR.  If the parameter `allow_extended' is TRUE then the file
    may contain more than one XML item.  The following attributes are
    defined in the root item of the XML tree.
    <TABLE>
    filename        Name of the XML input file
    filetime        Modification time of the file, "HHMMSSCC"
    filedate        Modification date of input file, "YYYYMMDD"
    </TABLE>
    Looks for the XML file on the specified path symbol, or in the current
    directory if the path argument is null.  Adds the extension ".xml"
    to the file name if there is none already included.
    ---------------------------------------------------------------------[>]-*/

int
xml_load_file (XML_ITEM  **item,
               const char *path,
               const char *filename,
               Bool allow_extended)
{
    ASSERT (filename);
    pname = filename;
    ppath = path;

    extended = allow_extended;

    if (! *item)
      {
        *item = xml_new (NULL, "root", "");
        ASSERT (*item);
      }
    root = *item;

    /*  Current line w/space for EOL                                         */
    xmlline = mem_alloc (LINE_MAX + 2);

    line_nbr = 0;

    the_next_event = file_event;

    xml_start_dialog ();
    mem_free (xmlline);
    return (feedback);
}


/*  ---------------------------------------------------------------------[<]-
    Function: xml_load_string

    Synopsis: Loads an XML string into a new XML tree.  The XML data is not
    checked against a DTD.  See xml_load() for details.
    ---------------------------------------------------------------------[>]-*/

int
xml_load_string (XML_ITEM  **item,
                 const char *xmlstring,
                 Bool allow_extended)
{
    ASSERT (xmlstring);
    pname = NULL;
    ppath = NULL;

    xmltext  = (char *) xmlstring;
    xmlline  = xmltext;

    extended = allow_extended;

    if (! *item)
      {
        *item = xml_new (NULL, "root", "");
        ASSERT (*item);
      }
    root = *item;
    
    char_nbr = 0;                       /*  Use string as input line         */
    line_nbr = 1;

    the_next_event = string_event;

    xml_start_dialog ();
    return (feedback);
}


int
xml_start_dialog (void)
{
    feedback = XML_NOERROR;             /*  Reset return feedback            */
#   include "sflxmll.i"                 /*  Include dialog interpreter       */
}


/*************************   INITIALISE THE PROGRAM   ************************/

MODULE initialise_the_program (void)
{
    xmlfile    = NULL;                  /*  No files open yet                */
    generation = 0;                     /*  No items yet                     */
    top_item   = NULL;

    init_charmaps ();                   /*  Initialise character maps        */
}


/*****************************   OPEN XML FILE   *****************************/

MODULE open_xml_file (void)
{
    fname = file_where ('r', ppath, pname, "xml");
    if (!fname)
      {
        error_exception ("Could not find XML file: %s", pname);
        feedback = XML_FILEERROR;
        return;
      }
    if ((xmlfile = file_open (fname, 'r')) == NULL)
      {
        error_exception ("Could not open XML file: %s", pname);
        feedback = XML_FILEERROR;
        return;
      }

    char_nbr    = 0;                    /*  Clear input line                 */
    xmlline [0] = 0;
}


/**************************   INITIALISE XML TREE   **************************/

MODULE initialise_xml_tree (void)
{
    static char
        buffer [60];

    if (pname && (! extended))
      {
        xml_put_attr (root, "filename", fname);
        sprintf (buffer, "%ld", timer_to_date (get_file_time (fname)));
        xml_put_attr (root, "filedate", buffer);
        sprintf (buffer, "%ld", timer_to_time (get_file_time (fname)));
        xml_put_attr (root, "filetime", buffer);
      }
    item = root;
}


/***************************   GET CONTENT TOKEN   ***************************/

MODULE get_content_token (void)
{
    char thisch;                        /*  Next char in token               */

    thisch = get_next_char ();
    if (thisch == '<')
      {
        if (xmlline [char_nbr] == '?')
          {
            char_nbr ++;
            the_next_event = processing_event;
          }
        else
        if (xmlline [char_nbr] == '/')
          {
            char_nbr ++;
            the_next_event = close_event;
          }
        else
        if (xmlline [char_nbr] == '!')
            if ((xmlline [char_nbr + 1] == '-')
            &&  (xmlline [char_nbr + 2] == '-'))
              {
                char_nbr += 3;
                the_next_event = comment_event;
              }
            else
              {
                char_nbr ++;
                the_next_event = ignore_event;
              }
        else
            the_next_event = open_event;
      }
    else
    if (thisch == '\0')
        the_next_event = end_of_file_event;
    else
    if (strchr (" \t\r\n", thisch))
        the_next_event = space_event;
    else
        the_next_event = char_event;
}


static char
get_next_char (void)
{
    if (xmlfile)
        while (xmlline [char_nbr] == 0)
          {
            char_nbr = 0;
            if (file_read (xmlfile, xmlline))
              {
                strcat (xmlline, "\n");
                line_nbr++;
              }
            else
              {
                xmlline [0] = '\0';
                the_next_event = end_of_file_event;
                return 0;
              }
          }
    else
      {
        if ((char_nbr > 0)
        &&  (xmlline [char_nbr - 1] == '\n'))
          {
            line_nbr++;
            xmlline += char_nbr;
            char_nbr = 0;
          }
        if (xmlline [char_nbr] == 0)
          {
            the_next_event = end_of_file_event;
            return 0;
          }
      }
    return (xmlline [char_nbr++]);
}


/*****************************   GET TAG TOKEN   *****************************/

MODULE get_tag_token (void)
{
    char thisch;                        /*  Next char in token               */

    thisch = get_next_non_white_char ();

    if (!thisch)
        return;

    if (thisch == '/')
        the_next_event = slash_event;
    else
    if (thisch == '>')
        the_next_event = close_event;
    else
    if is_name_open (thisch)
      {
        name [0] = thisch;
        collect_name ();
        strcpy (name + 1, token);
        the_next_event = name_event;
      }
    else
    if ((thisch == '<')
    &&  (xmlline [char_nbr] == '!')
    &&  (xmlline [char_nbr + 1] == '-')
    &&  (xmlline [char_nbr + 2] == '-'))
      {
        char_nbr += 3;
        the_next_event = comment_event;
      }
    else
        error_exception ("Unrecognised token: %c", thisch);
}


static char
get_next_non_white_char (void)
{
    char thisch;                        /*  Next char in token               */

    thisch = get_next_char ();
    while (strchr (" \t\r\n", thisch) && thisch)
        thisch = get_next_char ();

    return thisch;
}


static int
collect_name (void)
{
    int  size;                          /*  Size of token                    */
    char thisch;                        /*  Next char                        */

    /*  Name ::= (Letter | '_' | ':') (NameChar)*                            */

    size = 0;
    while (is_name (thisch = get_next_char ()))
        token [size++] = thisch;

    char_nbr--;

    token [size] = 0;                   /*  Terminate token string           */
    return size;
}


/***************************   COLLECT ITEM VALUE   **************************/

MODULE collect_item_value (void)
{
    char
        *value;

    char_nbr --;                        /*  Back up to starting char         */
    value = collect_literal ('<');
    char_nbr --;                        /*  Back up to terminating char      */

    xml_new (item, NULL, value);

    mem_free (value);
}


static char *
collect_literal (char terminator)
{
    char
        thisch,                         /*  Next character                   */
        *literal = NULL,                /*  The result                       */
        *bufptr;                        /*  Pointer to input buffer          */
    int
        length = 0,                     /*  Total length of literal          */
        chunk = 0,                      /*  Length of line in literal        */
        snippet;                        /*  Length of decoded line           */
    int
        start_line_nbr;                 /*  Where literal started            */

    thisch = get_next_char ();
    start_line_nbr = line_nbr;

    FOREVER
      {
        while (thisch && thisch != terminator && chunk < LINE_MAX)
          {
            token [chunk++] = thisch;
            thisch = get_next_char ();

⌨️ 快捷键说明

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