Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Jul 28, 2015
0 parents commit 64f977c
Show file tree
Hide file tree
Showing 10 changed files with 451 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copy-Paste Library
# Copyright (C) 2015 David Capello

cmake_minimum_required(VERSION 3.2)

project(copypaste CXX)
option(COPYPASTE_EXAMPLES "Compile copypaste examples" on)

include_directories(.)
if(WIN32)
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
endif()

add_library(copypaste copypaste.cpp)

if(COPYPASTE_EXAMPLES)
add_subdirectory(examples)
endif()
20 changes: 20 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2015 David Capello

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.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copy-Paste Library
*Copyright (C) 2015 David Capello*

> Distributed under [MIT license](LICENSE.txt)
Library to copy/retrieve content to/from the clipboard/pasteboard.
Supports Windows.

## Example

#include "copypaste.h"
#include <iostream>

int main() {
copypaste::set_text("Hello World");

std::string value;
copypaste::get_text(value);
std::cout << value << "\n";
}

## To-do

* Support Mac OS X pasteboard
* Support Linux (GTK? KDE? X11?) clipboard
* Add support to copy/paste images
* Add support to copy/paste user-defined formats
84 changes: 84 additions & 0 deletions copypaste.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copy-Paste Library
// Copyright (c) 2015 David Capello

#include "copypaste.h"

#ifdef _WIN32
#include "copypaste_win.h"
#else
#include "copypaste_none.h"
#endif

namespace copypaste {

lock::lock(void* native_handle)
: p(new impl(native_handle)) {
}

lock::~lock() {
delete p;
}

bool lock::clear() {
return p->clear();
}

bool lock::is_convertible(format f) const {
return p->is_convertible(f);
}

bool lock::set_data(format f, const char* buf, size_t length) {
return p->set_data(f, buf, length);
}

bool lock::get_data(format f, char* buf, size_t len) const {
return p->get_data(f, buf, len);
}

size_t lock::get_data_length(format f) const {
return p->get_data_length(f);
}

format empty_format() {
return 0;
}

format text_format() {
return 1;
}

bool has(format f) {
lock l(nullptr);
return l.is_convertible(f);
}

bool clear() {
lock l(nullptr);
return l.clear();
}

bool set_text(const std::string& value) {
lock l(nullptr);
return l.set_data(text_format(), value.c_str(), value.size()+1);
}

bool get_text(std::string& value) {
lock l(nullptr);
format f = text_format();
if (!l.is_convertible(f))
return false;

size_t len = l.get_data_length(f);
if (len > 0) {
std::vector<char> buf(len);
l.get_data(f, &buf[0], len);
value = &buf[0];
return true;
}
else {
value.clear();
return true;
}
}

} // namespace copypaste
54 changes: 54 additions & 0 deletions copypaste.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copy-Paste Library
// Copyright (c) 2015 David Capello

#ifndef COPYPASTE_H_INCLUDED
#define COPYPASTE_H_INCLUDED
#pragma once

#include <string>

namespace copypaste {

// Clipboard format identifier.
typedef size_t format;

// Low-level API to lock the clipboard/pasteboard and modify it.
class lock {
public:
lock(void* native_handle);
~lock();

bool clear();

// Returns true if the clipboard can be converted to the given
// format.
bool is_convertible(format f) const;
bool set_data(format f, const char* buf, size_t len);
bool get_data(format f, char* buf, size_t len) const;
size_t get_data_length(format f) const;

private:
class impl;
impl* p;
};

// This format is when the clipboard has no content.
format empty_format();

// When the clipboard has UTF8 text.
format text_format();

// Returns true if the clipboard has content of the given type.
bool has(format f);

// Clears the clipboard content.
bool clear();

// High-level API to put/get UTF8 text in/from the clipboard. These
// functions returns false in case of error.
bool set_text(const std::string& value);
bool get_text(std::string& value);

} // namespace copypaste

#endif // COPYPASTE_H_INCLUDED
53 changes: 53 additions & 0 deletions copypaste_none.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copy-Paste Library
// Copyright (c) 2015 David Capello

#include <cassert>
#include <vector>

namespace copypaste {

static format g_format;
static std::vector<char> g_data;

class lock::impl {
public:
impl(void* native_handle) {
}

bool clear() {
g_format = empty_format();
return true;
}

bool is_convertible(format f) const {
return (g_format == f);
}

bool set_data(format f, const char* buf, size_t len) {
g_format = f;
g_data.resize(len);
if (buf && len > 0)
std::copy(buf, buf+len, g_data.begin());
return true;
}

bool get_data(format f, char* buf) const {
assert(buf);

if (!buf || !is_convertible(f))
return false;

std::copy(g_data.begin(), g_data.end(), buf);
return true;
}

size_t get_data_length(format f) const {
if (is_convertible(f))
return g_data.size();
else
return 0;
}

};

} // namespace copypaste
Loading

0 comments on commit 64f977c

Please sign in to comment.