/scilab-master-1333395999/modules/api_scilab/src/cpp/api_pointer.cpp

# · C++ · 207 lines · 149 code · 38 blank · 20 comment · 15 complexity · 82024ae2c6ac49b705db3ef99fc1ef4c MD5 · raw file

  1. /*
  2. * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  3. * Copyright (C) 2009 - DIGITEO - Antoine ELIAS
  4. *
  5. * This file must be used under the terms of the CeCILL.
  6. * This source file is licensed as described in the file COPYING, which
  7. * you should have received as part of this distribution. The terms
  8. * are also available at
  9. * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  10. *
  11. * Please note that piece of code will be rewrited for the Scilab 6 family
  12. * However, the API (profile of the functions in the header files) will be
  13. * still available and supported in Scilab 6.
  14. */
  15. #include "api_scilab.h"
  16. #include "api_internal_common.h"
  17. #include "localization.h"
  18. #include "MALLOC.h"
  19. #include "call_scilab.h"
  20. SciErr getPointer(void* _pvCtx, int* _piAddress, void** _pvPtr)
  21. {
  22. SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0;
  23. int iType = 0;
  24. double *pdblTmp = NULL;
  25. if( _piAddress == NULL)
  26. {
  27. addErrorMessage(&sciErr, API_ERROR_INVALID_POINTER, _("%s: Invalid argument address"), "getPointer");
  28. return sciErr;
  29. }
  30. sciErr = getVarType(_pvCtx, _piAddress, &iType);
  31. if(sciErr.iErr)
  32. {
  33. addErrorMessage(&sciErr, API_ERROR_GET_POINTER, _("%s: Unable to get argument #%d"), "getPointer", getRhsFromAddress(_pvCtx, _piAddress));
  34. return sciErr;
  35. }
  36. if(iType != sci_pointer)
  37. {
  38. addErrorMessage(&sciErr, API_ERROR_INVALID_TYPE, _("%s: Invalid argument type, %s excepted"), "getPointer", _("pointer"));
  39. return sciErr;
  40. }
  41. pdblTmp = (double*)(_piAddress + 4);
  42. *_pvPtr = (void*)((unsigned long int)(*pdblTmp));
  43. return sciErr;
  44. }
  45. SciErr fillPointer(void* _pvCtx, int *_piAddress, void** _pvPtr)
  46. {
  47. SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0;
  48. if(_piAddress == NULL)
  49. {
  50. addErrorMessage(&sciErr, API_ERROR_INVALID_POINTER, _("%s: Invalid argument address"), "fillPointer");
  51. return sciErr;
  52. }
  53. _piAddress[0] = sci_pointer;
  54. _piAddress[1] = 1;
  55. _piAddress[2] = 1;
  56. _piAddress[3] = 0;
  57. *_pvPtr = _piAddress + 4;
  58. return sciErr;
  59. }
  60. SciErr allocPointer(void* _pvCtx, int _iVar, void** _pvPtr)
  61. {
  62. SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0;
  63. int iNewPos = Top - Rhs + _iVar;
  64. int iAddr = *Lstk(iNewPos);
  65. int* piAddr = NULL;
  66. void* pvPtr = NULL;
  67. int iMemSize = 2;
  68. int iFreeSpace = iadr(*Lstk(Bot)) - (iadr(iAddr));
  69. if (iMemSize > iFreeSpace)
  70. {
  71. addStackSizeError(&sciErr, ((StrCtx*)_pvCtx)->pstName, iMemSize);
  72. return sciErr;
  73. }
  74. getNewVarAddressFromPosition(_pvCtx, iNewPos, &piAddr);
  75. sciErr = fillPointer(_pvCtx, piAddr, &pvPtr);
  76. if(sciErr.iErr)
  77. {
  78. addErrorMessage(&sciErr, API_ERROR_ALLOC_POINTER, _("%s: Unable to create variable in Scilab memory"), "allocPointer");
  79. return sciErr;
  80. }
  81. *_pvPtr = pvPtr;
  82. updateInterSCI(_iVar, '$', iAddr, sadr(iadr(iAddr) + 4));
  83. updateLstk(iNewPos, sadr(iadr(iAddr) + 4), 2);
  84. return sciErr;
  85. }
  86. SciErr createPointer(void* _pvCtx, int _iVar, void* _pvPtr)
  87. {
  88. SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0;
  89. void* pvPtr = NULL;
  90. sciErr = allocPointer(_pvCtx, _iVar, &pvPtr);
  91. if(sciErr.iErr)
  92. {
  93. addErrorMessage(&sciErr, API_ERROR_CREATE_POINTER, _("%s: Unable to create variable in Scilab memory"), "createPointer");
  94. return sciErr;
  95. }
  96. ((double*)pvPtr)[0] = (double) ((unsigned long int)_pvPtr);
  97. return sciErr;
  98. }
  99. SciErr createNamedPointer(void* _pvCtx, const char* _pstName, int* _pvPtr)
  100. {
  101. SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0;
  102. int iVarID[nsiz];
  103. int iSaveRhs = Rhs;
  104. int iSaveTop = Top;
  105. void* pvPtr = NULL;
  106. int *piAddr = NULL;
  107. if (!checkNamedVarFormat(_pvCtx, _pstName))
  108. {
  109. addErrorMessage(&sciErr, API_ERROR_INVALID_NAME, _("%s: Invalid variable name."), "createNamedPointer");
  110. return sciErr;
  111. }
  112. C2F(str2name)(_pstName, iVarID, (int)strlen(_pstName));
  113. Top = Top + Nbvars + 1;
  114. int iMemSize = 1;
  115. int iFreeSpace = iadr(*Lstk(Bot)) - (iadr(*Lstk(Top)));
  116. if (iMemSize > iFreeSpace)
  117. {
  118. addStackSizeError(&sciErr, ((StrCtx*)_pvCtx)->pstName, iMemSize);
  119. return sciErr;
  120. }
  121. getNewVarAddressFromPosition(_pvCtx, Top, &piAddr);
  122. //write matrix information
  123. sciErr = fillPointer(_pvCtx, piAddr, &pvPtr);
  124. if(sciErr.iErr)
  125. {
  126. addErrorMessage(&sciErr, API_ERROR_CREATE_NAMED_POINTER, _("%s: Unable to create %s named \"%s\""), "createNamedPointer", _("pointer"), _pstName);
  127. return sciErr;
  128. }
  129. //copy data in stack
  130. ((double*)pvPtr)[0] = (double) ((unsigned long int)_pvPtr);
  131. updateLstk(Top, *Lstk(Top) + sadr(4), 2);
  132. Rhs = 0;
  133. //Add name in stack reference list
  134. createNamedVariable(iVarID);
  135. Top = iSaveTop;
  136. Rhs = iSaveRhs;
  137. return sciErr;
  138. }
  139. SciErr readNamedPointer(void* _pvCtx, const char* _pstName, void** _pvPtr)
  140. {
  141. SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0;
  142. int* piAddr = NULL;
  143. void *pvPtr = NULL;
  144. sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr);
  145. if(sciErr.iErr)
  146. {
  147. addErrorMessage(&sciErr, API_ERROR_READ_POINTER, _("%s: Unable to get variable \"%s\""), "readNamedMatrixOfBoolean", _pstName);
  148. return sciErr;
  149. }
  150. sciErr = getPointer(_pvCtx, piAddr, &pvPtr);
  151. if(sciErr.iErr)
  152. {
  153. addErrorMessage(&sciErr, API_ERROR_READ_POINTER, _("%s: Unable to get variable \"%s\""), "readNamedMatrixOfBoolean", _pstName);
  154. return sciErr;
  155. }
  156. *_pvPtr = pvPtr;
  157. return sciErr;
  158. }
  159. /*--------------------------------------------------------------------------*/
  160. int isPointerType(void* _pvCtx, int* _piAddress)
  161. {
  162. return checkVarType(_pvCtx, _piAddress, sci_pointer);
  163. }
  164. /*--------------------------------------------------------------------------*/
  165. int isNamedPointerType(void* _pvCtx, const char* _pstName)
  166. {
  167. return checkNamedVarType(_pvCtx, _pstName, sci_pointer);
  168. }
  169. /*--------------------------------------------------------------------------*/