Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.76 KB | None | 0 0
  1. //terminal.h
  2.  
  3. #ifndef __TERMINAL_H
  4. #define __TERMINAL_H
  5.  
  6. #include "platform.h"
  7. #ifdef _WINDOWS
  8. #include "process.h"
  9.  
  10. #ifndef TERMINAL_DEFAULT_CP
  11. #define TERMINAL_DEFAULT_CP 866
  12. #endif
  13.  
  14. class ITerminal
  15. {
  16. public:
  17.     virtual ~ITerminal(){}
  18.     virtual int WriteText(const char* pText) const = 0;
  19.     virtual int ReadText(char* pBuf,uint uMaxLen) const = 0;
  20.     virtual IProcess* GetProcess() const = 0;
  21.     virtual int GetCodePage() const = 0;
  22.     virtual void SetCodePage(int cp) = 0;
  23.  
  24.     virtual void SetReadLF(bool bRead = true) = 0;
  25.     virtual void SetWriteAutoCRLF(bool bWrite = false) = 0;
  26. };
  27.  
  28. class Terminal : public ITerminal
  29. {
  30. public:
  31.     Terminal(IProcess* pCmd);
  32.     virtual ~Terminal();
  33.  
  34.     virtual int WriteText(const char* pText) const;
  35.     virtual int ReadText(char* pBuf,uint uMaxLen) const;
  36.     virtual IProcess* GetProcess() const;
  37.     virtual int GetCodePage() const;
  38.     virtual void SetCodePage(int cp);
  39.  
  40.     virtual void SetReadLF(bool bRead = true);
  41.     virtual void SetWriteAutoCRLF(bool bWrite = false);
  42. private:
  43.     IProcess* m_pCmd;
  44.     int m_iCP;
  45.     bool m_bReadLF;
  46.     bool m_bWriteAutoCRLF;
  47. public:
  48.     static ITerminal* Create(bool bHidden = false,const char* cwd = NULL);
  49.     static void Destroy(ITerminal* term);
  50.  
  51.     static Terminal& This();
  52.     static void SetDefaultCP(int cp);
  53.     static int GetDefaultCP();
  54.  
  55.     static void SetInternalCP(int cp);
  56.     static int GetInternalCP();
  57. private:
  58.     static int s_iInternalCP;
  59.     static int s_iDefaultCP;
  60. };
  61.  
  62. #endif
  63.  
  64. #endif
  65.  
  66. //terminal.cpp
  67.  
  68. #include "terminal.h"
  69.  
  70. #ifdef _WINDOWS
  71. #include "unicode.h"
  72.  
  73. Terminal::Terminal(IProcess* pCmd)
  74.     : m_pCmd(pCmd)
  75. {
  76.     SetCodePage(GetDefaultCP());
  77.     SetReadLF(true);
  78.     SetWriteAutoCRLF(false);
  79. }
  80.  
  81. Terminal::~Terminal()
  82. {
  83. }
  84.  
  85. int Terminal::WriteText(const char* pRawText) const
  86. {
  87.     //Process line feeds
  88.     int iWritten;
  89.     uint uTextLen = strlen(pRawText)+1;
  90.     char* pText = new char[uTextLen + (m_bWriteAutoCRLF ? 2 : 1)]; //CR + null-terminator : auto_CRLF + null-terminator
  91.     memcpy(pText,pRawText,uTextLen);
  92.     if(m_bWriteAutoCRLF)
  93.     {
  94.         char* _end = &pText[uTextLen-1]; // points to null-terminator
  95.         *_end++ = '\r';
  96.         *_end++ = '\n';
  97.         *_end++ = '\0';
  98.     }
  99.     else
  100.     {
  101.         char* _lf = strrchr(pText,'\n');
  102.         if(_lf != NULL)
  103.         {
  104.             *_lf++ = '\r';
  105.             *_lf++ = '\n';
  106.             *_lf = '\0';
  107.         }
  108.     }
  109.  
  110.     if(GetCodePage() == GetInternalCP())
  111.         iWritten = GetProcess()->Write(pText,strlen(pText));
  112.     else
  113.     {
  114.         char* pBuf = encoding_convert(GetInternalCP(),GetCodePage(),pText);
  115.         iWritten = GetProcess()->Write(pBuf,strlen(pBuf));
  116.         delete[] pBuf;
  117.     }
  118.     delete[] pText;
  119.     return iWritten;
  120. }
  121.  
  122. int Terminal::ReadText(char* pBuf,uint uMaxLen) const
  123. {
  124.     char* pNewBuf = new char[uMaxLen];
  125.     int iRead = GetProcess()->Read(pNewBuf,uMaxLen);
  126.     if(iRead < 1)
  127.     {
  128.         delete[] pNewBuf;
  129.         return iRead;
  130.     }
  131.    
  132.     //Process line feeds
  133.     char* _cr = strrchr(pNewBuf,'\r');
  134.     if(_cr != NULL)
  135.     {
  136.         if(m_bReadLF) *_cr++ = '\n';
  137.         *_cr = '\0';
  138.     }
  139.  
  140.     char* pConvert;
  141.     if(GetCodePage() == GetInternalCP())
  142.         pConvert = _strdup(pNewBuf);
  143.     else pConvert = encoding_convert(GetCodePage(),GetInternalCP(),pNewBuf);
  144.     delete[] pNewBuf;
  145.     strncpy(pBuf,pConvert,uMaxLen);
  146.    
  147.     delete[] pConvert;
  148.     return iRead;
  149. }
  150.  
  151. IProcess* Terminal::GetProcess() const
  152. {
  153.     return m_pCmd;
  154. }
  155.  
  156. int Terminal::GetCodePage() const
  157. {
  158.     return m_iCP;
  159. }
  160.  
  161. void Terminal::SetCodePage(int cp)
  162. {
  163.     m_iCP = cp;
  164. }
  165.  
  166. void Terminal::SetReadLF(bool bRead)
  167. {
  168.     m_bReadLF = bRead;
  169. }
  170.  
  171. void Terminal::SetWriteAutoCRLF(bool bWrite)
  172. {
  173.     m_bWriteAutoCRLF = bWrite;
  174. }
  175.  
  176. ITerminal* Terminal::Create(bool bHidden,const char* cwd)
  177. {
  178.     char szCmdPath[MAX_PATH];
  179.     HANDLE hChildIO[Process::STDMAX];
  180.     HANDLE hParentIO[Process::STDMAX];
  181.     ProcessConfig config;
  182.    
  183.     GetWindowsDirectory(szCmdPath,MAX_PATH);
  184.     strcat(szCmdPath,"\\system32\\cmd.exe");
  185.     config.SetExecutablePath(szCmdPath);
  186.  
  187.     config.params.m_bShowWindow = !bHidden;
  188.     config.params.m_bUseStdHandles = true;
  189.     config.params.m_bInheritHandles = true;
  190.     Process::CreatePipedIO(hChildIO,hParentIO);
  191.     Process* cmd = Process::Create(config,hChildIO,hParentIO);
  192.     if(!cmd) return NULL;
  193.     return new Terminal(cmd);
  194. }
  195.  
  196. void Terminal::Destroy(ITerminal* iterm)
  197. {
  198.     Terminal* term = dynamic_cast<Terminal*>(iterm);
  199.     Process* cmd = dynamic_cast<Process*>(iterm->GetProcess());
  200.     cmd->Terminate(0);
  201.     delete cmd;
  202.     delete term;
  203. }
  204.  
  205. static Terminal _this_terminal = Terminal(const_cast<Process*>(&Process::This()));
  206.  
  207. int Terminal::s_iInternalCP = INTERNAL_CP;
  208. int Terminal::s_iDefaultCP = TERMINAL_DEFAULT_CP;
  209.  
  210. Terminal& Terminal::This()
  211. {
  212.     return _this_terminal;
  213. }
  214.  
  215. void Terminal::SetInternalCP(int cp)
  216. {
  217.     s_iInternalCP = cp;
  218. }
  219.  
  220. int Terminal::GetInternalCP()
  221. {
  222.     return s_iInternalCP;
  223. }
  224.  
  225. void Terminal::SetDefaultCP(int cp)
  226. {
  227.     s_iDefaultCP = cp;
  228. }
  229.  
  230. int Terminal::GetDefaultCP()
  231. {
  232.     return s_iDefaultCP;
  233. }
  234.  
  235. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement