Friday, September 30, 2016

std::string to std::wstring conversion (and vice versa) using Windows API



#pragma once

#include <windows.h>
#include <string>

inline std::wstring multi2wide(const std::string& str, UINT codePage = CP_THREAD_ACP)
{
    if (str.empty())
    {
        return std::wstring();
    }

    int required = ::MultiByteToWideChar(codePage, 0, str.data(), (int)str.size(), NULL, 0);
    if (0 == required)
    {
        return std::wstring();
    }

    std::wstring str2;
    str2.resize(required);

    int converted = ::MultiByteToWideChar(codePage, 0, str.data(), (int)str.size(), &str2[0], str2.capacity());
    if (0 == converted)
    {
        return std::wstring();
    }

    return str2;
}

inline std::string wide2multi(const std::wstring& str, UINT codePage = CP_THREAD_ACP)
{
    if (str.empty())
    {
        return std::string();
    }

    int required = ::WideCharToMultiByte(codePage, 0, str.data(), (int)str.size(), NULL, 0, NULL, NULL);
    if (0 == required)
    {
        return std::string();
    }

    std::string str2;
    str2.resize(required);

    int converted = ::WideCharToMultiByte(codePage, 0, str.data(), (int)str.size(), &str2[0], str2.capacity(), NULL, NULL);
    if (0 == converted)
    {
        return std::string();
    }

    return str2;
}

Tuesday, September 13, 2016

Export all third-party drivers from a Windows image to a destination folder


Export-WindowsDriver -Online -Destination E:\DriverBackup

Tuesday, July 12, 2016

Escape JSON string

std::string EscapeJson(const char* text)
{
    std::string string;
    
    int length = strlen(text);
    for (int i = 0; i < length; i++)
    {
        switch (text[i])
        {
            case '\\':
                string.append("\\\\");
                break;
            case '"':
                string.append("\\\"");
                break;
            case '/':
                string.append("\\/");
                break;
            case '\b':
                string.append("\\b");
                break;
            case '\f':
                string.append("\\f");
                break;
            case '\n':
                string.append("\\n");
                break;
            case '\r':
                string.append("\\r");
                break;
            case '\t':
                string.append("\\t");
                break;
            default:
                char c = text[i];
                string.append(1, c < 32 ? '?' : c);
                break;
        }
    }

    return string;
}

Thursday, June 30, 2016

Rename Git user in pushed commits

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="wrong@gmail.com"
CORRECT_NAME="vurdalakov"
CORRECT_EMAIL="vurdalakov@gmail.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Monday, June 27, 2016

Trim function for std::string

std::string stdtrim(std::string& str)
{
    size_t first = str.find_first_not_of(' ');
    size_t last = str.find_last_not_of(' ');
    return (std::string::npos == first) || (std::string::npos == last) ? "" : str.substr(first, (last - first + 1));
}