Skip to content

Commit

Permalink
main: introduce default trash box that can be used commonly in ctags
Browse files Browse the repository at this point in the history
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Apr 24, 2017
1 parent b1466ae commit a09da8e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#include "read.h"
#include "routines.h"
#include "trace.h"
#include "trashbox.h"
#include "writer.h"

#ifdef HAVE_JANSSON
Expand Down Expand Up @@ -628,6 +629,8 @@ extern int main (int argc CTAGS_ATTR_UNUSED, char **argv)
{
cookedArgs *args;

initDefaultTrashBox ();

DEBUG_INIT();

setErrorPrinter (stderrDefaultErrorPrinter, NULL);
Expand Down Expand Up @@ -668,6 +671,8 @@ extern int main (int argc CTAGS_ATTR_UNUSED, char **argv)
freeEncodingResources ();
#endif

finiDefaultTrashBox();

if (Option.printLanguage)
return (Option.printLanguage == true)? 0: 1;

Expand Down
33 changes: 32 additions & 1 deletion main/trashbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct sTrashBox {
Trash *trash;
};

static TrashBox* defaultTrashBox;

static Trash* trashPut (Trash* trash, void* item,
TrashDestroyItemProc destrctor);
static Trash* trashTakeBack (Trash* trash, void* item, TrashDestroyItemProc* destrctor);
Expand All @@ -41,20 +43,29 @@ extern TrashBox* trashBoxStack (TrashBox* trash_box)
{
TrashBox *t = trashBoxNew();

if (!trash_box)
trash_box = defaultTrashBox;

trashBoxPut (trash_box, t, (TrashBoxDestroyItemProc) trashBoxDelete);

return t;
}

extern void trashBoxDelete (TrashBox* trash_box)
{
if (!trash_box)
trash_box = defaultTrashBox;

trashBoxMakeEmpty(trash_box);

eFree (trash_box);
}

extern void* trashBoxPut (TrashBox* trash_box, void* item, TrashBoxDestroyItemProc destroy)
{
if (!trash_box)
trash_box = defaultTrashBox;

trash_box->trash = trashPut(trash_box->trash, item, destroy);
return item;
}
Expand All @@ -63,20 +74,29 @@ extern TrashBoxDestroyItemProc trashBoxTakeBack (TrashBox* trash_box, void* item
{
TrashBoxDestroyItemProc d;

if (!trash_box)
trash_box = defaultTrashBox;

trash_box->trash = trashTakeBack(trash_box->trash, item, &d);
return d;
}

extern void trashBoxMakeEmpty (TrashBox* trash_box)
{
trash_box->trash = trashMakeEmpty (trash_box->trash);
if (!trash_box)
trash_box = defaultTrashBox;

trash_box->trash = trashMakeEmpty (trash_box->trash);
}


extern void trashBoxFree (TrashBox* trash_box, void* item)
{
TrashBoxDestroyItemProc d;

if (!trash_box)
trash_box = defaultTrashBox;

d = trashBoxTakeBack (trash_box, item);
d (item);
}
Expand Down Expand Up @@ -145,6 +165,17 @@ static Trash* trashMakeEmpty (Trash* trash)
return NULL;
}

extern void initDefaultTrashBox (void)
{
defaultTrashBox = trashBoxNew ();
}

extern void finiDefaultTrashBox (void)
{
trashBoxDelete (defaultTrashBox);
defaultTrashBox = NULL;
}

#ifdef TRASH_TEST
#include <stdio.h>

Expand Down
4 changes: 4 additions & 0 deletions main/trashbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ extern TrashBoxDestroyItemProc trashBoxTakeBack (TrashBox* trash_box, void* ite
extern void trashBoxFree (TrashBox* trash_box, void* item);
extern void trashBoxMakeEmpty (TrashBox* trash_box);

#define TRASH_BOX(PTR,PROC) trashBoxPut(NULL, PTR,PROC)
extern void initDefaultTrashBox (void);
extern void finiDefaultTrashBox (void);

#endif /* CTAGS_MAIN_TRASH_H */

0 comments on commit a09da8e

Please sign in to comment.