void TextGapBuffer::InsertText(BFile *file, int32 fileOffset, int32 inNumItems, int32 inAtIndex) { off_t fileSize; if (file->GetSize(&fileSize) != B_OK || !file->IsReadable()) return; // Clamp the text length to the file size fileSize -= fileOffset; if (fileSize < inNumItems) inNumItems = fileSize; if (inNumItems < 1) return; inAtIndex = (inAtIndex > fItemCount) ? fItemCount : inAtIndex; inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex; if (inAtIndex != fGapIndex) MoveGapTo(inAtIndex); if (fGapCount < inNumItems) SizeGapTo(inNumItems + kTextGapBufferBlockSize); // Finally, read the data and put it into the buffer if (file->ReadAt(fileOffset, fBuffer + fGapIndex, inNumItems) > 0) { fGapCount -= inNumItems; fGapIndex += inNumItems; fItemCount += inNumItems; } }
const char * TextGapBuffer::RealText() { MoveGapTo(fItemCount); fBuffer[fItemCount] = '\0'; return fBuffer; }
const char * TextGapBuffer::RealText() { MoveGapTo(fItemCount); if (fGapCount == 0) SizeGapTo(kTextGapBufferBlockSize); fBuffer[fItemCount] = '\0'; return fBuffer; }
void TextGapBuffer::RemoveRange(int32 start, int32 end) { long inAtIndex = start; long inNumItems = end - start; if (inNumItems < 1) return; inAtIndex = (inAtIndex > fItemCount - 1) ? (fItemCount - 1) : inAtIndex; inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex; MoveGapTo(inAtIndex); fGapCount += inNumItems; fItemCount -= inNumItems; if (fGapCount > kTextGapBufferBlockSize) SizeGapTo(kTextGapBufferBlockSize); }
void TextGapBuffer::InsertText(const char *inText, int32 inNumItems, int32 inAtIndex) { if (inNumItems < 1) return; inAtIndex = (inAtIndex > fItemCount) ? fItemCount : inAtIndex; inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex; if (inAtIndex != fGapIndex) MoveGapTo(inAtIndex); if (fGapCount < inNumItems) SizeGapTo(inNumItems + kTextGapBufferBlockSize); memcpy(fBuffer + fGapIndex, inText, inNumItems); fGapCount -= inNumItems; fGapIndex += inNumItems; fItemCount += inNumItems; }