Beispiel #1
0
rc_t make_stdout_redir ( stdout_redir ** writer, const char * filename, size_t bufsize )
{
    KDirectory *dir;
    rc_t rc = 0;
    stdout_redir * wr = NULL;

    if ( writer == NULL )
    {
        *writer = NULL;
        rc = RC ( rcApp, rcNoTarg, rcAllocating, rcSelf, rcNull );
    }
    else
    {
        wr = calloc( 1, sizeof *wr );
        if ( wr == NULL )
            rc = RC ( rcApp, rcNoTarg, rcAllocating, rcMemory, rcExhausted );
    }

    if ( rc == 0 )
    {
        rc = KDirectoryNativeDir( &dir );
        if ( rc == 0 )
        {
            KFile *of;
            rc = KDirectoryCreateFile ( dir, &of, false, 0664, kcmInit | kcmCreate, "%s", filename );
            if ( rc == 0 )
            {
                KFile* buf;
                rc = KBufFileMakeWrite( &buf, of, false, bufsize );
                if ( rc == 0 )
                {
                    wr->kfile = buf;
                    wr->org_writer = KOutWriterGet();
                    wr->org_data = KOutDataGet();
                    rc = KOutHandlerSet( stdout_redir_callback, wr );
                    if ( rc != 0 )
                        LOGERR( klogInt, rc, "KOutHandlerSet() failed" );
                }
                KFileRelease( of );
            }
            KDirectoryRelease( dir );
        }
    }

    if ( rc == 0 )
        *writer = wr;
    else
    {
        if ( wr != NULL )
            free( wr );
    }

    return rc;
}
Beispiel #2
0
rc_t make_lookup_writer( KDirectory *dir, struct index_writer * idx,
                         struct lookup_writer ** writer, size_t buf_size,
                         const char * fmt, ... )
{
    rc_t rc;
    struct KFile * f;
    
    va_list args;
    va_start ( args, fmt );

    rc = KDirectoryVCreateFile( dir, &f, false, 0664, kcmInit, fmt, args );
    if ( rc != 0 )
        ErrMsg( "KDirectoryVCreateFile() -> %R", rc );
    else
    {
        if ( buf_size > 0 )
        {
            struct KFile * temp_file;
            rc = KBufFileMakeWrite( &temp_file, f, false, buf_size );
            if ( rc != 0 )
                ErrMsg( "KBufFileMakeWrite() -> %R", rc );
            else
            {
                KFileRelease( f );
                f = temp_file;
            }
        }

        if ( rc == 0 )
        {
            rc = make_lookup_writer_obj( writer, idx, f );
            if ( rc != 0 )
                KFileRelease( f );
        }
    }
    va_end ( args );
    return rc;
}
Beispiel #3
0
rc_t init_out_redir( out_redir * self, out_redir_mode_t mode, const char * filename, size_t bufsize )
{
    rc_t rc;
    KFile *output_file;

    if ( filename != NULL )
    {
        KDirectory *dir;
        rc = KDirectoryNativeDir( &dir );
        if ( rc != 0 )
            LOGERR( klogInt, rc, "KDirectoryNativeDir() failed" );
        else
        {
            rc = KDirectoryCreateFile ( dir, &output_file, false, 0664, kcmInit, "%s", filename );
            KDirectoryRelease( dir );
        }
    }
    else
        rc = KFileMakeStdOut ( &output_file );

    if ( rc == 0 )
    {
        KFile *temp_file;

        /* wrap the output-file in compression, if requested */
        switch ( mode )
        {
            case orm_gzip  : rc = KFileMakeGzipForWrite( &temp_file, output_file ); break;
            case orm_bzip2 : rc = KFileMakeBzip2ForWrite( &temp_file, output_file ); break;
            case orm_uncompressed : break;
        }
        if ( rc == 0 )
        {
            if ( mode != orm_uncompressed )
            {
                KFileRelease( output_file );
                output_file = temp_file;
            }

            /* wrap the output/compressed-file in buffering, if requested */
            if ( bufsize != 0 )
            {
                rc = KBufFileMakeWrite( &temp_file, output_file, false, bufsize );
                if ( rc == 0 )
                {
                    KFileRelease( output_file );
                    output_file = temp_file;
                }
            }

            if ( rc == 0 )
            {
                self->kfile = output_file;
                self->org_writer = KOutWriterGet();
                self->org_data = KOutDataGet();
                self->pos = 0;
                rc = KOutHandlerSet( out_redir_callback, self );
                if ( rc != 0 )
                    LOGERR( klogInt, rc, "KOutHandlerSet() failed" );
            }
        }
    }
    return rc;
}