123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "console/consoleDictionary.h"
- #include "console/consoleNamespace.h"
- #include "platform/platform.h"
- #include "console/console.h"
- #include "console/ast.h"
- #include "collection/findIterator.h"
- #include "io/resource/resourceManager.h"
- #include "string/findMatch.h"
- #include "io/fileStream.h"
- #include "console/compiler.h"
- static char scratchBuffer[1024];
- #define ST_INIT_SIZE 15
- struct StringValue
- {
- S32 size;
- char *val;
- operator char *() { return val; }
- StringValue &operator=(const char *string);
- StringValue() { size = 0; val = NULL; }
- ~StringValue() { dFree(val); }
- };
- StringValue & StringValue::operator=(const char *string)
- {
- if(!val)
- {
- val = dStrdup(string);
- size = dStrlen(val);
- }
- else
- {
- S32 len = dStrlen(string);
- if(len < size)
- dStrcpy(val, string);
- else
- {
- size = len;
- dFree(val);
- val = dStrdup(string);
- }
- }
- return *this;
- }
- static S32 QSORT_CALLBACK varCompare(const void* a,const void* b)
- {
- return dStricmp( (*((Dictionary::Entry **) a))->name, (*((Dictionary::Entry **) b))->name );
- }
- void Dictionary::exportVariables(const char *varString, const char *fileName, bool append)
- {
- const char *searchStr = varString;
- Vector<Entry *> sortList(__FILE__, __LINE__);
- for(S32 i = 0; i < hashTable->size;i ++)
- {
- Entry *walk = hashTable->data[i];
- while(walk)
- {
- if(FindMatch::isMatch((char *) searchStr, (char *) walk->name))
- sortList.push_back(walk);
- walk = walk->nextEntry;
- }
- }
- if(!sortList.size())
- return;
- dQsort((void *)sortList.address(), sortList.size(), sizeof(Entry *), varCompare);
- Vector<Entry *>::iterator s;
- char expandBuffer[1024];
- FileStream strm;
- if(fileName)
- {
- if(!ResourceManager->openFileForWrite(strm, fileName, append ? FileStream::ReadWrite : FileStream::Write))
- {
- Con::errorf(ConsoleLogEntry::General, "Unable to open file '%s for writing.", fileName);
- return;
- }
- if(append)
- strm.setPosition(strm.getStreamSize());
- }
- char buffer[1024];
- const char *cat = fileName ? "\r\n" : "";
- for(s = sortList.begin(); s != sortList.end(); s++)
- {
- switch((*s)->type)
- {
- case Entry::TypeInternalInt:
- dSprintf(buffer, sizeof(buffer), "%s = %d;%s", (*s)->name, (*s)->ival, cat);
- break;
- case Entry::TypeInternalFloat:
- dSprintf(buffer, sizeof(buffer), "%s = %g;%s", (*s)->name, (*s)->fval, cat);
- break;
- default:
- expandEscape(expandBuffer, (*s)->getStringValue());
- dSprintf(buffer, sizeof(buffer), "%s = \"%s\";%s", (*s)->name, expandBuffer, cat);
- break;
- }
- if(fileName)
- strm.write(dStrlen(buffer), buffer);
- else
- Con::printf("%s", buffer);
- }
- if(fileName)
- strm.close();
- }
- void Dictionary::deleteVariables(const char *varString)
- {
- const char *searchStr = varString;
- for(S32 i = 0; i < hashTable->size; i++)
- {
- Entry *walk = hashTable->data[i];
- while(walk)
- {
- Entry *matchedEntry = (FindMatch::isMatch((char *) searchStr, (char *) walk->name)) ? walk : NULL;
- walk = walk->nextEntry;
- if (matchedEntry)
- remove(matchedEntry); // assumes remove() is a stable remove (will not reorder entries on remove)
- }
- }
- }
- U32 HashPointer(StringTableEntry ptr)
- {
- return (U32)(((dsize_t)ptr) >> 2);
- }
- Dictionary::Entry *Dictionary::lookup(StringTableEntry name)
- {
- Entry *walk = hashTable->data[HashPointer(name) % hashTable->size];
- while(walk)
- {
- if(walk->name == name)
- return walk;
- else
- walk = walk->nextEntry;
- }
- return NULL;
- }
- Dictionary::Entry *Dictionary::add(StringTableEntry name)
- {
- Entry *walk = hashTable->data[HashPointer(name) % hashTable->size];
- while(walk)
- {
- if(walk->name == name)
- return walk;
- else
- walk = walk->nextEntry;
- }
- Entry *ret;
- hashTable->count++;
- if(hashTable->count > hashTable->size * 2)
- {
- Entry head(NULL), *walk;
- S32 i;
- walk = &head;
- walk->nextEntry = 0;
- for(i = 0; i < hashTable->size; i++) {
- while(walk->nextEntry) {
- walk = walk->nextEntry;
- }
- walk->nextEntry = hashTable->data[i];
- }
- delete[] hashTable->data;
- hashTable->size = hashTable->size * 4 - 1;
- hashTable->data = new Entry *[hashTable->size];
- for(i = 0; i < hashTable->size; i++)
- hashTable->data[i] = NULL;
- walk = head.nextEntry;
- while(walk)
- {
- Entry *temp = walk->nextEntry;
- S32 idx = HashPointer(walk->name) % hashTable->size;
- walk->nextEntry = hashTable->data[idx];
- hashTable->data[idx] = walk;
- walk = temp;
- }
- }
- ret = new Entry(name);
- S32 idx = HashPointer(name) % hashTable->size;
- ret->nextEntry = hashTable->data[idx];
- hashTable->data[idx] = ret;
- return ret;
- }
- // deleteVariables() assumes remove() is a stable remove (will not reorder entries on remove)
- void Dictionary::remove(Dictionary::Entry *ent)
- {
- Entry **walk = &hashTable->data[HashPointer(ent->name) % hashTable->size];
- while(*walk != ent)
- walk = &((*walk)->nextEntry);
- *walk = (ent->nextEntry);
- delete ent;
- hashTable->count--;
- }
- Dictionary::Dictionary()
- : hashTable( NULL ),
- exprState( NULL ),
- scopeName( NULL ),
- scopeNamespace( NULL ),
- code( NULL ),
- ip( 0 )
- {
- }
- Dictionary::Dictionary(ExprEvalState *state, Dictionary* ref)
- : hashTable( NULL ),
- exprState( NULL ),
- scopeName( NULL ),
- scopeNamespace( NULL ),
- code( NULL ),
- ip( 0 )
- {
- setState(state,ref);
- }
- void Dictionary::setState(ExprEvalState *state, Dictionary* ref)
- {
- exprState = state;
- if (ref)
- hashTable = ref->hashTable;
- else
- {
- hashTable = new HashTableData;
- hashTable->owner = this;
- hashTable->count = 0;
- hashTable->size = ST_INIT_SIZE;
- hashTable->data = new Entry *[hashTable->size];
-
- for(S32 i = 0; i < hashTable->size; i++)
- hashTable->data[i] = NULL;
- }
- }
- Dictionary::~Dictionary()
- {
- if ( hashTable->owner == this )
- {
- reset();
- delete [] hashTable->data;
- delete hashTable;
- }
- }
- void Dictionary::reset()
- {
- S32 i;
- Entry *walk, *temp;
- for(i = 0; i < hashTable->size; i++)
- {
- walk = hashTable->data[i];
- while(walk)
- {
- temp = walk->nextEntry;
- delete walk;
- walk = temp;
- }
- hashTable->data[i] = NULL;
- }
- hashTable->size = ST_INIT_SIZE;
- hashTable->count = 0;
- }
- const char *Dictionary::tabComplete(const char *prevText, S32 baseLen, bool fForward)
- {
- S32 i;
- const char *bestMatch = NULL;
- for(i = 0; i < hashTable->size; i++)
- {
- Entry *walk = hashTable->data[i];
- while(walk)
- {
- if(Namespace::canTabComplete(prevText, bestMatch, walk->name, baseLen, fForward))
- bestMatch = walk->name;
- walk = walk->nextEntry;
- }
- }
- return bestMatch;
- }
- char *typeValueEmpty = (char*)"";
- Dictionary::Entry::Entry(StringTableEntry in_name)
- {
- dataPtr = NULL;
- name = in_name;
- type = -1;
- ival = 0;
- fval = 0;
- sval = typeValueEmpty;
- }
- Dictionary::Entry::~Entry()
- {
- if(sval != typeValueEmpty)
- dFree(sval);
- }
- const char *Dictionary::getVariable(StringTableEntry name, bool *entValid)
- {
- Entry *ent = lookup(name);
- if(ent)
- {
- if(entValid)
- *entValid = true;
- return ent->getStringValue();
- }
- if(entValid)
- *entValid = false;
- // Warn users when they access a variable that isn't defined.
- if(gWarnUndefinedScriptVariables)
- Con::warnf(" *** Accessed undefined variable '%s'", name);
- return "";
- }
- void Dictionary::Entry::setStringValue(const char * value)
- {
- if(type <= TypeInternalString)
- {
- // Let's not remove empty-string-valued global vars from the dict.
- // If we remove them, then they won't be exported, and sometimes
- // it could be necessary to export such a global. There are very
- // few empty-string global vars so there's no performance-related
- // need to remove them from the dict.
- /*
- if(!value[0] && name[0] == '$')
- {
- gEvalState.globalVars.remove(this);
- return;
- }
- */
- U32 stringLen = dStrlen(value);
- // If it's longer than 256 bytes, it's certainly not a number.
- //
- // (This decision may come back to haunt you. Shame on you if it
- // does.)
- if(stringLen < 256)
- {
- fval = dAtof(value);
- ival = dAtoi(value);
- }
- else
- {
- fval = 0.f;
- ival = 0;
- }
- type = TypeInternalString;
- // may as well pad to the next cache line
- U32 newLen = ((stringLen + 1) + 15) & ~15;
-
- if(sval == typeValueEmpty)
- sval = (char *) dMalloc(newLen);
- else if(newLen > bufferLen)
- sval = (char *) dRealloc(sval, newLen);
- bufferLen = newLen;
- dStrcpy(sval, value);
- }
- else
- Con::setData(type, dataPtr, 0, 1, &value);
- }
- void Dictionary::setVariable(StringTableEntry name, const char *value)
- {
- Entry *ent = add(name);
- if(!value)
- value = "";
- ent->setStringValue(value);
- }
- void Dictionary::addVariable(const char *name, S32 type, void *dataPtr)
- {
- if(name[0] != '$')
- {
- scratchBuffer[0] = '$';
- dStrcpy(scratchBuffer + 1, name);
- name = scratchBuffer;
- }
- Entry *ent = add(StringTable->insert(name));
- ent->type = type;
- if(ent->sval != typeValueEmpty)
- {
- dFree(ent->sval);
- ent->sval = typeValueEmpty;
- }
- ent->dataPtr = dataPtr;
- }
- bool Dictionary::removeVariable(StringTableEntry name)
- {
- if( Entry *ent = lookup(name) ){
- remove( ent );
- return true;
- }
- return false;
- }
|