Debian Codegraph @

Contrib/System/Source/Plugin.c

raw
1 // Unicode support by Jim Park -- 08/23/2007
2
3 #include "stdafx.h"
4 #include "Plugin.h"
5 #include "Buffers.h"
6 #include "System.h"
7
8 HWND g_hwndParent;
9
10 #define isvalidnsisvarindex(varnum) ( ((unsigned int)(varnum)) < (__INST_LAST) )
11
12 TCHAR *AllocString()
13 {
14 return (TCHAR*) GlobalAlloc(GPTR,g_stringsize*sizeof(TCHAR));
15 }
16
17 TCHAR *AllocStr(TCHAR *str)
18 {
19 return lstrcpyn(AllocString(), str, g_stringsize);
20 }
21
22 TCHAR* system_popstring()
23 {
24 stack_t *pSt;
25 TCHAR *src, *dst, *retval;
26
27 if (!g_stacktop || !*g_stacktop) return NULL;
28 pSt = *g_stacktop, *g_stacktop = pSt->next, src = pSt->text, dst = (TCHAR*)pSt;
29
30 // We don't have to call AllocString+lstrcpy+GlobalFree if we convert the stack item to a string
31 for (retval = dst;;) if (!(*dst++ = *src++)) return retval;
32 }
33
34 TCHAR *system_pushstring(TCHAR *str)
35 {
36 stack_t *th;
37 if (!g_stacktop) return str;
38 th=(stack_t*)GlobalAlloc(GPTR,sizeof(stack_t)+(g_stringsize*sizeof(TCHAR)));
39 lstrcpyn(th->text,str,g_stringsize);
40 th->next=*g_stacktop;
41 *g_stacktop=th;
42 return str;
43 }
44
45 TCHAR *system_getuservariable(int varnum)
46 {
47 if (!isvalidnsisvarindex(varnum)) return AllocString();
48 return AllocStr(g_variables+varnum*g_stringsize);
49 }
50
51 TCHAR *system_setuservariable(int varnum, TCHAR *var)
52 {
53 if (var && isvalidnsisvarindex(varnum)) {
54 lstrcpy(g_variables + varnum*g_stringsize, var);
55 }
56 return var;
57 }
58
59 // Updated for int64 and simple bitwise operations
60 __int64 myatoi64(TCHAR *s)
61 {
62 __int64 v=0;
63 // Check for right input
64 if (!s) return 0;
65 if (*s == _T('0') && (s[1] == _T('x') || s[1] == _T('X')))
66 {
67 s++;
68 for (;;)
69 {
70 int c=*(++s);
71 if (c >= _T('0') && c <= _T('9')) c-=_T('0');
72 else if (c >= _T('a') && c <= _T('f')) c-=_T('a')-10;
73 else if (c >= _T('A') && c <= _T('F')) c-=_T('A')-10;
74 else break;
75 v<<=4;
76 v+=c;
77 }
78 }
79 else if (*s == _T('0') && s[1] <= _T('7') && s[1] >= _T('0'))
80 {
81 for (;;)
82 {
83 int c=*(++s);
84 if (c >= _T('0') && c <= _T('7')) c-=_T('0');
85 else break;
86 v<<=3;
87 v+=c;
88 }
89 }
90 else
91 {
92 int sign=0;
93 if (*s == _T('-')) sign++; else s--;
94 for (;;)
95 {
96 int c=*(++s) - _T('0');
97 if (c < 0 || c > 9) break;
98 v*=10;
99 v+=c;
100 }
101 if (sign) v = -v;
102 }
103
104 // Support for simple ORed expressions
105 if (*s == _T('|'))
106 {
107 v |= myatoi64(s+1);
108 }
109
110 return v;
111 }
112
113 void myitoa64(__int64 i, TCHAR *buffer)
114 {
115 TCHAR buf[128], *b = buf;
116
117 if (i < 0)
118 {
119 *(buffer++) = _T('-');
120 i = -i;
121 }
122 if (i == 0) *(buffer++) = _T('0');
123 else
124 {
125 while (i > 0)
126 {
127 *(b++) = _T('0') + ((TCHAR) (i%10));
128 i /= 10;
129 }
130 while (b > buf) *(buffer++) = *(--b);
131 }
132 *buffer = 0;
133 }
134
135 INT_PTR system_popintptr()
136 {
137 INT_PTR value;
138 TCHAR *str;
139 if ((str = system_popstring()) == NULL) return -1;
140 value = StrToIntPtr(str);
141 GlobalFree(str);
142 return value;
143 }
144
145 void system_pushintptr(INT_PTR value)
146 {
147 TCHAR buffer[50];
148 wsprintf(buffer, sizeof(void*) > 4 ? _T("%Id") : _T("%d"), value);
149 system_pushstring(buffer);
150 }
151
152 void *copymem(void *output, void *input, size_t cbSize)
153 {
154 BYTE *out = (BYTE*) output;
155 BYTE *in = (BYTE*) input;
156 if ((input != NULL) && (output != NULL))
157 {
158 while (cbSize-- > 0) *(out++) = *(in++);
159 }
160 return output;
161 }
162
163 HANDLE GlobalCopy(HANDLE Old)
164 {
165 size_t size = GlobalSize(Old);
166 return copymem(GlobalAlloc(GPTR, size), Old, size);
167 }
168
169 UINT_PTR NSISCallback(enum NSPIM msg)
170 {
171 return 0;
172 }
173
174 #ifdef _DEBUG
175 void main()
176 {
177 }
178 #endif