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: 10095
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 05 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
knock knock
General | News
By: omega | Dec 22 2016
 
knock knock
General | News
By: MIFUNE | Oct 10 2015
 
New HL HUD Message System
Half-Life | Coding | Shared Tutorials
By: chbrules | Dec 31 2011
 
knock knock
General | News
By: Whistler | Nov 05 2011
 
Particle Engine tutorial part 4
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 18 2010
 
Particle Engine tutorial part 2
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Particle Engine tutorial part 3
Half-Life | Coding | Client Side Tutorials
By: darkPhoenix | Feb 11 2010
 
Game Movement Series #2: Analog Jumping and Floating
Half-Life 2 | Coding | Shared Tutorials
By: mars3554 | Oct 26 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Deadpool | Aug 02 2009
 
Particle Engine tutorial part 5
Half-Life | Coding | Client Side Tutorials
By: Persuter | Aug 02 2009
 

Site Info
297 Approved Articless
8 Pending Articles
3940 Registered Members
0 People Online (4 guests)
About - Credits - Contact Us

Wavelength version: 3.0.0.9
Valid XHTML 1.0! Valid CSS!