/*--------------------------------------------------------------------------*/ int getAllocatedNamedSingleString(void* _pvCtx, const char* _pstName, char** _pstData) { SciErr sciErr = sciErrInit(); int iRows = 0; int iCols = 0; int iLen = 0; if (isNamedScalar(_pvCtx, _pstName) == 0 || isNamedStringType(_pvCtx, _pstName) == 0) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_SINGLE_STRING, _("%s: Wrong type for input argument \"%s\": A single string expected.\n"), "getAllocatedNamedSingleString", _pstName); printError(&sciErr, 0); return sciErr.iErr; } sciErr = readNamedMatrixOfString(_pvCtx, _pstName, &iRows, &iCols, &iLen, NULL); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_SINGLE_STRING, _("%s: Unable to get argument data"), "getAllocatedNamedSingleString"); printError(&sciErr, 0); return sciErr.iErr; } *_pstData = (char*)MALLOC(sizeof(char) * (iLen + 1)); //+1 for null termination sciErr = readNamedMatrixOfString(_pvCtx, _pstName, &iRows, &iCols, &iLen, _pstData); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_SINGLE_STRING, _("%s: Unable to get argument data"), "getAllocatedNamedSingleString"); printError(&sciErr, 0); FREE(*_pstData); return sciErr.iErr; } return 0; }
/*--------------------------------------------------------------------------*/ int getAllocatedNamedMatrixOfString(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, char*** _pstData) { SciErr sciErr = readNamedMatrixOfString(_pvCtx, _pstName, _piRows, _piCols, NULL, NULL); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_STRING_MATRIX, _("%s: Unable to get argument data"), "getAllocatedNamedMatrixOfString"); printError(&sciErr, 0); return sciErr.iErr; } int* piLen = (int*)MALLOC(sizeof(int) **_piRows **_piCols); sciErr = readNamedMatrixOfString(_pvCtx, _pstName, _piRows, _piCols, piLen, NULL); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_STRING_MATRIX, _("%s: Unable to get argument data"), "getAllocatedNamedMatrixOfString"); if (piLen) { FREE(piLen); piLen = NULL; } printError(&sciErr, 0); return sciErr.iErr; } *_pstData = (char**)MALLOC(sizeof(char*) **_piRows **_piCols); for (int i = 0 ; i < *_piRows **_piCols ; i++) { (*_pstData)[i] = (char*)MALLOC(sizeof(char) * (piLen[i] + 1));//+1 for null termination } sciErr = readNamedMatrixOfString(_pvCtx, _pstName, _piRows, _piCols, piLen, *_pstData); if (piLen) { FREE(piLen); piLen = NULL; } if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_STRING_MATRIX, _("%s: Unable to get argument data"), "getAllocatedNamedMatrixOfString"); printError(&sciErr, 0); for (int i = 0 ; i < *_piRows **_piCols ; i++) { FREE((*_pstData)[i]); } FREE(*_pstData); return sciErr.iErr; } return 0; }
/*------------------------------------------------------------*/ int main(void) { #ifdef _MSC_VER if ( StartScilab(NULL, NULL, 0) == FALSE ) #else if ( StartScilab(getenv("SCI"), NULL, 0) == FALSE ) #endif { fprintf(stderr, "Error while calling StartScilab\n"); return -1; } /******************************** WRITE ****************************/ /* * Write a single string into Scilab * A="my Message"; */ { SciErr sciErr; int row = 2, col = 1; /* Size of the matrix */ /* Declare the string */ char **myMatrixOfString = (char**)malloc(sizeof(char*) * row * col); char variableName[] = "A"; myMatrixOfString[0] = "my Message"; myMatrixOfString[1] = "on two lines"; /* Write it into Scilab's memory */ sciErr = createNamedMatrixOfString(pvApiCtx, variableName, row, col, myMatrixOfString); if (sciErr.iErr) { printError(&sciErr, 0); } printf("Display from Scilab of A:\n"); SendScilabJob("disp(A);"); /* Display A */ } { /* * Write a matrix into Scilab * B=['My' 'Great' ; * 'String' ';)' ] * Note that it is done column by column */ int row = 2, col = 2; /* Size of the matrix */ /* Declare the string */ char **myMatrixOfStringB = (char**)malloc(sizeof(char*) * row * col); char variableNameB[] = "B"; SciErr sciErr; printf("\n"); myMatrixOfStringB[0] = "My"; myMatrixOfStringB[1] = "String"; myMatrixOfStringB[2] = "Great"; myMatrixOfStringB[3] = ";)"; sciErr = createNamedMatrixOfString(pvApiCtx, variableNameB, row, col, myMatrixOfStringB); if (sciErr.iErr) { printError(&sciErr, 0); } printf("\n"); printf("Display from Scilab of B:\n"); SendScilabJob("disp(B);"); /* Display B */ } /******************************** READ ****************************/ /* Load the previously set variable A */ { char variableToBeRetrieved[] = "A"; int iRows = 0; int iCols = 0; int i, j; int* piAddr = NULL; int* piLen = NULL; char** pstData = NULL; SciErr sciErr; //first call to retrieve dimensions sciErr = readNamedMatrixOfString(pvApiCtx, variableToBeRetrieved, &iRows, &iCols, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); } piLen = (int*)malloc(sizeof(int) * iRows * iCols); //second call to retrieve length of each string sciErr = readNamedMatrixOfString(pvApiCtx, variableToBeRetrieved, &iRows, &iCols, piLen, NULL); if (sciErr.iErr) { printError(&sciErr, 0); } pstData = (char**)malloc(sizeof(char*) * iRows * iCols); for (i = 0 ; i < iRows * iCols ; i++) { pstData[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination } //third call to retrieve data sciErr = readNamedMatrixOfString(pvApiCtx, variableToBeRetrieved, &iRows, &iCols, piLen, pstData); if (sciErr.iErr) { printError(&sciErr, 0); } printf("\n"); printf("Load and display of A:\n"); for (j = 0 ; j < iCols ; j++) { for (i = 0 ; i < iRows ; i++) { /* Display the formated matrix with same scilab indice */ printf("[%d,%d] = %s\n", j + 1, i + 1, pstData[j * iRows + i]); } } printf("\n"); free(piLen); for (i = 0 ; i < iRows * iCols ; i++) { free(pstData[i]); } free(pstData); } /* Load an element of a the previously set variable B */ { char variableToBeRetrieved[] = "B"; int iRows = 0; int iCols = 0; int i, j; int* piAddr = NULL; int* piLen = NULL; char** pstData = NULL; SciErr sciErr; //first call to retrieve dimensions sciErr = readNamedMatrixOfString(pvApiCtx, variableToBeRetrieved, &iRows, &iCols, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); } piLen = (int*)malloc(sizeof(int) * iRows * iCols); //second call to retrieve length of each string sciErr = readNamedMatrixOfString(pvApiCtx, variableToBeRetrieved, &iRows, &iCols, piLen, NULL); if (sciErr.iErr) { printError(&sciErr, 0); } pstData = (char**)malloc(sizeof(char*) * iRows * iCols); for (i = 0 ; i < iRows * iCols ; i++) { pstData[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination } //third call to retrieve data sciErr = readNamedMatrixOfString(pvApiCtx, variableToBeRetrieved, &iRows, &iCols, piLen, pstData); if (sciErr.iErr) { printError(&sciErr, 0); } printf("\n"); printf("Load and display of B:\n"); for (j = 0 ; j < iCols ; j++) { for (i = 0 ; i < iRows ; i++) { /* Display the formated matrix with same scilab indice */ printf("[%d,%d] = %s\n", j + 1, i + 1, pstData[j * iRows + i]); } } printf("\n"); free(piLen); for (i = 0 ; i < iRows * iCols ; i++) { free(pstData[i]); } free(pstData); } if ( TerminateScilab(NULL) == FALSE ) { fprintf(stderr, "Error while calling TerminateScilab\n"); return -2; } return 0; }