-
- if ((d->Flags & pkgTagFile::SUPPORT_COMMENTS) == 0)
- d->iOffset += tagSize;
- else
- {
- auto first = d->chunks.begin();
- for (; first != d->chunks.end(); ++first)
- {
- if (first->good == false)
- d->iOffset += first->length;
- else
- {
- if (tagSize < first->length)
- {
- first->length -= tagSize;
- d->iOffset += tagSize;
- break;
- }
- else
- {
- tagSize -= first->length;
- d->iOffset += first->length;
- }
- }
- }
- d->chunks.erase(d->chunks.begin(), first);
- }
-
- Tag.Trim();
- return true;
-}
- /*}}}*/
-// TagFile::Fill - Top up the buffer /*{{{*/
-// ---------------------------------------------------------------------
-/* This takes the bit at the end of the buffer and puts it at the start
- then fills the rest from the file */
-static bool FillBuffer(pkgTagFilePrivate * const d)
-{
- unsigned long long Actual = 0;
- // See if only a bit of the file is left
- unsigned long long const dataSize = d->Size - ((d->End - d->Buffer) + 1);
- if (d->Fd->Read(d->End, dataSize, &Actual) == false)
- return false;
- if (Actual != dataSize)
- d->Done = true;
- d->End += Actual;
- return true;
-}
-static void RemoveCommentsFromBuffer(pkgTagFilePrivate * const d)
-{
- // look for valid comments in the buffer
- char * good_start = nullptr, * bad_start = nullptr;
- char * current = d->Start;
- if (d->isCommentedLine == false)
- {
- if (d->Start == d->Buffer)
- {
- // the start of the buffer is a newline as a record can't start
- // in the middle of a line by definition.
- if (*d->Start == '#')
- {
- d->isCommentedLine = true;
- ++current;
- if (current > d->End)
- d->chunks.emplace_back(false, 1);
- }
- }
- if (d->isCommentedLine == false)
- good_start = d->Start;
- else
- bad_start = d->Start;
- }
- else
- bad_start = d->Start;
-
- std::vector<std::pair<char*, size_t>> good_parts;
- while (current <= d->End)
- {
- size_t const restLength = (d->End - current);
- if (d->isCommentedLine == false)
- {
- current = static_cast<char*>(memchr(current, '#', restLength));
- if (current == nullptr)
- {
- size_t const goodLength = d->End - good_start;
- d->chunks.emplace_back(true, goodLength);
- if (good_start != d->Start)
- good_parts.push_back(std::make_pair(good_start, goodLength));
- break;
- }
- bad_start = current;
- --current;
- // ensure that this is really a comment and not a '#' in the middle of a line
- if (*current == '\n')
- {
- size_t const goodLength = (current - good_start) + 1;
- d->chunks.emplace_back(true, goodLength);
- good_parts.push_back(std::make_pair(good_start, goodLength));
- good_start = nullptr;
- d->isCommentedLine = true;
- }
- current += 2;
- }
- else // the current line is a comment
- {
- current = static_cast<char*>(memchr(current, '\n', restLength));
- if (current == nullptr)
- {
- d->chunks.emplace_back(false, (d->End - bad_start));
- break;
- }
- ++current;
- // is the next line a comment, too?
- if (current >= d->End || *current != '#')
- {
- d->chunks.emplace_back(false, (current - bad_start));
- good_start = current;
- bad_start = nullptr;
- d->isCommentedLine = false;
- }
- ++current;
- }
- }
-
- if (good_parts.empty() == false)
- {
- // we found comments, so move later parts over them
- current = d->Start;
- for (auto const &good: good_parts)
- {
- memmove(current, good.first, good.second);
- current += good.second;
- }
- d->End = current;
- }
-
- if (d->isCommentedLine == true)
- {
- // deal with a buffer containing only comments
- // or an (unfinished) comment at the end
- if (good_parts.empty() == true)
- d->End = d->Start;
- else
- d->Start = d->End;
- }
- else
- {
- // the buffer was all comment, but ended with the buffer
- if (good_parts.empty() == true && good_start >= d->End)
- d->End = d->Start;
- else
- d->Start = d->End;
- }
-}
-bool pkgTagFile::Fill()
-{
- unsigned long long const EndSize = d->End - d->Start;
- if (EndSize != 0)
- {
- memmove(d->Buffer,d->Start,EndSize);
- d->Start = d->End = d->Buffer + EndSize;
- }
- else
- d->Start = d->End = d->Buffer;
-
- unsigned long long Actual = 0;
- while (d->Done == false && d->Size > (Actual + 1))
- {
- if (FillBuffer(d) == false)
- return false;
- if ((d->Flags & pkgTagFile::SUPPORT_COMMENTS) != 0)
- RemoveCommentsFromBuffer(d);
- Actual = d->End - d->Buffer;
- }
- d->Start = d->Buffer;
-
- if (d->Done == true)
- {
- if (EndSize <= 3 && Actual == 0)
- return false;
- if (d->Size - (d->End - d->Buffer) < 4)
- return true;
-
- // Append a double new line if one does not exist
- unsigned int LineCount = 0;
- for (const char *E = d->End - 1; E - d->End < 6 && (*E == '\n' || *E == '\r'); E--)
- if (*E == '\n')
- ++LineCount;
- if (LineCount < 2)
- {
- if (static_cast<unsigned long long>(d->End - d->Buffer) >= d->Size)
- Resize(d->Size + 3);
- for (; LineCount < 2; ++LineCount)
- *d->End++ = '\n';
- }
- }