atesdal

SpaceTextToClipboardWin10

Dec 11th, 2018
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <string>
  3. #include <iostream>
  4. #include <cctype>
  5.  
  6. bool ToClipboard(const std::string &s)
  7. {
  8.     OpenClipboard(0);
  9.     EmptyClipboard();
  10.     HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, s.size());
  11.     if (!hg) {
  12.         CloseClipboard();
  13.         return false;
  14.     }
  15.     memcpy(GlobalLock(hg), s.c_str(), s.size());
  16.     GlobalUnlock(hg);
  17.     SetClipboardData(CF_TEXT, hg);
  18.     CloseClipboard();
  19.     GlobalFree(hg);
  20.     return true;
  21. }
  22.  
  23. std::string ToSpace(const std::string &s)
  24. {
  25.     unsigned int spaces = 0;
  26.     unsigned int req_size = 0;
  27.    
  28.     //count spaces
  29.     for (auto c : s) {
  30.         if (c == ' ') { spaces++; }
  31.     }
  32.  
  33.     unsigned int chars = s.size() - spaces;
  34.     req_size = (chars * 2) + (spaces * 2) + 1;
  35.  
  36.     char *string = new char[req_size];
  37.    
  38.     size_t string_index = 0;
  39.     for (size_t i{ 0 }; i < s.size(); i++) {
  40.         if (s[i] != ' ') {
  41.             string[string_index] = s[i];
  42.             string[string_index] = toupper(string[string_index]);
  43.             string[string_index + 1] = ' ';
  44.             string_index = string_index + 2;
  45.         }
  46.         else {
  47.             string[string_index] = s[i];
  48.             string[string_index + 1] = ' ';
  49.             string_index = string_index + 2;
  50.         }
  51.     }
  52.     string[req_size - 1] = '\0';
  53.  
  54.     std::string return_string = std::string(string);
  55.     delete[] string;
  56.  
  57.     return return_string;
  58. }
  59.  
  60. int main()
  61. {
  62.     bool correct_input = false;
  63.     std::string in = "";
  64.     while(!correct_input) {
  65.         in = "";
  66.         std::string valid = "";
  67.         std::cout << "S P A C E   Y O U R   T E X T" << std::endl;
  68.         std::getline(std::cin, in);
  69.         std::cout << "S P A C E : " + in + " ? (y/n)" << std::endl;
  70.         std::cin >> valid;
  71.         if (valid.front() == 'y' || valid.front() == 'Y') { correct_input = true; }
  72.     }
  73.  
  74.     std::string spaced_string = ToSpace(in);
  75.  
  76.     if (ToClipboard(spaced_string)) {
  77.         return 0;
  78.     }
  79.     return -1;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment