33 * Copyright (C) 2016-2023 simplecpp team
44 */
55
6- #if defined(_WIN32)
7- # ifndef _WIN32_WINNT
8- # define _WIN32_WINNT 0x0602
9- # endif
10- # ifndef NOMINMAX
11- # define NOMINMAX
12- # endif
13- # ifndef WIN32_LEAN_AND_MEAN
14- # define WIN32_LEAN_AND_MEAN
15- # endif
16- # include < windows.h>
17- # undef ERROR
18- #endif
19-
206#include " simplecpp.h"
217
228#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
5541# include < direct.h>
5642#else
5743# include < sys/stat.h>
44+ # include < sys/types.h>
45+ #endif
46+
47+ #if defined(_WIN32)
48+ # ifndef _WIN32_WINNT
49+ # define _WIN32_WINNT 0x0602
50+ # endif
51+ # ifndef NOMINMAX
52+ # define NOMINMAX
53+ # endif
54+ # ifndef WIN32_LEAN_AND_MEAN
55+ # define WIN32_LEAN_AND_MEAN
56+ # endif
57+ # include < windows.h>
58+ # undef ERROR
5859#endif
5960
6061static bool isHex (const std::string &s)
@@ -3075,6 +3076,61 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
30753076 return " " ;
30763077}
30773078
3079+ struct FileID {
3080+ #ifdef _WIN32
3081+ struct {
3082+ std::uint64_t VolumeSerialNumber;
3083+ struct {
3084+ std::uint64_t IdentifierHi;
3085+ std::uint64_t IdentifierLo;
3086+ } FileId;
3087+ } fileIdInfo;
3088+ #else
3089+ dev_t dev;
3090+ ino_t ino;
3091+ #endif
3092+ bool operator ==(const FileID& that) const noexcept {
3093+ #ifdef _win32
3094+ return fileIdInfo.VolumeSerialNumber == that.fileIdInfo .VolumeSerialNumber &&
3095+ fileIdInfo.FileId .IdentifierHi == that.fileIdInfo .FileId .IdentifierHi &&
3096+ fileIdInfo.FileId .IdentifierLo == that.fileIdInfo .FileId .IdentifierLo ;
3097+ #else
3098+ return dev == that.dev && ino == that.ino ;
3099+ #endif
3100+ }
3101+
3102+ struct Hasher {
3103+ std::size_t operator ()(const FileID &id) const {
3104+ #ifdef _WIN32
3105+ return static_cast <std::size_t >(id.fileIdInfo .FileId .IdentifierHi ^ id.fileIdInfo .FileId .IdentifierLo ^
3106+ id.fileIdInfo .VolumeSerialNumber );
3107+ #else
3108+ return static_cast <std::size_t >(id.dev ) ^ static_cast <std::size_t >(id.ino );
3109+ #endif
3110+ }
3111+ };
3112+ };
3113+
3114+ struct simplecpp ::FileDataCache::Impl
3115+ {
3116+ void clear ()
3117+ {
3118+ mIdMap .clear ();
3119+ }
3120+
3121+ using id_map_type = std::unordered_map<FileID, FileData *, FileID::Hasher>;
3122+
3123+ id_map_type mIdMap ;
3124+ };
3125+
3126+ simplecpp::FileDataCache::FileDataCache ()
3127+ : mImpl(new Impl)
3128+ {}
3129+
3130+ simplecpp::FileDataCache::~FileDataCache () = default ;
3131+
3132+ static bool getFileId (const std::string &path, FileID &id);
3133+
30783134std::pair<simplecpp::FileData *, bool > simplecpp::FileDataCache::tryload (FileDataCache::name_map_type::iterator &name_it, const simplecpp::DUI &dui, std::vector<std::string> &filenames, simplecpp::OutputList *outputList)
30793135{
30803136 const std::string &path = name_it->first ;
@@ -3083,8 +3139,8 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat
30833139 if (!getFileId (path, fileId))
30843140 return {nullptr , false };
30853141
3086- const auto id_it = mIdMap .find (fileId);
3087- if (id_it != mIdMap .end ()) {
3142+ const auto id_it = mImpl -> mIdMap .find (fileId);
3143+ if (id_it != mImpl -> mIdMap .end ()) {
30883144 name_it->second = id_it->second ;
30893145 return {id_it->second , false };
30903146 }
@@ -3095,7 +3151,7 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat
30953151 data->tokens .removeComments ();
30963152
30973153 name_it->second = data;
3098- mIdMap .emplace (fileId, data);
3154+ mImpl -> mIdMap .emplace (fileId, data);
30993155 mData .emplace_back (data);
31003156
31013157 return {data, true };
@@ -3147,7 +3203,14 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::get(const std::
31473203 return {nullptr , false };
31483204}
31493205
3150- bool simplecpp::FileDataCache::getFileId (const std::string &path, FileID &id)
3206+ void simplecpp::FileDataCache::clear ()
3207+ {
3208+ mImpl ->clear ();
3209+ mNameMap .clear ();
3210+ mData .clear ();
3211+ }
3212+
3213+ bool getFileId (const std::string &path, FileID &id)
31513214{
31523215#ifdef _WIN32
31533216 HANDLE hFile = CreateFileA (path.c_str (), 0 , FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr );
0 commit comments