summaryrefslogtreecommitdiff
path: root/Build/source/libs/teckit/TECkit-2.5.1/source/REALplugin.cp
blob: b561c462ab8c760920873824b36328c3f2c59950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
	TECkit REALbasic plugin
	
	Jonathan Kew
	25-Jan-2002
*/

/*
	Implements two REALbasic classes:
		TECkit_Compiler
		TECkit_Engine
	as well as the Win32 utility function
		WinNewDoc(long appWindowHWND, REALstring docPathName)
	which creates a new instance of the current app, launching it with the given document
*/

#include "rb_plugin.h"

#include <string.h>
#include <stdlib.h>

#pragma mark === Compiler ===

#include "TECkit_Compiler.h"

extern REALclassDefinition TECkit_Compiler_Class;

struct TECkit_Compiler_Class_Data {
	Byte*	compiledTable;
	UInt32	compiledSize;
};

static REALevent
TECkit_Compiler_Class_Events[] = {
	{ "ErrorMessage(msg as String, param as String, lineNo as Integer)" },	// 0
};

static
CALLBACK
void
errorFunction(void* userData, char* msg, char* param, UInt32 line)
{
	REALstring	msgStr = REALBuildString(msg, strlen(msg));
	REALstring	paramStr = (param != 0)
							? REALBuildString(param, strlen(param))
							: REALBuildString("", 0);

    REALcontrolInstance instance = (REALcontrolInstance)userData;
	void (*fp)(REALcontrolInstance, REALstring, REALstring, int);
	fp = (void (*)(REALcontrolInstance, REALstring, REALstring, int))
							REALGetEventInstance(instance, &TECkit_Compiler_Class_Events[0]);
	if (fp != 0)
		fp(instance, msgStr, paramStr, (int)line);

	REALUnlockString(paramStr);
	REALUnlockString(msgStr);
}

static int
myCompile(REALobject instance, REALstring s, long doCompression)
{
	if (s->Length() <= 0)
		return 0;

	ClassData(TECkit_Compiler_Class, instance, TECkit_Compiler_Class_Data, data);

	Byte*	outTable;
	UInt32	outLen;
	REALLockString(s);
	TECkit_Status	status = TECkit_Compile((char*)REALCString(s), s->Length(), doCompression,
											errorFunction, instance, &outTable, &outLen);
	REALUnlockString(s);

	if (status == kStatus_NoError) {
		data->compiledTable = outTable;
		data->compiledSize = outLen;
	}

	return status;
}

static void
myReset(REALobject instance)
{
	ClassData(TECkit_Compiler_Class, instance, TECkit_Compiler_Class_Data, data);

	if (data->compiledTable != 0) {
		TECkit_DisposeCompiled(data->compiledTable);
		data->compiledTable = 0;
		data->compiledSize = 0;
	}
}

static REALstring
myTableGetter(REALobject instance, long param)
{
#pragma unused(param)

	ClassData(TECkit_Compiler_Class, instance, TECkit_Compiler_Class_Data, data);
	if (data->compiledTable != 0) {
		return REALBuildString((char*)data->compiledTable, data->compiledSize);
	}
	return nil;
}

static void
TECkit_Compiler_Class_Constructor(REALobject instance)
{
	ClassData(TECkit_Compiler_Class, instance, TECkit_Compiler_Class_Data, data);
	data->compiledTable = 0;
	data->compiledSize = 0;
}

static void
TECkit_Compiler_Class_Destructor(REALobject instance)
{
	ClassData(TECkit_Compiler_Class, instance, TECkit_Compiler_Class_Data, data);
	if (data->compiledTable != 0) {
		TECkit_DisposeCompiled(data->compiledTable);
		data->compiledTable = 0;
		data->compiledSize = 0;
	}
}

static REALproperty 
TECkit_Compiler_Class_Properties[] = {
	{ nil, "CompiledTable", "String", 0, (REALproc)myTableGetter, nil, 0 },
	{ nil, "CompiledSize", "Integer", 0, REALstandardGetter, nil,
							FieldOffset(TECkit_Compiler_Class_Data, compiledSize) },
};

static REALmethodDefinition
TECkit_Compiler_Class_Methods[] = {
	{ (REALproc)TECkit_Compiler_Class_Constructor, REALnoImplementation, "TECkit_Compiler()" },
	{ (REALproc)myCompile, REALnoImplementation, "Compile(data as String, doCompression as Integer) as Integer" },	
	{ (REALproc)myReset, REALnoImplementation, "Reset()" },
};

static REALclassDefinition
TECkit_Compiler_Class = {
	kCurrentREALControlVersion,
	"TECkit_Compiler",
	nil,
	sizeof(TECkit_Compiler_Class_Data),
	0,
	nil,
	(REALproc)TECkit_Compiler_Class_Destructor,
	TECkit_Compiler_Class_Properties,
	sizeof(TECkit_Compiler_Class_Properties) / sizeof(REALproperty),
	TECkit_Compiler_Class_Methods,
	sizeof(TECkit_Compiler_Class_Methods) / sizeof(REALmethodDefinition),
	TECkit_Compiler_Class_Events,
	sizeof(TECkit_Compiler_Class_Events) / sizeof(REALevent)
};

#pragma mark === Engine ===

#include "TECkit_Engine.h"

extern REALclassDefinition TECkit_Engine_Class;

struct TECkit_Engine_Class_Data {
	TECkit_Converter	converter;
	TECkit_Status		status;
	UInt32				sourceFlags;
	UInt32				targetFlags;
};

static void
TECkit_Engine_Class_Constructor(REALobject instance, REALstring table, Boolean fwd, long inputForm, long outputForm)
{
	ClassData(TECkit_Engine_Class, instance, TECkit_Engine_Class_Data, data);
	data->converter = 0;
	data->status = TECkit_CreateConverter((Byte*)table->CString(), table->Length(), fwd, inputForm, outputForm, &data->converter);
	if (data->status == kStatus_NoError)
		data->status = TECkit_GetConverterFlags(data->converter, &data->sourceFlags, &data->targetFlags);
}

static void
TECkit_Engine_Class_Destructor(REALobject instance)
{
	ClassData(TECkit_Engine_Class, instance, TECkit_Engine_Class_Data, data);
	if (data->converter != 0) {
		TECkit_DisposeConverter(data->converter);
		data->converter = 0;
		data->status = kStatus_InvalidConverter;
	}
}

static REALstring
myConvert(REALobject instance, REALstring inputData, Boolean complete)
{
	ClassData(TECkit_Engine_Class, instance, TECkit_Engine_Class_Data, data);
	if (data->converter == 0)
		REALRaiseException(REALnewInstance("NilObjectException"));
	else {
		UInt32	outLength = inputData->Length() * 8 + 8;	// should be plenty, we hope
		Byte*	outBuffer;
		UInt32	inUsed, outUsed;

		REALmemoryBlock	outBufferBlock;
		while (1) {
			outBufferBlock = REALNewMemoryBlock(outLength);
			outBuffer = (Byte*)REALMemoryBlockGetPtr(outBufferBlock);
			data->status = TECkit_ConvertBuffer(data->converter,
												(Byte*)inputData->CString(), inputData->Length(), &inUsed,
												outBuffer, outLength, &outUsed, complete);
			if (data->status == kStatus_OutputBufferFull) {
				// try again with a bigger buffer
				REALUnlockObject((REALobject)outBufferBlock);
				outLength <<= 1;
				continue;
			}
			break;
		}
		
		REALstring	result = REALBuildString((char*)outBuffer, outUsed);
		REALUnlockObject((REALobject)outBufferBlock);

		if (complete)
			TECkit_ResetConverter(data->converter);

		return result;
	}
	return nil;
}

static REALstring
myNameGetter(REALobject instance, long param)
{
	ClassData(TECkit_Engine_Class, instance, TECkit_Engine_Class_Data, data);
	if (data->converter != 0) {
		const UInt16	bufferSize = 256;
		Byte			nameBuffer[bufferSize];
		UInt32			nameLength;
		data->status = TECkit_GetConverterName(data->converter,
												param,
												nameBuffer,
												bufferSize,
												&nameLength);
		if (data->status == kStatus_NoError)
			return REALBuildString((char*)nameBuffer, nameLength);
	}
	return nil;
}

static REALproperty
TECkit_Engine_Class_Properties[] = {
	{ nil, "status", "Integer", 0, REALstandardGetter, nil, FieldOffset(TECkit_Engine_Class_Data, status) },
	{ nil, "lhsName", "String", 0, (REALproc)myNameGetter, nil, kNameID_LHS_Name },
	{ nil, "rhsName", "String", 0, (REALproc)myNameGetter, nil, kNameID_RHS_Name },
	{ nil, "sourceFlags", "Integer", 0, REALstandardGetter, nil, FieldOffset(TECkit_Engine_Class_Data, sourceFlags) },
	{ nil, "targetFlags", "Integer", 0, REALstandardGetter, nil, FieldOffset(TECkit_Engine_Class_Data, targetFlags) },
};

static REALmethodDefinition
TECkit_Engine_Class_Methods[] = {
	{ (REALproc)TECkit_Engine_Class_Constructor, REALnoImplementation, "TECkit_Engine(table as String, fwd as Boolean, inputForm as Integer, outputForm as Integer)" },
	{ (REALproc)myConvert, REALnoImplementation, "Convert(inputData as String, complete as Boolean) as String" },
};

static REALevent
TECkit_Engine_Class_Events[] = {
	{ "ErrorMessage(msg as String, param as String, lineNo as Integer)" },	// 0
};

static REALclassDefinition
TECkit_Engine_Class = {
	kCurrentREALControlVersion,
	"TECkit_Engine",
	nil,
	sizeof(TECkit_Engine_Class_Data),
	0,
	nil,
	(REALproc)TECkit_Engine_Class_Destructor,
	TECkit_Engine_Class_Properties,	sizeof(TECkit_Engine_Class_Properties) / sizeof(REALproperty),
	TECkit_Engine_Class_Methods,	sizeof(TECkit_Engine_Class_Methods) / sizeof(REALmethodDefinition),
	TECkit_Engine_Class_Events,		sizeof(TECkit_Engine_Class_Events) / sizeof(REALevent)
};

#pragma mark === WinNewDoc ===

#if TARGET_WIN32
#include <winable.h>	// GetWindowModuleFileName is in one or other of these, depending on WINVER
#include <winuser.h>
#endif

static int
WinNewDoc(long appWindowHWND, REALstring docPathName)
{
#if TARGET_CARBON
#pragma unused(appWindowHWND, docPathName)
#endif
#if TARGET_WIN32
	char*	docCStr = "";
	if (docPathName != 0)
		docCStr = (char*)docPathName->CString();

	const int	kFileNameLimit = 2048;
	char		appFileName[kFileNameLimit];
	int			fileNameLen = GetWindowModuleFileName((HWND)appWindowHWND, appFileName, kFileNameLimit);
	appFileName[fileNameLen] = 0;
	
	int	len = 0;
	for (char* dp = docCStr; *dp; ++dp)
		if (*dp == '\\')
			len += 2;
		else
			++len;
	len += 2;	// for quotes around doc name
	len += 1;	// for space between app and doc names
	char*	ap = &appFileName[fileNameLen] - 1;
	while (ap >= &appFileName[0] && *ap != '\\' && *ap != '/' && *ap != ':') {
		if (*ap == '\\')
			len += 2;
		else
			len += 1;
		--ap;
	}
	len += 2;	// for quotes around app name
	char*	cmdLine = (char*)malloc(len + 1);
	if (cmdLine == 0)
		return 0;
	char*	cp = cmdLine;

	*cp++ = '"';
	while (*++ap) {
		if (*ap == '\\')
			*cp++ = '\\';
		*cp++ = *ap;
	}
	*cp++ = '"';
	*cp++ = ' ';

	*cp++ = '"';
	ap = docCStr;
	while (*ap) {
		if (*ap == '\\')
			*cp++ = '\\';
		*cp++ = *ap++;
	}
	*cp++ = '"';
	*cp = 0;
	
	STARTUPINFO startupInfo = {
    	sizeof(STARTUPINFO),	//DWORD   cb;
		0,	//LPSTR   lpReserved;
		0,	//LPSTR   lpDesktop;
		0,	//LPSTR   lpTitle;
		0,	//DWORD   dwX;
		0,	//DWORD   dwY;
		0,	//DWORD   dwXSize;
		0,	//DWORD   dwYSize;
		0,	//DWORD   dwXCountChars;
		0,	//DWORD   dwYCountChars;
		0,	//DWORD   dwFillAttribute;
		STARTF_USESHOWWINDOW,	//DWORD   dwFlags;
		SW_SHOWDEFAULT,	//WORD    wShowWindow;
		0,	//WORD    cbReserved2;
		0,	//LPBYTE  lpReserved2;
		0,	//HANDLE  hStdInput;
		0,	//HANDLE  hStdOutput;
		0	//HANDLE  hStdError;
	};
	PROCESS_INFORMATION	processInfo;
	if (CreateProcess(
			appFileName,
			cmdLine,
			0,
			0,
			false,
			0,
			0,
			0,
			&startupInfo,
			&processInfo
	    )) {
	   	CloseHandle(processInfo.hProcess);
	   	CloseHandle(processInfo.hThread);
    }
	free(cmdLine);
#endif
	return 0;
}

static REALmethodDefinition
WinNewDoc_Method = {
	(REALproc)WinNewDoc, REALnoImplementation,
		"WinNewDoc(appWindowHWND as Integer, docPathName as String) as Integer"
};

#pragma mark === Plugin setup ===

#ifdef WIN32
	extern "C" far void _RunInit(void);
	#define __InitCode__ _RunInit
#elif defined(powerc)
	extern "C" void __sinit(void);
	#define __InitCode__ __sinit
#else // it's 68K
	extern "C" far void __InitCode__ (void);
#endif

void
PluginEntry(void)
{
	__InitCode__();		// calls constructors for static and global C++ objects
	REALRegisterClass(&TECkit_Compiler_Class);
	REALRegisterClass(&TECkit_Engine_Class);

	REALRegisterMethod(&WinNewDoc_Method);
}