⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wrhash.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#include <windows.h>
#include "win1632.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "wrglbl.h"
#include "wrmaini.h"
#include "wrmain.h"
#include "wrmem.h"
#include "wrstrdup.h"
#include "wrmsg.h"
#include "wredit.h"
#include "wrdel.h"
#include "wrfindt.h"
#include "wrctl3d.h"
#include "wrlist.h"
#include "wrcmsg.gh"
#include "wrhash.h"
#include "editsym.h"
#include "addsym.h"
#include "jdlg.h"

/****************************************************************************/
/* macro definitions                                                        */
/****************************************************************************/
#define FIRST_DEFAULT_ID        101

/****************************************************************************/
/* type definitions                                                         */
/****************************************************************************/
typedef struct SymInitStruct {
    char        *symbol;
    int         value;
} SymInitStruct;

typedef struct WREditSymInfo {
    FARPROC             hcb;
    WRHashTable         *table;
    BOOL                modified;
    WRHashEntryFlags    flags;
} WREditSymInfo;

typedef struct WRAddSymInfo {
    FARPROC     hcb;
    WRHashTable *table;
    char        *symbol;
    WRHashValue value;
    BOOL        modify;
} WRAddSymInfo;

/****************************************************************************/
/* external variables                                                       */
/****************************************************************************/

/****************************************************************************/
/* external function prototypes                                             */
/****************************************************************************/
BOOL WR_EXPORT WREditSymbolsProc( HWND hDlg, UINT message,
                                  WPARAM wParam, LPARAM lParam );
BOOL WR_EXPORT WRAddSymProc( HWND hDlg, UINT message,
                             WPARAM wParam, LPARAM lParam );

/****************************************************************************/
/* static function prototypes                                               */
/****************************************************************************/

/****************************************************************************/
/* static variables                                                         */
/****************************************************************************/
static SymInitStruct SymInit[] =
{
    { "IDOK"            , IDOK          }
,   { "IDCANCEL"        , IDCANCEL      }
,   { "IDABORT"         , IDABORT       }
,   { "IDRETRY"         , IDRETRY       }
,   { "IDIGNORE"        , IDIGNORE      }
,   { "IDYES"           , IDYES         }
,   { "IDNO"            , IDNO          }
,   { NULL              , -1            }
};

#define NUM_INIT_ENTRIES ( sizeof( SymInit ) / sizeof( SymInitStruct ) - 1 )

static unsigned int WRGetNameHash( char *name )
{
    unsigned int        hash;

    hash = 0;

    for( ; *name; ++name ) {
        hash = (hash << 4) + toupper( *name );
        hash = (hash ^ _rotl( hash & 0xF000, 4 )) & 0x0FFF;
    }

    return( hash % NAME_SIZE );
}

static unsigned int WRGetValueHash( WRHashValue value )
{
    return( ( (unsigned int) value ) % VALUE_SIZE );
}

static WRHashEntry *WRFindHashEntryFromName( WRHashTable *table, char *name )
{
    WRHashEntry         *entry;
    unsigned int        hash;

    if( !table || !name ) {
        return( NULL );
    }

    hash = WRGetNameHash ( name );
    for( entry = table->names[ hash ]; entry; entry = entry->name_next ) {
        if( stricmp( entry->name, name ) == 0 ) {
            return( entry );
        }
    }

    return( NULL );
}

static int WRCompareHashEntry( void const *_e1, void const *_e2 )
{
    WRHashEntry * const *e1 = _e1;
    WRHashEntry * const *e2 = _e2;

    if( e1 && e2 && *e1 && *e2 ) {
        if( (*e1)->value < (*e2)->value ) {
            return( -1 );
        } else if( (*e1)->value > (*e2)->value ) {
            return( 1 );
        }
    }

    return( 0 );
}

BOOL WR_EXPORT WRIsDefaultHashTable( WRHashTable *table )
{
    if( table ) {
        return(  table->count == NUM_INIT_ENTRIES );
    }
    return( TRUE );
}

unsigned long WR_EXPORT WRNumInHashTable( WRHashTable *table )
{
    if( table ) {
        return( table->count );
    }
    return ( 0 );
}

BOOL WR_EXPORT WRIsHashTableDirty( WRHashTable *table )
{
    return( table && ( table->flags & WR_HASH_DIRTY ) );
}

void WR_EXPORT WRMakeHashTableClean( WRHashTable *table )
{
    if( table ) {
        table->flags &= ~WR_HASH_DIRTY;
    }
}

BOOL WR_EXPORT WRDeleteDLGInclude( WResDir dir )
{
    WResTypeNode        *tnode;
    WResResNode         *rnode;
    WResLangNode        *lnode;
    WResLangType        lang;
    BOOL                ok;

    ok = ( dir != NULL );

    if( ok ) {
        tnode = WRFindTypeNode( dir, (uint_16)RT_RCDATA, NULL );
        ok = ( tnode != NULL );
    }

    if( ok ) {
        rnode = WRFindResNode( tnode, 0, "DLGINCLUDE" );
        ok = ( rnode != NULL );
    }

    if( ok ) {
        lang.lang    = DEF_LANG;
        lang.sublang = DEF_SUBLANG;
        lnode = WRFindLangNodeFromLangType( rnode, &lang );
        ok = ( lnode != NULL );
    }

    if( ok ) {
        ok = WRRemoveLangNodeFromDir( dir, &tnode, &rnode, &lnode );
    }

    return( ok );
}

BOOL WR_EXPORT WRCreateDLGInclude( WResDir *dir, char *include )
{
    WResID              *type;
    WResID              *res;
    WResLangType        lang;
    char                *str;
    int                 len;
    BOOL                ok;

    type = NULL;
    res = NULL;
    str = NULL;
    ok = ( dir && include );

    if( ok ) {
        if( *dir == NULL ) {
            *dir = WResInitDir();
            ok = ( *dir != NULL );
        }
    }

    if( ok ) {
        WRDeleteDLGInclude( *dir );
        type = WResIDFromNum( (uint_16)RT_RCDATA );
        ok = ( type != NULL );
    }

    if( ok ) {
        res = WResIDFromStr( "DLGINCLUDE" );
        ok = ( res != NULL );
    }

    if( ok ) {
        str = WRStrDup( include );
        ok = ( str != NULL );
    }

    if( ok ) {
        lang.lang    = DEF_LANG;
        lang.sublang = DEF_SUBLANG;
        len = strlen( include ) + 1;
        ok = !WResAddResource( type, res, MEMFLAG_DISCARDABLE,
                               0, len, *dir, &lang, NULL );
    }

    if( ok ) {
        ok = WRFindAndSetData( *dir, type, res, &lang, str );
    }

    if( type != NULL ) {
        WRMemFree( type );
    }

    if( res != NULL ) {
        WRMemFree( res );
    }

    if( !ok ) {
        if( str != NULL ) {
            WRMemFree( str );
        }
    }

    return( ok );
}

static void WRAddSymInitInfo( WRHashTable *table )
{
    BOOL        dup;
    int         i;
    WRHashEntry *new;

    for( i=0; SymInit[i].symbol; i++ ) {
        dup = FALSE;
        new = WRAddHashEntry( table, SymInit[i].symbol, SymInit[i].value,
                              &dup, FALSE, FALSE );
        if( new ) {
            new->flags |= WR_HASHENTRY_STANDARD;
            // standard symbols are always in use????
            new->flags &= ~WR_HASHENTRY_UNUSED;
        }
    }

    WRMakeHashTableClean( table );
}

WRHashTable * WR_EXPORT WRInitHashTable( void )
{
    WRHashTable         *table;

    table = (WRHashTable *)WRMemAlloc( sizeof(WRHashTable) );
    if( table == NULL ) {
        return( NULL );
    }

    memset( table, 0, sizeof(WRHashTable) );

    table->next_default_value = FIRST_DEFAULT_ID;

    WRAddSymInitInfo( table );

    return( table );
}

void WR_EXPORT WRFreeHashTable( WRHashTable *table )
{
    unsigned int        nhash;
    WRHashEntry         *entry;

    for( nhash = 0; nhash < NAME_SIZE; nhash++ ) {
        for( ; ; ) {
            entry = table->names[nhash];
            if( entry == NULL ) {
                break;
            }
            table->names[nhash] = entry->name_next;
            WRMemFree( entry );
        }
    }

    WRMemFree( table );
}

BOOL WR_EXPORT WRMergeHashTable( WRHashTable **dest, WRHashTable *src )
{
    WRHashEntry         *entry;
    WRHashEntry         *new;
    unsigned int        nhash;
    BOOL                ok, dup;

    if( dest ) {
        if( !*dest ) {
            *dest = WRInitHashTable();
        }
    } else {
        return( FALSE );
    }

    if( !src ) {
        return( TRUE );
    }

    ok = TRUE;

    for( nhash = 0; nhash < NAME_SIZE; nhash++ ) {
        for( entry=src->names[nhash] ;entry; entry=entry->name_next ) {
            dup = FALSE;
            new = WRAddHashEntry( *dest, entry->name, entry->value,
                                  &dup, FALSE, FALSE );
            ok = ( new != NULL );
        }
    }

    (*dest)->flags = src->flags;
    (*dest)->next_default_value = src->next_default_value;

    return( ok );
}

BOOL WR_EXPORT WRCopyHashTable( WRHashTable **dest, WRHashTable *src )
{
    WRHashEntry         *entry;
    WRHashEntry         *new;
    unsigned int        nhash;
    BOOL                ok, dup;

    if( dest ) {
        if( *dest ) {
            WRFreeHashTable( *dest );
            *dest = NULL;
        }
    } else {
        return( FALSE );
    }

    if( !src ) {
        return( TRUE );
    }

    *dest = WRInitHashTable();

    if( !*dest ) {
        return( FALSE );
    }

    ok = TRUE;

    for( nhash = 0; nhash < NAME_SIZE; nhash++ ) {
        for( entry=src->names[nhash] ;entry; entry=entry->name_next ) {
            if( !( entry->flags & WR_HASHENTRY_STANDARD ) ) {
                dup = FALSE;
                new = WRAddHashEntry( *dest, entry->name, entry->value,
                                      &dup, FALSE, FALSE );
                if ( !new || dup ) {
                    ok = FALSE;
                }
            }
        }
    }

    (*dest)->flags = src->flags;
    (*dest)->next_default_value = src->next_default_value;

    return( ok );
}

static unsigned WRAddSymbolsToLorCBox( WRHashTable *table, HWND hDlg,
                                       int id, WRHashEntryFlags flags,
                                       UINT add_string_msg,
                                       UINT add_data_msg,
                                       UINT reset_msg,
                                       LRESULT err, LRESULT errspace )
{
    HWND                box;
    LRESULT             ret;
    unsigned int        nhash;
    WRHashEntry         *entry;
    unsigned            count;

    if( table == NULL ) {
        return( 0 );
    }

    count = 0;
    box = GetDlgItem( hDlg, id );
    SendMessage( box, WM_SETREDRAW, FALSE, 0 );
    SendMessage( box, reset_msg, 0, 0 );

    for( nhash = 0; nhash < NAME_SIZE; nhash++ ) {
        for( entry=table->names[nhash] ;entry; entry=entry->name_next ) {
            if( entry->flags & flags ) {
                ret = SendMessage( box, add_string_msg, 0,
                                   (LPARAM)(LPCSTR)entry->name );
                if( ( ret == err ) || ( ret == errspace ) ) {
                    break;
                }
                SendMessage( box, add_data_msg, (WPARAM)ret, (LPARAM)entry );
                count++;
            }
        }
    }

    SendMessage( box, WM_SETREDRAW, TRUE, 0 );
    InvalidateRect( box, NULL, TRUE );

    return( count );
}

unsigned WR_EXPORT WRAddSymbolsToListBox( WRHashTable *table, HWND hDlg,
                                          int id, WRHashEntryFlags flags )
{
    return( WRAddSymbolsToLorCBox( table, hDlg, id, flags,
                                   LB_ADDSTRING, LB_SETITEMDATA,
                                   LB_RESETCONTENT,
                                   LB_ERR, LB_ERRSPACE ) );
}

unsigned WR_EXPORT WRAddSymbolsToComboBox( WRHashTable *table, HWND hDlg,
                                           int id, WRHashEntryFlags flags )
{
    return( WRAddSymbolsToLorCBox( table, hDlg, id, flags,
                                   CB_ADDSTRING, CB_SETITEMDATA,
                                   CB_RESETCONTENT,
                                   CB_ERR, CB_ERRSPACE ) );
}

BOOL WR_EXPORT WRWriteSymbolsToFile( WRHashTable *table, char *filename )
{
    WRHashEntry         *entry;
    WRHashEntry         **tbl;
    FILE                *handle;
    unsigned int        nhash;
    unsigned int        i;
    unsigned int        count;
    int                 max_len;
    int                 len;

    if( !table || !table->count || !filename ) {
        return( FALSE );
    }

    handle = fopen( filename, "wt" );

    if( !handle ) {
        WRPrintErrorMsg( WR_FILEOPENFAILED, filename, strerror( errno ) );
        return( FALSE );
    }

    tbl = (WRHashEntry **) WRMemAlloc( sizeof(WRHashEntry *)*table->count );
    if( !tbl ) {
        return( FALSE );
    }

    count = 0;
    max_len = 0;
    for( nhash = 0; nhash < NAME_SIZE; nhash++ ) {
        for( entry=table->names[nhash] ;entry; entry=entry->name_next ) {
            if( !( entry->flags & WR_HASHENTRY_STANDARD ) ) {
                tbl[count] = entry;
                len = strlen( entry->name );
                max_len = max( len, max_len );
                count++;
            }
        }
    }

    qsort( tbl, count, sizeof(WRHashEntry *), WRCompareHashEntry );

    for( i = 0; i < count; i++ ) {
        fprintf( handle, "#define %-*s %ld\n", max_len + 2,
                 tbl[i]->name, tbl[i]->value );
    }

    if( fclose( handle ) != 0 ) {
        WRPrintErrorMsg( WR_FILECLOSEFAILED, filename, strerror( errno ) );

⌨️ 快捷键说明

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