String Lookup Table
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
This article has no links to other VDC articles. Please help improve this article by adding links that are relevant to the context within the existing text.
January 2024
January 2024
This article is an orphan, meaning that few or no articles link to it.
You can help by adding links to this article from other relevant articles.
January 2024
You can help by adding links to this article from other relevant articles.
January 2024
I created this code for a mod I was working on and I thought I'd share it with the community. --deathz0rz 11:31, 13 Nov 2005 (PST)
/* Copyright (c) 2005 Jethro "deathz0rz" Beekman. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by Jethro Beekman. 4. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ================================================================================ The contents of this file are designed for use with the Valve Source SDK, however, it's function can be adapted 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(unsinged int,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. ================================================================================ */ #include <UtlStringMap.h> #include <UtlVector.h> #ifndef STRING_LOOKUP_H #define STRING_LOOKUP_H template<typename T> class CStringLookupConstantTable { protected: typedef struct m_stringconstant_s { const char* pszKey; T tValue; } m_stringconstant_t; CUtlStringMap<T*> m_StringMap; public: inline bool FindKey(const char *pszKey, T *ptValue) { if (m_StringMap.Defined(pszKey)) { if (ptValue) *ptValue=*m_StringMap[pszKey]; return true; } else { return false; } }; CUtlVector<T>* FromString(const char* pszStr) { static CUtlVector<T> LocalArray; static char szNoSep[]="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890_"; char* pszCur=NULL; char* pszIt; char szTmp[256]; T tTmp; LocalArray.RemoveAll(); for(pszIt=(char*)pszStr;*pszIt;pszIt++) { if (pszCur) { if (!strchr(szNoSep,*pszIt)) { Q_strncpy(szTmp,pszCur,min((int)(pszIt-pszCur)+1,256)); if (FindKey(szTmp,&tTmp)) LocalArray.AddToTail(tTmp); pszCur=NULL; } } else { if (strchr(szNoSep,*pszIt)) { pszCur=pszIt; } } } if (pszCur) { Q_strncpy(szTmp,pszCur,min((int)(pszIt-pszCur)+1,256)); if (FindKey(szTmp,&tTmp)) LocalArray.AddToTail(tTmp); } return &LocalArray; } }; //SLTP means String Lookup Table Private #define DECLARE_STRING_LOOKUP_CONSTANTS(type,identifier) \ namespace SLTP__##identifier \ { \ class CStringLookupConstantTable__##identifier : public CStringLookupConstantTable<type> \ { \ public: \ inline CUtlVector<type>* FromString(const char* pszStr) { return CStringLookupConstantTable<type>::FromString(pszStr); }; \ CStringLookupConstantTable__##identifier(); \ ~CStringLookupConstantTable__##identifier() {}; \ }; \ } \ \ extern SLTP__##identifier::CStringLookupConstantTable__##identifier g_StringLookupConstantTable__##identifier; #define STRING_LOOKUP(identifier,key,var) \ g_StringLookupConstantTable__##identifier.FindKey(key,&var) #define STRING_EXISTS(identifier,key) \ g_StringLookupConstantTable__##identifier.FindKey(key,NULL) #define CONVERT_STRING(identifier,str) \ g_StringLookupConstantTable__##identifier.FromString(str); #define DEFINE_STRING_LOOKUP_CONSTANTS(type,identifier) \ SLTP__##identifier::CStringLookupConstantTable__##identifier g_StringLookupConstantTable__##identifier; \ namespace SLTP__##identifier \ { \ CStringLookupConstantTable__##identifier::CStringLookupConstantTable__##identifier() \ { \ static m_stringconstant_t aConstants[] = { #define ADD_LOOKUP(macro) \ #macro, macro, \ #define END_STRING_LOOKUP_CONSTANTS() \ }; \ for (int i=0;i<sizeof(aConstants)/sizeof(m_stringconstant_t);i++) \ { \ m_StringMap[aConstants[i].pszKey]=&aConstants[i].tValue; \ } \ }; \ }; #endif //!STRING_LOOKUP_H