mlview-hacking
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Mlview-hacking] Patch to support gzipped files


From: Pierre Barbier de Reuille
Subject: [Mlview-hacking] Patch to support gzipped files
Date: Sat, 30 Nov 2002 00:12:35 +0100
User-agent: Mutt/1.3.28i

Hi,

I just modified the loading of the local files in order to load
correctly zipped files. Then, if I store in the xmlDoc that the file is
compressed (as I don't know how much it's compressed, I say it's
compressed at best). Then, when the xml is saved, it's automatically
receompressed. The only bad point is that I don't know really how to
test if a file is compressed, so I just test the 2 first bytes, but I
don't know if it's enough ! At least, it's enough to part a gzip file
and a text file though ...

Ok, so I send the code of the function I modified.

/******** Beginning **********/

static enum MlViewStatus
load_xml_document_from_local_file (gchar * a_xml_file_name, 
                   xmlParserCtxt ** a_parser_context,
                   gboolean a_store_external_subset_info,
                   MlViewAppContext * a_app_context)
{
    FILE * file_ptr ;
    int c1,c2;
    int compressed;
    gzFile zfile_ptr;
    int size = 1024, num_of_chars_read = 0, parse_status ;
    gchar file_buffer[size] ;
    MlViewParserCtxt * custom_parser_context ;
    gchar * validation_is_on = NULL ;

    /*initialyze validation flag to "false" => no validation.
     *That way, libxml2 won't validate the document at load time.
     *We can then do post parsing validation.
     */
    xmlDoValidityCheckingDefaultValue = 0 ;

    if (a_app_context)
        validation_is_on = 
            mlview_app_context_get_settings_value 
            (a_app_context, MLVIEW_STG_K_IS_VALIDATION_ON) ;

    /*now, open the xml file...*/
    g_return_val_if_fail ( a_xml_file_name != NULL, 
                   MLVIEW_NULL_FILE_NAME_ERROR) ;
    
    file_ptr = fopen (a_xml_file_name, "rb") ;
    c1 = fgetc(file_ptr);
    c2 = fgetc(file_ptr);
    fclose(file_ptr);
    if((c1 == 0x1F) && (c2 == 0x8B))
    {
        compressed = 1;
    }
    else
    {
        compressed = 0;
    }
    zfile_ptr = gzopen(a_xml_file_name, "rb") ;
    //g_return_val_if_fail ( file_ptr != NULL,
    g_return_val_if_fail ( zfile_ptr != NULL,
                   MLVIEW_BAD_FILE_ERROR ) ;
    //num_of_chars_read = fread ( file_buffer, 1 ,4,file_ptr) ;
    num_of_chars_read = gzread ( zfile_ptr, file_buffer, 4) ;
    g_return_val_if_fail ( num_of_chars_read > 0,
                   MLVIEW_EMPTY_FILE_ERROR) ;

    /*
     *Force the parser to ignore ignorable white spaces so that
     *indentation can be made properly at save time.
     */
    xmlKeepBlanksDefault (0) ;

    //fprintf("file_buffer : %s\n", file_buffer);
    (*a_parser_context) = 
        xmlCreatePushParserCtxt (NULL,NULL,
                     file_buffer,num_of_chars_read,
                     a_xml_file_name) ;

    /*let's create a custom parser context that inherits the native
     *libxml's xmlParserCtxt. 
     *That custom parser context embedds the MlViewAppContext.
     *that's usefull for our custom SAX handlers ... 
     *that way, the custom SAX handlers
     *we've written can take full advantage of 
     *the capabilities of the MlViewAppContext object.
     */
    custom_parser_context = mlview_parser_ctxt_new 
        (*a_parser_context,
         a_app_context) ;

    g_return_val_if_fail (custom_parser_context != NULL, 
                  MLVIEW_ERROR) ;

    if (a_store_external_subset_info == TRUE) 
        {
            /*
         *overload the externalSubset sax handler with our custom one
         *that stores the info about the external 
         *subset declared in the xml doc
         *so that we can do post parsing validation.
         */

        /*overload the externalSubset sax handler*/
        ((xmlParserCtxt*)custom_parser_context)->sax->externalSubset = 
            mlview_custom_external_subset_sax_handler ;

        ((xmlParserCtxt*)custom_parser_context)->sax->resolveEntity = 
            mlview_custom_sax_resolve_entity ;
        
        /*
         *tell the custom sax handler to save the 
         *external subset definition ... or not
         */
        p_store_external_subset_def = a_store_external_subset_info ;
    }
    
    /*override the error messaging stream of libxml*/
    xmlSetGenericErrorFunc(a_app_context, 
                   (xmlGenericErrorFunc) 
                   mlview_app_context_bufferize_error) ;

    //while ( (num_of_chars_read = fread (file_buffer,1,size,file_ptr))
    //>0 ) 
    while ( (num_of_chars_read = gzread (zfile_ptr, file_buffer,size))
>0 ) 
        {
        parse_status = 
            xmlParseChunk 
            ((xmlParserCtxt*) custom_parser_context,
             file_buffer,num_of_chars_read, 0) ;

        if (parse_status != XML_ERR_OK)
            break ;
    }

    /*
     *call the xmlParseChunk once more 2 
     *consume all the data stored in 
     *the parser internal cache ...
     */
    if (parse_status == XML_ERR_OK) 
        {
        parse_status = 
            xmlParseChunk ((xmlParserCtxt*) custom_parser_context,
                       file_buffer,num_of_chars_read,1) ;
    }

        /*restore the error display stream of libxml*/
    xmlSetGenericErrorFunc(NULL, NULL) ;

    if ( a_app_context  
         &&  ! mlview_app_context_error_buffer_is_empty (a_app_context))
        {
        mlview_app_context_display_buffered_error 
            (a_app_context) ;
        }
    else
        {
        mlview_app_context_set_error_dialog_title 
            (a_app_context, NULL) ; 
        }

    memcpy (*a_parser_context, custom_parser_context, 
        sizeof (xmlParserCtxt)) ;

    mlview_parser_ctxt_destroy (custom_parser_context) ;
    custom_parser_context = NULL ;
    gzclose(zfile_ptr);

    /*
     * If the file is compressed ... we set it inside the doc
     */
    if(compressed)
    {
        printf("Compression detected\n");
        xmlSetDocCompressMode((**a_parser_context).myDoc,9);
    }

    return (parse_status == XML_ERR_OK) ? 0 : parse_status ;
}


/********** End **************/

-- 
Pierre Barbier de Reuille




reply via email to

[Prev in Thread] Current Thread [Next in Thread]