void SpawnMonitor::loadSpawnPoints() { QString fileName; fileName = QString(LOGDIR "/") + m_zoneName + ".sp"; if (!findFile(fileName)) { printf("Can't find spawn point file %s\n", (const char*)fileName); return; } QFile spFile(fileName); if (!spFile.open(IO_ReadOnly)) { printf( "Can't open spawn point file %s\n", (const char*)fileName ); return; } QTextStream input( &spFile ); int16_t x, y, z; unsigned long diffTime; uint32_t count; QString name; while (!input.atEnd()) { input >> x; input >> y; input >> z; input >> diffTime; input >> count; name = input.readLine(); name = name.stripWhiteSpace(); EQPoint loc(x, y, z); SpawnPoint* p = new SpawnPoint( 0, loc, name, diffTime, count ); if (p) { QString key = p->key(); if (!m_points.find(key)) { m_points.insert(key, p); emit newSpawnPoint(p); } else { printf("Warning: spawn point key already in use!\n"); delete p; } } } printf("Loaded spawn points: %s\n", (const char*)fileName); m_modified = false; }
/** * Create new data file. */ Database::IDataFile* CDatabaseImpl::OpenDataFile(const TCHAR* pFileName) { std::auto_ptr<CDataFile> spFile(new CDataFile()); try { spFile->Open(pFileName); } RAISE() CDataFile* pFile = spFile.release(); m_files.push_back(pFile); return pFile; }
void CTestResults::SaveResults() { TRY_CATCH /// Get file name to store results tstring fileName = SETTINGS_INSTANCE.GetResultsFileName(); /// Open file HANDLE hFile = CreateFile( fileName.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(INVALID_HANDLE_VALUE == hFile) throw MCException_Win(_T("Can not create file with results")); /// Create shared pointer to file's handle boost::shared_ptr< boost::remove_pointer<HANDLE>::type > spFile(hFile,CloseHandle); /// Write results DWORD write = 0; for(int i = 0; i < m_current; ++i) { SPResultEntry entry = m_entries[i]; tstring resultStr = Format(_T("%d;%3.16f\n"), entry->m_time, entry->m_fps); DWORD len = static_cast<DWORD>(resultStr.length()) * sizeof(TCHAR); WriteFile(hFile, resultStr.c_str(), len, &write, NULL); if(len != write) throw MCException_Win(_T("Error at saving test results")); } CATCH_THROW() }
void SpawnMonitor::loadSpawnPoints() { QString fileName; fileName = m_zoneName + ".sp"; QFileInfo fileInfo = m_dataLocMgr->findExistingFile("spawnpoints", fileName, false); if (!fileInfo.exists()) { seqWarn("Can't find spawn point file %s", (const char*)fileInfo.absFilePath()); return; } fileName = fileInfo.absFilePath(); QFile spFile(fileName); if (!spFile.open(IO_ReadOnly)) { seqWarn( "Can't open spawn point file %s", (const char*)fileName ); return; } QTextStream input( &spFile ); int16_t x, y, z; unsigned long diffTime; uint32_t count; QString name; while (!input.atEnd()) { input >> x; input >> y; input >> z; input >> diffTime; input >> count; name = input.readLine(); name = name.stripWhiteSpace(); EQPoint loc(x, y, z); SpawnPoint* p = new SpawnPoint( 0, loc, name, diffTime, count ); if (p) { QString key = p->key(); if (!m_points.find(key)) { m_points.insert(key, p); emit newSpawnPoint(p); } else { seqWarn("Warning: spawn point key already in use!"); delete p; } } } seqInfo("Loaded spawn points: %s", (const char*)fileName); m_modified = false; }
void SpawnMonitor::saveSpawnPoints() { // only save if modified if (!m_modified) return; if ( !m_zoneName.length() ) { seqWarn("Zone name not set in 'SpawnMonitor::saveSpawnPoints'!" ); return; } QString fileName; fileName = m_zoneName + ".sp"; QFileInfo fileInfo = m_dataLocMgr->findWriteFile("spawnpoints", fileName, false); fileName = fileInfo.absFilePath(); QString newName = fileName + ".new"; QFile spFile( newName ); if (!spFile.open(IO_WriteOnly)) { seqWarn("Failed to open %s for writing", (const char*)newName); return; } QTextStream output(&spFile); QAsciiDictIterator<SpawnPoint> it( m_points ); SpawnPoint* sp; while ((sp = it.current())) { ++it; output << sp->x() << " " << sp->y() << " " << sp->z() << " " << (unsigned long)sp->diffTime() << " " << sp->count() << " " << sp->name() << '\n'; } spFile.close(); QFileInfo fi( spFile ); QFile old( fileName ); QDir dir( fi.dir() ); QString backupName = fileName + ".bak"; if (old.exists()) { if (dir.rename( fileName, backupName)) { if (!dir.rename( newName, fileName)) seqWarn( "Failed to rename %s to %s", (const char*)newName, (const char*)fileName); } } else { if (!dir.rename(newName, fileName)) seqWarn("Failed to rename %s to %s", (const char*)newName, (const char*)fileName); } m_modified = false; seqInfo("Saved spawn points: %s", (const char*)fileName); }
shared_ptr<CMabiFile> CMabiFileSystem::CreateFile() {; shared_ptr<CMabiFile> spFile(new CMabiFile()); spFile->m_fileSystem = shared_from_this(); return spFile; }
bool CPackResourceSet::Open( LPCTSTR lpszPackFile ) { shared_ptr<CWin32File> spFile(new CWin32File(lpszPackFile)); if (!spFile->IsOK()) { // 打开文件错误 return false; } UINT tmp; PACKAGE_HEADER header; tmp = spFile->Read(&header, sizeof(PACKAGE_HEADER)); if ( tmp != sizeof(PACKAGE_HEADER)) { // 读取文件错误 return false; } // 检查文件头 if ( memcmp(header.signature, "PACK", 4) != 0) { // 文件头错误 return false; } PACKAGE_LIST_HEADER listHeader; tmp = spFile->Read(&listHeader, sizeof(PACKAGE_LIST_HEADER)); if (tmp != sizeof(PACKAGE_LIST_HEADER)) { // 读取文件错误 return false; } shared_ptr< vector<char> > spListInfoData(new vector<char>(listHeader.list_header_size)); tmp = spFile->Read( &(*spListInfoData->begin()), listHeader.list_header_size ); if (tmp != listHeader.list_header_size) { // 读取文件错误 return false; } char * pTemp = &(*spListInfoData->begin()); for (size_t i = 0;i < listHeader.sum; i++) { ITEM_NAME * pItemName = (ITEM_NAME *)pTemp; unsigned long ulSize; if (pItemName->len_or_type < 4) { // 第一字节小于4 ulSize = (0x10 * (pItemName->len_or_type + 1)); } else if (pItemName->len_or_type == 4) { // 可恶的恶魔猫,这里怎么要搞特殊的 ulSize = 0x60 ; } else { // 基本参考mabiunpack代码 ulSize = pItemName->len + 5; } tstring name; if ( pItemName->len_or_type <= 0x04 ) { name = CA2T(pItemName->sz_ansi_name); } else // 0x05 { name = CA2T(pItemName->sz_ansi_name2); } // 指针跨越名称定义区 pTemp += ulSize; ITEM_INFO * pInfo = (ITEM_INFO*)pTemp; // 累计文件偏移,其实这里修改了内存 pInfo->offset += sizeof(PACKAGE_HEADER) + sizeof(PACKAGE_LIST_HEADER) + listHeader.list_header_size; // 指针定位到下一项 pTemp += sizeof(ITEM_INFO); shared_ptr<IResource> spResource(new CPackResource(name, spFile, pInfo)); m_Resources.push_back( spResource ); } // 对于容器进行排序,以便可以进行二分查找 sort(m_Resources.begin(), m_Resources.end(), ResourceGreater()); return true; }
void parseCommandArguments( int argc, // in char *argv[], //in Parameters *params, //in/out char *paramFile, //out char *additionalParams, //out char *Error, // aux char *prefix //out ) { additionalParams[0]=0; if(argc<2) { puts("Usage: ./calculateFreeEnergy prefix [parameters.txt] [Additional parameters string]"); puts("For proper work of the program one shoud have the following files:"); puts("\t- ${prefix}gamma${site}.3d"); puts("\t- ${prefix}c${site}.3d"); puts("\t- ${prefix}clong${site}.3d"); puts("\t- ${prefix}X.grd"); puts("\t- ${prefix}Y.grd"); puts("\t- ${prefix}Z.grd"); puts("If no parameters file is given, the file ${prefix}_parameters.txt is used"); throw new Exception(NULL,"incorrect command line arguments"); return; } strcpy(prefix,argv[1]); if(argc>2) strcpy(paramFile,argv[2]); else sprintf(paramFile,"%sparameters.txt",prefix); if(argc>3) { strcpy(additionalParams,argv[3]); } Integer L=sizeOfFile(paramFile); FILE *f=fopen(paramFile,"r"); if(L==0 || f==NULL) { sprintf(Error,"Cannot read parameters form file %s",paramFile); throw new Exception(NULL,Error); } char *str = (char*)malloc(L+1); fread(str,1,L,f); fclose(f); StringParser spFile(str); ParseParameters parseParameters("Bohr", //const char *distanceUnits, "Hartree", //const char * energyUnits, params //Parameters *parameters ); parseParameters.parse(&spFile); free(str); StringParser spArg(additionalParams); parseParameters.parse(&spArg); }
int main(int argc, const char *ppArgv[]) { std::shared_ptr<FILE> spfOutput(stdout, fclose_checked); const char *pszInputFile = nullptr; bool bLaserMode = false; unsigned int uiLinesOut = 0; // // Parse command-line arguments: // -o outputfile // -i inputfile // -l or -laser = laser-mode std::string strAppName(ppArgv[0]); #ifdef _WIN32 std::string::size_type szSlash = strAppName.rfind('\\'); #else std::string::size_type szSlash = strAppName.rfind('/'); #endif if (szSlash != strAppName.npos) { strAppName.erase(0, szSlash); } // // Turn on laser mode if the executable name contains the word laser, e.g. gcodeoptlaser std::transform(strAppName.begin(), strAppName.end(), strAppName.begin(), ::tolower); if (strAppName.npos != strAppName.find("laser")) { bLaserMode = true; } for (int iArg = 1; iArg < argc; ++iArg) { const std::string strOption(ppArgv[iArg]); if (strOption == "-o" && iArg < (argc - 1) && spfOutput.get() == stdout) { spfOutput.reset(fopen(ppArgv[iArg + 1], "wt"), fclose); if (nullptr == spfOutput) { fprintf(stderr, "Error %i opening %hs\n", errno, ppArgv[iArg + 1]); return 0; } ++iArg; } else if (strOption == "-laser" || strOption == "-l") { bLaserMode = true; } else if (strOption == "-i" && pszInputFile == nullptr && iArg < (argc - 1)) { if (false == FileExists(ppArgv[iArg + 1])) { return 0; } pszInputFile = ppArgv[iArg + 1]; ++iArg; } else if (pszInputFile == nullptr && FileExists(strOption)) { pszInputFile = ppArgv[iArg]; } else { fprintf(stderr, "Unknown command-line argument: %hs\n", strOption.c_str()); return 0; } } if (pszInputFile == nullptr) { fprintf(stderr, "USAGE: %hs inputfile [-o outputfile][-laser|-l][-x|-y|-xy]\n", ppArgv[0]); return 0; } std::shared_ptr<FILE> spFile(fopen(ppArgv[1], "rt"), fclose); if (spFile.get() == nullptr) { fprintf(stderr, "Error %i opening %hs\n", errno, ppArgv[1]); return 1; } unsigned int uiLine = 0; std::deque<GCodeSet> vCurrentSets; bool bInPrologue = true; bool bInEpilogue = false; GCodeSet gcCurrent(0.0f, 0.0f, 0.0f); std::string strPreviousLine; double dCurrentZ = 0.0f; double dCurrentX = 0.0f; double dCurrentY = 0.0f; double dLastZ = 0.0f; double dLastX = 0.0f; double dLastY = 0.0f; unsigned int uiSets = 0; bool bCutterEngaged = false; // // Add an attribution fprintf(spfOutput.get(), "(Optimized by %hs)\n(Written by Andrew L. Sandoval)\n", strAppName.c_str()); // Read file and break into sections... while (!feof(spFile.get())) { std::array<char, 2048> szLine = { }; if (nullptr == fgets(&szLine[0], szLine.size(), spFile.get())) { continue; } ++uiLine; std::string strLine(&szLine[0]); std::string strLineCodes(strLine); std::string::size_type szLE = strLineCodes.find_first_of("\r\n"); if (szLE != strLineCodes.npos) { strLineCodes.erase(szLE); } PreviousLine prevLine(strPreviousLine, strLine); std::string::size_type szX = strLine.find(" X"); std::string::size_type szY = strLine.find(" Y"); std::string::size_type szZ = strLine.find(" Z"); if (strLine.length() > 1 && strLine[0] == 'G' && strLine[1] == '0') { bCutterEngaged = false; } else if (strLine.length() > 2 && strLine[0] == 'G' && strLine[1] == '1' && false == isdigit(strLine[2])) // Don't clobber G17 { bCutterEngaged = true; } else if (bInPrologue) { if (strLineCodes == "M3" && bLaserMode) { fprintf(spfOutput.get(), "(M3 - removed in laser mode)\n"); } else { fprintf(spfOutput.get(), strLine.c_str()); } ++uiLinesOut; continue; // Stay in prologue until a G0/G1 } // // From here on, past prologue // // Check for start of epilogue (before we much with M5's) if (strLine.find("M5") == 0 || strLine.find("M30") == 0) { // sort and flush last set, in epilogue -- sort and output vCurrentSets vCurrentSets.push_back(gcCurrent); OptimizeAndOutputSets(vCurrentSets, uiLinesOut, spfOutput.get()); gcCurrent.Reset(); vCurrentSets.clear(); // Only supporting one tool currently, so first spindle stop means prologue... bInEpilogue = true; bInPrologue = false; if (dCurrentZ < 0.0f && bLaserMode == false) { fprintf(stderr, "WARNING!!!! Program Ends with cutter down, Z = %.02f\n", dCurrentZ); } } if (bInEpilogue) { fprintf(spfOutput.get(), strLine.c_str()); ++uiLinesOut; continue; } // // G0 w/Z or G1 w/Z // If Laser Mode, change any G0 Z to an M5 (laser off) // If Laser Mode, change and G1 Z to M3 (laser on) if (strLine[0] == 'G' && szZ != strLine.npos) { if (bCutterEngaged) { dLastZ = dCurrentZ; dCurrentZ = atof(strLine.c_str() + szZ + 2); gcCurrent.SetZStart(dCurrentZ); if (dLastZ != dCurrentZ) { // sort and flush last set, new depth -- sort and output vCurrentSets // This line will end up in gcCurrent so it can be ignored OptimizeAndOutputSets(vCurrentSets, uiLinesOut, spfOutput.get()); vCurrentSets.clear(); } } if (bLaserMode) { // G0 Z (replace with M3 - shut off laser) // G1 Z (replace with M5) if (bCutterEngaged == false) { // Moving with laser disengaged strLine = "M5 (laser off - was "; strLine += strLineCodes; strLine += ")\n"; } else { strLine = "M3 (laser on - was "; strLine += strLineCodes; strLine += ")\n"; } if (bInPrologue) // Only place this should happen... { fprintf(spfOutput.get(), strLine.c_str()); } } } // // G0 w/X or G1 w/X if (strLine[0] == 'G' && // Cutter engaged or not, move to X szX != strLine.npos) { // Update current position for tracking end position of set dLastX = dCurrentX; dCurrentX = atof(strLine.c_str() + szX + 2); } // // G0 w/Y or G1 w/Y if (strLine[0] == 'G' && // Cutter engaged or not, move to Y szY != strLine.npos) { // Update current position for tracking end position of set dLastY = dCurrentY; dCurrentY = atof(strLine.c_str() + szY + 2); } // // G0 X - start of a set if (strLine.find("G0 X") == 0) // Rapid Linear Motion, cutter not engaged { bInPrologue = false; bInEpilogue = false; // NOTE: Though x and y will be the end of the G0 rapid linear motion, it is the start of a set // where cutting begins! // Error if the depth is not positive!!! // Start of a new set... if (szX == strLine.npos || szY == strLine.npos) { fprintf(stderr, "Critical Error: expected a G0 line to contain X and Y axis settings on line #%u: %hs\n", uiLine, strLine.c_str()); return 0; } // // Update the last set's end point: gcCurrent.SetZEnd(dLastZ); gcCurrent.SetXEnd(dLastX); gcCurrent.SetYEnd(dLastY); if (gcCurrent.GetLines().empty() == false) { vCurrentSets.push_back(gcCurrent); } // A new set is started with a G0 X // When a G1 Z to a new depth is encountered all previous sets can be sorted and output // Same for epilogue start // // Record new set's start point and update references gcCurrent.Reset(dCurrentX, dCurrentY, dCurrentZ); // Current Z may change on next line ++uiSets; } // // Part of a set... if (bInPrologue == true) { continue; } gcCurrent.LineVector().push_back(strLine); } fprintf(stderr, "Output Lines: %lu\n", uiLinesOut); fprintf(stderr, "Input Lines: %lu\n", uiLine); return 0; }