RInside Version 0.2.6
src/setenv/setenv.c
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 //
00003 // RInside.cpp: R/C++ interface class library -- Easier R embedding into C++
00004 //
00005 // Copyright (C) 2009 - 2010 Dirk Eddelbuettel and Richard Holbrey
00006 // Copyright (C) 2012        Dirk Eddelbuettel and Romain Francois
00007 //
00008 // This file is part of RInside.
00009 //
00010 // RInside is free software: you can redistribute it and/or modify it
00011 // under the terms of the GNU General Public License as published by
00012 // the Free Software Foundation, either version 2 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // RInside is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public License
00021 // along with RInside.  If not, see <http://www.gnu.org/licenses/>.
00022 
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 
00027 // borrowed from Renviron.c
00028 extern "C" int setenv(const char *env_var, const char *env_val, int dummy) {
00029     char *buf, *value, *p, *q, *a, *b, quote='\0';
00030     int inquote = 0;
00031 
00032     //make non-const copies
00033     a = (char *) malloc((strlen(env_var) + 1) * sizeof(char));
00034     b = (char *) malloc((strlen(env_val) + 1) * sizeof(char));
00035     if (!a || !b) {
00036         Rf_error("memory allocation failure in setenv");
00037     }
00038     strcpy(a, env_var);
00039     strcpy(b, env_val);
00040 
00041     buf = (char *) malloc((strlen(a) + strlen(b) + 2) * sizeof(char));
00042     if (!buf) { 
00043         Rf_error("memory allocation failure in setenv");
00044     }
00045     strcpy(buf, a); strcat(buf, "=");
00046     value = buf+strlen(buf);
00047 
00048     /* now process the value */
00049     for(p = b, q = value; *p; p++) {
00050         /* remove quotes around sections, preserve \ inside quotes */
00051         if(!inquote && (*p == '"' || *p == '\'')) {
00052             inquote = 1;
00053             quote = *p;
00054             continue;
00055         }
00056 
00057         if(inquote && *p == quote && *(p-1) != '\\') {
00058             inquote = 0;
00059             continue;
00060         }
00061 
00062         if(!inquote && *p == '\\') {
00063             if(*(p+1) == '\n') p++;
00064             else if(*(p+1) == '\\') *q++ = *p;
00065             continue;
00066         }
00067 
00068         if(inquote && *p == '\\' && *(p+1) == quote)
00069             continue;
00070         *q++ = *p;
00071     }
00072     *q = '\0';
00073     //if (putenv(buf)) 
00074         //warningcall(R_NilValue, _("problem in setting variable '%s' in Renviron"), a);
00075     return putenv(buf);
00076 
00077     /* no free here: storage remains in use */
00078 }
00079 
 All Classes Files Functions Variables Enumerations Enumerator Defines