#include "stdafx.h"
#include "helper.h"
#include <memory.h>
using namespace std;

CString GetAppPath()
{
    TCHAR sDrive[_MAX_DRIVE]; 
    TCHAR sDir[_MAX_DIR];
    TCHAR sFilename[_MAX_FNAME], Filename[_MAX_FNAME];
    TCHAR sExt[_MAX_EXT];

    GetModuleFileName(NULL, Filename, _MAX_PATH);
    _tsplitpath_s(Filename,  sDrive, sDir, sFilename, sExt);

    CString path;
    path.Format(_T("%s%s"), sDrive, sDir);

    return path; 
}

wstring s2ws(const string& s)
{
    int lenU16 = MultiByteToWideChar(CP_OEMCP, 0, s.c_str(), s.size(), NULL, 0);
    if (0 == lenU16)
        return L"";
    vector<wchar_t> buf;
    buf.resize(lenU16);
    MultiByteToWideChar(CP_OEMCP, 0, s.c_str(), -1, &buf[0], lenU16);

    return wstring(buf.begin(), buf.end());
}

string ws2s(const wstring& ws)
{
    int len = WideCharToMultiByte(CP_ACP,0,ws.c_str(),ws.size(),NULL,0,NULL,NULL);
    if (0 == len)
        return "";
    vector<char> buf;
    buf.resize(len);
    WideCharToMultiByte(CP_ACP,0,ws.c_str(),ws.size(),&buf[0],len,NULL,NULL);

    return string(buf.begin(), buf.end());
}
