home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / Math / Source / plugin.c < prev   
C/C++ Source or Header  |  2003-09-22  |  4KB  |  135 lines

  1. #include <windows.h>
  2. #include <crtdbg.h>
  3. #include "MyMath.h"
  4. #include "Math.h"
  5.  
  6. unsigned int g_stringsize;
  7. stack_t **g_stacktop;
  8. char *g_variables;
  9.  
  10. #ifdef _DEBUG_LEAKS
  11.  
  12. int blocksnum = 0;
  13. HGLOBAL blocks[100000];
  14.  
  15. HGLOBAL watchGlobalAlloc(UINT Flags, UINT size)
  16. {
  17.     HGLOBAL block = GlobalAlloc(Flags, size);
  18.     blocks[blocksnum++] = block;
  19.     return block;
  20. }
  21.  
  22. void watchGlobalFree(HGLOBAL block)
  23. {
  24.     for (int i = 0; i < blocksnum; i++)
  25.         if (blocks[i] == block) blocks[i] = NULL;
  26.     GlobalFree(block);
  27. }
  28.  
  29. void watchGlobal()
  30. {
  31.     for (int i = 0; i < blocksnum; i++)
  32.         if (blocks[i] != NULL)
  33.         {
  34.             _RPT2(_CRT_WARN, "Memory leak %d at %8X\n", i, blocks[i]);
  35.         }
  36. }
  37.  
  38. #endif
  39.  
  40. // utility functions (not required but often useful)
  41. int popstring(char *str)
  42. {
  43.   stack_t *th;
  44.   if (!g_stacktop || !*g_stacktop) return 1;
  45.   th=(*g_stacktop);
  46.   lstrcpy(str,th->text);
  47.   *g_stacktop = th->next;
  48.   dbgGlobalFree((HGLOBAL)th);
  49.   return 0;
  50. }
  51.  
  52. void pushstring(char *str)
  53. {
  54.   stack_t *th;
  55.   if (!g_stacktop) return;
  56.   th=(stack_t*)dbgGlobalAlloc(GPTR,sizeof(stack_t)+g_stringsize);
  57.   lstrcpyn(th->text,str,g_stringsize);
  58.   th->next=*g_stacktop;
  59.   *g_stacktop=th;
  60. }
  61.  
  62. char *getuservariable(int varnum)
  63. {
  64.   if (varnum < 0 || varnum >= __INST_LAST) return NULL;
  65.   return g_variables+varnum*g_stringsize;
  66. }
  67.  
  68. void setuservariable(int varnum, char *var)
  69. {
  70.     if (var != NULL && varnum >= 0 && varnum < __INST_LAST) 
  71.     lstrcpy(g_variables + varnum*g_stringsize, var);
  72. }
  73.  
  74. char *AllocString()
  75. {
  76.     return (char*) dbgGlobalAlloc(GPTR,g_stringsize);
  77. }
  78.  
  79. ExpressionItem *AllocItem()
  80. {
  81.     ExpressionItem *item = (ExpressionItem*)dbgGlobalAlloc(GPTR,sizeof(ExpressionItem));
  82.     item->next = NULL;
  83.     item->type = IT_CONST | ITC_INT;
  84.     item->param1 = item->param2 = 0;
  85.     return item;
  86. }
  87.  
  88. ExpressionItem *AllocArray(int s)
  89. {
  90.     int size = DEFAULT_ARRAY_SIZE;
  91.     while (s > size) size*=2;
  92.  
  93.     ExpressionItem *ai = (ExpressionItem*)dbgGlobalAlloc(GPTR,sizeof(ExpressionItem));
  94.     ai->type = IT_CONST | ITC_ARRAY;
  95.     ai->param1 = (EIPARAM) dbgGlobalAlloc(GPTR, sizeof(ArrayDesc));
  96.     
  97.     ArrayDesc *ad = *((ArrayDesc**)&(ai->param1));
  98.     // initialize and clear the array memory
  99.     ad->array = (ExpressionItem**) dbgGlobalAlloc(GPTR, size*sizeof(ExpressionItem*));
  100.     ad->size = size;
  101.     ad->count = 0;
  102.     ad->references = 1;
  103.     return ai;
  104. }
  105.  
  106. ExpressionItem *CopyItem(ExpressionItem *citem, int NeedConst)
  107. {
  108.     if (!citem) return NULL;
  109.     ExpressionItem *item = NULL;
  110.     if ((NeedConst) && ((citem->type & ITEMTYPE) != IT_CONST))
  111.     {
  112.         // in case of non constant expression - flat it to const
  113.         RunTree(citem, item, RTO_NEEDCONST | ITC_INT | ITC_STRING | ITC_FLOAT | ITC_ARRAY);
  114.         if (item) return item;
  115.     } 
  116.  
  117.     item = AllocItem();
  118.     item->type = citem->type;
  119.     if ((item->type & (ITEMTYPE | ITEMSUBTYPE)) == (IT_CONST | ITC_STRING))
  120.     {
  121.         item->param1 = (EIPARAM) AllocString();
  122.         lstrcpy((LPSTR) item->param1, (LPSTR) citem->param1);
  123.     } else if (((item->type & (ITEMTYPE | ITEMSUBTYPE)) == (IT_CONST | ITC_ARRAY))
  124.         ||
  125.         ((item->type & (ITEMTYPE | ITEMSUBTYPE)) == (IT_VARIABLE | ITV_ARRITEM)))
  126.     {        
  127.         item->param1 = citem->param1;
  128.         ArrayDesc *ad = (ArrayDesc*) item->param1;
  129.         ad->references++;
  130.     } 
  131.     else item->param1 = citem->param1;
  132.     item->param2 = citem->param2;
  133.     item->next = NULL;
  134.     return item;
  135. }