如何解决使用基于范围的 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 举报,一经查实,本站将立刻删除。