使用基于范围的 for 循环的 TAR 存档迭代

问题描述

任务是实现 TAR 档案阅读器。根据格式的定义,定义大小为512B的块有两种类型。

class tar {
public:
    class alignas( 512 ) header { 
        bool is_terminator( void ) { ... };
    };

    class alignas( 512 ) data { ... };
};

class tar_iterator {
public:
    using value_type = tar::data;
    using pointer = value_type *;

    tar_iterator( tar::header * header ) noexcept : m_header( header ) {}

    tar_iterator( nullptr_t ) noexcept : m_header( nullptr ) {}

    /* Prefix increment operator */
    tar_iterator & operator++( void ) noexcept {
        m_header = m_header->next();
        return *this;
    }

    /* Postfix increment operator */
    tar_iterator operator++( int ) noexcept {
        tar_iterator temp { *this };
        m_header = m_header->next();
        return temp;
    }

    /* Todo: Dereference operator shall access the stored file */
    tar::data * operator*( void ) const noexcept {

    }


    bool operator==( const tar_iterator & other ) const noexcept {
        return m_header == other.m_header;
    }

    bool operator!=( const tar_iterator & other ) const noexcept {
        return m_header != other.m_header;
    }

    tar::header * get( void ) const {
        return m_header;
    }

    tar_iterator get_next( void ) const noexcept {
        if( m_header != nullptr ) {
            return tar_iterator { m_header->next() };
        } else {
            return tar_iterator { nullptr };
        }
    }

private:
    tar::header * m_header;
};

class tar_fs {
public:
    tar_iterator begin( void ) {
        return { m_start };
    }

    tar_iterator end( void ) {
        /* Todo: <------------ HERE COMES THE MAGIC */
    }

private:
    tar::header * m_start { nullptr }
};

TAR 格式只是一个 512B 长块的流,它是标头或数据。一个标题后面跟着一个或多个数据块。因此,除非对整个 TAR 存档进行迭代和手动计数,否则不知道其中有多少文件

问题是:如果不明确知道标头计数,如何实现 tar_fs::end() 成员函数?有没有其他实现强制函数方法来使 tar_fs 支持基于范围的 for 循环来遍历所有 tar::headers?存档的结尾由全零块 (512B) 标识 - 终止符 - 标头使用 tar::header::is_terminator() 提供信息。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)