Add macros for iterating the containing entry
This commit is contained in:
+22
-14
@@ -23,32 +23,38 @@ void add_block(struct File *f, const char *txt)
|
||||
|
||||
void print_file(struct File *f)
|
||||
{
|
||||
struct TailQ *entry;
|
||||
struct Block *entry;
|
||||
printf("File: %s\n", f->name);
|
||||
UC_TAILQ_FOREACH(entry, &f->head) {
|
||||
struct Block *b = UC_CONTAINER_OF(entry, struct Block, blocks);
|
||||
printf("Block: %s\n", b->text);
|
||||
UC_TAILQ_FOREACH_CONTAINER(entry, blocks, &f->head) {
|
||||
printf("Block: %s\n", entry->text);
|
||||
}
|
||||
}
|
||||
|
||||
void print_file_reverse(struct File *f)
|
||||
{
|
||||
struct Block *entry;
|
||||
printf("File: %s\n", f->name);
|
||||
UC_TAILQ_FOREACH_REVERSE_CONTAINER(entry, blocks, &f->head) {
|
||||
printf("Block: %s\n", entry->text);
|
||||
}
|
||||
}
|
||||
|
||||
void delete_block(struct File *f, const char *txt)
|
||||
{
|
||||
struct TailQ *entry, *next;
|
||||
UC_TAILQ_FOREACH_SAFE(entry, next, &f->head) {
|
||||
struct Block *b = UC_CONTAINER_OF(entry, struct Block, blocks);
|
||||
if (strcmp(txt, b->text) == 0) {
|
||||
uc_tailq_remove(entry);
|
||||
free(b);
|
||||
struct Block *entry, *next;
|
||||
UC_TAILQ_FOREACH_CONTAINER_SAFE(entry, next, blocks, &f->head) {
|
||||
if (strcmp(txt, entry->text) == 0) {
|
||||
uc_tailq_remove(&entry->blocks);
|
||||
free(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void delete_all_blocks(struct File *f)
|
||||
{
|
||||
struct TailQ *entry, *next;
|
||||
UC_TAILQ_FOREACH_SAFE(entry, next, &f->head) {
|
||||
struct Block *b = UC_CONTAINER_OF(entry, struct Block, blocks);
|
||||
free(b);
|
||||
struct Block *entry, *next;
|
||||
UC_TAILQ_FOREACH_CONTAINER_SAFE(entry, next, blocks, &f->head) {
|
||||
free(entry);
|
||||
}
|
||||
uc_tailq_init(&f->head);
|
||||
}
|
||||
@@ -74,6 +80,8 @@ int main(void)
|
||||
add_block(&f,"three");
|
||||
add_block(&f,"three");
|
||||
print_file(&f);
|
||||
puts("\nReverse order:");
|
||||
print_file_reverse(&f);
|
||||
|
||||
puts("\nDeleting three");
|
||||
delete_block(&f, "three");
|
||||
|
||||
Reference in New Issue
Block a user