Skip to content

自定义结构体实现本地缓存

利用QDataStream实现自定义结构体的写入写出:

struct GHImage
{
    QString html_url { "" };
    QString download_url { "" };
    QString name { "" };
    QString github_path { "" };
    QString sha { "" };
    QImage pix;

    friend QDataStream &operator<<(QDataStream &out, const GHImage &info)
    {
        return out << info.html_url << info.download_url << info.name << info.github_path << info.sha << info.pix;
    }

    friend QDataStream &operator>>(QDataStream &in, GHImage &info)
    {
        return in >> info.html_url >> info.download_url >> info.name >> info.github_path >> info.sha >> info.pix;
    }

    inline bool operator==(const GHImage &info)
    {
        return sha == info.sha && github_path == info.github_path;
    }
};

struct GithubInfo
{
    QString gh_token;
    QString user_name;
    QString repo_name;

    friend QDataStream &operator<<(QDataStream &out, const GithubInfo &info)
    {
        return out << info.gh_token << info.user_name << info.repo_name;
    }

    friend QDataStream &operator>>(QDataStream &in, GithubInfo &info)
    {
        return in >> info.gh_token >> info.user_name >> info.repo_name;
    }

    friend QDebug &operator<<(QDebug out, const GithubInfo &info)
    {
        out << info.gh_token << info.user_name << info.repo_name;
        return out;
    }

    inline bool operator==(const GithubInfo &info)
    {
        return gh_token == info.gh_token && user_name == info.user_name && repo_name == info.repo_name;
    }

    inline bool isEmpty() const
    {
        return gh_token.isEmpty() || user_name.isEmpty() || repo_name.isEmpty();
    }
};
using GHImageMap = QMap<QString, QList<GHImage>>;
static GHImageMap images_;
static QSize img_size_;
static GithubInfo info_;

GithubInfo GitHubImageHost::info_;
QSize GitHubImageHost::img_size_(200, 150);
GitHubImageHost::GHImageMap GitHubImageHost::images_;

void GitHubImageHost::WriteImages()
{

    QFile file(image_map_cfg_);
    if (file.open(QIODevice::WriteOnly)) {
        QDataStream out(&file);
        out << info_;
        out << int(images_.size());
        foreach (auto key, images_.keys()) {
            auto &list = images_.value(key);
            out << key << int(list.size());
            foreach (auto var, list) {
                out << var;
            }
        }
        file.close();
    }
}

void GitHubImageHost::ReadImages()
{
    images_.clear();
    QFile file(image_map_cfg_);
    if (file.open(QIODevice::ReadOnly)) {
        QDataStream in(&file);

        GithubInfo info;
        in >> info;
        if (info == info_) {
            int path_size;
            in >> path_size;
            while (path_size > 0) {
                path_size--;
                QString path;
                QList<GHImage> img_list;
                int img_size;
                in >> path >> img_size;
                while (img_size > 0) {
                    img_size--;
                    QString path;
                    GHImage img;
                    in >> img;
                    img_list << img;
                }
                images_[path] = img_list;
            }

        } else {
            qDebug() << u8"githubinfo.cfg 校验失败";
        }

        file.close();
    }
}