Ignore:
Timestamp:
Apr 18, 2013 6:20:13 PM (11 years ago)
Author:
gah
Message:

add fix from release branch: matlab rpLibGetString doesn't handle binary strings (with NULs)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lang/matlab/rpLibGetString.cc

    r3177 r3634  
    2828 */
    2929
    30 void mexFunction(int nlhs, mxArray *plhs[],
    31                  int nrhs, const mxArray *prhs[])
     30
     31void
     32mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    3233{
    33     int         libIndex    = 0;
    34     int         retLibIndex = 0;
    35     int         err         = 1;
    36     RpLibrary*  lib         = NULL;
    37     std::string path        = "";
    38     std::string retStr      = "";
     34    int libIndex;
     35    int err = 1;
     36    std::string path;
    3937
    4038    /* Check for proper number of arguments. */
    4139    if (nrhs != 2) {
    42         mexErrMsgTxt("Two input required.");
     40        mexErrMsgTxt("wrong # of arguments: "
     41                     "should be rpLibGetString(lib, path)");
    4342    }
    4443
     
    4847    /* Call the C subroutine. */
    4948    if ( (libIndex > 0) && (!path.empty()) ) {
    50         lib = (RpLibrary*) getObject_Void(libIndex);
     49        RpLibrary*  lib;
    5150
    52         if (lib) {
    53             retStr = lib->getString(path);
    54             err = 0;
    55         }
     51        lib = (RpLibrary*)getObject_Void(libIndex);
     52        if (lib != NULL) {
     53            std::string contents;
     54            contents = lib->getString(path);
     55
     56            mwSize dims[2];
     57            dims[0] = 1;                        /* Treat as a row vector. */
     58            dims[1] = contents.length();
     59            plhs[0] = mxCreateCharArray(2, dims);
     60           
     61            /* Copy character array (may include NULs) into the matlab
     62             * array. */
     63            const char *src = contents.c_str();
     64            mxChar *dest = (mxChar *)mxGetPr(plhs[0]);
     65            for (int i = 0; i < contents.length(); i++) {
     66                dest[i] = src[i];
     67            }
     68            plhs[1] = mxCreateDoubleScalar(0);
     69            return;
     70        }
    5671    }
    57 
    5872    /* Set C-style string output_buf to MATLAB mexFunction output*/
    59     plhs[0] = mxCreateString(retStr.c_str());
     73    plhs[0] = mxCreateString("");
    6074    plhs[1] = mxCreateDoubleScalar(err);
    61 
    6275    return;
    6376}
Note: See TracChangeset for help on using the changeset viewer.