atesdal icon

SpaceTextToClipboardWin10

atesdal | PRO | 12/11/18 05:49:30 PM UTC | 0 ⭐ | 321 👁️ | Never ⏰ | []
C++ |

1.73 KB

|

None

|

0 👍

/

0 👎

#include <Windows.h>
#include <string>
#include <iostream>
#include <cctype>
 
bool ToClipboard(const std::string &s)
{
    OpenClipboard(0);
    EmptyClipboard();
    HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size());
    if (!hg) {
        CloseClipboard();
        return false;
    }
    memcpy(GlobalLock(hg), s.c_str(), s.size());
    GlobalUnlock(hg);
    SetClipboardData(CF_TEXT, hg);
    CloseClipboard();
    GlobalFree(hg);
    return true;
}
 
std::string ToSpace(const std::string &s)
{
    unsigned int spaces = 0;
    unsigned int req_size = 0;
    
    //count spaces
    for (auto c : s) {
        if (c == ' ') { spaces++; }
    }
 
    unsigned int chars = s.size() - spaces;
    req_size = (chars * 2) + (spaces * 2) + 1;
 
    char *string = new char[req_size];
    
    size_t string_index = 0;
    for (size_t i{ 0 }; i < s.size(); i++) {
        if (s[i] != ' ') {
            string[string_index] = s[i];
            string[string_index] = toupper(string[string_index]);
            string[string_index + 1] = ' ';
            string_index = string_index + 2;
        }
        else {
            string[string_index] = s[i];
            string[string_index + 1] = ' ';
            string_index = string_index + 2;
        }
    }
    string[req_size - 1] = '\0';
 
    std::string return_string = std::string(string);
    delete[] string;
 
    return return_string;
}
 
int main()
{
    bool correct_input = false;
    std::string in = "";
    while(!correct_input) {
        in = "";
        std::string valid = "";
        std::cout << "S P A C E   Y O U R   T E X T" << std::endl;
        std::getline(std::cin, in);
        std::cout << "S P A C E : " + in + " ? (y/n)" << std::endl;
        std::cin >> valid;
        if (valid.front() == 'y' || valid.front() == 'Y') { correct_input = true; }
    }
 
    std::string spaced_string = ToSpace(in);
 
    if (ToClipboard(spaced_string)) {
        return 0;
    }
    return -1;
}

Comments