Skip to content

Commit 3e67a0f

Browse files
committed
readtags,refactor: make Sorter work when multiple tags is given
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 6edc863 commit 3e67a0f

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

extra-cmds/readtags-cmd.c

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ struct actionSpec {
6464
const char *name; /* for ACTION_FIND */
6565
bool canonicalizing;
6666
struct canonWorkArea canon;
67+
ptrArray *tagEntryArray;
68+
void (* walkerfn) (const tagEntry *, void *);
69+
void *dataForWalkerFn;
6770
};
6871

6972
static const char *ProgramName;
@@ -183,9 +186,7 @@ static void walkTags (tagFile *const file, tagEntry *first_entry,
183186
void (* actionfn) (const tagEntry *, void *), void *data,
184187
struct actionSpec *actionSpec)
185188
{
186-
ptrArray *a = NULL;
187-
if (Sorter)
188-
a = ptrArrayNew ((ptrArrayDeleteFunc)freeCopiedTag);
189+
ptrArray *a = actionSpec->tagEntryArray;
189190

190191
do
191192
{
@@ -233,14 +234,8 @@ static void walkTags (tagFile *const file, tagEntry *first_entry,
233234

234235
if (a)
235236
{
236-
ptrArraySort (a, compareTagEntry);
237-
size_t count = ptrArrayCount(a);
238-
for (unsigned int i = 0; i < count; i++)
239-
{
240-
tagEntry *e = ptrArrayItem (a, i);
241-
(* actionfn) (e, data);
242-
}
243-
ptrArrayDelete (a);
237+
actionSpec->walkerfn = actionfn;
238+
actionSpec->dataForWalkerFn = data;
244239
}
245240
}
246241
#else
@@ -774,6 +769,9 @@ extern int main (int argc, char **argv)
774769
.ptags = false,
775770
/* .absoluteOnly = false, */
776771
},
772+
.tagEntryArray = NULL,
773+
.walkerfn = NULL,
774+
.dataForWalkerFn = NULL,
777775
};
778776

779777
memset (&printOpts, 0, sizeof (printOpts));
@@ -1106,7 +1104,14 @@ extern int main (int argc, char **argv)
11061104
break;
11071105
}
11081106

1109-
for (unsigned int i = 0; i < ptrArrayCount (inputSpecs); i++)
1107+
unsigned int count = ptrArrayCount (inputSpecs);
1108+
/* TODO: if Sorter == NULL but if count > 1 && sort is set to true in !_TAG_FILE_SORT,
1109+
* we have to sort as if !_TAG_FILE_SORT is true.
1110+
* Sorter must be build here. */
1111+
if (Sorter || count > 1)
1112+
actionSpec.tagEntryArray = ptrArrayNew ((ptrArrayDeleteFunc)freeCopiedTag);
1113+
1114+
for (unsigned int i = 0; i < count; i++)
11101115
{
11111116
struct inputSpec *inputSpec = ptrArrayItem (inputSpecs, i);
11121117

@@ -1137,6 +1142,19 @@ extern int main (int argc, char **argv)
11371142
}
11381143
}
11391144

1145+
if (actionSpec.tagEntryArray)
1146+
{
1147+
if (Sorter)
1148+
ptrArraySort (actionSpec.tagEntryArray, compareTagEntry);
1149+
size_t count = ptrArrayCount(actionSpec.tagEntryArray);
1150+
for (unsigned int i = 0; i < count; i++)
1151+
{
1152+
tagEntry *e = ptrArrayItem (actionSpec.tagEntryArray, i);
1153+
actionSpec.walkerfn (e, actionSpec.dataForWalkerFn);
1154+
}
1155+
ptrArrayDelete (actionSpec.tagEntryArray);
1156+
}
1157+
11401158
out:
11411159
ptrArrayDelete (inputSpecs);
11421160

0 commit comments

Comments
 (0)