Doxygen
Loading...
Searching...
No Matches
FilterCache Class Reference

Classes

struct  FilterCacheItem

Public Member Functions

bool getFileContents (const QCString &fileName, size_t startLine, size_t endLine, std::string &str)

Static Public Member Functions

static FilterCacheinstance ()

Private Types

using LineOffsets = std::vector<size_t>

Private Member Functions

bool getFileContentsPipe (const QCString &fileName, const QCString &filter, size_t startLine, size_t endLine, std::string &str)
bool getFileContentsDisk (const QCString &fileName, size_t startLine, size_t endLine, std::string &str)
void compileLineOffsets (const QCString &fileName, const std::string &str)
auto getFragmentLocation (const LineOffsets &lineOffsets, size_t startLine, size_t endLine) -> std::tuple< size_t, size_t >
void shrinkBuffer (std::string &str, const QCString &fileName, size_t startLine, size_t endLine)
void readFragmentFromFile (std::string &str, const QCString &fileName, size_t startOffset, size_t size=0)
 FilterCache ()

Private Attributes

std::unordered_map< std::string, FilterCacheItemm_cache
std::unordered_map< std::string, LineOffsetsm_lineOffsets
std::mutex m_mutex
size_t m_endPos

Detailed Description

Cache for storing the result of filtering a file

Definition at line 529 of file definition.cpp.

Member Typedef Documentation

◆ LineOffsets

using FilterCache::LineOffsets = std::vector<size_t>
private

Definition at line 537 of file definition.cpp.

Constructor & Destructor Documentation

◆ FilterCache()

FilterCache::FilterCache ( )
inlineprivate

Definition at line 721 of file definition.cpp.

721: m_endPos(0) { }
size_t m_endPos

References m_endPos.

Referenced by instance().

Member Function Documentation

◆ compileLineOffsets()

void FilterCache::compileLineOffsets ( const QCString & fileName,
const std::string & str )
inlineprivate

computes the starting offset for each line for file fileName, whose contents should already be stored in buffer str.

Definition at line 665 of file definition.cpp.

666 {
667 // line 1 (index 0) is at offset 0
668 auto it = m_lineOffsets.emplace(fileName.data(),LineOffsets{0}).first;
669 const char *p=str.data();
670 while (*p)
671 {
672 char c=0;
673 while ((c=*p)!='\n' && c!=0) p++; // search until end of the line
674 if (c!=0) p++;
675 it->second.push_back(p-str.data());
676 }
677 }
std::vector< size_t > LineOffsets
std::unordered_map< std::string, LineOffsets > m_lineOffsets
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition qcstring.h:172

References QCString::data(), and m_lineOffsets.

Referenced by shrinkBuffer().

◆ getFileContents()

bool FilterCache::getFileContents ( const QCString & fileName,
size_t startLine,
size_t endLine,
std::string & str )
inline

collects the part of file fileName starting at startLine and ending at endLine into buffer str. Applies filtering if FILTER_SOURCE_FILES is enabled and the file extension matches a filter. Caches file information so that subsequent extraction of blocks from the same file can be performed efficiently

Definition at line 546 of file definition.cpp.

547 {
548 bool filterSourceFiles = Config_getBool(FILTER_SOURCE_FILES);
549 QCString filter = getFileFilter(fileName,TRUE);
550 bool usePipe = !filter.isEmpty() && filterSourceFiles;
551 return usePipe ? getFileContentsPipe(fileName,filter,startLine,endLine,str)
552 : getFileContentsDisk(fileName,startLine,endLine,str);
553 }
bool getFileContentsDisk(const QCString &fileName, size_t startLine, size_t endLine, std::string &str)
bool getFileContentsPipe(const QCString &fileName, const QCString &filter, size_t startLine, size_t endLine, std::string &str)
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:163
#define Config_getBool(name)
Definition config.h:33
#define TRUE
Definition qcstring.h:37
QCString getFileFilter(const QCString &name, bool isSourceCode)
Definition util.cpp:1367

References Config_getBool, getFileContentsDisk(), getFileContentsPipe(), getFileFilter(), QCString::isEmpty(), and TRUE.

Referenced by readCodeFragment().

◆ getFileContentsDisk()

bool FilterCache::getFileContentsDisk ( const QCString & fileName,
size_t startLine,
size_t endLine,
std::string & str )
inlineprivate

reads the fragment start at startLine and ending at endLine from file fileName into buffer str

Definition at line 639 of file definition.cpp.

640 {
641 std::unique_lock<std::mutex> lock(m_mutex);
642 // normal file
643 //printf("getFileContents(%s): no filter\n",qPrint(fileName));
644 auto it = m_lineOffsets.find(fileName.str());
645 if (it == m_lineOffsets.end()) // new file
646 {
647 // read file completely into str buffer
648 readFragmentFromFile(str,fileName,0);
649 // shrink buffer to [startLine..endLine] part
650 shrinkBuffer(str,fileName,startLine,endLine);
651 }
652 else // file already processed before
653 {
654 lock.unlock();
655 auto [ startLineOffset, fragmentSize] = getFragmentLocation(it->second,startLine,endLine);
656 //printf("%s: existing file [%zu-%zu] -> start=%zu size=%zu\n",
657 // qPrint(fileName),startLine,endLine,startLineOffset,fragmentSize);
658 readFragmentFromFile(str,fileName,startLineOffset,fragmentSize);
659 }
660 return true;
661 }
void shrinkBuffer(std::string &str, const QCString &fileName, size_t startLine, size_t endLine)
auto getFragmentLocation(const LineOffsets &lineOffsets, size_t startLine, size_t endLine) -> std::tuple< size_t, size_t >
void readFragmentFromFile(std::string &str, const QCString &fileName, size_t startOffset, size_t size=0)
std::mutex m_mutex
const std::string & str() const
Definition qcstring.h:552

References getFragmentLocation(), m_lineOffsets, m_mutex, readFragmentFromFile(), shrinkBuffer(), and QCString::str().

Referenced by getFileContents().

◆ getFileContentsPipe()

bool FilterCache::getFileContentsPipe ( const QCString & fileName,
const QCString & filter,
size_t startLine,
size_t endLine,
std::string & str )
inlineprivate

Definition at line 555 of file definition.cpp.

557 {
558 std::unique_lock<std::mutex> lock(m_mutex);
559 auto it = m_cache.find(fileName.str());
560 if (it!=m_cache.end()) // cache hit: reuse stored result
561 {
562 lock.unlock();
563 auto item = it->second;
564 //printf("getFileContents(%s): cache hit\n",qPrint(fileName));
565 // file already processed, get the results after filtering from the tmp file
566 Debug::print(Debug::FilterOutput,0,"Reusing filter result for {} from {} at offset={} size={}\n",
567 fileName,Doxygen::filterDBFileName,item.filePos,item.fileSize);
568
569 auto it_off = m_lineOffsets.find(fileName.str());
570 assert(it_off!=m_lineOffsets.end());
571 auto [ startLineOffset, fragmentSize] = getFragmentLocation(it_off->second,startLine,endLine);
572 //printf("%s: existing file [%zu-%zu]->[%zu-%zu] size=%zu\n",
573 // qPrint(fileName),startLine,endLine,startLineOffset,endLineOffset,fragmentSize);
575 item.filePos+startLineOffset, fragmentSize);
576 return true;
577 }
578 else // cache miss: filter active but file not previously processed
579 {
580 //printf("getFileContents(%s): cache miss\n",qPrint(fileName));
581 // filter file
582 QCString cmd=filter+" \""+fileName+"\"";
583 Debug::print(Debug::ExtCmd,0,"Executing popen(`{}`)\n",cmd);
584 FILE *f = Portable::popen(cmd,"r");
585 if (f==nullptr)
586 {
587 // handle error
588 err("Error opening filter pipe command '{}'\n",cmd);
589 return false;
590 }
592 FilterCacheItem item;
593 item.filePos = m_endPos;
594 if (bf==nullptr)
595 {
596 // handle error
597 err("Error opening filter database file {}\n",Doxygen::filterDBFileName);
599 return false;
600 }
601 // append the filtered output to the database file
602 size_t size=0;
603 while (!feof(f))
604 {
605 const int blockSize = 4096;
606 char buf[blockSize];
607 size_t bytesRead = fread(buf,1,blockSize,f);
608 size_t bytesWritten = fwrite(buf,1,bytesRead,bf);
609 if (bytesRead!=bytesWritten)
610 {
611 // handle error
612 err("Failed to write to filter database {}. Wrote {} out of {} bytes\n",
613 Doxygen::filterDBFileName,bytesWritten,bytesRead);
615 fclose(bf);
616 return false;
617 }
618 size+=bytesWritten;
619 str+=std::string_view(buf,bytesWritten);
620 }
621 item.fileSize = size;
622 // add location entry to the dictionary
623 m_cache.emplace(fileName.str(),item);
624 Debug::print(Debug::FilterOutput,0,"Storing new filter result for {} in {} at offset={} size={}\n",
625 fileName,Doxygen::filterDBFileName,item.filePos,item.fileSize);
626 // update end of file position
627 m_endPos += size;
629 fclose(bf);
630
631 // shrink buffer to [startLine..endLine] part
632 shrinkBuffer(str,fileName,startLine,endLine);
633 }
634 return true;
635 }
@ FilterOutput
Definition debug.h:38
@ ExtCmd
Definition debug.h:36
static void print(DebugMask mask, int prio, fmt::format_string< Args... > fmt, Args &&... args)
Definition debug.h:76
static QCString filterDBFileName
Definition doxygen.h:133
std::unordered_map< std::string, FilterCacheItem > m_cache
#define err(fmt,...)
Definition message.h:127
FILE * popen(const QCString &name, const QCString &type)
Definition portable.cpp:480
FILE * fopen(const QCString &fileName, const QCString &mode)
Definition portable.cpp:350
int pclose(FILE *stream)
Definition portable.cpp:489
int fclose(FILE *f)
Definition portable.cpp:370

References err, Debug::ExtCmd, FilterCache::FilterCacheItem::filePos, FilterCache::FilterCacheItem::fileSize, Doxygen::filterDBFileName, Debug::FilterOutput, Portable::fopen(), getFragmentLocation(), m_cache, m_endPos, m_lineOffsets, m_mutex, Portable::pclose(), Portable::popen(), Debug::print(), readFragmentFromFile(), shrinkBuffer(), and QCString::str().

Referenced by getFileContents().

◆ getFragmentLocation()

auto FilterCache::getFragmentLocation ( const LineOffsets & lineOffsets,
size_t startLine,
size_t endLine ) -> std::tuple< size_t, size_t >
inlineprivate

Returns the byte offset and size within a file of a fragment given the array of line offsets and the start and end line of the fragment.

Definition at line 681 of file definition.cpp.

683 {
684 assert(startLine > 0);
685 assert(startLine <= endLine);
686 const size_t startLineOffset = lineOffsets[std::min(startLine-1,lineOffsets.size()-1)];
687 const size_t endLineOffset = lineOffsets[std::min(endLine, lineOffsets.size()-1)];
688 assert(startLineOffset <= endLineOffset);
689 const size_t fragmentSize = endLineOffset-startLineOffset;
690 return std::tie(startLineOffset,fragmentSize);
691 }

Referenced by getFileContentsDisk(), getFileContentsPipe(), and shrinkBuffer().

◆ instance()

FilterCache & FilterCache::instance ( )
static

Definition at line 728 of file definition.cpp.

729{
730 static FilterCache theInstance;
731 return theInstance;
732}

References FilterCache().

Referenced by readCodeFragment().

◆ readFragmentFromFile()

void FilterCache::readFragmentFromFile ( std::string & str,
const QCString & fileName,
size_t startOffset,
size_t size = 0 )
inlineprivate

Reads the fragment start at byte offset startOffset of file fileName into buffer str. Result will be a null terminated. If size==0 the whole file will be read and startOffset is ignored. If size>0, size bytes will be read.

Definition at line 712 of file definition.cpp.

713 {
714 std::ifstream ifs = Portable::openInputStream(fileName,true,true);
715 if (size==0) { startOffset=0; size = static_cast<size_t>(ifs.tellg()); }
716 ifs.seekg(startOffset, std::ios::beg);
717 str.resize(size);
718 ifs.read(str.data(), size);
719 }
std::ifstream openInputStream(const QCString &name, bool binary=false, bool openAtEnd=false)
Definition portable.cpp:660

References Portable::openInputStream().

Referenced by getFileContentsDisk(), and getFileContentsPipe().

◆ shrinkBuffer()

void FilterCache::shrinkBuffer ( std::string & str,
const QCString & fileName,
size_t startLine,
size_t endLine )
inlineprivate

Shrinks buffer str which should hold the contents of fileName to the fragment starting a line startLine and ending at line endLine

Definition at line 695 of file definition.cpp.

696 {
697 // compute offsets from start for each line
698 compileLineOffsets(fileName,str);
699 auto it = m_lineOffsets.find(fileName.str());
700 assert(it!=m_lineOffsets.end());
701 const LineOffsets &lineOffsets = it->second;
702 auto [ startLineOffset, fragmentSize] = getFragmentLocation(lineOffsets,startLine,endLine);
703 //printf("%s: new file [%zu-%zu]->[%zu-%zu] size=%zu\n",
704 // qPrint(fileName),startLine,endLine,startLineOffset,endLineOffset,fragmentSize);
705 str.erase(0,startLineOffset);
706 str.resize(fragmentSize);
707 }
void compileLineOffsets(const QCString &fileName, const std::string &str)

References compileLineOffsets(), getFragmentLocation(), m_lineOffsets, and QCString::str().

Referenced by getFileContentsDisk(), and getFileContentsPipe().

Member Data Documentation

◆ m_cache

std::unordered_map<std::string,FilterCacheItem> FilterCache::m_cache
private

Definition at line 722 of file definition.cpp.

Referenced by getFileContentsPipe().

◆ m_endPos

size_t FilterCache::m_endPos
private

Definition at line 725 of file definition.cpp.

Referenced by FilterCache(), and getFileContentsPipe().

◆ m_lineOffsets

std::unordered_map<std::string,LineOffsets> FilterCache::m_lineOffsets
private

◆ m_mutex

std::mutex FilterCache::m_mutex
private

Definition at line 724 of file definition.cpp.

Referenced by getFileContentsDisk(), and getFileContentsPipe().


The documentation for this class was generated from the following file: