The intention is to have a more reliable copy / paste for use with wine on the linux operating system utilizing Notepad++.

I found alternative solutions not working, for whatever reason, so simple is better. Here is xclip.exe for windows ( with the intention of usage of wine running on linux ).

orig, C++ Source code for xclip

https://gist.github.com/Rapptz/9664773178da1bc397cf

Modified C++ Source Code to do double quoted strings input / output style.

Notice, I added a read_string function to accept a string of text in double quotes.

Usage: with modified source code.

INPUT xclip.exe “hey there”

OUTPUT xclip.exe -o

should output to console hey there

modified xclip.exe with a read_string function.

// The MIT License (MIT)

// Copyright (c) 2014 Rapptz

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <windows.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>

struct clipboard {
private:
    bool open;
public:
    clipboard(HWND owner = nullptr) : open(OpenClipboard(owner)) {}

    clipboard(const clipboard&) = delete;
    clipboard(clipboard&&) = delete;
    clipboard& operator=(const clipboard&) = delete;
    clipboard& operator=(clipboard&&) = delete;

    ~clipboard() {
        if (open) {
            CloseClipboard();
        }
    }

    bool clear() const noexcept {
        return EmptyClipboard();
    }

    bool is_open() const noexcept {
        return open;
    }

    bool copy(const std::string& str) const {
        if (open) {
            clear();
            HGLOBAL buffer = GlobalAlloc(GMEM_DDESHARE, str.size() + 1);
            if (buffer) {
                std::copy(std::begin(str), std::end(str), static_cast<char*>(GlobalLock(buffer)));
                GlobalUnlock(buffer);
                return SetClipboardData(CF_TEXT, buffer) != nullptr;
            }
        }
        return false;
    }

    // no error checking on purpose
    std::string paste() const {
        return static_cast<char*>(GetClipboardData(CF_TEXT));
    }
};

int help() {
    std::cout <<
        "usage: xclip [options]\n\n"
        "   -i, --in                  reads text from stdin (default)\n"
        "   -o, --out                 outputs text to stdout\n"
        "   -f, --file <filename>     reads text from a file\n"
        "   -s, --string              accepts a string in double quotes\n"
        "   -v, --version             outputs version information\n"
        "   -h, --help                prints this message and exits\n";
    return EXIT_SUCCESS;
}

int version() {
    std::cout <<
        "xclip version 1.0 (Windows native port)\n"
        "Written by Rapptz\n\n"
        "Copyright (C) 2014 Rapptz\n"
        "This is free software; see the source for copying conditions.  There is NO\n"
        "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
    return EXIT_SUCCESS;
}

int error(const char* str) {
    std::cout << "xclip: error: " << str << '\n';
    return EXIT_FAILURE;
}

int read_string(const clipboard& clip, const char* str) {

    std::ostringstream ss;

    ss << str;

    return clip.copy(ss.str()) ? EXIT_SUCCESS : EXIT_FAILURE;

}

int read_text(const clipboard& clip) {
    std::ostringstream ss;
    for (std::string line; std::getline(std::cin, line); ) {
        ss << line;
    }
    return clip.copy(ss.str()) ? EXIT_SUCCESS : EXIT_FAILURE;
}

int read_file(const clipboard& clip, const std::string& filename) {
    if (filename.empty()) {
        return error("no input file given");
    }
    std::ifstream in(filename);
    std::ostringstream ss;
    ss << in.rdbuf();
    return clip.copy(ss.str()) ? EXIT_SUCCESS : EXIT_FAILURE;
}

int main(int argc, char** argv) {
    clipboard clip;
    if (!clip.is_open()) {
        return error("unable to open clipboard");
    }

    std::vector<std::string> args{ argv, argv + argc };

    if (argc < 2) {
        return read_text(clip);
    }

    for (int i = 1; i < argc; ++i) {

        auto&& arg = args[i];

        if (arg == "-h" || arg == "--help") {
            return help();
        }

        if (arg == "-v" || arg == "--version") {
            return version();
        }

        if (arg == "-i" || arg == "--in") {
            return read_text(clip);
        }

        if (arg == "-s" || arg == "--string") {
            std::cout << "->" << argv[2];
            read_string(clip, argv[2]);
        }



        if (arg == "-o" || arg == "--out") {
            std::cout << clip.paste();
            return EXIT_SUCCESS;
        }

        if (arg == "-f") {
            if (i + 1 < argc) {
                return read_file(clip, args[i + 1]);
            }
            return error("no input file given");
        }

        auto&& pos = arg.find("--file");
        if (pos == 0) {
            // len(--file) == 6
            if (i + 1 < argc && arg.size() == 6) {
                return read_file(clip, args[i + 1]);
            }
            else if (arg[6] == '=') {
                return read_file(clip, arg.substr(7));
            }
            return error("no input file given");
        }
    }
}

So to get this compiled, within QEMU, I downloaded a latest Windows 11 pro and installed it , then downloaded Visual Studio 2022. Then it compiled ok.

Happy Coding!

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *