diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1edcbe5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.sw* +build/ +deps/ +test/test + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8dd9f9d --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ + +DEPS ?= gyp libuv + +all: build test + +.PHONY: deps +deps: $(DEPS) + +gyp: deps/gyp +deps/gyp: + git clone --depth 1 https://chromium.googlesource.com/external/gyp.git ./deps/gyp + +libuv: deps/libuv +deps/libuv: + git clone --depth 1 git://github.com/libuv/libuv.git ./deps/libuv + +build: $(DEPS) + deps/gyp/gyp --depth=. -Goutput_dir=./out -Icommon.gypi --generator-output=./build -Dlibrary=static_library -Duv_library=static_library -f make + +.PHONY: test +test: ./test/test.cc + make -C ./build/ test + cp ./build/out/Release/test ./test/test + diff --git a/README.md b/README.md new file mode 100644 index 0000000..caafe70 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# SYNOPSIS + +# USAGE + +## AS A MODULE USING `BALE`. +```cpp +import Buffer "buffer" +Buffer buf(10); +``` + +## AS A REGLUAR C++ LIBRARY. +Build with `buffer.cc` as a dependency and include the header file. + +```cpp +#include "buffer.h" +Buffer buf(10); +``` + +# API + +## CONSTRUCTOR + +## INSTANCE METHODS + +## STATIC METHODS + diff --git a/buffer.h b/buffer.h new file mode 100644 index 0000000..e0e75b1 --- /dev/null +++ b/buffer.h @@ -0,0 +1,44 @@ +#ifndef NODEUV_BUFFER_H +#define NODEUV_BUFFER_H + +#define NODEUV_MIN(a, b) ((a) < (b) ? (a) : (b)) + +#include +#include +#include +#include +#include +#include + +extern "C" { +#include "uv.h" +} + +namespace nodeuv { + + using namespace std; + + class Buffer { + + public: + + uv_buf_t data; + + string toString(); + int length(); + int copy(Buffer b); + int copy(Buffer b, int target_start); + int copy(Buffer b, int target_start, int source_start); + int copy(Buffer b, int target_start, int source_start, int source_end); + + Buffer(); + Buffer(int size); + Buffer(char* str); + Buffer(string str); + Buffer(const Buffer &buf); + Buffer& operator= (const Buffer &buf); + }; +} + +#endif + diff --git a/build.gyp b/build.gyp new file mode 100644 index 0000000..e7a37a6 --- /dev/null +++ b/build.gyp @@ -0,0 +1,29 @@ +{ + 'includes': [ 'common.gypi' ], + 'targets': [ + { + 'target_name': 'nodeuv-buffer', + 'product_name': 'nodeuv-buffer', + 'type': 'static_library', + 'sources': [ + 'buffer.h', + './src/buffer.cc' + ], + 'dependencies': [ + './deps/libuv/uv.gyp:libuv', + ], + }, + { + 'target_name': 'test', + 'type': 'executable', + 'sources': [ + './src/buffer.cc', + './test/test.cc', + ], + 'dependencies': [ + './deps/libuv/uv.gyp:libuv', + 'nodeuv-buffer', + ], + } + ] +} diff --git a/common.gypi b/common.gypi new file mode 100644 index 0000000..36394e5 --- /dev/null +++ b/common.gypi @@ -0,0 +1,83 @@ +{ + 'variables': { + 'conditions': [ + ['OS == "mac"', { + 'target_arch%': 'x64' + }, { + 'target_arch%': 'ia32' + }] + ] + }, + 'target_defaults': { + 'default_configuration': 'Release', + 'conditions': [ + ['OS == "mac"', { + 'defines': [ 'DARWIN' ] + }], + ['OS == "linux"', { + 'defines': [ 'LINUX' ] + }], + ['OS == "win"', { + 'defines': [ 'WIN32' ] + }], + ['OS == "mac" and target_arch == "x64"', { + 'xcode_settings': { + 'ARCHS': [ 'x86_64' ] + }, + }] + ], + 'configurations': { + 'Debug': { + 'cflags': [ '-g', '-O0', '-std=c++1y' ], + 'defines': [ 'DEBUG' ], + 'xcode_settings': { + 'OTHER_CPLUSPLUSFLAGS' : [ '-std=c++1y' ], + 'OTHER_LDFLAGS': ['-std=c++1y'], + 'OTHER_CFLAGS': [ '-g', '-O0', '-Wall' ] + } + }, + 'Release': { + 'cflags': [ + '-O3', + '-std=c++1y', + '-fstrict-aliasing', + '-fomit-frame-pointer', + '-fdata-sections', + '-ffunction-sections', + '-fPIC', + ], + 'defines': [ 'NDEBUG' ], + 'xcode_settings': { + 'ALWAYS_SEARCH_USER_PATHS': 'NO', + 'GCC_CW_ASM_SYNTAX': 'NO', + 'GCC_DYNAMIC_NO_PIC': 'NO', + 'GCC_ENABLE_PASCAL_STRINGS': 'NO', + 'GCC_INLINES_ARE_PRIVATE_EXTERN': 'YES', + 'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', + 'GCC_THREADSAFE_STATICS': 'NO', + 'GCC_WARN_ABOUT_MISSING_NEWLINE': 'YES', + 'PREBINDING': 'NO', + 'USE_HEADERMAP': 'NO', + 'OTHER_CPLUSPLUSFLAGS' : [ + '-O3', + '-std=c++1y', + '-fstrict-aliasing', + '-fomit-frame-pointer', + '-fdata-sections', + '-ffunction-sections', + '-fPIC', + ], + 'OTHER_CFLAGS': [ + '-O3', + '-fstrict-aliasing', + '-fomit-frame-pointer', + '-fdata-sections', + '-ffunction-sections', + '-fPIC', + ] + } + } + } + } +} + diff --git a/src/buffer.cc b/src/buffer.cc new file mode 100644 index 0000000..9bbd3cd --- /dev/null +++ b/src/buffer.cc @@ -0,0 +1,71 @@ +#include "../buffer.h" + +namespace nodeuv { + + using namespace std; + + string Buffer::toString() { + string s; + s.assign(data.base, data.len); + return s; + } + + int Buffer::length() { + return data.len; + } + + int Buffer::copy(Buffer b) { + return this->copy(b, 0, 0, b.length()); + } + + int Buffer::copy(Buffer b, int target_start) { + return this->copy(b, target_start, 0, b.length()); + } + + int Buffer::copy(Buffer b, int target_start, int source_start) { + return this->copy(b, target_start, source_start, b.length()); + } + + int Buffer::copy(Buffer b, int target_start, int source_start, int source_end) { + + size_t obj_length = b.length(); + size_t target_length = this->length(); + char* target_data = this->data.base; + + if (target_start >= target_length || source_start >= source_end) { + return 0; + } + + if (source_start > obj_length) { + throw runtime_error("out of range index"); + } + + uint32_t to_copy = NODEUV_MIN(NODEUV_MIN(source_end - source_start, + target_length - target_start), + obj_length - source_start); + + memmove(this->data.base + target_start, b.data.base + source_start, to_copy); + return to_copy; + } + + Buffer::Buffer() { + } + + Buffer::Buffer(int size) { + data.base = (char*) malloc(size); + data.len = size; + } + + Buffer::Buffer(string str) { + data = uv_buf_init((char*) str.c_str(), str.length()); + } + + Buffer::Buffer(char* str) { + data = uv_buf_init(str, strlen(str)); + } + + Buffer::Buffer(const Buffer &buf) { + data = buf.data; + } +} + diff --git a/test/test.cc b/test/test.cc new file mode 100644 index 0000000..682a66f --- /dev/null +++ b/test/test.cc @@ -0,0 +1,26 @@ +#include "../buffer.h" +#include + +using namespace nodeuv; + +#define ASSERT(message, ...) do { \ + if(!(__VA_ARGS__)) { \ + std::cerr << "FAIL: " << message << std::endl; \ + } \ + else { \ + std::cout << "OK: " << message << std::endl; \ + } \ +} while(0); + +int main() { + + // + // sanity test. + // + ASSERT("sanity: true is false", true == false); + ASSERT("sanity: true is true", true == true); + + + +} +