Scilab: refactor SwigScilabStringToCharPtrAndSize (use String API getAllocatedString)

This commit is contained in:
Simon Marchetto 2013-06-18 16:43:16 +02:00
commit 347dee5cda

View file

@ -123,57 +123,33 @@ SwigScilabStringToCharPtr(void *_pvApiCtx, int _iVar, char *_pcValue, int _iLeng
SWIGINTERN int SWIGINTERN int
SwigScilabStringToCharPtrAndSize(void *_pvApiCtx, int _iVar, char **_pcValue, size_t *_piLength, int *alloc, char *_fname) { SwigScilabStringToCharPtrAndSize(void *_pvApiCtx, int _iVar, char **_pcValue, size_t *_piLength, int *alloc, char *_fname) {
SciErr sciErr; SciErr sciErr;
int iRows = 0;
int iCols = 0;
int iType = 0;
int *piAddrVar = NULL; int *piAddrVar = NULL;
char *_pstStrings = NULL; int iRet;
int piLength = 0; char *pstStrings = NULL;
sciErr = getVarAddressFromPosition(pvApiCtx, _iVar, &piAddrVar); sciErr = getVarAddressFromPosition(_pvApiCtx, _iVar, &piAddrVar);
if (sciErr.iErr) { if (sciErr.iErr) {
printError(&sciErr, 0); printError(&sciErr, 0);
return SWIG_ERROR; return SWIG_ERROR;
} }
sciErr = getVarType(pvApiCtx, piAddrVar, &iType); iRet = getAllocatedSingleString(_pvApiCtx, piAddrVar, &pstStrings);
if (sciErr.iErr) { if (iRet) {
printError(&sciErr, 0);
return SWIG_ERROR;
}
if (iType != sci_strings) {
Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), _fname, _iVar);
return SWIG_ERROR; return SWIG_ERROR;
} }
sciErr = getMatrixOfString(pvApiCtx, piAddrVar, &iRows, &iCols, &piLength, NULL); // TODO: return SWIG_ERROR if _pcValue NULL (now returning SWIG_ERROR fails some typechecks)
if (sciErr.iErr) { if (_pcValue)
printError(&sciErr, 0); {
return SWIG_ERROR; *_pcValue = pstStrings;
}
if (iRows * iCols != 1) {
Scierror(999, _("%s: Wrong size for input argument #%d: A string expected.\n"), _fname, _iVar);
return SWIG_ERROR;
} }
_pstStrings = (char *)malloc(sizeof(char) * (piLength + 1)); if (alloc != NULL) {
sciErr = getMatrixOfString(pvApiCtx, piAddrVar, &iRows, &iCols, &piLength, (char **)&_pstStrings);
if (sciErr.iErr) {
printError(&sciErr, 0);
return SWIG_ERROR;
}
if (alloc) {
*_pcValue = %new_copy_array(_pstStrings, piLength + 1, char);
*alloc = SWIG_NEWOBJ; *alloc = SWIG_NEWOBJ;
} else if (_pcValue) {
*_pcValue = _pstStrings;
} }
free(_pstStrings);
if (_piLength != NULL) { if (_piLength != NULL) {
*_piLength = (size_t) piLength; *_piLength = strlen(*_pcValue);
} }
return SWIG_OK; return SWIG_OK;