[Rinside-commits] r99 - in pkg: inst src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Feb 12 21:04:49 CET 2010


Author: romain
Date: 2010-02-12 21:04:48 +0100 (Fri, 12 Feb 2010)
New Revision: 99

Modified:
   pkg/inst/ChangeLog
   pkg/src/MemBuf.cpp
   pkg/src/MemBuf.h
   pkg/src/RInside.cpp
Log:
simplify MemBuf using std::string

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-02-12 14:10:46 UTC (rev 98)
+++ pkg/inst/ChangeLog	2010-02-12 20:04:48 UTC (rev 99)
@@ -1,5 +1,7 @@
 2010-02-12  Romain Francois <romain at r-enthusiasts.com>
 
+	* src/MemBuf.h: MemBuf simplified using std::string
+
 	* src/RInside.h: RInside gains an operator[](string) to allow
 	treating the RInside instance as a proxy to the global environment
 	so that we can do: RInside R; R["x"] = 10 ; All the actual work

Modified: pkg/src/MemBuf.cpp
===================================================================
--- pkg/src/MemBuf.cpp	2010-02-12 14:10:46 UTC (rev 98)
+++ pkg/src/MemBuf.cpp	2010-02-12 20:04:48 UTC (rev 99)
@@ -28,55 +28,27 @@
 extern bool verbose;
 extern const char *programName;
 
-MemBuf::~MemBuf() {
-    if (verbose) std::cout << "MemBuf::dtor BEGIN" << std::endl;
-    free(p_m);
-    if (verbose) std::cout << "MemBuf::dtor END" << std::endl;
-}
+MemBuf::~MemBuf() {}
 
-MemBuf::MemBuf(int sizebytes) {
+MemBuf::MemBuf(int sizebytes) : buffer() {
     if (verbose) std::cout << "MemBuf::ctor BEGIN" << std::endl;
-    p_m = (membuf_st*) malloc(sizebytes+sizeof(struct membuf_st));
-
-    if (p_m == NULL) {
-	std::cerr << programName << ": MemBuf ctor failed! Exiting!!!\n\n" << std::endl;
-	exit(1);
-	//return NULL; /* unreached */
-    }
-
-    p_m->size = sizebytes;
-    p_m->count = 0;
-    p_m->buf = (unsigned char *)p_m+sizeof(struct membuf_st);
-    
+    buffer.reserve(sizebytes) ;
     if (verbose) std::cout << "MemBuf::ctor END" << std::endl;
 }
 
 void MemBuf::resize() {		// Use power of 2 resizing 
-    membuf_t lb = p_m;
-    lb = p_m = (membuf_st*) realloc(lb, lb->size*2+sizeof(struct membuf_st));
-    if (lb == NULL) {
-	std::cerr << programName << ": MemBuff::resize() failed! Exiting!!!\n\n" << std::endl;
-	exit(1);
-    }
-    lb->size =  lb->size * 2;
-    lb->buf = (unsigned char *)lb+sizeof(struct membuf_st);
+    buffer.reserve( 2*buffer.capacity() ) ;
 }
 
 void MemBuf::rewind(){
-    p_m->count = 0;
+    buffer.clear() ;
 }
 
 void MemBuf::add(char *buf){
-    membuf_t mb = p_m;
     int buflen = strlen(buf);
-
-    while ((buflen + (mb->count)) >= mb->size) {
+    while ( ( buflen + buffer.size() ) >= buffer.capacity() ) {
 	resize();
-	mb = p_m;
     }
-
-    memcpy(mb->buf + mb->count, buf, buflen);
-    mb->buf[mb->count+buflen] = '\0';
-    mb->count += buflen;
+    buffer += buf ;
 }
 

Modified: pkg/src/MemBuf.h
===================================================================
--- pkg/src/MemBuf.h	2010-02-12 14:10:46 UTC (rev 98)
+++ pkg/src/MemBuf.h	2010-02-12 20:04:48 UTC (rev 99)
@@ -2,7 +2,8 @@
 //
 // MemBuf.h: R/C++ interface class library -- Easier R embedding into C++
 //
-// Copyright (C) 2009 - 2010 Dirk Eddelbuettel
+// Copyright (C) 2009 Dirk Eddelbuettel
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of RInside.
 //
@@ -21,17 +22,13 @@
 
 class MemBuf {			// simple C++-ification of littler's membuf
 private:
-    typedef struct membuf_st {
-	int size;
-	int count;
-	unsigned char *buf;
-    } *membuf_t;
-    membuf_t p_m;
+    std::string buffer ;
+    
 public:    
     MemBuf(int sizebytes=1024);
     ~MemBuf();
     void resize();
     void rewind();
     void add(char *buf);
-    unsigned char* getBufPtr() { return p_m->buf; };
+    const char* getBufPtr() { return buffer.c_str() ; };
 };

Modified: pkg/src/RInside.cpp
===================================================================
--- pkg/src/RInside.cpp	2010-02-12 14:10:46 UTC (rev 98)
+++ pkg/src/RInside.cpp	2010-02-12 20:04:48 UTC (rev 99)
@@ -239,7 +239,7 @@
     mb_m.add((char*)line.c_str());
     
     PROTECT(cmdSexp = Rf_allocVector(STRSXP, 1));
-    SET_STRING_ELT(cmdSexp, 0, Rf_mkChar((char*)mb_m.getBufPtr()));
+    SET_STRING_ELT(cmdSexp, 0, Rf_mkChar(mb_m.getBufPtr()));
 
     cmdexpr = PROTECT(R_ParseVector(cmdSexp, -1, &status, R_NilValue));
 



More information about the Rinside-commits mailing list