Rizin
unix-like reverse engineering framework and cli tools
rz_spaces.h
Go to the documentation of this file.
1 #ifndef RZ_SPACES_H
2 #define RZ_SPACES_H
3 
4 #define RZ_SPACES_MAX 512
5 
6 #include "rz_util.h"
7 #include "rz_serialize.h"
8 #include <sdb.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /*
15  * RzSpaces represents a set of Spaces.
16  * A Space is used to group similar objects and it can have a name. Name
17  * "*"/""/NULL is reserved to indicate "all spaces".
18  *
19  * You can have groups of "meta" (e.g. bin meta, format meta, etc.), groups of
20  * flags, etc.
21  *
22  * It is possible to hook into the RzSpaces functions by using RzEvent.
23  * RZ_SPACE_EVENT_COUNT: called when you need to count how many elements there are in a given RzSpace
24  * RZ_SPACE_EVENT_RENAME: called when renaming a RzSpace with an oldname to a newname
25  * RZ_SPACE_EVENT_UNSET: called when deleting a RzSpace with a given name
26  */
27 
28 typedef struct rz_space_t {
29  char *name;
32 
33 typedef enum {
38 
39 typedef struct rz_space_event_t {
40  union {
41  struct {
42  const RzSpace *space;
43  } count;
44  struct {
45  const RzSpace *space;
46  } unset;
47  struct {
48  const RzSpace *space;
49  const char *oldname;
50  const char *newname;
51  } rename;
52  } data;
53  int res;
55 
56 typedef struct rz_spaces_t {
57  const char *name;
63 
64 // Create a new RzSpaces with the given name
65 RZ_API RzSpaces *rz_spaces_new(const char *name);
66 // Initialize an existing RzSpaces with the given name
67 RZ_API bool rz_spaces_init(RzSpaces *sp, const char *name);
68 // Finalize an existing RzSpaces
70 // Finalize and free an existing RzSpaces
72 // Delete all spaces
74 // Get the RzSpace with the given name
75 RZ_API RzSpace *rz_spaces_get(RzSpaces *sp, const char *name);
76 // Add a new RzSpace if one does not already exist, otherwise return the existing one
77 RZ_API RzSpace *rz_spaces_add(RzSpaces *sp, const char *name);
78 // Add and select a new RzSpace if one does not already exist, otherwise return and select the existing one
79 RZ_API RzSpace *rz_spaces_set(RzSpaces *sp, const char *name);
80 // Remove the RzSpace with the given name or all of them if name is NULL
81 RZ_API bool rz_spaces_unset(RzSpaces *sp, const char *name);
82 // Change the name of RzSpace with oname to nname
83 RZ_API bool rz_spaces_rename(RzSpaces *sp, const char *oname, const char *nname);
84 // Count the elements that belong to the RzSpace with the given name
85 RZ_API int rz_spaces_count(RzSpaces *sp, const char *name);
86 // Add/Select the RzSpace with the given name and save the current one in the history
87 RZ_API bool rz_spaces_push(RzSpaces *sp, const char *name);
88 // Select the RzSpace that was set before the current one
90 
92  return sp->current;
93 }
94 
95 static inline const char *rz_spaces_current_name(RzSpaces *sp) {
96  return sp->current ? sp->current->name : "*";
97 }
98 
99 static inline bool rz_spaces_is_empty(RzSpaces *sp) {
100  RBIter it = rz_rbtree_first(sp->spaces);
101  return it.len == 0;
102 }
103 
105 #define rz_spaces_foreach(sp, it, s) \
106  rz_rbtree_foreach ((sp)->spaces, (it), (s), RzSpace, rb)
107 
108 /* serialize */
109 
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 
120 #endif // RZ_SPACES_H
#define RZ_API
const char * spaces(int count)
RZ_API RBIter rz_rbtree_first(RBNode *root)
Definition: rbtree.c:340
RZ_API RzSpace * rz_spaces_get(RzSpaces *sp, const char *name)
Definition: spaces.c:82
struct rz_space_event_t RzSpaceEvent
RZ_API void rz_spaces_free(RzSpaces *sp)
Definition: spaces.c:42
RZ_API bool rz_spaces_push(RzSpaces *sp, const char *name)
Definition: spaces.c:177
RZ_API void rz_spaces_fini(RzSpaces *sp)
Definition: spaces.c:59
RZ_API RzSpaces * rz_spaces_new(const char *name)
Definition: spaces.c:7
RZ_API RzSpace * rz_spaces_set(RzSpaces *sp, const char *name)
Definition: spaces.c:122
static const char * rz_spaces_current_name(RzSpaces *sp)
Definition: rz_spaces.h:95
RZ_API RzSpace * rz_spaces_add(RzSpaces *sp, const char *name)
Definition: spaces.c:96
RBIter RzSpaceIter
Definition: rz_spaces.h:104
RZ_API int rz_spaces_count(RzSpaces *sp, const char *name)
Definition: spaces.c:167
RZ_API bool rz_spaces_pop(RzSpaces *sp)
Definition: spaces.c:185
RZ_API void rz_spaces_purge(RzSpaces *sp)
Definition: spaces.c:70
struct rz_spaces_t RzSpaces
RzSpaceEventType
Definition: rz_spaces.h:33
@ RZ_SPACE_EVENT_COUNT
Definition: rz_spaces.h:34
@ RZ_SPACE_EVENT_RENAME
Definition: rz_spaces.h:35
@ RZ_SPACE_EVENT_UNSET
Definition: rz_spaces.h:36
static bool rz_spaces_is_empty(RzSpaces *sp)
Definition: rz_spaces.h:99
RZ_API bool rz_spaces_init(RzSpaces *sp, const char *name)
Definition: spaces.c:16
static RzSpace * rz_spaces_current(RzSpaces *sp)
Definition: rz_spaces.h:91
RZ_API bool rz_spaces_rename(RzSpaces *sp, const char *oname, const char *nname)
Definition: spaces.c:196
RZ_API bool rz_serialize_spaces_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzSpaces *spaces, bool load_name, RZ_NULLABLE RzSerializeResultInfo *res)
RZ_API bool rz_spaces_unset(RzSpaces *sp, const char *name)
Definition: spaces.c:141
struct rz_space_t RzSpace
RZ_API void rz_serialize_spaces_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzSpaces *spaces)
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_NONNULL
Definition: rz_types.h:64
Definition: z80asm.h:102
struct rz_space_event_t::@311::@314 rename
const char * oldname
Definition: rz_spaces.h:49
struct rz_space_event_t::@311::@313 unset
const RzSpace * space
Definition: rz_spaces.h:42
struct rz_space_event_t::@311::@312 count
const char * newname
Definition: rz_spaces.h:50
union rz_space_event_t::@311 data
RBNode rb
Definition: rz_spaces.h:30
char * name
Definition: rz_spaces.h:29
RzSpace * current
Definition: rz_spaces.h:58
RzList * spacestack
Definition: rz_spaces.h:60
const char * name
Definition: rz_spaces.h:57
RzEvent * event
Definition: rz_spaces.h:61
RBTree spaces
Definition: rz_spaces.h:59
Definition: sdb.h:63
static int sp
Definition: z80asm.c:91