xclip for windows
// 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() + );
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"
" -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_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 < ) {
return read_text(clip);
} for(int i = ; 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 == "-o" || arg == "--out") {
std::cout << clip.paste();
return EXIT_SUCCESS;
} if(arg == "-f") {
if(i + < argc) {
return read_file(clip, args[i + ]);
}
return error("no input file given");
} auto&& pos = arg.find("--file");
if(pos == ) {
// len(--file) == 6
if(i + < argc && arg.size() == ) {
return read_file(clip, args[i + ]);
}
else if(arg[] == '=') {
return read_file(clip, arg.substr());
}
return error("no input file given");
}
}
}
xclip for windows的更多相关文章
- Git 配置ssh key的步骤
First start by setting up your own public/private key pair set. This can use either DSA or RSA, so b ...
- linux与windows共享剪贴板(clipboard)
linux与windows共享剪贴板(clipboard)的方法 先说两句废话,其实linux和windows之间不需要共享剪贴板,直接在putty中,按住SHIFT+鼠标选择就可以了. 但是作为一种 ...
- 在Windows下搭建Gitlab服务器
一.GitLab简介 GitLab 是一个用于仓库管理系统的开源项目.使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 可通过Web界面进行访问公开的或者私人项目.它拥有与Github类 ...
- Windows server 2012 添加中文语言包(英文转为中文)(离线)
Windows server 2012 添加中文语言包(英文转为中文)(离线) 相关资料: 公司环境:亚马孙aws虚拟机 英文版Windows2012 中文SQL Server2012安装包,需要安装 ...
- Windows Server 2012 NIC Teaming介绍及注意事项
Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...
- C# 注册 Windows 热键
闲扯: 前几日,一个朋友问我如何实现按 F1 键实现粘贴(Ctrl+V)功能,百度了一个方法,发给他,他看不懂(已经是 Boss 的曾经的码农),我就做了个Demo给他参考.今日得空,将 Demo 整 ...
- Windows 7上执行Cake 报错原因是Powershell 版本问题
在Windows 7 SP1 电脑上执行Cake的的例子 http://cakebuild.net/docs/tutorials/getting-started ,运行./Build.ps1 报下面的 ...
- 在离线环境中发布.NET Core至Windows Server 2008
在离线环境中发布.NET Core至Windows Server 2008 0x00 写在开始 之前一篇博客中写了在离线环境中使用.NET Core,之后一边学习一边写了一些页面作为测试,现在打算发布 ...
- Windows平台分布式架构实践 - 负载均衡
概述 最近.NET的世界开始闹腾了,微软官方终于加入到了对.NET跨平台的支持,并且在不久的将来,我们在VS里面写的代码可能就可以通过Mono直接在Linux和Mac上运行.那么大家(开发者和企业)为 ...
随机推荐
- Hbase记录-备份与恢复方案推荐
热备份和冷备份参考方案,如在生产环境,请结合业务情况考虑
- 如何跨线程调用Windows窗体控件
通过一个子线程来操作主线程中的控件,但是,这样作会出现一个问题(如图1所示),就是TextBox控件是在主线程中创建的,在子线程中并没有对其进行创建,也就是从不是创建控件的线程访问它.那么,如何解决跨 ...
- ACM-ICPC 2018 徐州赛区网络预赛 I Characters with Hash(模拟)
https://nanti.jisuanke.com/t/31461 题意 一个hash规则,每个字母映射成一个两位数,求给的字符串最后的编码位数,要求去除最终结果的前导零 分析 按题意模拟就是了 # ...
- 高性能的代理服务-Envoy
Envoy最初建于Lyft,是一个高性能的代理服务,为服务网格提供了基础. 它与应用程序并行运行,通过以平台无关的方式提供通用功能来抽象网络. 当基础架构中的所有服务流量都通过Envoy网格时,通过一 ...
- 052、overlay如何实现跨主机通信?(2019-03-19 周二)
参考https://www.cnblogs.com/CloudMan6/p/7305989.html 今天开始学习 overlay 网络跨主机通信的原理 root@host01:~# ufw ...
- python中的顺序表
Python中的list和tuple两种类型采用了顺序表的实现技术,tuple是不可变类型,即不变的顺序表,因此不支持改变其内部状态的任何操作,而其他方面,则与list的性质类似. list的基本实现 ...
- 细说shiro之三:在独立应用中使用shiro
官网:https://shiro.apache.org/ 1. 下载在非Web环境的独立应用中使用Shiro时,只需要shiro-core组件.在Maven项目中的依赖配置如下: <depend ...
- 细说java平台日志组件
1. java.util.logging JDK自带日志组件,使用方式简单,不需要依赖第三方日志组件.支持将日志打印到控制台,文件,甚至可以将日志通过网络打印到指定主机.相对于第三方独立日志框架来说, ...
- sql审核工具
https://github.com/Meituan-Dianping/SQLAdvisor/blob/master/doc/QUICK_START.md
- verilog仿真文件编写
verilog仿真文件大概框架: ·timescale 1ns/1ps //但需要时间 module xxx_tb(); //仿真文件不需要输入和输出, intput clk; ] xx; //根据需 ...