📄 ecutils.cpp
字号:
//####COPYRIGHTBEGIN####// // ----------------------------------------------------------------------------// Copyright (C) 1998, 1999, 2000 Red Hat, Inc.// Copyright (C) 2003 John Dallaway//// This program is part of the eCos host tools.//// This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free // Software Foundation; either version 2 of the License, or (at your option) // any later version.// // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for // more details.// // You should have received a copy of the GNU General Public License along with// this program; if not, write to the Free Software Foundation, Inc., // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// ----------------------------------------------------------------------------// //####COPYRIGHTEND####////===========================================================================//===========================================================================//#####DESCRIPTIONBEGIN####//// Author(s): sdf, jld// Contact(s): sdf// Date: 1998/08/11// Version: 0.01// Purpose: // Description: This is a collection of utility functions.// Modified by julians for wxWindows (originally CTUtils.h)// Requires: // Provides: // See also: // Known bugs: // Usage: ////####DESCRIPTIONEND####////===========================================================================#ifdef __GNUG__#pragma implementation "ecutils.h"#endif// Includes other headers for precompiled compilation#include "ecpch.h"#ifdef __BORLANDC__#pragma hdrstop#endif#include "ecutils.h"#include "wx/listctrl.h"#include "wx/stream.h"#include <float.h> // for DBL_DIG macro#include <sys/types.h>#include <sys/stat.h>#ifndef __WXMSW__#include <errno.h>#endif#ifdef __WXMSW__#include <tlhelp32.h>#endif#ifdef __CYGWIN__#include <sys/cygwin.h> /* for cygwin_conv_to_*_path() */#endif#if 0#define INCLUDEFILE <string>#include "IncludeSTL.h"#endif// Chop str into pieces, using cSep as separator.// " and \ have usual semantics// Return value is array of pieces found.int ecUtils::Chop(const wxString& psz, wxArrayString &ar, wxChar cSep, bool bObserveStrings/*=false*/,bool bBackslashQuotes/*=false*/){ if(wxT(' ') == cSep) { return Chop(psz, ar, wxT("\t\n\v\f\r "), bObserveStrings, bBackslashQuotes); } else { wxASSERT(wxT('\0')!=cSep); wxChar c[2]={cSep,wxT('\0')}; return Chop(psz,ar,wxString(c),bObserveStrings,bBackslashQuotes); }}int ecUtils::Chop(const wxString& str, wxArrayString &ar, const wxString& sep, bool bObserveStrings/*=false*/,bool bBackslashQuotes/*=false*/){ ar.Clear(); const wxChar* pszSep = (const wxChar*) sep; const wxChar* psz = (const wxChar*) str; int i=0; for(;;){ // Skip multiple separators while(*psz && wxStrchr(pszSep,*psz)){ psz++; } if(!*psz){ return i; } wxString strTok; if(bObserveStrings){ bool bInString=FALSE; do{ if(*psz == wxT('\\') && bBackslashQuotes && psz[1]){ strTok += psz[1]; psz++; } else if(*psz == wxT('"')){ bInString ^= 1; } else if (!bInString && *psz && NULL != wxStrchr(pszSep,*psz)) { break; } else { strTok+=*psz; } } while (*++psz); } else { const wxChar* pszStart=psz; do { psz++; } while (*psz && ! wxStrchr(pszSep,*psz)); strTok=wxString(pszStart,psz-pszStart); } ar.Add(strTok); i++; } return ar.GetCount();}#if 0// vararg-style message box formatterint ecUtils::MessageBoxF (LPCTSTR pszFormat, ...){ int rc; va_list args; va_start(args, pszFormat); rc=ecUtils::vMessageBox(MB_OK, pszFormat,args); va_end(args); return rc;}// As above, but with type as first parameter.int ecUtils::MessageBoxFT (UINT nType, LPCTSTR pszFormat, ...){ int rc; va_list args; va_start(args, pszFormat); rc=ecUtils::vMessageBox(nType, pszFormat,args); va_end(args); return rc;}int ecUtils::vMessageBox(UINT nType, LPCTSTR pszFormat, va_list marker){ int rc=0; for(int nLength=100;nLength;) { TCHAR *buf=new TCHAR[1+nLength]; int n=_vsntprintf(buf, nLength, pszFormat, marker ); if(-1==n){ nLength*=2; // NT behavior } else if (n<nLength){ rc=AfxMessageBox(buf,nType); nLength=0; // trigger exit from loop } else { nLength=n+1; // UNIX behavior generally, or NT behavior when buffer size exactly matches required length } delete [] buf; } return rc;}#endifbool ecUtils::StrToItemIntegerType(const wxString & str, long &d){ wxChar* pEnd; bool rc; errno=0; bool bHex=(str.Len() > 2 && str[0]==wxT('0') && (str[1]==wxT('x')||str[1]==wxT('X'))); //d=_tcstol(str,&pEnd,bHex?16:10); d=wxStrtol(str,&pEnd,bHex?16:10); rc=(0==errno && (*pEnd==wxT('\0'))); return rc;}const wxString ecUtils::IntToStr(long d,bool bHex){ wxString s; s.Printf(bHex?wxT("0x%08x"):wxT("%d"),d); return s;}const wxString ecUtils::DoubleToStr (double dValue){ wxString s; s.Printf(wxT("%.*e"), DBL_DIG, dValue); return s;}bool ecUtils::StrToDouble (const wxString & strValue, double &dValue){ wxChar* pEnd; errno = 0; //dValue = _tcstod (strValue, &pEnd); dValue = wxStrtod(strValue, &pEnd); return (0 == errno) && (*pEnd == wxT('\0'));}#if 0const wxString ecUtils::Explanation(CFileException & exc){ wxString strMsg; switch(exc.m_cause){ case CFileException::none: strMsg=wxT("No error occurred.");break; case CFileException::generic: strMsg=wxT(" An unspecified error occurred.");break; case CFileException::fileNotFound: strMsg=wxT(" The file could not be located.");break; case CFileException::badPath: strMsg=wxT(" All or part of the path is invalid.");break; case CFileException::tooManyOpenFiles: strMsg=wxT(" The permitted number of open files was exceeded.");break; case CFileException::accessDenied: strMsg=wxT(" The file could not be accessed.");break; case CFileException::invalidFile: strMsg=wxT(" There was an attempt to use an invalid file handle.");break; case CFileException::removeCurrentDir: strMsg=wxT(" The current working directory cannot be removed.");break; case CFileException::directoryFull: strMsg=wxT(" There are no more directory entries.");break; case CFileException::badSeek: strMsg=wxT(" There was an error trying to set the file pointer.");break; case CFileException::hardIO: strMsg=wxT(" There was a hardware error.");break; case CFileException::sharingViolation: strMsg=wxT(" SHARE.EXE was not loaded, or a shared region was locked.");break; case CFileException::lockViolation: strMsg=wxT(" There was an attempt to lock a region that was already locked.");break; case CFileException::diskFull: strMsg=wxT(" The disk is full.");break; case CFileException::endOfFile: strMsg=wxT(" The end of file was reached. ");break; default: strMsg=wxT(" Unknown cause"); break; } return strMsg;}const wxString ecUtils::LoadString(UINT id){ wxString str; str.LoadString(id); return str;}#endifconst wxString ecUtils::NativeToPosixPath(const wxString & native){#ifdef __CYGWIN__ if (native.IsEmpty()) return native; else { wxString posix; cygwin_conv_to_posix_path(native.c_str(), posix.GetWriteBuf(MAXPATHLEN + 1)); posix.UngetWriteBuf(); return posix; }#else return native;#endif}const wxString ecUtils::PosixToNativePath(const wxString & posix){#ifdef __CYGWIN__ if (posix.IsEmpty()) return posix; else { wxString native; cygwin_conv_to_win32_path(posix.c_str(), native.GetWriteBuf(MAXPATHLEN + 1)); native.UngetWriteBuf(); return native; }#else return posix;#endif}bool ecUtils::AddToPath(const ecFileName & strFolder, bool bAtFront){ wxString strPath,strOldPath; if (wxGetEnv(wxT("PATH"), & strOldPath)) { // Place the user tools folders at the head or tail of the new path if(bAtFront) { strPath.Printf(wxT("%s;%s"), (const wxChar*) strFolder.ShortName(), (const wxChar*) strOldPath); } else { strPath.Printf(wxT("%s;%s"), (const wxChar*) strOldPath, (const wxChar*) strFolder.ShortName()); } } else { // unlikely, but ... strPath = strFolder; } return (TRUE == wxSetEnv(wxT("PATH"),strPath));}#if 0 wxString ecUtils::GetLastErrorMessageString(){ wxString str; PTCHAR pszMsg; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR)&pszMsg, 0, NULL ); // Display the string. str=pszMsg; str.TrimRight(); // Free the buffer. LocalFree(pszMsg); return str;}bool ecUtils::Launch(const ecFileName &strFileName, const ecFileName &strViewer){ bool rc=false; if(!strViewer.IsEmpty())//use custom editor { wxString strCmdline(strViewer); PTCHAR pszCmdLine=strCmdline.GetBuffer(strCmdline.GetLength()); GetShortPathName(pszCmdLine,pszCmdLine,strCmdline.GetLength()); strCmdline.ReleaseBuffer(); strCmdline+=_TCHAR(' '); strCmdline+=strFileName; PROCESS_INFORMATION pi; STARTUPINFO si; si.cb = sizeof(STARTUPINFO); si.lpReserved = NULL; si.lpReserved2 = NULL; si.cbReserved2 = 0; si.lpDesktop = NULL; si.dwFlags = 0; si.lpTitle=NULL; if(CreateProcess( NULL, // app name //strCmdline.GetBuffer(strCmdline.GetLength()), // command line strCmdline.GetBuffer(strCmdline.GetLength()), // command line NULL, // process security NULL, // thread security TRUE, // inherit handles 0, NULL, // environment NULL, // current dir &si, // startup info &pi)){ CloseHandle(pi.hProcess); CloseHandle(pi.hThread); rc=true; } else { MessageBoxF(wxT("Failed to invoke %s.\n"),strCmdline); } strCmdline.ReleaseBuffer(); } else {// Use association TCHAR szExe[MAX_PATH]; HINSTANCE h=FindExecutable(strFileName,wxT("."),szExe); if(int(h)<=32){ wxString str; switch(int(h)){ case 0: str=wxT("The system is out of memory or resources.");break; case 31: str=wxT("There is no association for the specified file type.");break; case ERROR_FILE_NOT_FOUND: str=wxT("The specified file was not found.");break; case ERROR_PATH_NOT_FOUND: str=wxT("The specified path was not found.");break; case ERROR_BAD_FORMAT: str=wxT("The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).");break; default: break; } MessageBoxF(wxT("Failed to open document %s.\r\n%s"),strFileName,str); } else { SHELLEXECUTEINFO sei = {sizeof(sei), 0, AfxGetMainWnd()->GetSafeHwnd(), wxT("open"), strFileName, NULL, NULL, SW_SHOWNORMAL, AfxGetInstanceHandle( )}; sei.hInstApp=0; HINSTANCE hInst=ShellExecute(AfxGetMainWnd()->GetSafeHwnd(),wxT("open"), strFileName, NULL, wxT("."), 0)/*ShellExecuteEx(&sei)*/;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -