aboutsummaryrefslogtreecommitdiff
path: root/project/src/lib-object.c
blob: 23ffcdd9d9e3120c0a8f31935ad4f26abbf2f074 (plain) (blame)
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

/***************************************************************************
 *                         _______ _______ _______                         *
 *                        |  ___  |____   |  ___  |                        *
 *                        | |   |_|    / /| |   |_|                        *
 *                        | |_____    / / | |_____                         *
 *                        |_____  |  / /  |_____  |                        *
 *                         _    | | / /    _    | |                        *
 *                        | |___| |/ /____| |___| |                        *
 *                        |_______|_______|_______|                        *
 *                                                                         *
 *                            Wiimms SZS Tools                             *
 *                          https://szs.wiimm.de/                          *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   This file is part of the SZS project.                                 *
 *   Visit https://szs.wiimm.de/ for project details and sources.          *
 *                                                                         *
 *   Copyright (c) 2011-2022 by Dirk Clemens <wiimm@wiimm.de>              *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt       *
 *                                                                         *
 ***************************************************************************/

#include "lib-object.h"

//
///////////////////////////////////////////////////////////////////////////////
///////////////			find helpers			///////////////
///////////////////////////////////////////////////////////////////////////////

static uint Mark
(
    const s16	*start,		// start of index field
    u8		*status,	// status vector
    uint	size,		// size of status vector (for assertions)
    u8		mask		// =0: add +1 / >0: OR mask
)
{
    DASSERT(start);
    DASSERT(status);

    const s16 *ptr = start;
    if (mask)
    	while ( *ptr >= 0 )
	{
	    DASSERT( *ptr < size );
	    noPRINT("MARK[%u] |= %02x\n",*ptr,mask);
	    status[*ptr++] |= mask;
	}
    else
    	while ( *ptr >= 0 )
	{
	    DASSERT( *ptr < size );
	    if (!++status[*ptr++])
		status[ptr[-1]]--;
	}

    return ptr - start;
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			Find DB files			///////////////
///////////////////////////////////////////////////////////////////////////////

int FindDbFile
(
    // returns the index of the file or -1 if not found

    ccp			file		// file to find, preeceding './' possible
)
{
    DASSERT(file);
    if ( file[0] == '.' && file[1] == '/' )
	file += 2;

    int beg = 0;
    int end = N_DB_FILE_FILE - 1;
    while ( beg <= end )
    {
	uint idx = (beg+end)/2;
	int stat = strcmp(file,DbFileFILE[idx].file);
	if ( stat < 0 )
	    end = idx - 1 ;
	else if ( stat > 0 )
	    beg = idx + 1;
	else
	    return idx;
    }
    return -1;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

uint FindDbFileByGroup
(
    // returns the number of found files

    UsedFileFILE_t	*status,	// modify a byte for each found file
    bool		init_status,	// clear 'status' first
    uint		group,		// index of group (robust)
    u8			mask		// =0: add +1 / >0: OR mask
)
{
    noPRINT("FindDbFileByGroup(,%d,%u) size=%zu\n",
		init_status, group, sizeof(*status) );

    DASSERT(status);
    if (init_status)
	memset(status,0,sizeof(*status));

    if ( group < N_DB_FILE_GROUP )
    {
	DASSERT( DbFileGROUP[group].ref >= 0 );
	return Mark( DbFileRefGROUP + DbFileGROUP[group].ref,
			status->d, N_DB_FILE_FILE, mask );
    }
    return 0;
}

///////////////////////////////////////////////////////////////////////////////

uint FindDbFileByGroups
(
    // returns the number of found files

    UsedFileFILE_t		*status,	// modify a byte for each found file
    bool			init_status,	// clear 'status' first
    const UsedFileGROUP_t	*group		// NULL or list of used groups
)
{
    noPRINT("FindDbFileByGroups(,%d,)\n",init_status);

    DASSERT(status);
    if (init_status)
	memset(status,0,sizeof(*status));

    int count = 0;
    if (group)
    {
	int gi;
	for ( gi = 0; gi < N_DB_FILE_GROUP; gi++ )
	    if (group->d[gi])
	    {
		noPRINT("group[%u] = %u -> %d -> %d, %d, %d\n",gi,
			group->d[gi],DbFileGROUP[gi].ref,
			DbFileRefGROUP[DbFileGROUP[gi].ref+0],
			DbFileRefGROUP[DbFileGROUP[gi].ref+1],
			DbFileRefGROUP[DbFileGROUP[gi].ref+2]);

		count += Mark( DbFileRefGROUP + DbFileGROUP[gi].ref,
			    status->d, N_DB_FILE_FILE, group->d[gi] );
	    }
    }

    #if HAVE_PRINT0
    {
	int fi;
	for ( fi = 0; fi < N_DB_FILE_FILE; fi++ )
	    if (status->d[fi])
		printf(" -> FILE: %4u %02x %p\n",
			fi, status->d[fi], DbFileFILE[fi].file );
    }
    #endif

    return count;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

uint FindDbFileByObject
(
    // returns the number of found files

    UsedFileFILE_t	*status,	// modify a byte for each found file
    bool		init_status,	// clear 'status' first
    uint		object,		// index of object (robust)
    u8			mask		// =0: add +1 / >0: OR mask
)
{
    noPRINT("FindDbFileByObject(,%d,%u) size=%zu\n",
		init_status, object, sizeof(*status) );

    DASSERT(status);
    if (init_status)
	memset(status,0,sizeof(*status));

    UsedFileGROUP_t used_group;
    FindDbGroupByObject(&used_group,true,object,mask);
    return FindDbFileByGroups(status,init_status,&used_group);
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			Find DB groups			///////////////
///////////////////////////////////////////////////////////////////////////////

uint FindDbGroupByObject
(
    // returns the number of found groups

    UsedFileGROUP_t	*status,	// modify a byte for each found file
    bool		init_status,	// clear 'status' first
    uint		object,		// index of object (robust)
    u8			mask		// =0: add +1 / >0: OR mask
)
{
    noPRINT("FindDbGroupByObjects(,%d,)\n",init_status);

    DASSERT(status);
    if (init_status)
	memset(status,0,sizeof(*status));

    int count = 0;
    if ( object < N_KMP_GOBJ )
    {
	const ObjectInfo_t *oi = ObjectInfo + object;
	if ( oi->fname )
	{
	    int i;
	    for ( i = 0; i < sizeof(oi->group_idx)/sizeof(*oi->group_idx); i++ )
	    {
		uint fid = oi->group_idx[i];
		if ( fid < N_DB_FILE_GROUP )
		{
		    if (mask)
			status->d[fid] |= mask;
		    else if (!++status->d[fid])
			status->d[fid]--;
		    count++;
		}
	    }
	}
    }
    return count;
}

///////////////////////////////////////////////////////////////////////////////

uint FindDbGroupByObjects
(
    // returns the number of found groups

    UsedFileGROUP_t		*status,	// modify a byte for each found file
    bool			init_status,	// clear 'status' first
    const UsedObject_t		*object		// NULL or list of used objects
)
{
    noPRINT("FindDbGroupByObjects(,%d,)\n",init_status);

    DASSERT(status);
    if (init_status)
	memset(status,0,sizeof(*status));

    int count = 0;
    if (object)
    {
	int obj;
	const ObjectInfo_t *oi = ObjectInfo;
	for ( obj = 0; obj < N_KMP_GOBJ; obj++, oi++ )
	    if ( object->d[obj] && oi->fname )
	    {
		int i;
		for ( i = 0; i < sizeof(oi->group_idx)/sizeof(*oi->group_idx); i++ )
		{
		    uint fid = oi->group_idx[i];
		    if ( fid < N_DB_FILE_GROUP )
		    {
			status->d[fid] |= object->d[obj];
			count++;
		    }
		}
	    }
    }

    #if HAVE_PRINT0
    {
	int gi;
	for ( gi = 0; gi < N_DB_FILE_GROUP; gi++ )
	    if (status->d[gi])
		printf(" -> GROUP: %4u %02x %s\n",
			gi, status->d[gi], DbFileGROUP[gi].name );
    }
    #endif

    return count;
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			for external modules		///////////////
///////////////////////////////////////////////////////////////////////////////

void DefineObjectNameVars ( VarMap_t *vm )
{
    DASSERT(vm);

    char varname[50], *varname_end = varname + sizeof(varname)-1;
    const ObjectInfo_t *oi, *oi_end = ObjectInfo + N_KMP_GOBJ;
    int index = 0;
    for ( oi = ObjectInfo; oi < oi_end; oi++, index++ )
	if (oi->name)
	{
	    char * vn = varname;
	    *vn++ = 'O';
	    *vn++ = '$';
	    ccp src = oi->name;
	    while ( *src && vn < varname_end )
		*vn++ = toupper((int)*src++);
	    *vn = 0;

	    DefineIntVar(vm,varname,index);
	}
}

//
///////////////////////////////////////////////////////////////////////////////
///////////////			    END				///////////////
///////////////////////////////////////////////////////////////////////////////