Changeset 483 for trunk/thune/files.c

Show
Ignore:
Timestamp:
10/05/07 04:54:10 (14 months ago)
Author:
krobillard
Message:

select! evaluation now pushes all values other than function! & call!.
Added ur_decompress().
Now using GLV from the Outguard repository.
gl/project.r now builds thuneGL library.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/thune/files.c

    r478 r483  
    696696 
    697697 
     698/** 
     699   Decompress data into intialized array. 
     700 
     701   \return  Non-zero if successful. 
     702*/ 
     703int ur_decompress( const void* data, int len, UArray* arr ) 
     704{ 
     705#define BUF_LOW     32 
     706    bz_stream strm; 
     707    int ok; 
     708 
     709    strm.bzalloc = 0; 
     710    strm.bzfree  = 0; 
     711    strm.opaque  = 0; 
     712 
     713    ok = BZ2_bzDecompressInit( &strm, 0, 0 ); 
     714    if( ok != BZ_OK ) 
     715    { 
     716        //fprintf( stderr, "BZ2_bzDecompressInit failure (%d)", ok ); 
     717        return 0; 
     718    } 
     719 
     720    strm.next_in  = (char*) data; 
     721    strm.avail_in = len; 
     722 
     723    ur_arrayReserve( arr, 1, (len < BUF_LOW) ? BUF_LOW : len ); 
     724    strm.next_out  = arr->ptr.c; 
     725    strm.avail_out = arr->avail; 
     726 
     727    do 
     728    { 
     729        if( strm.avail_out < BUF_LOW ) 
     730        { 
     731            ur_arrayReserve( arr, 1, arr->avail + (2 * BUF_LOW) ); 
     732            strm.next_out  = arr->ptr.c + strm.total_out_lo32; 
     733            strm.avail_out = arr->avail - strm.total_out_lo32; 
     734        } 
     735 
     736        ok = BZ2_bzDecompress( &strm ); 
     737        arr->used = strm.total_out_lo32; 
     738    } 
     739    while( ok == BZ_OK ); 
     740 
     741    BZ2_bzDecompressEnd( &strm ); 
     742 
     743    return (ok == BZ_STREAM_END) ? 1 : 0; 
     744} 
     745 
     746 
    698747// (binary -- string) 
    699748UR_CALL( uc_decompress ) 
    700749{ 
    701 #define BUF_LOW     32 
    702     int ok; 
    703     int len; 
    704     bz_stream strm; 
    705750    UIndex strN; 
    706751    UString* str; 
     
    708753    char* cpB; 
    709754 
    710  
    711755    if( ! ur_binarySlice( ut, tos, &cpA, &cpB ) || (cpA == 0) ) 
    712756        return; 
    713757 
    714     strm.bzalloc = 0; 
    715     strm.bzfree  = 0; 
    716     strm.opaque  = 0; 
    717  
    718     ok = BZ2_bzDecompressInit( &strm, 0, 0 ); 
    719     if( ok == BZ_OK ) 
    720     { 
    721         len = cpB - cpA; 
    722         strm.next_in   = cpA; 
    723         strm.avail_in  = len; 
    724  
    725         strN = ur_makeBinary( (len < BUF_LOW) ? BUF_LOW : len, &str ); 
    726         strm.next_out  = str->ptr.c; 
    727         strm.avail_out = str->avail; 
    728  
    729         while( ok == BZ_OK ) 
    730         { 
    731             if( strm.avail_out < BUF_LOW ) 
    732             { 
    733                 ur_arrayReserve( str, 1, str->avail + (2 * BUF_LOW) ); 
    734                 strm.next_out  = str->ptr.c + strm.total_out_lo32; 
    735                 strm.avail_out = str->avail - strm.total_out_lo32; 
    736             } 
    737  
    738             ok = BZ2_bzDecompress( &strm ); 
    739             str->used = strm.total_out_lo32; 
    740         } 
    741  
    742         BZ2_bzDecompressEnd( &strm ); 
    743  
    744         if( ok == BZ_STREAM_END ) 
    745         { 
    746             ur_initType( tos, UT_STRING ); 
    747             ur_setSeries( tos, strN, 0 ); 
    748             return; 
    749         } 
    750         ur_throwErr( UR_ERR_INTERNAL, "decompress failure (%d)", ok ); 
    751     } 
    752     ur_throwErr( UR_ERR_INTERNAL, "decompress init failure (%d)", ok ); 
     758    strN = ur_makeBinary( 0, &str ); 
     759 
     760    if( ur_decompress( cpA, cpB - cpA, str ) ) 
     761    { 
     762        ur_initString( tos, strN, 0 ); 
     763    } 
     764    else 
     765    { 
     766        ur_throwErr( UR_ERR_INTERNAL, "decompress failure" ); 
     767    } 
    753768} 
    754769#endif