Welcome, Guest! Login | Register

Implementation of a string lookup table [Print this Article]
Posted by: deathz0rz
Date posted: Nov 13 2005
User Rating: N/A
Number of views: 2666
Number of comments: 0
Description: A string lookup table is a set of functions that is used to convert strings to pre-defined values
I created this code for a mod I was working on and I thought I'd share it with the community.

 QUOTE  
The contents of this file are designed for use with the Valve Source SDK, however, it's function can be adepted to work with other code, such as STL. Supplied are convenience macro's designed to convert from constant names to constant values. I think an example explains their usages best.


sourcecontrol.h
 CODE  
//(...)
#include "stringlookup.h"
//(...)

#define FSTATUS_UNCHANGED   0
#define FSTATUS_ADDED       0x01
#define FSTATUS_REMOVED     0x02
#define FSTATUS_CHANGED     (FSTATUS_ADDED|FSTATUS_REMOVED)
#define FSTATUS_DIRECTORY   0x04
#define FSTATUS_HIDDEN      0x08
#define FSTATUS_SYSTEM      0x10
#define FSTATUS_READONLY    0x20
#define FSTATUS_ARCHIVE     0x40

DECLARE_STRING_LOOKUP_CONSTANTS(unsinged int,FSTATUS_LOOKUP)

//You don't have to do this, but it saves typing
#define FSTATUS(key,var) STRING_LOOKUP(FSTATUS_LOOKUP,key,var)

//(...)


sourcecontrol.cpp
 CODE  
//(...)
#include "sourcecontrol.h"
//(...)

DEFINE_STRING_LOOKUP_CONSTANTS(FSTATUS_LOOKUP)
    ADD_LOOKUP(FSTATUS_UNCHANGED)
    ADD_LOOKUP(FSTATUS_ADDED)
    ADD_LOOKUP(FSTATUS_REMOVED)
    ADD_LOOKUP(FSTATUS_CHANGED)
    ADD_LOOKUP(FSTATUS_DIRECTORY)
    ADD_LOOKUP(FSTATUS_HIDDEN)
    ADD_LOOKUP(FSTATUS_SYSTEM)
    ADD_LOOKUP(FSTATUS_READONLY)
    ADD_LOOKUP(FSTATUS_ARCHIVE)
END_STRING_LOOKUP_CONSTANTS()

//(...)
    KeyValues *pKV;
//(...)
    unsinged int uiStatus;
    if (!FSTATUS(pKV->GetString("status"),uiStatus))
        Warning("Invalid status constant %s!\n",pKV->GetString("status"));
//(...)

//Of course, this is quite inconvenient, because the declared constants are
//obviously meant to be used as bit flags. For that, a different macro is
//supplied.

//(...)
    KeyValues *pKV;
//(...)
    unsinged int uiStatus=0;
    CUtlVector<unsinged int>* auiFlags;
    auiFlags=CONVERT_STRING(FSTATUS_LOOKUP,pKV->GetString("status"));
    for (int i=0;i<auiFlags->Count();i++)
        uiStatus|=(*auiFlags)[i];
//(...)

//NOTE: A character that is not a letter, a capital letter, a number or an
//underscore is considered a separator.


P.S.: Sorry for the zip, it's just one 8KB header file, but I cant upload .h's.

Rate This Article
This article has not yet been rated.

You have to register to rate this article.
Related Files
Zip FileFilename: stringlookup.zip
File Size: 2.7 KB

User Comments

No User Comments

You must register to post a comment. If you have already registered, you must login.

Latest Articles
3rd person View in Multiplayer
Half-Life 2 | Coding | Client Side Tutorials
How to enable it in HL2DM

By: cct | Nov 13 2006

Making a Camera
Half-Life 2 | Level Design
This camera is good for when you join a map, it gives you a view of the map before you join a team

By: slackiller | Mar 04 2006

Making a camera , Part 2
Half-Life 2 | Level Design
these cameras are working monitors that turn on when a button is pushed.

By: slackiller | Mar 04 2006

Storing weapons on ladder
Half-Life 2 | Coding | Snippets
like Raven Sheild or BF2

By: British_Bomber | Dec 24 2005

Implementation of a string lookup table
Half-Life 2 | Coding | Snippets
A string lookup table is a set of functions that is used to convert strings to pre-defined values

By: deathz0rz | Nov 13 2005


Latest Comments
Where do we go from here
General | News
By: MIFUNE | Jun 09 2008
 
The Input/Output system
Half-Life 2 | Level Design
By: nazitaco | Dec 23 2007
 
Where do we go from here
General | News
By: Rob_F | Nov 22 2007
 
Rescaling Half-Life
Half-Life | Coding | Shared Tutorials
By: christoph | Nov 12 2007
 
GameUI
Half-Life 2 | Coding | Client Side Tutorials
By: Evil_j | Oct 29 2007
 
3 State Zoom For Any Weapon
Half-Life 2 | Coding | Server Side Tutorials
By: Ennuified | Oct 18 2007
 
Storing weapons on ladder
Half-Life 2 | Coding | Snippets
By: cct | Sep 07 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: DarkNight | Aug 28 2007
 
CTF Gameplay Part 1
Half-Life | Coding | Shared Tutorials
By: deedok | Aug 20 2007
 
Debugging your half-life 2 mod
Half-Life 2 | Coding | Articles
By: blackshark | Aug 17 2007
 

Site Info
296 Approved Articless
5 Pending Articles
3940 Registered Members
0 People Online (22 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!