Skip to content

Commit 14eb82f

Browse files
committed
main: add a helper function for counting entries in a scope
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent da68867 commit 14eb82f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

main/entry.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,43 @@ extern bool foreachEntriesInScope (int corkIndex,
13381338
return true;
13391339
}
13401340

1341+
struct countData {
1342+
bool onlyDefinitionTag;
1343+
unsigned int count;
1344+
entryForeachFunc func;
1345+
void *cbData;
1346+
};
1347+
1348+
static bool countEntryMaybe (int corkIndex, tagEntryInfo *entry, void *cbData)
1349+
{
1350+
struct countData *data = cbData;
1351+
if (data->onlyDefinitionTag
1352+
&& !isRoleAssigned (entry, ROLE_DEFINITION_INDEX))
1353+
return true;
1354+
1355+
if (data->func == NULL
1356+
|| data->func (corkIndex, entry, data->cbData))
1357+
data->count++;
1358+
return true;
1359+
}
1360+
1361+
unsigned int countEntriesInScope (int corkIndex, bool onlyDefinitionTag,
1362+
entryForeachFunc func, void *cbData)
1363+
{
1364+
struct countData data = {
1365+
.onlyDefinitionTag = onlyDefinitionTag,
1366+
.count = 0,
1367+
.func = func,
1368+
.cbData = cbData,
1369+
};
1370+
1371+
foreachEntriesInScope (corkIndex, NULL,
1372+
&countEntryMaybe,
1373+
&data);
1374+
1375+
return data.count;
1376+
}
1377+
13411378
struct anyEntryInScopeData {
13421379
int index;
13431380
bool onlyDefinitionTag;

main/entry.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ bool foreachEntriesInScope (int corkIndex,
192192
entryForeachFunc func,
193193
void *data);
194194

195+
unsigned int countEntriesInScope (int corkIndex, bool onlyDefinitionTag,
196+
entryForeachFunc func, void *data);
197+
195198
/* Return the cork index for NAME in the scope specified with CORKINDEX.
196199
* Even if more than one entries for NAME are in the scope, this function
197200
* just returns one of them. Returning CORK_NIL means there is no entry

0 commit comments

Comments
 (0)