aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/PalmOS/Src/args.cpp
blob: 941072aff80986c29a1d6b9728bf36417ea1b3c3 (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
#include <PalmOS.h>
#include "palmdefs.h"
#include "args.h"
#include "pace.h"
#include "endianutils.h"

Char **ArgsInit() {
	// first 4 bytes keep the real address of the chunk
	MemHandle argvH = MemHandleNew(sizeof(Char *) + (MAX_ARG * sizeof(Char **) + 2));
	// real addr
	Char *lockP = (Char *)MemHandleLock(argvH);
	// 4byte aligned
	Char **argvP = (Char **)ALIGN_4BYTE(lockP);
	// save real addr
	argvP[0] = lockP;
	// initial position
	argvP++;

	for(UInt8 count = 0; count < MAX_ARG; count++)
		argvP[count] = NULL;

	return argvP;
}

void ArgsAdd(Char **argvP, const Char *argP, const Char *parmP, UInt8 *countArgP) {
	if (argP) {
		MemHandle newArg;
		UInt16 len2 = 0;
		UInt16 len1 = StrLen(argP);

		if (len1 > 0) {
			if (parmP)
				len2 = StrLen(parmP);

			(*countArgP)++;
			newArg = MemHandleNew(len1 + len2 + 1); // +1 = NULL CHAR
			*argvP = (Char *)MemHandleLock(newArg);
			StrCopy(*argvP, argP);

			if (parmP)
				StrCat(*argvP, parmP);
		}
	}
}

void ArgsFree(Char **argvP) {
	if (!argvP)
		return;

	MemHandle oldH;

	for(UInt8 count = 0; count < MAX_ARG; count++)
		if (argvP[count]) {
			oldH = MemPtrRecoverHandle(argvP[count]);
			MemHandleUnlock(oldH);
			MemHandleFree(oldH);
		}

	argvP--;
	oldH = MemPtrRecoverHandle(argvP[0]);
	MemHandleUnlock(oldH);
	MemHandleFree(oldH);
}

void ArgsSetOwner(Char **argvP, UInt16 owner) {
	if (!argvP)
		return;

	MemHandle oldH;

	for(UInt8 count = 0; count < MAX_ARG; count++)
		if (argvP[count]) {
			oldH = MemPtrRecoverHandle(argvP[count]);
			MemHandleSetOwner(oldH, owner);
		}

	argvP--;
	oldH = MemPtrRecoverHandle(argvP[0]);
	MemHandleSetOwner(oldH, owner);
}

void ArgsExportInit(Char **argvP, UInt32 countArg, Boolean arm) {
	if (arm) {
		for(UInt8 count = 0; count < MAX_ARG; count++)
			if (argvP[count])
				argvP[count] = (Char *)ByteSwap32(argvP[count]);
	}

	FtrSet(appFileCreator, ftrArgsData , (UInt32)argvP);
	FtrSet(appFileCreator, ftrArgsCount, (UInt32)countArg);
}

void ArgsExportRelease(Boolean arm) {
	if (arm) {
		Char **argvP;
		Err e = FtrGet(appFileCreator, ftrArgsData, (UInt32 *)&argvP);

		if (argvP)
			for(UInt8 count = 0; count < MAX_ARG; count++)
				if (argvP[count])
					argvP[count] = (Char *)ByteSwap32(argvP[count]);
	}

	FtrUnregister(appFileCreator, ftrArgsCount);
	FtrUnregister(appFileCreator, ftrArgsData);
}