STDMETHODIMP CLogFactory::Close(BSTR AppName, BSTR LogName) { if (NULL == LogName) return E_FAIL; for (INT i = 0; i < MAX_SERVER_COUNT; i++) { LOGSERVER &LogServer = m_sLogServer[i]; if (LogServer.strLogName == LogName && 0 < LogServer.iClient) { if (0 == --LogServer.iClient) { LogServer.oLogServer.Close(); LogServer.strLogName.Empty(); } CString strAppName(AppName); LogServer.oClientName.Delete(strAppName); LONG Count = UnlockServer(); LOGGER_WRITE(_T("进程(") << strAppName.GetBuffer() << _T(")已关闭.")); LOGGER_WRITE2(_T("删除客户端, 服务计数:") << Count); break; } } return S_OK; }
// Spidy - auto start option void getExeNameStr(std::string * strResult) { CString CStrcaExePath; CStrcaExePath.Preallocate(MAX_PATH); GetModuleFileName(NULL, CStrcaExePath.GetBuffer(), MAX_PATH); CT2CA pszConvertedAnsiString (CStrcaExePath); std::string strAppName(pszConvertedAnsiString); strResult->assign(strAppName.substr(strAppName.rfind("\\") + 1)); CStrcaExePath.ReleaseBuffer(); }
int Host::ShellEx(const OOBase::CmdArgs::results_t& args) { OOBase::LocalString strAppName(args.get_allocator()); if (!args.find("@0",strAppName)) LOG_ERROR_RETURN(("No arguments passed with --shellex"),EXIT_FAILURE); OOBase::TempPtr<wchar_t> wszAppName(args.get_allocator()); int err = OOBase::Win32::utf8_to_wchar_t(strAppName.c_str(),wszAppName); if (err) LOG_ERROR_RETURN(("Failed to convert string: %s",OOBase::system_error_text(err)),EXIT_FAILURE); OOBase::LocalString strCmdLine(args.get_allocator()); for (size_t i = 1;; ++i) { OOBase::LocalString strId(args.get_allocator()); int err = strId.printf("@%u",i); if (err) LOG_ERROR_RETURN(("Failed to format string: %s",OOBase::system_error_text(err)),EXIT_FAILURE); OOBase::LocalString strArg(args.get_allocator()); if (!args.find(strId,strArg)) break; err = strCmdLine.append(strArg.c_str()); if (!err && i != 0) err = strCmdLine.append(" "); if (err) LOG_ERROR_RETURN(("Failed to append string: %s",OOBase::system_error_text(err)),EXIT_FAILURE); } OOBase::TempPtr<wchar_t> wszCmdLine(args.get_allocator()); if (!strCmdLine.empty()) { err = OOBase::Win32::utf8_to_wchar_t(strCmdLine.c_str(),wszCmdLine); if (err) LOG_ERROR_RETURN(("Failed to convert string: %s",OOBase::system_error_text(err)),EXIT_FAILURE); } HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (FAILED(hr)) LOG_ERROR_RETURN(("CoInitializeEx failed: %s",OOBase::system_error_text()),EXIT_FAILURE); SHELLEXECUTEINFOW sei = {0}; sei.cbSize = sizeof(sei); sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NOASYNC | SEE_MASK_FLAG_NO_UI | SEE_MASK_NO_CONSOLE; sei.lpFile = wszAppName; sei.lpParameters = wszCmdLine; sei.nShow = SW_SHOWDEFAULT; if (!ShellExecuteExW(&sei)) LOG_ERROR_RETURN(("ShellExecuteExW failed: %s",OOBase::system_error_text()),EXIT_FAILURE); if (sei.hProcess) { WaitForSingleObject(sei.hProcess,INFINITE); CloseHandle(sei.hProcess); } CoUninitialize(); return EXIT_SUCCESS; }
/** * @brief used for static application data (e.g. the help) */ QString strDocumentationDir() { return QString("/usr/share/doc/%1/").arg(strAppName()); }
/** * @brief used for temporary files like rss news-feed */ QString strCacheDir() { return QString(".cache/%1/").arg(strAppName()); }
/** * @brief the app must not be run as root */ QString strErrorDoNotRunAsRoot() { return QString("critical: ") + strAppName() + " was not designed to be run as root."; }
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; }