//----------------------------------------------------------------------------- // Munge the specified path. static void MungePath(char* dest, S32 destSize, const char* src, const char* absolutePrefix) { char tempBuf[MaxPath]; dStrncpy(dest, src, MaxPath); // translate all \ to / ForwardSlash(dest); // if it is relative, make it absolute with the absolutePrefix if (dest[0] != '/') { AssertFatal(absolutePrefix, "Absolute Prefix must not be NULL"); dSprintf(tempBuf, MaxPath, "%s/%s", absolutePrefix, dest); // copy the result back into dest dStrncpy(dest, tempBuf, destSize); } // if the path exists, we're done struct stat filestat; if (stat(dest, &filestat) != -1) return; // otherwise munge the case of the path MungeCase(dest, destSize); }
ASMShaderParameter* ASMShader::getNamedParameter(StringTableEntry name) { for(U32 i = 0; i < mParameters.size(); i++) { if(dStricmp(mParameters[i]->mName, name) == 0) { return mParameters[i]; } } //No parameter... const char* paramString; ASMShaderParameter* param = NULL; paramString = dStrstr(mVertexSourceString, name); if(paramString) { param = new ASMShaderParameter; const char* openBracket = dStrstr(paramString, "["); const char* closeBracket = dStrstr(paramString, "]"); char* num = (char *)dMalloc((closeBracket - openBracket + 1) * sizeof(U8)); num = dStrncpy(num, openBracket + 1, (closeBracket - (openBracket + 1))); num[(closeBracket - (openBracket + 1))] = NULL; param->mName = StringTable->insert(name); param->mVertexId = dAtoi(num); const char* env = dStrstr(paramString, "program.env"); param->mVertexIsEnv = (env != NULL && env < openBracket); param->mFragmentId = -1; param->mFragmentIsEnv = false; mParameters.push_back(param); dFree(num); } paramString = dStrstr(mPixelSourceString, name); if(paramString) { if(!param) { param = new ASMShaderParameter; mParameters.push_back(param); param->mVertexId = -1; param->mVertexIsEnv = false; param->mName = StringTable->insert(name); } const char* openBracket = dStrstr(paramString, "["); const char* closeBracket = dStrstr(paramString, "]"); char* num = (char *)dMalloc((closeBracket - openBracket + 1) * sizeof(U8)); num = dStrncpy(num, openBracket + 1, (closeBracket - (openBracket + 1))); num[(closeBracket - (openBracket + 1))] = NULL; param->mFragmentId = dAtoi(num); const char* env = dStrstr(paramString, "program.env"); param->mFragmentIsEnv = (env != NULL && env < openBracket); dFree(num); } return param; }
bool GuiInspectorField::hasSameValueInAllObjects() { char value1[ 2048 ]; // Get field value from first object. const char* data1 = getData( 0 ); if( data1 ) { dStrncpy( value1, data1, sizeof( value1 ) ); value1[ sizeof( value1 ) - 1 ] = 0; } else value1[ 0 ] = 0; // Check if all other objects have the same value. const U32 numObjects = mInspector->getNumInspectObjects(); for( U32 i = 1; i < numObjects; ++ i ) { const char* value2 = getData( i ); if( !value2 ) value2 = ""; if( dStrcmp( value1, value2 ) != 0 ) return false; } return true; }
//----------------------------------------------------------------------------- bool Platform::createPath(const char *file) { char pathbuf[MaxPath]; const char *dir; pathbuf[0] = 0; U32 pathLen = 0; // all paths should be created in home directory char prefPathName[MaxPath]; MungePath(prefPathName, MaxPath, file, GetPrefDir()); file = prefPathName; // does the directory exist already? if (DirExists(prefPathName, true)) // true means that the path is a filepath return true; while((dir = dStrchr(file, '/')) != NULL) { dStrncpy(pathbuf + pathLen, file, dir - file); pathbuf[pathLen + dir-file] = 0; bool ret = mkdir(pathbuf, 0700); pathLen += dir - file; pathbuf[pathLen++] = '/'; file = dir + 1; } return true; }
void initDisplayDeviceInfo() { Con::printf( "Reading Display Device information..." ); U8 i = 0; DISPLAY_DEVICEA ddData; ddData.cb = sizeof( DISPLAY_DEVICEA ); // Search for the primary display adapter, because that is what the rendering // context will get created on. while( EnumDisplayDevicesA( NULL, i, &ddData, 0 ) != 0 ) { // If we find the primary display adapter, break out if( ddData.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE ) break; i++; } Con::printf( " Primary Display Device Found:" ); // Ok, now we have the primary display device. Parse the device information. char ven[9]; char dev[9]; ven[8] = dev[8] = '\0'; // It may seem a bit silly here to cast, but there are two implimentations in Platform.h // This usage is the "const" version... char *pos = dStrstr( ddData.DeviceID, (const char *)"VEN_"); dStrncpy( ven, ( pos ) ? pos : "VEN_0000", 8 ); Con::printf( " Vendor Id: %s", ven ); pos = dStrstr( ddData.DeviceID, (const char *)"DEV_" ); dStrncpy( dev, ( pos ) ? pos : "DEV_0000", 8 ); Con::printf( " Device Id: %s", dev ); // We now have the information, set them to console variables so we can parse // the file etc in script using getField and so on. Con::setVariable( "$PCI_VEN", ven ); Con::setVariable( "$PCI_DEV", dev ); }
//-------------------------------------- StringTableEntry _StringTable::insertn(const char* src, S32 len, const bool caseSens) { char val[256]; AssertFatal(len < 255, "Invalid string to insertn"); dStrncpy(val, src, len); val[len] = 0; return insert(val, caseSens); }
InputEventManager::VirtualMapData* InputEventManager::findVirtualMap(const char* description) { char desc[256]; desc[0] = 0; dStrncpy(desc, description, 255); dStrlwr(desc); return mVirtualMap.retreive(desc); }
StringTableEntry getModNameFromPath(const char *path) { if(path == NULL || *path == 0) return NULL; char buf[1024]; buf[0] = 0; if(path[0] == '/' || path[1] == ':') { // It's an absolute path const StringTableEntry exePath = Platform::getMainDotCsDir(); U32 len = dStrlen(exePath); if(dStrnicmp(exePath, path, len) == 0) { const char *ptr = path + len + 1; const char *slash = dStrchr(ptr, '/'); if(slash) { dStrncpy(buf, ptr, slash - ptr); buf[slash - ptr] = 0; } else return NULL; } else return NULL; } else { const char *slash = dStrchr(path, '/'); if(slash) { dStrncpy(buf, path, slash - path); buf[slash - path] = 0; } else return NULL; } return StringTable->insert(buf); }
S32 PASCAL WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, S32) { #if 0 // Run a unit test. StandardMainLoop::initCore(); UnitTesting::TestRun tr; tr.test("Platform", true); #else Vector<char *> argv( __FILE__, __LINE__ ); char moduleName[256]; #ifdef TORQUE_UNICODE { TCHAR buf[ 256 ]; GetModuleFileNameW( NULL, buf, sizeof( buf ) ); convertUTF16toUTF8( buf, moduleName, sizeof( moduleName ) ); } #else GetModuleFileNameA(NULL, moduleName, sizeof(moduleName)); #endif argv.push_back(moduleName); for (const char* word,*ptr = lpszCmdLine; *ptr; ) { // Eat white space for (; dIsspace(*ptr) && *ptr; ptr++) ; // Pick out the next word for (word = ptr; !dIsspace(*ptr) && *ptr; ptr++) ; // Add the word to the argument list. if (*word) { S32 len = ptr - word; char *arg = (char *) dMalloc(len + 1); dStrncpy(arg, word, len); arg[len] = 0; argv.push_back(arg); } } winState.appInstance = hInstance; S32 retVal = run(argv.size(), (const char **) argv.address()); for(U32 j = 1; j < argv.size(); j++) dFree(argv[j]); return retVal; #endif }
//----------------------------------------------------------------------------- // Returns true if the pathname exists, false otherwise. If isFile is true, // the pathname is assumed to be a file path, and only the directory part // will be examined (everything before last /) bool DirExists(char* pathname, bool isFile) { static char testpath[20000]; dStrncpy(testpath, pathname, sizeof(testpath)); if (isFile) { // find the last / and make it into null char* lastSlash = dStrrchr(testpath, '/'); if (lastSlash != NULL) *lastSlash = 0; } return Platform::isDirectory(testpath); }
void SFXProfile::packData(BitStream* stream) { Parent::packData( stream ); char buffer[256]; if ( mFilename.isEmpty() ) buffer[0] = 0; else dStrncpy( buffer, mFilename.c_str(), 256 ); stream->writeString( buffer ); stream->writeFlag( mPreload ); }
void setExePathName(const char* exePathName) { if (exePathName == NULL) sBinPathName[0] = '\0'; else dStrncpy(sBinPathName, exePathName, sizeof(sBinPathName)); // set the base exe name field char *binName = dStrrchr(sBinPathName, '/'); if( !binName ) binName = sBinPathName; else *binName++ = '\0'; sBinName = binName; }
U32 getOrCreateHashNoCase() const { if( mHashNoCase == U32_MAX) { PROFILE_SCOPE(StringData_getOrCreateHashNoCase); UTF8 *lower = new UTF8[ mLength + 1 ]; dStrncpy( lower, utf8(), mLength ); lower[ mLength ] = 0; dStrlwr( lower ); mHashNoCase = Torque::hash( (const U8*)lower, mLength, 0 ); delete [] lower; } return mHashNoCase; }
void InputEventManager::addVirtualMap(const char* description, InputEventType type, InputObjectInstances code) { // Make sure the description is lower case char desc[256]; desc[0] = 0; dStrncpy(desc, description, 255); dStrlwr(desc); VirtualMapData* data = new VirtualMapData(); data->type = type; data->code = code; data->desc = StringTable->insert(desc); mVirtualMap.insert(data, desc); mActionCodeMap.insertUnique(data->code, *data); }
void AtlasResourceConfigTOC::addConfig(const char *name, AtlasConfigChunk *acc) { // Find the first unused stub and associate the chunk with that. for(S32 i=0; i<mStubCount; i++) { // Unused == null name. if(mStubs[i].mName[0] == 0) { dStrncpy((char*)mStubs[i].mName, name, AtlasResourceConfigStub::MaxConfigChunkNameLength); instateNewChunk(mStubs+i, acc, true); return; } } Con::errorf("AtlasResourceConfigTOC::addConfig - could not add config due to full TOC!"); }
void DebugDrawer::drawText(const Point3F& pos, const String& text, const ColorF &color) { if(isFrozen || !isDrawing) return; DebugPrim *n = mPrimChunker.alloc(); n->useZ = false; n->dieTime = 0; n->a = pos; n->color = color; dStrncpy(n->mText, text.c_str(), 256); n->type = DebugPrim::Text; n->next = mHead; mHead = n; }
//----------------------------------------------------------------------------- // FileName //----------------------------------------------------------------------------- bool default_scan(const String &data, FileName & result) { char buffer[1024]; if(data.c_str()[0] == '$') { dStrncpy(buffer, data.c_str(), sizeof(buffer) - 1); buffer[sizeof(buffer)-1] = 0; } else if (!Con::expandScriptFilename(buffer, sizeof(buffer), data)) { Con::warnf("(TypeFilename) illegal filename detected: %s", data.c_str()); return false; } result = String(buffer); return true; }
//------------------------------------------------------------------------------ void DisplayErrorAlert(const char* errMsg, bool showSDLError) { char fullErrMsg[2048]; dStrncpy(fullErrMsg, errMsg, sizeof(fullErrMsg)); if (showSDLError) { char* sdlerror = SDL_GetError(); if (sdlerror != NULL && dStrlen(sdlerror) > 0) { dStrcat(fullErrMsg, " (Error: "); dStrcat(fullErrMsg, sdlerror); dStrcat(fullErrMsg, ")"); } } Platform::AlertOK("Error", fullErrMsg); }
StringTableEntry Platform::getExecutablePath() { if( !sBinPathName[0] ) { const char *cpath; if( (cpath = torque_getexecutablepath()) ) { dStrncpy(sBinPathName, cpath, sizeof(sBinPathName)); chdir(sBinPathName); } else { getcwd(sBinPathName, sizeof(sBinPathName)-1); } } return StringTable->insert(sBinPathName); }
ZipArchive::ZipEntry *ZipArchive::findZipEntry(const char *filename) { char path[1024]; dStrncpy(path, filename, sizeof(path)); path[sizeof(path) - 1] = 0; for(S32 i = 0;i < dStrlen(path);++i) { if(path[i] == '\\') path[i] = '/'; } ZipEntry *root = mRoot; char *ptr = path, *slash = NULL; do { slash = dStrchr(ptr, '/'); if(slash) { // Find the directory *slash = 0; // check child dict for new root ZipEntry *newRoot = NULL; if (!root->mChildren.tryGetValue(ptr, newRoot)) return NULL; root = newRoot; ptr = slash + 1; } else { // Find the file ZipEntry* entry = NULL; if (root->mChildren.tryGetValue(ptr, entry)) return entry; } } while(slash); return NULL; }
const char* setUnit(const char *string, U32 index, const char *replace, const char *set) { U32 sz; const char *start = string; AssertFatal( dStrlen(string) + dStrlen(replace) + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" ); char *ret = &_returnBuffer[0]; ret[0] = '\0'; U32 padCount = 0; while(index--) { sz = dStrcspn(string, set); if(string[sz] == 0) { string += sz; padCount = index + 1; break; } else string += (sz + 1); } // copy first chunk sz = string-start; dStrncpy(ret, start, sz); for(U32 i = 0; i < padCount; i++) ret[sz++] = set[0]; // replace this unit ret[sz] = '\0'; dStrcat(ret, replace); // copy remaining chunks sz = dStrcspn(string, set); // skip chunk we're replacing if(!sz && !string[sz]) return ret; string += sz; dStrcat(ret, string); return ret; }
const char *getUnits(const char *string, S32 startIndex, S32 endIndex, const char *set) { if( startIndex > endIndex ) return ""; S32 sz; S32 index = startIndex; while(index--) { if(!*string) return ""; sz = dStrcspn(string, set); if (string[sz] == 0) return ""; string += (sz + 1); } const char *startString = string; for( U32 i = startIndex; i <= endIndex && *string; i ++ ) { sz = dStrcspn(string, set); string += sz; if( i < endIndex ) string ++; } S32 totalSize = ( S32 )( string - startString ); AssertWarn( totalSize + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" ); totalSize = getMin( totalSize, S32( sizeof( _returnBuffer ) - 1 ) ); if( totalSize > 0 ) { char *ret = &_returnBuffer[0]; dStrncpy( ret, startString, totalSize ); ret[ totalSize ] = '\0'; return ret; } return ""; }
// Function to return the 'index' column from 'string' given delimeters in 'set' static const char *getColumn(const char *string, char* returnbuff, U32 index, const char *set) { U32 sz; while(index--) { if(!*string) return ""; sz = dStrcspn(string, set); if (string[sz] == 0) return ""; string += (sz + 1); } sz = dStrcspn(string, set); if (sz == 0) return ""; char *ret = returnbuff; dStrncpy(ret, string, sz); ret[sz] = '\0'; return ret; }
void InputEventManager::buildVirtualMap() { char desc[256]; VirtualMapData* data; for (U32 j = 0; gVirtualMap[j].code != 0xFFFFFFFF; j++) { // Make sure the description is lower case desc[0] = 0; dStrncpy(desc, gVirtualMap[j].pDescription, 255); dStrlwr(desc); data = new VirtualMapData(); data->type = gVirtualMap[j].type; data->code = gVirtualMap[j].code; data->desc = StringTable->insert(desc); mVirtualMap.insert(data, desc); mActionCodeMap.insertUnique(data->code, *data); } }
void NetAsync::queueLookup(const char* remoteAddr, NetSocket socket) { // do we have it already? unsigned int i = 0; for (i = 0; i < mLookupRequests.size(); ++i) { if (mLookupRequests[i].sock == socket) // found it. ignore more than one lookup at a time for a socket. return; } // not found, so add it mLookupRequests.increment(); NameLookupRequest& lookupRequest = mLookupRequests.last(); lookupRequest.sock = socket; dStrncpy(lookupRequest.remoteAddr, remoteAddr, sizeof(lookupRequest.remoteAddr)); ThreadSafeRef< NameLookupWorkItem > workItem( new NameLookupWorkItem( lookupRequest ) ); ThreadPool::GLOBAL().queueWorkItem( workItem ); }
const char* removeUnit(const char *string, U32 index, const char *set) { U32 sz; const char *start = string; AssertFatal( dStrlen(string) + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" ); char *ret = &_returnBuffer[0]; ret[0] = '\0'; while(index--) { sz = dStrcspn(string, set); // if there was no unit out there... return the original string if(string[sz] == 0) return start; else string += (sz + 1); } // copy first chunk sz = string-start; dStrncpy(ret, start, sz); ret[sz] = 0; // copy remaining chunks sz = dStrcspn(string, set); // skip chunk we're removing if(string[sz] == 0) { // if that was the last... if(string != start) { ret[string - start - 1] = 0; // then kill any trailing delimiter } return ret; // and bail } string += sz + 1; // skip the extra field delimiter dStrcat(ret, string); return ret; }
const char *getUnit(const char *string, U32 index, const char *set, char* buffer, U32 bufferSize) { if( !buffer ) { buffer = _returnBuffer; bufferSize = sizeof( _returnBuffer ); } AssertFatal( bufferSize, "StringUnit::getUnit - bufferSize cannot be zero!" ); if( !bufferSize ) return ""; buffer[0] = 0; U32 sz; while(index--) { if(!*string) return buffer; sz = dStrcspn(string, set); if (string[sz] == 0) return buffer; string += (sz + 1); } sz = dStrcspn(string, set); if (sz == 0) return buffer; AssertWarn( sz + 1 < bufferSize, "Size of returned string too large for return buffer" ); sz = getMin( sz, bufferSize - 1 ); dStrncpy(buffer, string, sz); buffer[sz] = '\0'; return buffer; }
S32 torque_winmain( HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, S32) { Vector<char *> argv( __FILE__, __LINE__ ); char moduleName[256]; #ifdef TORQUE_UNICODE { TCHAR buf[ 256 ]; GetModuleFileNameW( NULL, buf, sizeof( buf ) ); convertUTF16toUTF8( buf, moduleName, sizeof( moduleName ) ); } #else GetModuleFileNameA(NULL, moduleName, sizeof(moduleName)); #endif argv.push_back(moduleName); for (const char* word,*ptr = lpszCmdLine; *ptr; ) { // Eat white space for (; dIsspace(*ptr) && *ptr; ptr++) ; // Test for quotes bool withinQuotes = dIsquote(*ptr); if (!withinQuotes) { // Pick out the next word for (word = ptr; !dIsspace(*ptr) && *ptr; ptr++) ; } else { // Advance past the first quote. We don't want to include it. ptr++; // Pick out the next quote for (word = ptr; !dIsquote(*ptr) && *ptr; ptr++) ; } // Add the word to the argument list. if (*word) { S32 len = ptr - word; char *arg = (char *) dMalloc(len + 1); dStrncpy(arg, word, len); arg[len] = 0; argv.push_back(arg); } // If we had a quote, skip past it for the next arg if (withinQuotes && *ptr) { ptr++; } } winState.appInstance = hInstance; S32 retVal = TorqueMain(argv.size(), (const char **) argv.address()); for(U32 j = 1; j < argv.size(); j++) dFree(argv[j]); return retVal; }
void ZipArchive::insertEntry(ZipEntry *ze) { char path[1024]; dStrncpy(path, ze->mCD.mFilename.c_str(), sizeof(path)); path[sizeof(path) - 1] = 0; for(S32 i = 0;i < dStrlen(path);++i) { if(path[i] == '\\') path[i] = '/'; } ZipEntry *root = mRoot; char *ptr = path, *slash = NULL; do { slash = dStrchr(ptr, '/'); if(slash) { // Add the directory *slash = 0; // try to get root, create if not found ZipEntry *newEntry = NULL; if (!root->mChildren.tryGetValue(ptr, newEntry)) { newEntry = new ZipEntry; newEntry->mParent = root; newEntry->mName = String(ptr); newEntry->mIsDirectory = true; newEntry->mCD.setFilename(path); root->mChildren[ptr] = newEntry; } root = newEntry; *slash = '/'; ptr = slash + 1; } else { // Add the file. if(*ptr) { ze->mIsDirectory = false; ze->mName = ptr; ze->mParent = root; root->mChildren[ptr] = ze; mEntries.push_back(ze); } else { // [tom, 2/6/2007] If ptr is empty, this was a directory entry. Since // we created a new entry for it above, we need to delete the old // pointer otherwise it will leak as it won't have got inserted. delete ze; } } } while(slash); }
//-------------------------------------------------------------------------- // Set name //-------------------------------------------------------------------------- void LangElement::setName(const char* newName ) { dStrncpy( ( char* ) name, newName, sizeof( name ) ); name[ sizeof( name ) - 1 ] = '\0'; }