static bool read_list(int _iDatasetId, int _iVarType, int _iItemPos, int *_piAddress, VarInfo* _pInfo) { int iRet = 0; int iItems = 0; int *piListAddr = NULL; hobj_ref_t *piItemRef = NULL; iRet = getListDims(_iDatasetId, &iItems); if (iRet) { return false; } if (iItems == 0) { //special case for empty list } else { iRet = getListItemReferences(_iDatasetId, &piItemRef); if (iRet) { return false; } } //_pInfo = (VarInfo*)MALLOC(sizeof(VarInfo)); _pInfo->iDims = 1; _pInfo->piDims[0] = iItems; _pInfo->iSize = (2 + iItems + 1) * 4; for (int i = 0; i < iItems; i++) { int iItemDataset = 0; iRet = getListItemDataset(_iDatasetId, piItemRef, i, &iItemDataset); if (iRet || iItemDataset == 0) { return false; } VarInfo info; bool bRet = read_data(iItemDataset, i + 1, piListAddr, &info); if (bRet == false) { return false; } _pInfo->iSize += info.iSize; } if (_iVarType == sci_list) { generateInfo(_pInfo, "list"); } else if (_iVarType == sci_tlist) { generateInfo(_pInfo, "tlist"); } else if (_iVarType == sci_mlist) { generateInfo(_pInfo, "mlist"); } iRet = deleteListItemReferences(_iDatasetId, piItemRef); if (iRet) { return false; } return true; }
static bool import_cell(int* pvCtx, int _iDatasetId, int _iVarType, int _iItemPos, int *_piAddress, char *_pstVarname) { int iRet = 0; int iComplex = 0; int iDims = 0; int iItems = 0; hobj_ref_t *piItemRef = NULL; // an hypermatrix is stored in an mlist if (_iVarType != sci_mlist) { return false; } iRet = getListDims(_iDatasetId, &iItems); if (iRet) { return false; } if (iItems != 3) { // cell have 3 elements return false; } iRet = getListItemReferences(_iDatasetId, &piItemRef); if (iRet) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } // get first item int iItemDataset = 0; iRet = getListItemDataset(_iDatasetId, piItemRef, 0, &iItemDataset); if (iRet || iItemDataset == 0) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } // get first item type int iItemType = getScilabTypeFromDataSet(iItemDataset); if (iItemType != sci_strings) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } // get size of first item iRet = getDatasetInfo(iItemDataset, &iComplex, &iDims, NULL); if (iRet < 0 || iDims != 2) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } int* piDims = new int[2]; int iSize = getDatasetInfo(iItemDataset, &iComplex, &iDims, piDims); if (iSize != 3) { deleteListItemReferences(_iDatasetId, piItemRef); delete[] piDims; return false; } delete[] piDims; piDims = NULL; // get data of first item for check the type of mlist char** pstData = new char*[iSize]; iRet = readStringMatrix(iItemDataset, pstData); if (iRet || strcmp(pstData[0], "ce") != 0) { // if not the good type, do not h5close (deleteListItemReferences) FREE(piItemRef); freeStringMatrix(iItemDataset, pstData); delete[] pstData; return false; } freeStringMatrix(iItemDataset, pstData); delete[] pstData; pstData = NULL; // get second item, the Size of cell iRet = getListItemDataset(_iDatasetId, piItemRef, 1, &iItemDataset); if (iRet) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } iRet = getDatasetInfo(iItemDataset, &iComplex, &iDims, NULL); if (iRet < 0 || iDims != 2) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } piDims = new int[2]; iSize = getDatasetInfo(iItemDataset, &iComplex, &iDims, piDims); if (piDims[0] != 1) { deleteListItemReferences(_iDatasetId, piItemRef); delete[] piDims; return false; } int* piDimsArray = new int[piDims[1]]; iRet = readInteger32Matrix(iItemDataset, piDimsArray); if (iRet) { deleteListItemReferences(_iDatasetId, piItemRef); delete[] piDims; delete[] piDimsArray; return false; } types::Cell* pCell = new types::Cell(piDims[1], piDimsArray); types::List* pList = new types::List(); pList->set(0, types::Double::Empty()); iRet = getListItemDataset(_iDatasetId, piItemRef, 2, &iItemDataset); if (iRet || iItemDataset == 0) { deleteListItemReferences(_iDatasetId, piItemRef); delete pList; delete pCell; return false; } bool bRet = import_data(pvCtx, iItemDataset, 1, (int*)pList, NULL); if (bRet == false) { deleteListItemReferences(_iDatasetId, piItemRef); delete pList; delete pCell; return false; } types::List* pListData = pList->get(0)->getAs<types::List>(); for (int iWriteData = 0; iWriteData < pCell->getSize(); ++iWriteData) { pCell->set(iWriteData, pListData->get(iWriteData)); } delete pList; if (_piAddress == NULL) { wchar_t* pwstName = to_wide_string(_pstVarname); symbol::Context::getInstance()->put(symbol::Symbol(pwstName), pCell); FREE(pwstName); } else { types::List* pParentList = (types::List*)_piAddress; pParentList->set(_iItemPos - 1, pCell); } iRet = deleteListItemReferences(_iDatasetId, piItemRef); if (iRet) { return false; } return true; }
static bool import_hypermat(int* pvCtx, int _iDatasetId, int _iVarType, int _iItemPos, int *_piAddress, char *_pstVarname) { int iRet = 0; int iComplex = 0; int iDims = 0; int iItems = 0; hobj_ref_t *piItemRef = NULL; // an hypermatrix is stored in an mlist if (_iVarType != sci_mlist) { return false; } iRet = getListDims(_iDatasetId, &iItems); if (iRet) { return false; } if (iItems != 3) { // hypermatrix have 3 elements return false; } iRet = getListItemReferences(_iDatasetId, &piItemRef); if (iRet) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } // get first item int iItemDataset = 0; iRet = getListItemDataset(_iDatasetId, piItemRef, 0, &iItemDataset); if (iRet || iItemDataset == 0) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } // get first item type int iItemType = getScilabTypeFromDataSet(iItemDataset); if (iItemType != sci_strings) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } // get size of first item iRet = getDatasetInfo(iItemDataset, &iComplex, &iDims, NULL); if (iRet < 0 || iDims != 2) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } int* piDims = new int[2]; int iSize = getDatasetInfo(iItemDataset, &iComplex, &iDims, piDims); if (iSize != 3) { deleteListItemReferences(_iDatasetId, piItemRef); delete[] piDims; return false; } delete[] piDims; piDims = NULL; // get data of first item for check the type of mlist char** pstData = new char*[iSize]; iRet = readStringMatrix(iItemDataset, pstData); if (iRet || strcmp(pstData[0], "hm") != 0) { // if not the good type, do not h5close (deleteListItemReferences) FREE(piItemRef); freeStringMatrix(iItemDataset, pstData); delete[] pstData; return false; } freeStringMatrix(iItemDataset, pstData); delete[] pstData; pstData = NULL; // get second item, the Size of hypermatrix iRet = getListItemDataset(_iDatasetId, piItemRef, 1, &iItemDataset); if (iRet) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } iRet = getDatasetInfo(iItemDataset, &iComplex, &iDims, NULL); if (iRet < 0 || iDims != 2) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } piDims = new int[2]; iSize = getDatasetInfo(iItemDataset, &iComplex, &iDims, piDims); if (piDims[0] != 1) { deleteListItemReferences(_iDatasetId, piItemRef); delete[] piDims; return false; } int* piDimsArray = new int[piDims[1]]; iRet = readInteger32Matrix(iItemDataset, piDimsArray); if (iRet) { deleteListItemReferences(_iDatasetId, piItemRef); delete[] piDims; delete[] piDimsArray; return false; } // get third item, the Data of hypermatrix // import data like a "type" (Double, Int, ...) instead of mlist iRet = getListItemDataset(_iDatasetId, piItemRef, 2, &iItemDataset); bool bRet = import_data(pvCtx, iItemDataset, _iItemPos, _piAddress, _pstVarname); if (bRet == false) { deleteListItemReferences(_iDatasetId, piItemRef); delete[] piDims; delete[] piDimsArray; return false; } // get imported hypermatrix from List or Context types::GenericType* pGT = NULL; types::InternalType* pIT = NULL; if (_piAddress) { types::List* pL = (types::List*)_piAddress; pIT = pL->get(_iItemPos - 1); } else { wchar_t* pwcsName = to_wide_string(_pstVarname); pIT = symbol::Context::getInstance()->getCurrentLevel(symbol::Symbol(pwcsName)); FREE(pwcsName); } // reshape data with size of hypermatrix pGT = pIT->getAs<types::GenericType>(); pGT->reshape(piDimsArray, piDims[1]); delete[] piDims; delete[] piDimsArray; iRet = deleteListItemReferences(_iDatasetId, piItemRef); if (iRet) { return false; } return true; }
static bool import_struct(int* pvCtx, int _iDatasetId, int _iVarType, int _iItemPos, int *_piAddress, char *_pstVarname) { int iRet = 0; int iComplex = 0; int iDims = 0; int iItems = 0; hobj_ref_t *piItemRef = NULL; // an struct is stored in an mlist if (_iVarType != sci_mlist) { return false; } iRet = getListDims(_iDatasetId, &iItems); if (iRet) { return false; } if (iItems < 2) { // struct have 2 elements minimal return false; } iRet = getListItemReferences(_iDatasetId, &piItemRef); if (iRet) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } // get first item int iItemDataset = 0; iRet = getListItemDataset(_iDatasetId, piItemRef, 0, &iItemDataset); if (iRet || iItemDataset == 0) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } // get first item type int iItemType = getScilabTypeFromDataSet(iItemDataset); if (iItemType != sci_strings) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } // get size of first item iRet = getDatasetInfo(iItemDataset, &iComplex, &iDims, NULL); if (iRet < 0 || iDims != 2) { deleteListItemReferences(_iDatasetId, piItemRef); return false; } int* piDims = new int[2]; int iSize = getDatasetInfo(iItemDataset, &iComplex, &iDims, piDims); if (iSize != iItems) { deleteListItemReferences(_iDatasetId, piItemRef); delete[] piDims; return false; } delete[] piDims; piDims = NULL; // get data of first item for check the type of mlist char** pstData = new char*[iSize]; char** pstDataSave = new char*[iSize - 2]; iRet = readStringMatrix(iItemDataset, pstData); if (iRet || strcmp(pstData[0], "st") != 0) { // if not the good type, do not h5close (deleteListItemReferences) FREE(piItemRef); freeStringMatrix(iItemDataset, pstData); delete[] pstData; delete[] pstDataSave; return false; } for (int i = 2; i < iSize; ++i) { pstDataSave[-2 + i] = new char[strlen(pstData[i]) + 1]; strcpy(pstDataSave[-2 + i], pstData[i]); } freeStringMatrix(iItemDataset, pstData); delete[] pstData; pstData = NULL; // get second item, the Size of struct iRet = getListItemDataset(_iDatasetId, piItemRef, 1, &iItemDataset); if (iRet) { deleteListItemReferences(_iDatasetId, piItemRef); for (int i = 0; i < (-2 + iItems); ++i) { delete pstDataSave[i]; } delete[] pstDataSave; pstDataSave = NULL; return false; } iRet = getDatasetInfo(iItemDataset, &iComplex, &iDims, NULL); if (iRet < 0 || iDims != 2) { deleteListItemReferences(_iDatasetId, piItemRef); for (int i = 0; i < (-2 + iItems); ++i) { delete pstDataSave[i]; } delete[] pstDataSave; pstDataSave = NULL; return false; } piDims = new int[2]; iSize = getDatasetInfo(iItemDataset, &iComplex, &iDims, piDims); if (piDims[0] != 1) { deleteListItemReferences(_iDatasetId, piItemRef); for (int i = 0; i < (-2 + iItems); ++i) { delete pstDataSave[i]; } delete[] pstDataSave; pstDataSave = NULL; delete[] piDims; return false; } int* piDimsArray = new int[piDims[1]]; iRet = readInteger32Matrix(iItemDataset, piDimsArray); if (iRet) { deleteListItemReferences(_iDatasetId, piItemRef); for (int i = 0; i < (-2 + iItems); ++i) { delete pstDataSave[i]; } delete[] pstDataSave; pstDataSave = NULL; delete[] piDims; delete[] piDimsArray; return false; } types::Struct* pStruct = new types::Struct(piDims[1], piDimsArray); wchar_t* pwstName = NULL; for (int i = 0; i < (-2 + iItems); ++i) { pwstName = to_wide_string(pstDataSave[i]); pStruct->addField(pwstName); delete pstDataSave[i]; FREE(pwstName); } delete[] pstDataSave; pstDataSave = NULL; types::SingleStruct** ppSStruct = pStruct->get(); types::String* pStr = pStruct->getFieldNames(); types::List* pList = new types::List(); // fill the list to avoid api_scilab error about the list size. pList->set(0, types::Double::Empty()); if (pStruct->getSize() == 1) { types::InternalType* pIT = NULL; for (int i = 0; i < pStr->getSize(); ++i) { int iItemDataset = 0; iRet = getListItemDataset(_iDatasetId, piItemRef, i + 2, &iItemDataset); if (iRet || iItemDataset == 0) { deleteListItemReferences(_iDatasetId, piItemRef); delete pList; delete pStruct; return false; } wchar_t* pwcsName = pStr->get(i); char* pcName = wide_string_to_UTF8(pwcsName); bool bRet = import_data(pvCtx, iItemDataset, 1, (int*)pList, pcName); if (bRet == false) { deleteListItemReferences(_iDatasetId, piItemRef); delete pList; delete pStruct; return false; } pIT = pList->get(0); ppSStruct[0]->set(pwcsName, pIT); FREE(pcName); } } else if (pStruct->getSize() > 1) { for (int i = 0; i < pStr->getSize(); ++i) { int iItemDataset = 0; iRet = getListItemDataset(_iDatasetId, piItemRef, i + 2, &iItemDataset); if (iRet || iItemDataset == 0) { deleteListItemReferences(_iDatasetId, piItemRef); delete pList; delete pStruct; return false; } wchar_t* pwcsName = pStr->get(i); char* pcName = wide_string_to_UTF8(pwcsName); bool bRet = import_data(pvCtx, iItemDataset, 1, (int*)pList, pcName); if (bRet == false) { deleteListItemReferences(_iDatasetId, piItemRef); delete pList; delete pStruct; return false; } types::List* pListData = pList->get(0)->getAs<types::List>(); for (int iWriteData = 0; iWriteData < pStruct->getSize(); ++iWriteData) { ppSStruct[iWriteData]->set(pwcsName, pListData->get(iWriteData)); } FREE(pcName); } } delete pList; if (_piAddress == NULL) { pwstName = to_wide_string(_pstVarname); symbol::Context::getInstance()->put(symbol::Symbol(pwstName), pStruct); FREE(pwstName); } else { types::List* pParentList = (types::List*)_piAddress; pParentList->set(_iItemPos - 1, pStruct); } iRet = deleteListItemReferences(_iDatasetId, piItemRef); if (iRet) { return false; } return true; }
static bool import_list(int* pvCtx, int _iDatasetId, int _iVarType, int _iItemPos, int *_piAddress, char *_pstVarname) { int iRet = 0; int i = 0; int iItems = 0; int *piListAddr = NULL; hobj_ref_t *piItemRef = NULL; SciErr sciErr; iRet = getListDims(_iDatasetId, &iItems); if (iRet) { return false; } if (iItems == 0) { //special case for empty list } else { iRet = getListItemReferences(_iDatasetId, &piItemRef); if (iRet) { return false; } } if (_piAddress == 0) { switch (_iVarType) { case sci_list: sciErr = createNamedList(pvCtx, _pstVarname, iItems, &piListAddr); break; case sci_tlist: sciErr = createNamedTList(pvCtx, _pstVarname, iItems, &piListAddr); break; case sci_mlist: sciErr = createNamedMList(pvCtx, _pstVarname, iItems, &piListAddr); break; default: return false; } } else //if not null this variable is in a list { switch (_iVarType) { case sci_list: sciErr = createListInNamedList(pvCtx, _pstVarname, _piAddress, _iItemPos, iItems, &piListAddr); break; case sci_tlist: sciErr = createTListInNamedList(pvCtx, _pstVarname, _piAddress, _iItemPos, iItems, &piListAddr); break; case sci_mlist: sciErr = createMListInNamedList(pvCtx, _pstVarname, _piAddress, _iItemPos, iItems, &piListAddr); break; default: return false; } } if (sciErr.iErr) { printError(&sciErr, 0); return false; } for (i = 0; i < iItems; i++) { int iItemDataset = 0; iRet = getListItemDataset(_iDatasetId, piItemRef, i, &iItemDataset); if (iRet || iItemDataset == 0) { return false; } bool bRet = import_data(pvCtx, iItemDataset, i + 1, piListAddr, _pstVarname); if (bRet == false) { return false; } } iRet = deleteListItemReferences(_iDatasetId, piItemRef); if (iRet) { return false; } return true; }