[Rprotobuf-commits] r638 - pkg/src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Dec 30 21:38:39 CET 2013
Author: murray
Date: 2013-12-30 21:38:38 +0100 (Mon, 30 Dec 2013)
New Revision: 638
Modified:
pkg/src/SocketCopyingInputStream.cpp
pkg/src/lookup.cpp
pkg/src/streams.cpp
Log:
Whitespace change only (clang-format)
Consistent indentation, brace placement, and 80-char line wrapping.
Modified: pkg/src/SocketCopyingInputStream.cpp
===================================================================
--- pkg/src/SocketCopyingInputStream.cpp 2013-12-30 20:34:10 UTC (rev 637)
+++ pkg/src/SocketCopyingInputStream.cpp 2013-12-30 20:38:38 UTC (rev 638)
@@ -1,25 +1,21 @@
#include "rprotobuf.h"
#include "SocketCopyingInputStream.h"
-namespace rprotobuf{
-
- SocketCopyingInputStream::SocketCopyingInputStream(int id){
- socket_id = id ;
- }
-
- /**
- * read from the socket
- *
- * @param buffer buffer to fill with at most size bytes
- * @param size maximum number of bytes
- *
- * @return the number of bytes actually read
- */
- int SocketCopyingInputStream::Read(void * buffer, int size){
- int received = recv( socket_id, buffer, size, 0 ) ;
- if( received < 0 ) THROW_SOCKET_ERROR( "recv" ) ;
- return received ;
- }
-
-}
+namespace rprotobuf {
+SocketCopyingInputStream::SocketCopyingInputStream(int id) { socket_id = id; }
+
+/**
+ * read from the socket
+ *
+ * @param buffer buffer to fill with at most size bytes
+ * @param size maximum number of bytes
+ *
+ * @return the number of bytes actually read
+ */
+int SocketCopyingInputStream::Read(void* buffer, int size) {
+ int received = recv(socket_id, buffer, size, 0);
+ if (received < 0) THROW_SOCKET_ERROR("recv");
+ return received;
+}
+}
Modified: pkg/src/lookup.cpp
===================================================================
--- pkg/src/lookup.cpp 2013-12-30 20:34:10 UTC (rev 637)
+++ pkg/src/lookup.cpp 2013-12-30 20:38:38 UTC (rev 638)
@@ -1,19 +1,18 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil;
+// -*-
#include "rprotobuf.h"
-#include "DescriptorPoolLookup.h"
+#include "DescriptorPoolLookup.h"
/* This uses the mechanism of the RObjectTables package
see: http://www.omegahat.org/RObjectTables/ */
-namespace rprotobuf{
+namespace rprotobuf {
/**
* Returns the R_UnboundValue
*/
-SEXP R_getUnboundValue() {
- return(R_UnboundValue);
-}
+SEXP R_getUnboundValue() { return (R_UnboundValue); }
/**
* @param name potential message type
@@ -22,152 +21,151 @@
*
* @return _TRUE_ if there is a message of the given type in the DescriptorPool
*/
-Rboolean rProtoBufTable_exists(const char * const name, Rboolean *canCache, R_ObjectTable *tb){
+Rboolean rProtoBufTable_exists(const char *const name, Rboolean *canCache,
+ R_ObjectTable *tb) {
#ifdef LOOKUP_DEBUG
- Rprintf( " >> rProtoBufTable_exists\n" );
+ Rprintf(" >> rProtoBufTable_exists\n");
#endif
-
- if(tb->active == _FALSE_)
- return( _FALSE_ );
- tb->active = _FALSE_;
- Rboolean val = _FALSE_ ;
- if( DescriptorPoolLookup::contains( name ) ){
- /* first check the cache */
- val = _TRUE_ ;
- } else {
- /* try the generated pool */
- const GPB::DescriptorPool* pool = GPB::DescriptorPool::generated_pool() ;
- if( pool->FindMessageTypeByName( name ) ||
- pool->FindEnumTypeByName( name ) ||
- pool->FindServiceByName( name ) ||
- pool->FindMethodByName( name ) ||
- pool->FindExtensionByName( name )){
- DescriptorPoolLookup::add( name ) ;
- val = _TRUE_ ;
- } else {
- /* try the runtime pool */
- pool = DescriptorPoolLookup::pool() ;
- if( pool->FindMessageTypeByName( name ) ||
- pool->FindEnumTypeByName( name ) ||
- pool->FindServiceByName( name ) ||
- pool->FindMethodByName( name ) ||
- pool->FindExtensionByName( name )){
- DescriptorPoolLookup::add( name ) ;
- val = _TRUE_ ;
- }
- }
- }
- tb->active = _TRUE_;
-
- return( val );
-}
+ if (tb->active == _FALSE_) return (_FALSE_);
-SEXP findSomething( const GPB::DescriptorPool* pool, const char * const name){
- const GPB::Descriptor* desc = pool->FindMessageTypeByName( name ) ;
- std::string name_string(name) ;
- if( desc ){
- /* message */
- DescriptorPoolLookup::add( name_string ) ;
- return S4_Descriptor( desc ) ;
- } else {
- const GPB::EnumDescriptor* enum_desc = pool->FindEnumTypeByName( name_string ) ;
- if( enum_desc ){
- /* enum */
- DescriptorPoolLookup::add( name_string ) ;
- return S4_EnumDescriptor( enum_desc );
+ tb->active = _FALSE_;
+ Rboolean val = _FALSE_;
+ if (DescriptorPoolLookup::contains(name)) {
+ /* first check the cache */
+ val = _TRUE_;
+ } else {
+ /* try the generated pool */
+ const GPB::DescriptorPool *pool = GPB::DescriptorPool::generated_pool();
+ if (pool->FindMessageTypeByName(name) ||
+ pool->FindEnumTypeByName(name) || pool->FindServiceByName(name) ||
+ pool->FindMethodByName(name) || pool->FindExtensionByName(name)) {
+ DescriptorPoolLookup::add(name);
+ val = _TRUE_;
+ } else {
+ /* try the runtime pool */
+ pool = DescriptorPoolLookup::pool();
+ if (pool->FindMessageTypeByName(name) ||
+ pool->FindEnumTypeByName(name) ||
+ pool->FindServiceByName(name) || pool->FindMethodByName(name) ||
+ pool->FindExtensionByName(name)) {
+ DescriptorPoolLookup::add(name);
+ val = _TRUE_;
+ }
+ }
+ }
+ tb->active = _TRUE_;
- } else{
- const GPB::FieldDescriptor* extension_desc =
- pool->FindExtensionByName( name_string ) ;
- if( extension_desc ){
- /* extension */
- DescriptorPoolLookup::add( name_string ) ;
- return S4_FieldDescriptor( extension_desc ) ;
- } else{
- const GPB::ServiceDescriptor* service_desc = pool->FindServiceByName( name_string ) ;
- if( service_desc ){
- DescriptorPoolLookup::add( name_string ) ;
- return S4_ServiceDescriptor( service_desc ) ;
- } else {
- const GPB::MethodDescriptor* method_desc = pool->FindMethodByName( name_string );
- if( method_desc ){
- DescriptorPoolLookup::add( name_string ) ;
- return S4_MethodDescriptor( method_desc );
- }
- }
- }
- }
- }
- return R_NilValue ;
+ return (val);
}
+SEXP findSomething(const GPB::DescriptorPool *pool, const char *const name) {
+ const GPB::Descriptor *desc = pool->FindMessageTypeByName(name);
+ std::string name_string(name);
+ if (desc) {
+ /* message */
+ DescriptorPoolLookup::add(name_string);
+ return S4_Descriptor(desc);
+ } else {
+ const GPB::EnumDescriptor *enum_desc =
+ pool->FindEnumTypeByName(name_string);
+ if (enum_desc) {
+ /* enum */
+ DescriptorPoolLookup::add(name_string);
+ return S4_EnumDescriptor(enum_desc);
+ } else {
+ const GPB::FieldDescriptor *extension_desc =
+ pool->FindExtensionByName(name_string);
+ if (extension_desc) {
+ /* extension */
+ DescriptorPoolLookup::add(name_string);
+ return S4_FieldDescriptor(extension_desc);
+ } else {
+ const GPB::ServiceDescriptor *service_desc =
+ pool->FindServiceByName(name_string);
+ if (service_desc) {
+ DescriptorPoolLookup::add(name_string);
+ return S4_ServiceDescriptor(service_desc);
+ } else {
+ const GPB::MethodDescriptor *method_desc =
+ pool->FindMethodByName(name_string);
+ if (method_desc) {
+ DescriptorPoolLookup::add(name_string);
+ return S4_MethodDescriptor(method_desc);
+ }
+ }
+ }
+ }
+ }
+ return R_NilValue;
+}
+
/**
* Returns a new "Descriptor" if there is such a message type
* in the descriptor pool. NULL otherwise
*
* @param name message type name (without package path)
- * @param canCache
+ * @param canCache
* @param tb lookup table
*/
-SEXP rProtoBufTable_get(const char * const name, Rboolean *canCache, R_ObjectTable *tb){
+SEXP rProtoBufTable_get(const char *const name, Rboolean *canCache,
+ R_ObjectTable *tb) {
#ifdef LOOKUP_DEBUG
- Rprintf( " >> rProtoBufTable_get\n" );
+ Rprintf(" >> rProtoBufTable_get\n");
#endif
- if(tb->active == _FALSE_)
- return(R_UnboundValue);
+ if (tb->active == _FALSE_) return (R_UnboundValue);
- tb->active = _FALSE_;
-
- SEXP res_generated ;
- SEXP res_runtime ;
- int np = 0 ;
-
- /* first try the generated pool */
- const GPB::DescriptorPool* pool = GPB::DescriptorPool::generated_pool() ;
- res_generated = PROTECT( findSomething( pool, name ) ) ;
- np++;
- if( res_generated == R_NilValue ){
- /* try the runtime pool */
- pool = DescriptorPoolLookup::pool() ;
- res_runtime = PROTECT( findSomething( pool, name ) );
- np++ ;
- }
- tb->active = _TRUE_;
- SEXP res = PROTECT( (np==2) ? res_runtime : res_generated ) ;
- UNPROTECT(np+1) ;
- if( res != R_NilValue ) return res ;
- return R_getUnboundValue() ; // -Wall
+ tb->active = _FALSE_;
+
+ SEXP res_generated;
+ SEXP res_runtime;
+ int np = 0;
+
+ /* first try the generated pool */
+ const GPB::DescriptorPool *pool = GPB::DescriptorPool::generated_pool();
+ res_generated = PROTECT(findSomething(pool, name));
+ np++;
+ if (res_generated == R_NilValue) {
+ /* try the runtime pool */
+ pool = DescriptorPoolLookup::pool();
+ res_runtime = PROTECT(findSomething(pool, name));
+ np++;
+ }
+ tb->active = _TRUE_;
+ SEXP res = PROTECT((np == 2) ? res_runtime : res_generated);
+ UNPROTECT(np + 1);
+ if (res != R_NilValue) return res;
+ return R_getUnboundValue(); // -Wall
}
/**
* Does nothing. Not applicable
*/
-int rProtoBufTable_remove(const char * const name, R_ObjectTable *tb){
+int rProtoBufTable_remove(const char *const name, R_ObjectTable *tb) {
#ifdef LOOKUP_DEBUG
- Rprintf( " >> rProtoBufTable_remove( %s) \n", name );
+ Rprintf(" >> rProtoBufTable_remove( %s) \n", name);
#endif
- Rf_error( "cannot remove from protobuf descriptor pool" ) ;
- return(0); // make -Wall happy
+ Rf_error("cannot remove from protobuf descriptor pool");
+ return (0); // make -Wall happy
}
/**
- * Indicates if R can cache the variable name.
+ * Indicates if R can cache the variable name.
* Currently allways return _FALSE_
*
* @param name message type name
* @param tb lookup table
* @return allways _FALSE_ (for now)
- */
-Rboolean rProtoBufTable_canCache(const char * const name, R_ObjectTable *tb){
+ */
+Rboolean rProtoBufTable_canCache(const char *const name, R_ObjectTable *tb) {
#ifdef LOOKUP_DEBUG
- Rprintf( " >> rProtoBufTable_canCache\n" );
+ Rprintf(" >> rProtoBufTable_canCache\n");
#endif
- return( _FALSE_ );
+ return (_FALSE_);
}
/**
@@ -177,48 +175,48 @@
* NULL to indicate assign is not possible on this lookup table
* without giving such a hard error.
*/
-SEXP rProtoBufTable_assign(const char * const name, SEXP value, R_ObjectTable *tb){
+SEXP rProtoBufTable_assign(const char *const name, SEXP value,
+ R_ObjectTable *tb) {
#ifdef LOOKUP_DEBUG
- Rprintf( " >> rProtoBufTable_assign( %s ) \n", name );
+ Rprintf(" >> rProtoBufTable_assign( %s ) \n", name);
#endif
- return(R_NilValue); // make -Wall happy
+ return (R_NilValue); // make -Wall happy
}
/**
- * Returns the list of classes known to be included in the
- * packages.
+ * Returns the list of classes known to be included in the
+ * packages.
*
* @param tb lookup table
*/
SEXP rProtoBufTable_objects(R_ObjectTable *tb) {
#ifdef LOOKUP_DEBUG
- Rprintf( " >> rProtoBufTable_objects\n" );
+ Rprintf(" >> rProtoBufTable_objects\n");
#endif
- tb->active = _FALSE_;
- SEXP objects = PROTECT( DescriptorPoolLookup::getElements() ) ;
- tb->active = _TRUE_;
- UNPROTECT(1); /* objects */
-
- return( objects );
+ tb->active = _FALSE_;
+ SEXP objects = PROTECT(DescriptorPoolLookup::getElements());
+ tb->active = _TRUE_;
+ UNPROTECT(1); /* objects */
+
+ return (objects);
}
-SEXP newProtocolBufferLookup(SEXP possexp){
+SEXP newProtocolBufferLookup(SEXP possexp) {
#ifdef LOOKUP_DEBUG
- Rprintf( "<newProtocolBufferLookup>\n" );
+ Rprintf("<newProtocolBufferLookup>\n");
#endif
R_ObjectTable *tb;
SEXP val, klass;
- tb = (R_ObjectTable *) malloc(sizeof(R_ObjectTable));
- if(!tb)
- Rf_error( "cannot allocate space for an internal R object table" );
-
- tb->type = RPROTOBUF_LOOKUP ; /* FIXME: not sure what this should be */
+ tb = (R_ObjectTable *)malloc(sizeof(R_ObjectTable));
+ if (!tb) Rf_error("cannot allocate space for an internal R object table");
+
+ tb->type = RPROTOBUF_LOOKUP; /* FIXME: not sure what this should be */
tb->cachedNames = NULL;
-
- tb->privateData = (void*)0 ;
+ tb->privateData = (void *)0;
+
tb->exists = rProtoBufTable_exists;
tb->get = rProtoBufTable_get;
tb->remove = rProtoBufTable_remove;
@@ -229,23 +227,22 @@
tb->onAttach = NULL;
tb->onDetach = NULL;
- PROTECT(val = R_MakeExternalPtr(tb, Rf_install("UserDefinedDatabase"), R_NilValue));
- PROTECT(klass = Rf_mkString( "UserDefinedDatabase" ) );
- Rf_setAttrib(val, R_ClassSymbol, klass) ;
+ PROTECT(val = R_MakeExternalPtr(tb, Rf_install("UserDefinedDatabase"),
+ R_NilValue));
+ PROTECT(klass = Rf_mkString("UserDefinedDatabase"));
+ Rf_setAttrib(val, R_ClassSymbol, klass);
UNPROTECT(2); /* val, klass */
-
+
#ifdef LOOKUP_DEBUG
- Rprintf( "</newProtocolBufferLookup>\n" );
+ Rprintf("</newProtocolBufferLookup>\n");
#endif
Rcpp::Function fun("attach");
int pos = Rcpp::as<int>(possexp);
- fun(val, Rcpp::Named("pos")=pos, Rcpp::Named("name")="RProtoBuf:DescriptorPool");
+ fun(val, Rcpp::Named("pos") = pos,
+ Rcpp::Named("name") = "RProtoBuf:DescriptorPool");
- return(val);
+ return (val);
}
-
-
-} // namespace rprotobuf
-
+} // namespace rprotobuf
Modified: pkg/src/streams.cpp
===================================================================
--- pkg/src/streams.cpp 2013-12-30 20:34:10 UTC (rev 637)
+++ pkg/src/streams.cpp 2013-12-30 20:38:38 UTC (rev 638)
@@ -6,246 +6,251 @@
/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
-namespace rprotobuf{
+namespace rprotobuf {
- void ZeroCopyInputStreamWrapper_finalizer( SEXP xp){
- delete (ZeroCopyInputStreamWrapper*)XPP(xp) ;
- }
- void ZeroCopyOutputStreamWrapper_finalizer( SEXP xp){
- delete (ZeroCopyOutputStreamWrapper*)XPP(xp) ;
- }
+void ZeroCopyInputStreamWrapper_finalizer(SEXP xp) {
+ delete (ZeroCopyInputStreamWrapper*)XPP(xp);
+}
+void ZeroCopyOutputStreamWrapper_finalizer(SEXP xp) {
+ delete (ZeroCopyOutputStreamWrapper*)XPP(xp);
+}
-
- // {{{ input streams
-
- // {{{ FileInputStream
- SEXP FileInputStream_new( SEXP filename, SEXP block_size, SEXP close_on_delete){
-
- NEW_S4_OBJECT("FileInputStream") ;
- int fd = open( CHAR(STRING_ELT(filename, 0 )), O_RDONLY | O_BINARY) ;
-
- GPB::io::FileInputStream* stream =
- new GPB::io::FileInputStream( fd, INTEGER(block_size)[0] ) ;
- stream->SetCloseOnDelete( LOGICAL(close_on_delete)[0] ) ;
- ZeroCopyInputStreamWrapper* wrapper = new ZeroCopyInputStreamWrapper(stream) ;
-
- SEXP ptr = PROTECT(
- R_MakeExternalPtr( (void*)wrapper, R_NilValue, R_NilValue));
- R_RegisterCFinalizerEx( ptr, ZeroCopyInputStreamWrapper_finalizer , _FALSE_ ) ;
- SET_SLOT( oo, Rf_install("pointer"), ptr ) ;
-
- UNPROTECT(2); /* oo, ptr */
- return oo ;
- }
- SEXP FileInputStream_GetErrno( SEXP xp ){
- GPB::io::FileInputStream* stream = GET_FIS(xp);
- return Rf_ScalarInteger( stream->GetErrno() ) ;
- }
- SEXP FileInputStream_SetCloseOnDelete( SEXP xp, SEXP close ){
- GPB::io::FileInputStream* stream = GET_FIS(xp);
- stream->SetCloseOnDelete( LOGICAL(close) ) ;
- return R_NilValue ;
- }
-
- SEXP FileInputStream_Close( SEXP xp ){
- GPB::io::FileInputStream* stream = GET_FIS(xp);
- bool res = stream->Close() ;
- return Rf_ScalarLogical( res ? _TRUE_ : _FALSE_ ) ;
- }
- // }}}
- // {{{ ConnectionInputStream
- SEXP ConnectionInputStream_new( SEXP con, SEXP was_open){
- NEW_S4_OBJECT( "ConnectionInputStream" ) ;
- ConnectionInputStream* stream =
- new ConnectionInputStream( con, (bool)LOGICAL(was_open)[0] ) ;
- ZeroCopyInputStreamWrapper* wrapper = new ZeroCopyInputStreamWrapper(stream);
- SEXP ptr = PROTECT(
- R_MakeExternalPtr( (void*)wrapper, R_NilValue, con) );
- R_RegisterCFinalizerEx( ptr, ZeroCopyInputStreamWrapper_finalizer , _FALSE_ ) ;
- SET_SLOT( oo, Rf_install("pointer"), ptr ) ;
-
- UNPROTECT(2); /* oo, ptr */
- return oo ;
- }
- // }}}
- // }}}
-
- // {{{ output streams
- // {{{ ZeroCopyOutputStream
- SEXP ZeroCopyOutputStream_Next( SEXP xp, SEXP payload){
- GPB::io::ZeroCopyOutputStream* stream = GET_ZCOS(xp) ;
- void* out ;
- int s = LENGTH(payload) ;
- bool res = stream->Next( &out, &s );
- if( !res ){
- Rf_error( "cannot write to stream" ) ;
- }
- memcpy( out, RAW(payload), s ) ;
- return Rf_ScalarInteger(s) ;
- }
-
- SEXP ZeroCopyOutputStream_ByteCount(SEXP xp){
- GPB::io::ZeroCopyOutputStream* stream = GET_ZCOS(xp);
- return( Rf_ScalarReal((double)stream->ByteCount())) ;
- }
-
- SEXP ZeroCopyOutputStream_BackUp(SEXP xp, SEXP count){
- GPB::io::ZeroCopyOutputStream* stream = GET_ZCOS(xp);
- int s = GET_int(count, 0) ;
- stream->BackUp( s ) ;
- return R_NilValue ;
- }
- // }}}
- // {{{ ArrayOutputStream
- // }}}
- // {{{ FileOutputStream
- SEXP FileOutputStream_new( SEXP filename, SEXP block_size, SEXP close_on_delete){
- NEW_S4_OBJECT( "FileOutputStream" ) ;
- int fd = open( CHAR(STRING_ELT(filename, 0 )),
- O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
-
- GPB::io::FileOutputStream* stream =
- new GPB::io::FileOutputStream( fd, INTEGER(block_size)[0] ) ;
- stream->SetCloseOnDelete( LOGICAL(close_on_delete)[0] ) ;
- ZeroCopyOutputStreamWrapper* wrapper = new ZeroCopyOutputStreamWrapper(stream) ;
-
- SEXP ptr = PROTECT(
- R_MakeExternalPtr( (void*)wrapper, R_NilValue, R_NilValue));
- R_RegisterCFinalizerEx( ptr, ZeroCopyOutputStreamWrapper_finalizer , _FALSE_ ) ;
- SET_SLOT( oo, Rf_install("pointer"), ptr ) ;
-
- UNPROTECT(2); /* oo, ptr */
- return oo ;
- }
- SEXP FileOutputStream_Flush( SEXP xp ){
- GPB::io::FileOutputStream* stream = GET_FOS(xp);
- bool res = stream->Flush() ;
- return Rf_ScalarLogical( res ? _TRUE_ : _FALSE_ ) ;
- }
- SEXP FileOutputStream_Close( SEXP xp ){
- GPB::io::FileOutputStream* stream = GET_FOS(xp);
- bool res = stream->Close() ;
- return Rf_ScalarLogical( res ? _TRUE_ : _FALSE_ ) ;
- }
- SEXP FileOutputStream_GetErrno( SEXP xp ){
- GPB::io::FileOutputStream* stream = GET_FOS(xp);
- return Rf_ScalarInteger( stream->GetErrno() ) ;
- }
- SEXP FileOutputStream_SetCloseOnDelete( SEXP xp, SEXP close ){
- GPB::io::FileOutputStream* stream = GET_FOS(xp);
- stream->SetCloseOnDelete( LOGICAL(close) ) ;
- return R_NilValue ;
- }
- // }}}
- // {{{ ConnectionOutputStream
- SEXP ConnectionOutputStream_new( SEXP con, SEXP was_open){
- NEW_S4_OBJECT( "ConnectionOutputStream" ) ;
- ConnectionOutputStream* stream =
- new ConnectionOutputStream( con, (bool)LOGICAL(was_open)[0] ) ;
- ZeroCopyOutputStreamWrapper* wrapper = new ZeroCopyOutputStreamWrapper(stream) ;
- /* we keep the R connection protected as long as the
- external pointer is kept out of GC */
- SEXP ptr = PROTECT( R_MakeExternalPtr( (void*)wrapper, R_NilValue, con) );
- R_RegisterCFinalizerEx( ptr, ZeroCopyOutputStreamWrapper_finalizer , _FALSE_ ) ;
- SET_SLOT( oo, Rf_install("pointer"), ptr ) ;
-
- UNPROTECT(2); /* oo, ptr */
- return oo ;
- }
- // }}}
-
- // }}}
-
- // {{{ Read*** functions using CodedInputStream
- SEXP ZeroCopyInputStream_ReadRaw( SEXP xp, SEXP size){
- GPB::io::CodedInputStream* coded_stream = GET_CIS(xp) ;
- int s = INTEGER(size)[0] ;
- SEXP payload = PROTECT( Rf_allocVector(RAWSXP, s) ) ;
- if( !coded_stream->ReadRaw( RAW(payload), s ) ) Rf_error("error reading raw bytes") ;
- UNPROTECT(1) ; /* payload */
- return payload;
- }
-
- SEXP ZeroCopyInputStream_ReadString( SEXP xp, SEXP size){
- GPB::io::CodedInputStream* coded_stream = GET_CIS(xp) ;
- int s = INTEGER(size)[0] ;
- std::string buffer("") ;
- if( !coded_stream->ReadString( &buffer, s ) ) Rf_error( "error reading string" ) ;
-
- return Rf_mkString( buffer.c_str() ) ;
- }
-
- SEXP ZeroCopyInputStream_ReadVarint32( SEXP xp){
- GPB::io::CodedInputStream* coded_stream = GET_CIS(xp) ;
- uint32 res = 0 ;
- if( !coded_stream->ReadVarint32( &res ) ) Rf_error( "error reading varint32" ) ;
- return Rf_ScalarInteger( res ) ;
- }
-
- SEXP ZeroCopyInputStream_ReadLittleEndian32( SEXP xp){
- GPB::io::CodedInputStream* coded_stream = GET_CIS(xp) ;
- uint32 res = 0 ;
- if( !coded_stream->ReadVarint32( &res ) ) Rf_error( "error reading little endian int32" ) ;
- return Rf_ScalarInteger( res ) ;
- }
-
- SEXP ZeroCopyInputStream_ReadLittleEndian64( SEXP xp){
- GPB::io::CodedInputStream* coded_stream = GET_CIS(xp) ;
- uint64 res = 0 ;
- if( !coded_stream->ReadVarint64( &res ) ) Rf_error( "error reading little endian int32" ) ;
- return Rf_ScalarReal( (double)res ) ;
- }
-
- SEXP ZeroCopyInputStream_ReadVarint64( SEXP xp){
- GPB::io::CodedInputStream* coded_stream = GET_CIS(xp) ;
- uint64 res = 0 ;
- if( !coded_stream->ReadVarint64( &res ) ) Rf_error( "error reading varint64" ) ;
- return Rf_ScalarReal( (double)res ) ;
- }
- // }}}
-
- // {{{ Write*** functions using CodedOuputStream
- SEXP ZeroCopyOutputStream_WriteRaw( SEXP xp, SEXP payload){
- GPB::io::CodedOutputStream* stream = GET_COS(xp) ;
- stream->WriteRaw( RAW(payload), LENGTH(payload) ) ;
- return R_NilValue ;
- }
- SEXP ZeroCopyOutputStream_WriteString( SEXP xp, SEXP payload){
- if( LENGTH( payload ) > 1 ){
- Rf_warning( "only the first element is used" ) ;
- }
- if( LENGTH( payload ) == 0 ){
- Rf_error( "need at least one element" ) ;
- }
- GPB::io::CodedOutputStream* stream = GET_COS(xp) ;
- stream->WriteString( CHAR(STRING_ELT(payload,0) ) );
- return R_NilValue ;
- }
-
- SEXP ZeroCopyOutputStream_WriteLittleEndian32( SEXP xp, SEXP payload ){
- GPB::io::CodedOutputStream* stream = GET_COS(xp) ;
- stream->WriteLittleEndian32( GET_int32(payload,0) );
- return R_NilValue ;
- }
-
- SEXP ZeroCopyOutputStream_WriteLittleEndian64( SEXP xp, SEXP payload ){
- GPB::io::CodedOutputStream* stream = GET_COS(xp) ;
- stream->WriteLittleEndian64( GET_int64(payload,0) );
- return R_NilValue ;
- }
-
- SEXP ZeroCopyOutputStream_WriteVarint32( SEXP xp, SEXP payload ){
- GPB::io::CodedOutputStream* stream = GET_COS(xp) ;
- stream->WriteVarint32( GET_int32(payload,0) );
- return R_NilValue ;
- }
-
- SEXP ZeroCopyOutputStream_WriteVarint64( SEXP xp, SEXP payload ){
- GPB::io::CodedOutputStream* stream = GET_COS(xp) ;
- stream->WriteVarint64( GET_int64(payload,0) );
- return R_NilValue ;
- }
- // }}}
-
-} // namespace rprotobuf
+// {{{ input streams
+// {{{ FileInputStream
+SEXP FileInputStream_new(SEXP filename, SEXP block_size, SEXP close_on_delete) {
+
+ NEW_S4_OBJECT("FileInputStream");
+ int fd = open(CHAR(STRING_ELT(filename, 0)), O_RDONLY | O_BINARY);
+
+ GPB::io::FileInputStream* stream =
+ new GPB::io::FileInputStream(fd, INTEGER(block_size)[0]);
+ stream->SetCloseOnDelete(LOGICAL(close_on_delete)[0]);
+ ZeroCopyInputStreamWrapper* wrapper =
+ new ZeroCopyInputStreamWrapper(stream);
+
+ SEXP ptr =
+ PROTECT(R_MakeExternalPtr((void*)wrapper, R_NilValue, R_NilValue));
+ R_RegisterCFinalizerEx(ptr, ZeroCopyInputStreamWrapper_finalizer, _FALSE_);
+ SET_SLOT(oo, Rf_install("pointer"), ptr);
+
+ UNPROTECT(2); /* oo, ptr */
+ return oo;
+}
+SEXP FileInputStream_GetErrno(SEXP xp) {
+ GPB::io::FileInputStream* stream = GET_FIS(xp);
+ return Rf_ScalarInteger(stream->GetErrno());
+}
+SEXP FileInputStream_SetCloseOnDelete(SEXP xp, SEXP close) {
+ GPB::io::FileInputStream* stream = GET_FIS(xp);
+ stream->SetCloseOnDelete(LOGICAL(close));
+ return R_NilValue;
+}
+
+SEXP FileInputStream_Close(SEXP xp) {
+ GPB::io::FileInputStream* stream = GET_FIS(xp);
+ bool res = stream->Close();
+ return Rf_ScalarLogical(res ? _TRUE_ : _FALSE_);
+}
+// }}}
+// {{{ ConnectionInputStream
+SEXP ConnectionInputStream_new(SEXP con, SEXP was_open) {
+ NEW_S4_OBJECT("ConnectionInputStream");
+ ConnectionInputStream* stream =
+ new ConnectionInputStream(con, (bool)LOGICAL(was_open)[0]);
+ ZeroCopyInputStreamWrapper* wrapper =
+ new ZeroCopyInputStreamWrapper(stream);
+ SEXP ptr = PROTECT(R_MakeExternalPtr((void*)wrapper, R_NilValue, con));
+ R_RegisterCFinalizerEx(ptr, ZeroCopyInputStreamWrapper_finalizer, _FALSE_);
+ SET_SLOT(oo, Rf_install("pointer"), ptr);
+
+ UNPROTECT(2); /* oo, ptr */
+ return oo;
+}
+// }}}
+// }}}
+
+// {{{ output streams
+// {{{ ZeroCopyOutputStream
+SEXP ZeroCopyOutputStream_Next(SEXP xp, SEXP payload) {
+ GPB::io::ZeroCopyOutputStream* stream = GET_ZCOS(xp);
+ void* out;
+ int s = LENGTH(payload);
+ bool res = stream->Next(&out, &s);
+ if (!res) {
+ Rf_error("cannot write to stream");
+ }
+ memcpy(out, RAW(payload), s);
+ return Rf_ScalarInteger(s);
+}
+
+SEXP ZeroCopyOutputStream_ByteCount(SEXP xp) {
+ GPB::io::ZeroCopyOutputStream* stream = GET_ZCOS(xp);
+ return (Rf_ScalarReal((double)stream->ByteCount()));
+}
+
+SEXP ZeroCopyOutputStream_BackUp(SEXP xp, SEXP count) {
+ GPB::io::ZeroCopyOutputStream* stream = GET_ZCOS(xp);
+ int s = GET_int(count, 0);
+ stream->BackUp(s);
+ return R_NilValue;
+}
+// }}}
+// {{{ ArrayOutputStream
+// }}}
+// {{{ FileOutputStream
+SEXP FileOutputStream_new(SEXP filename, SEXP block_size,
+ SEXP close_on_delete) {
+ NEW_S4_OBJECT("FileOutputStream");
+ int fd = open(CHAR(STRING_ELT(filename, 0)),
+ O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
+
+ GPB::io::FileOutputStream* stream =
+ new GPB::io::FileOutputStream(fd, INTEGER(block_size)[0]);
+ stream->SetCloseOnDelete(LOGICAL(close_on_delete)[0]);
+ ZeroCopyOutputStreamWrapper* wrapper =
+ new ZeroCopyOutputStreamWrapper(stream);
+
+ SEXP ptr =
+ PROTECT(R_MakeExternalPtr((void*)wrapper, R_NilValue, R_NilValue));
+ R_RegisterCFinalizerEx(ptr, ZeroCopyOutputStreamWrapper_finalizer, _FALSE_);
+ SET_SLOT(oo, Rf_install("pointer"), ptr);
+
+ UNPROTECT(2); /* oo, ptr */
+ return oo;
+}
+SEXP FileOutputStream_Flush(SEXP xp) {
+ GPB::io::FileOutputStream* stream = GET_FOS(xp);
+ bool res = stream->Flush();
+ return Rf_ScalarLogical(res ? _TRUE_ : _FALSE_);
+}
+SEXP FileOutputStream_Close(SEXP xp) {
+ GPB::io::FileOutputStream* stream = GET_FOS(xp);
+ bool res = stream->Close();
+ return Rf_ScalarLogical(res ? _TRUE_ : _FALSE_);
+}
+SEXP FileOutputStream_GetErrno(SEXP xp) {
+ GPB::io::FileOutputStream* stream = GET_FOS(xp);
+ return Rf_ScalarInteger(stream->GetErrno());
+}
+SEXP FileOutputStream_SetCloseOnDelete(SEXP xp, SEXP close) {
+ GPB::io::FileOutputStream* stream = GET_FOS(xp);
+ stream->SetCloseOnDelete(LOGICAL(close));
+ return R_NilValue;
+}
+// }}}
+// {{{ ConnectionOutputStream
+SEXP ConnectionOutputStream_new(SEXP con, SEXP was_open) {
+ NEW_S4_OBJECT("ConnectionOutputStream");
+ ConnectionOutputStream* stream =
+ new ConnectionOutputStream(con, (bool)LOGICAL(was_open)[0]);
+ ZeroCopyOutputStreamWrapper* wrapper =
+ new ZeroCopyOutputStreamWrapper(stream);
+ /* we keep the R connection protected as long as the
+ external pointer is kept out of GC */
+ SEXP ptr = PROTECT(R_MakeExternalPtr((void*)wrapper, R_NilValue, con));
+ R_RegisterCFinalizerEx(ptr, ZeroCopyOutputStreamWrapper_finalizer, _FALSE_);
+ SET_SLOT(oo, Rf_install("pointer"), ptr);
+
+ UNPROTECT(2); /* oo, ptr */
+ return oo;
+}
+// }}}
+
+// }}}
+
+// {{{ Read*** functions using CodedInputStream
+SEXP ZeroCopyInputStream_ReadRaw(SEXP xp, SEXP size) {
+ GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
+ int s = INTEGER(size)[0];
+ SEXP payload = PROTECT(Rf_allocVector(RAWSXP, s));
+ if (!coded_stream->ReadRaw(RAW(payload), s))
+ Rf_error("error reading raw bytes");
+ UNPROTECT(1); /* payload */
+ return payload;
+}
+
+SEXP ZeroCopyInputStream_ReadString(SEXP xp, SEXP size) {
+ GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
+ int s = INTEGER(size)[0];
+ std::string buffer("");
+ if (!coded_stream->ReadString(&buffer, s)) Rf_error("error reading string");
+
+ return Rf_mkString(buffer.c_str());
+}
+
+SEXP ZeroCopyInputStream_ReadVarint32(SEXP xp) {
+ GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
+ uint32 res = 0;
+ if (!coded_stream->ReadVarint32(&res)) Rf_error("error reading varint32");
+ return Rf_ScalarInteger(res);
+}
+
+SEXP ZeroCopyInputStream_ReadLittleEndian32(SEXP xp) {
+ GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
+ uint32 res = 0;
+ if (!coded_stream->ReadVarint32(&res))
+ Rf_error("error reading little endian int32");
+ return Rf_ScalarInteger(res);
+}
+
+SEXP ZeroCopyInputStream_ReadLittleEndian64(SEXP xp) {
+ GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
+ uint64 res = 0;
+ if (!coded_stream->ReadVarint64(&res))
+ Rf_error("error reading little endian int32");
+ return Rf_ScalarReal((double)res);
+}
+
+SEXP ZeroCopyInputStream_ReadVarint64(SEXP xp) {
+ GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
+ uint64 res = 0;
+ if (!coded_stream->ReadVarint64(&res)) Rf_error("error reading varint64");
+ return Rf_ScalarReal((double)res);
+}
+// }}}
+
+// {{{ Write*** functions using CodedOuputStream
+SEXP ZeroCopyOutputStream_WriteRaw(SEXP xp, SEXP payload) {
+ GPB::io::CodedOutputStream* stream = GET_COS(xp);
+ stream->WriteRaw(RAW(payload), LENGTH(payload));
+ return R_NilValue;
+}
+SEXP ZeroCopyOutputStream_WriteString(SEXP xp, SEXP payload) {
+ if (LENGTH(payload) > 1) {
+ Rf_warning("only the first element is used");
+ }
+ if (LENGTH(payload) == 0) {
+ Rf_error("need at least one element");
+ }
+ GPB::io::CodedOutputStream* stream = GET_COS(xp);
+ stream->WriteString(CHAR(STRING_ELT(payload, 0)));
+ return R_NilValue;
+}
+
+SEXP ZeroCopyOutputStream_WriteLittleEndian32(SEXP xp, SEXP payload) {
+ GPB::io::CodedOutputStream* stream = GET_COS(xp);
+ stream->WriteLittleEndian32(GET_int32(payload, 0));
+ return R_NilValue;
+}
+
+SEXP ZeroCopyOutputStream_WriteLittleEndian64(SEXP xp, SEXP payload) {
+ GPB::io::CodedOutputStream* stream = GET_COS(xp);
+ stream->WriteLittleEndian64(GET_int64(payload, 0));
+ return R_NilValue;
+}
+
+SEXP ZeroCopyOutputStream_WriteVarint32(SEXP xp, SEXP payload) {
+ GPB::io::CodedOutputStream* stream = GET_COS(xp);
+ stream->WriteVarint32(GET_int32(payload, 0));
+ return R_NilValue;
+}
+
+SEXP ZeroCopyOutputStream_WriteVarint64(SEXP xp, SEXP payload) {
+ GPB::io::CodedOutputStream* stream = GET_COS(xp);
+ stream->WriteVarint64(GET_int64(payload, 0));
+ return R_NilValue;
+}
+// }}}
+
+} // namespace rprotobuf
More information about the Rprotobuf-commits
mailing list