Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Last active April 3, 2017 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnhmj/26f43c57c0a6d7044f1450f3ca33e80f to your computer and use it in GitHub Desktop.
Save johnhmj/26f43c57c0a6d7044f1450f3ca33e80f to your computer and use it in GitHub Desktop.
#include "WinMainCmdline.h"
//
CCmdLine::CCmdLine()
{
this->m_lpCmdLine = NULL;
}
CCmdLine::CCmdLine(const CCmdLine& cl)
{
unsigned int nBufferSize = 0;
this->m_lpCmdLine = NULL;
if (this == &cl)
{
return;
}
if (cl.m_lpCmdLine == NULL)
{
return;
}
nBufferSize = (this->getStringLength(cl.m_lpCmdLine) + 1) * sizeof(TCHAR);
this->m_lpCmdLine = new TCHAR[nBufferSize];
ZeroMemory(this->m_lpCmdLine, nBufferSize);
CopyMemory(this->m_lpCmdLine, cl.m_lpCmdLine, nBufferSize);
this->m_vCmdLineList = cl.m_vCmdLineList;
}
CCmdLine& CCmdLine::operator=(const CCmdLine& cl)
{
unsigned int nBufferSize = 0;
if (this != &cl)
{
if (cl.m_lpCmdLine != NULL)
{
nBufferSize = (this->getStringLength(cl.m_lpCmdLine) + 1) * sizeof(TCHAR);
this->m_lpCmdLine = new TCHAR[nBufferSize];
ZeroMemory(this->m_lpCmdLine, nBufferSize);
CopyMemory(this->m_lpCmdLine, cl.m_lpCmdLine, nBufferSize);
this->m_vCmdLineList = cl.m_vCmdLineList;
}
}
return *this;
}
CCmdLine::~CCmdLine()
{
if (this->m_lpCmdLine != NULL)
{
delete [] this->m_lpCmdLine;
this->m_lpCmdLine = NULL;
}
this->m_vCmdLineList.clear();
}
void CCmdLine::setCmdLine(LPTSTR lpcl)
{
unsigned int nBufferSize = 0, i = 0;
TString s;
if (this->m_lpCmdLine != NULL)
{
delete [] this->m_lpCmdLine;
this->m_lpCmdLine = NULL;
}
nBufferSize = (this->getStringLength(lpcl) + 1) * sizeof(TCHAR);
this->m_lpCmdLine = new TCHAR[nBufferSize];
ZeroMemory(this->m_lpCmdLine, nBufferSize);
CopyMemory(this->m_lpCmdLine, lpcl, nBufferSize);
for (; this->m_lpCmdLine[i] != 0; i ++)
{
if (this->m_lpCmdLine[i] == 0x20)
{
i ++;
if (! s.empty())
{
this->m_vCmdLineList.push_back(s);
s.clear();
}
}
s += this->m_lpCmdLine[i];
}
this->m_vCmdLineList.push_back(s);
}
unsigned int CCmdLine::getStringLength(LPTSTR s)
{
unsigned int i = 0;
for (; s[i] != 0; i ++);
return i;
}
void CCmdLine::getCmdLine(TStrVector& v)
{
v = this->m_vCmdLineList;
}
#ifndef _WinMainCmdline_H_
#define _WinMainCmdline_H_
//
#include <Windows.h>
#include <vector>
#include <string>
//
using std::vector;
using std::basic_string;
typedef basic_string<TCHAR> TString;
typedef vector<TString> TStrVector;
//
class CCmdLine
{
public:
CCmdLine();
CCmdLine(__in const CCmdLine& cl);
CCmdLine& operator=(__in const CCmdLine& cl);
~CCmdLine();
void setCmdLine(__in LPTSTR lpcl);
void getCmdLine(__out TStrVector& v);
protected:
private:
TCHAR* m_lpCmdLine;
TStrVector m_vCmdLineList;
unsigned int getStringLength(__in LPTSTR s);
};
//
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment