Beispiel #1
0
/**
 * Process a file, checking if it should be listed and retrieving its data in
 * XML format.
 */
xmlNodePtr
file_proc(const gchar *real_path,
          const gchar *file_name)
{
    gchar *file_name_utf8;
    const gchar *file_extension;
    FileParser *parser;
    xmlNodePtr file_node = NULL;

    /* get UTF-8 encoded filename and extension */
    file_name_utf8 = g_filename_to_utf8(file_name, -1, NULL, NULL, NULL);
    file_extension = filename_get_extension(file_name_utf8);

    /* check if we support this file */
    if (file_extension == NULL || file_extension[0] == '\0') {
        /* extension-less file, we just ignore */
    }
    else if ((parser = file_parser_get_by_extension(file_extension)) != NULL) {
        /* we have a parser for this file -- good -- go ahead */
        FILE *fp;

        /* create file node */
        file_node = xmlNewNode(NULL, "file");
        xmlNewProp(file_node, "name", file_name_utf8);

        if ((fp = fopen(real_path, "rb")) != NULL) {
            glong file_size;
            gchar size_string[64];

            /* retrieve file size */
            /* @TODO support 64-bit file sizes */
            fseek(fp, 0, SEEK_END);
            file_size = ftell(fp);

            g_snprintf(size_string, sizeof size_string, "%ld", file_size);
            xmlNewProp(file_node, "size", size_string);

            /* parse (or at least try to) the file */
            rewind(fp);
            parser->func(file_node, fp);
        }
        else {                          /* fopen() returned NULL */
            xmlNodePtr comment = xmlNewComment("error opening file");
            xmlAddChild(file_node, comment);

            g_warning("%s: %s", real_path, g_strerror(errno));
        }

    }

    g_free(file_name_utf8);             /* free UTF-8 converted filename */
    return file_node;
}