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上运行.那么大家(开发者和企业)为 ...
随机推荐
- Linux 检查 外部设备 是否存在
以 USB 为例,如果移植了udev,那么在usb插入的时候,/dev下面会出现usb有关的设备,同时,自动挂载到文件系统的某个节点 如果以文件系统usb对应的挂载点来检测USB是否插入,是不够严谨的 ...
- 浏览器内核 Trident Gecko Presto Webkit
Trident内核:IE,MaxThon,TT,The World,360,搜狗浏览器等 Gecko内核:Netscape6及以上版本,FF,MozillaSuite/SeaMonkey等 Prest ...
- js学习总结:DOM节点二(dom基本操作)
一.DOM继承树 DOM——Document Object Model DOM定义了表示修改文档所需要的方法.DOM对象即为宿主对象,由浏览器厂商定义,用来操作html和xml的一类厂商定义,也有人称 ...
- Java方向如何准备技术面试答案(汇总版)
本文转载自:"Java团长"公众号 1.面向对象和面向过程的区别 面向过程优点:性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源;比如单片机.嵌入式开发.Linu ...
- Centos6下安装中文字体
先安装字体管理软件 [root@localhost ~]# yum install fontconfig 将需要安装的字体放到/usr/share/fonts/chinese/目录下 如果不存在这个目 ...
- js对象,字符串 互相 转换
JSON.stringify(jsonobj); //可以将json对象转换成json字符串 反过来: JSON.parse(jsonstr); //可以将json字符串转换成json ...
- Groovy 类名称赋值为变量使用(newInstance & new)
类创建实例一般方式 http://groovy-lang.org/objectorientation.html#_class class Person { String name Integer ag ...
- lua基于oopclass的属性节点类 和 集合类
--[[---------------------------------------------------------------------------- --@ Descrption: 属性节 ...
- Spring IoC 依赖注入的方法大全 XML配置方式
Spring 依赖注入 构造方法注入 ① 根据索引注入 <bean name="student" class="cn.bdqn.SpringDI.Student ...
- WebSocket起航 JavaScript客户端和Server通信
Message =>JSON => Move 客户端发给服务器总是Move server.send(JSON.stringify({row: row, column: column} ...