Posted by: deathz0rz
Date posted: Nov 13 2005 User Rating: N/A | Number of views: 12605 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.
| | 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
| | //(...) #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
| | //(...) #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. |
|
User Comments
No User Comments
You must register to post a comment. If you have already registered, you must login.
|
297 Approved Articless
8 Pending Articles
3940 Registered Members
0 People Online (9 guests)
|
|