Casting

You can cast any BkFileBase to a more elaborate structure. First you use the macros from the previous section to find out what type the BkFileBase is and then you use the macros in this section to cast them.

BK_DIR_PTR()

Use this macro when you have a BkFileBase* that you're sure (having checked with IS_DIR()) is a directory.

void printName(BkFileBase* base)
{
    if(IS_DIR(base->posixFileMode))
    {
        /* print name of the directory */
        printf("directory %s\n", base->name);
        
        /* print all the directory's children */
        BkFileBase* child = BK_DIR_PTR(base)->children;
        while(child != NULL)
        {
            printName(child);
            child = child->next;
        }
    }
}

BK_FILE_PTR()

Use this macro when you have a BkFileBase* that you're sure (having checked with IS_REG_FILE()) is a regular file.

void printName(BkFileBase* base)
{
    if(IS_REG_FILE(base->posixFileMode))
    {
        /* print name and size of the file */
        printf("regular file %s, size %u\n", base->name, BK_FILE_PTR(base)->size);
    }
}

BK_SYMLINK_PTR()

Use this macro when you have a BkFileBase* that you're sure (having checked with IS_SYMLINK()) is a symbolic link.

void printName(BkFileBase* base)
{
    if(IS_SYMLINK(base->posixFileMode))
    {
        /* print name and target of the symbolic link */
        printf("symbolic link %s -> %s\n", base->name, BK_SYMLINK_PTR(base)->target);
    }
}

BK_BASE_PTR()

Sometimes you may have a BkDir* or a BkFile* or a BkSymLink* and would like to use the BkFileBase* part of it. That's what this macro is for.

void printDirName(BkDir* dir)
{
    printf("%s\n", BK_FILE_BASE(dir)->name);
}