b_write.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 1,191 行 · 第 1/3 页

C
1,191
字号
    BRI_lseek( handle, handle->start, BRI_SEEK_SET );
    BRI_write( handle, handle->hdr, sizeof(*handle->hdr) );

    BRI_free( handle, handle->hdr );

    {
        BRI_FileCounter *current;
        BRI_FileCounter *prev;

        prev = NULL;
        current = handle->files;
        while( current != NULL ){
            prev = current;
            current = current->next;
            BRI_free( handle, prev );
        }
    }
    CarveDestroy( handle->carve_types );
    CarveDestroy( handle->carve_string );
    BRI_free( handle, handle->char_buf );

    BRI_free( handle, handle );
}


/*  BRICreate           -- Create a browse handle, but don't prepare
                           it for use.  Handle can be prepared later
                           by calling BRIOpen.
*/

BRI_HANDLE BRICreate( BRI_Routines const *rtns )
/**********************************************/
{
    BRI_HANDLE          result;

    result = (BRI_HANDLE) rtns->malloc( sizeof(BRI_Handle) );
    if( result == NULL ) {
        // TODO:  FATAL ERROR
        return (BRI_HANDLE) NULL;
    }

    memset( result, 0, sizeof( *result ) );
    result->rtns = *rtns;
    result->io_cookie = NULL;
    result->start = 0;
    result->files = NULL;
    result->types = NULL;
    result->carve_types = CarveCreate( sizeof(BRI_TypeCounter), LIST_BLOCK );
    result->char_buf = BRI_malloc( result, CHAR_BLOCK*sizeof(char) );
    result->buf_size = CHAR_BLOCK;
    result->buf_top = 0;
    memset( result->table, 0, NUM_BUCKETS*sizeof(BRI_HashString*) );
    result->carve_string = CarveCreate( sizeof(BRI_HashString), LIST_BLOCK );
    result->hdr = NULL;

    return result;
}


/*  BRIOpen             -- Prepare a previously created browse handle
                           for use.
*/

BRI_HANDLE BRIOpen( BRI_HANDLE handle, BRI_Routines const *rtns,
                    int io_cookie, unsigned long start )
/*****************************************************************/
{
    BRI_Header *        hdr;

    if( handle == NULL ){
        return NULL;
    }

    handle->rtns = *rtns;
    handle->io_cookie = io_cookie;
    handle->start = start;
    handle->files = NULL;

    hdr = (BRI_Header *) BRI_malloc( handle, sizeof( BRI_Header ) );
    memset( hdr, 0, sizeof(*hdr) );
    hdr->magic = BRI_MAGIC;
    hdr->major_ver = BRI_MAJOR_VER;
    hdr->minor_ver = BRI_MINOR_VER;
    BRI_write( handle, hdr, sizeof( *hdr ) );
    handle->hdr = hdr;

    return handle;
}


/*  BRIClose            -- Stop using a browse handle.  The handle can
                           be re-used later by calling BRIOpen.
*/

BRI_HANDLE BRIClose( BRI_HANDLE handle )
/***********************************/
{
    BRI_FileCounter     *current;
    BRI_FileCounter     *prev;
    int                 out_len;

    // Get the current position in the file.
    out_len = BRI_lseek( handle, 0, BRI_SEEK_CUR );

    handle->hdr->file_len = out_len - handle->start;
    BRI_lseek( handle, handle->start, BRI_SEEK_SET );
    BRI_write( handle, handle->hdr, sizeof(*handle->hdr) );

    BRI_free( handle, handle->hdr );
    handle->hdr = NULL;

    prev = NULL;
    current = handle->files;
    while( current != NULL ){
        prev = current;
        current = current->next;
        BRI_free( handle, prev );
    }
    handle->files = NULL;

    return handle;
}


/*   BRIStartFile
*/

void BRIStartFile( BRI_HANDLE handle, BRI_StringID filename )
/*************************************************************/
{
    BRI_RecordType      file_rt = BRI_Rec_File;
    BRI_FileCounter *   new_counter;

    new_counter = (BRI_FileCounter *)
                  BRI_malloc( handle, sizeof( BRI_FileCounter ) );
    new_counter->filename_id = filename;
    new_counter->line = 0;
    new_counter->column = 0;
    new_counter->template = FALSE;
    new_counter->next = handle->files;
    handle->files = new_counter;

    BRI_write( handle, &file_rt, 1 );
    BRI_write( handle, &filename, sizeof(filename) );
    handle->hdr->num_file += 1;
}


/*   BRIEndFile
*/

void BRIEndFile( BRI_HANDLE handle )
/************************************/
{
    BRI_RecordType      fileend_rt = BRI_Rec_FileEnd;
    BRI_FileCounter *   old_counter;

    assert(handle->files != NULL);
    old_counter = handle->files;
    handle->files = handle->files->next;

    BRI_free( handle, old_counter );

    BRI_write( handle, &fileend_rt, BRI_SIZE_RECORDTYPE );
}


/*  BRIStartTemplate
*/

void BRIStartTemplate( BRI_HANDLE handle, BRI_StringID filename )
/***************************************************************/
{
    BRI_RecordType      template_rt = BRI_Rec_Template;
    BRI_FileCounter *   new_counter;

    new_counter = (BRI_FileCounter *)
                  BRI_malloc( handle, sizeof( BRI_FileCounter ) );
    new_counter->filename_id = filename;
    new_counter->line = 0;
    new_counter->column = 0;
    new_counter->template = TRUE;
    new_counter->next = handle->files;
    handle->files = new_counter;

    BRI_write( handle, &template_rt, 1 );
    BRI_write( handle, &filename, sizeof( filename ) );
    handle->hdr->num_template += 1;
}


/*  BRIEndTemplate
*/

void BRIEndTemplate( BRI_HANDLE handle )
/**************************************/
{
    BRI_RecordType      fileend_rt = BRI_Rec_FileEnd;
    BRI_RecordType      template_rt = BRI_Rec_TemplateEnd;
    BRI_FileCounter *   current;
    BRI_FileCounter *   prev;
    boolean             template = FALSE;

    for( current = handle->files; current != NULL && !template; ){
        template = current->template;
        prev = current;
        current = current->next;

        if( template ) {
            BRI_write( handle, &template_rt, 1 );
        } else {
            BRI_write( handle, &fileend_rt, 1 );
        }
        BRI_free( handle, prev );
    }
    handle->files = current;
}


/*  BRIStartScope
*/

BRI_ScopeID BRIStartScope( BRI_HANDLE handle, BRI_ScopeID index,
                           BRI_ScopeType flags, uint_32 owner )
/**************************************************************/
{
    BRI_RecordType      scope_rt = BRI_Rec_Scope;

    BRI_write( handle, &scope_rt, BRI_SIZE_RECORDTYPE );
    BRI_write( handle, &index, sizeof(BRI_ScopeID) );
    BRI_write( handle, &flags, BRI_SIZE_SCOPETYPE );
    BRI_write( handle, &owner, sizeof(uint_32) );

    handle->hdr->num_scope += 1;

    return index;
}


/*  BRIStartFnScope
*/

BRI_ScopeID BRIStartFnScope( BRI_HANDLE handle, BRI_ScopeID index,
                             BRI_StringID name_id, BRI_TypeID type_id )
/*********************************************************************/
{
    BRI_RecordType      scope_rt = BRI_Rec_Scope;
    BRI_ScopeType       flags = BRI_ST_Function;

    BRI_write( handle, &scope_rt,  BRI_SIZE_RECORDTYPE );
    BRI_write( handle, &index, sizeof(BRI_ScopeID) );
    BRI_write( handle, &flags, BRI_SIZE_SCOPETYPE );
    BRI_write( handle, &name_id, sizeof(BRI_StringID) );
    BRI_write( handle, &type_id, sizeof(BRI_TypeID) );

    handle->hdr->num_scope += 1;

    return index;
}


/*  BRIEndScope
*/

void BRIEndScope( BRI_HANDLE handle )
/***********************************/
{
    BRI_RecordType      scope_end_rt = BRI_Rec_ScopeEnd;

    BRI_write( handle, &scope_end_rt, BRI_SIZE_RECORDTYPE );
}


/*  BRIAddString
*/

BRI_StringID BRIAddString( BRI_HANDLE handle, BRI_StringID index,
                   const char *string )
/*****************************************************************/
{
    BRI_RecordType      string_rt = BRI_Rec_String;
    BRI_StringID        result;
    uint_32             string_length;

    if( string == NULL ){
        result = (BRI_StringID) 0;
    } else {
        string_length = strlen(string) + 1;
        result = insertStringID( handle, index, string, string_length );
        if( result == index ){
            BRI_write( handle, &string_rt,  BRI_SIZE_RECORDTYPE );
            BRI_write( handle, &index, sizeof(index) );
            BRI_write( handle, &string_length, sizeof(string_length) );
            BRI_write( handle, string, string_length * sizeof( char ) );
            handle->hdr->num_string += 1;
        }
    }

    return result;
}


/*  BRIAddDefinition
*/

void BRIAddDefinition( BRI_HANDLE handle, BRI_StringID file,
                      uint_32 line, uint_32 col,
                      BRI_SymbolID symbol )
/***********************************************************/
{
    BRI_RecordType      defn_rt  = BRI_Rec_Definition;

    BRI_write( handle, &defn_rt,  BRI_SIZE_RECORDTYPE );
    BRI_write( handle, &col, sizeof(uint_32) );
    BRI_write( handle, &line, sizeof(uint_32) );
    BRI_write( handle, &file, sizeof(BRI_StringID) );
    BRI_write( handle, &symbol, sizeof(symbol) );
    handle->hdr->num_definition += 1;
}


/*  BRIAddReference
*/

void BRIAddReference( BRI_HANDLE handle, BRI_StringID file,
                      uint_32 line, uint_32 col,
                      uint_32 target, BRI_ReferenceType ref_type )
/*******************************************************************/
{
    BRI_RecordType      delta_rt = BRI_Rec_Delta;
    BRI_RecordType      usage_rt   = BRI_Rec_Usage;

    int         line_sign;
    int         col_sign;
    uint_32     delta_line;
    uint_32     delta_col;
    int_8       col_incr;
    int_16      line_incr;

    if( file != BRI_NO_CHANGE ){
        BRI_FileCounter *       current;

        current = handle->files;
        while( current != NULL && current->filename_id != file ){
            if( current->template ){
                current = NULL;
                break;
            }
            current = current->next;
        }
        if( current != NULL ){
            while( handle->files != current ){
                BRIEndFile( handle );
            }
        } else {
            BRIStartFile( handle, file );
        }
    }

    if( line == BRI_NO_CHANGE ){
        delta_line = 0;
        line_sign = 1;
    } else {
        line_sign = (line >= handle->files->line) ? 1 : -1;
        if( line_sign == 1 ){
            delta_line = line - handle->files->line;
        } else {
            delta_line = handle->files->line - line;
        }
        handle->files->line = line;
    }
    if( col == BRI_NO_CHANGE ){
        delta_col = 0;
        col_sign = 1;
    } else {
        col_sign  = (col  >= handle->files->column) ? 1 : -1;
        if( col_sign == 1 ){
            delta_col = col - handle->files->column;
        } else {
            delta_col = handle->files->column - col;
        }
        handle->files->column = col;
    }

    while( delta_line > BRI_MAX_LINE || delta_col > BRI_MAX_COL ){
        if( delta_line > BRI_MAX_LINE ){
            line_incr = (int_16) ( BRI_MAX_LINE * line_sign );
            delta_line -= BRI_MAX_LINE;
        } else {
            line_incr = (int_16) ( delta_line * line_sign);
            delta_line = 0;
        }
        if( delta_col > BRI_MAX_COL ){
            col_incr = (int_8) ( BRI_MAX_COL * col_sign );
            delta_col -= BRI_MAX_COL;
        } else {
            col_incr = (int_8) ( delta_col * col_sign );

⌨️ 快捷键说明

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