#include "BufferAccessor.h" #include //constructor for class BufferAccessor::BufferAccessor(const char* buffer, bool DumpFoldLevels, bool _verbose) : Accessor(), //virtual base class _DumpFoldLevels(DumpFoldLevels) { Verbose = _verbose; const int x = (int) strlen(buffer); size_p_buffer = x; p_colors = new int[x + 1]; memset (p_colors, 0, x*sizeof(int)); p_buffer = buffer; p_FoldLevels = (int *) 0; p_NewlinePoints = (int *) 0; // Languages that call LineStart() need this info. // Add this info to track line #s // This is going to be painful -- first count the newlines, // allocate the array, and then run through the buffer again and // assign the positions. const char* p; for (p = buffer, NumLines = 0; *p; p++) { if (*p == '\r') { NumLines += 1; if (*(p + 1) == '\n') { p++; } } else if (*p == '\n') { // assert *(p - 1) != '\r' if p > buffer NumLines += 1; } } // Check to see if the buffer doesn't end with a newline bool addFinalLine = false; if (p > buffer && *(p - 1) != '\n' && *(p - 1) != '\r') { addFinalLine = true; ++NumLines; } p_FoldLevels = (int *) malloc(sizeof(int) * (NumLines + 1)); memset(p_FoldLevels, 0, sizeof(int) * (NumLines + 1)); p_NewlinePoints = (int *) malloc(sizeof(int) * (NumLines + 1)); memset(p_NewlinePoints, 0, sizeof(int) * (NumLines + 1)); int thisLine; // This is done by the memset // p_NewlinePoints[0] = 0; for (p = buffer, thisLine = 0; *p; p++) { if (thisLine >= NumLines - 1) { break; } if (*p == '\r') { thisLine += 1; if (*(p + 1) == '\n') { p++; } p_NewlinePoints[thisLine] = (int) ((p + 1) - buffer); } else if (*p == '\n') { thisLine += 1; // assert *(p - 1) != '\r' if p > buffer p_NewlinePoints[thisLine] = (int) ((p + 1) - buffer); } } Fill(0); if (Verbose) printf("\nBufferAccessor::BufferAccessor(%d byte buffer)\n", x); } //destructor (be sure to free everything) BufferAccessor::~BufferAccessor() { if (Verbose) printf("\nBufferAccessor::~BufferAccessor()\n"); if (_DumpFoldLevels) { if (p_FoldLevels) free(p_FoldLevels); if (p_NewlinePoints) free(p_NewlinePoints); } delete [] p_colors; } //therefore return false since no DBCS possible bool BufferAccessor::InternalIsLeadByte(char byte) { return false; } // stub for new virtual method introduced sometime around Aug 2002 bool BufferAccessor::Match(int pos, const char *s) { return false; } //output the style to scintilla layer char BufferAccessor::StyleAt(int position) { // printf("BufferAccessor::StyleAt(%d)\n", position); if ((position < (int) size_p_buffer) && (position >= 0)) return p_colors[position]; else { if (Verbose) { printf ( "\nOut of range value %d input to StyleAt[]",position); } return 0; } } int BufferAccessor::GetLine(int position) { // printf ("BufferAccessor::GetLine\n"); // Get the line # containing this position int lpos = 0, hpos = NumLines, mpos; while (lpos < hpos) { mpos = (lpos + hpos) / 2; if (p_NewlinePoints[mpos] == position) { if (Verbose) { printf ("GetLine(%d) => %d\n", position, mpos); } return mpos; } else if (p_NewlinePoints[mpos] > position) { // Can't happen that mpos > 0 hpos = mpos - 1; if (p_NewlinePoints[hpos] <= position) { return hpos; } } else { if (mpos == (NumLines - 1) || p_NewlinePoints[mpos + 1] > position) { if (Verbose) { printf ("GetLine(%d) => %d\n", position, mpos); } return mpos; } lpos = mpos + 1; } } if (Verbose) { printf ("GetLine(%d) => %d\n", position, lpos); } return lpos; }; int BufferAccessor::LineStart(int line) { // Used only for folding and the template meta-lexer if (Verbose) { printf ("BufferAccessor::LineStart\n"); } if (line < 0) { return 0; } else if (line >= NumLines) { return Length(); } return (p_NewlinePoints) ? p_NewlinePoints[line] : 0; }; int BufferAccessor::LevelAt(int line) { if (_DumpFoldLevels && line <= NumLines) { if (Verbose && 0) { printf ("BufferAccessor::LevelAt(%d) => %d\n", line, p_FoldLevels[line]); } return p_FoldLevels[line]; } else { if (Verbose && 0) { printf ("BufferAccessor::LevelAt(%d), we aren't tracking.\n", line); } return 0; } }; int BufferAccessor::Length() { return (size_p_buffer); } int BufferAccessor::GetLineState(int line) { // Not used if (Verbose) { printf ("BufferAccessor::GetLineState\n"); } return 0; } int BufferAccessor::SetLineState(int line, int state) { // Not used if (Verbose) { printf ("BufferAccessor::SetLineState\n"); } return 0; } int BufferAccessor::GetPropertyInt(const char *key, int defaultValue) { if (Verbose) { printf ("BufferAccessor::GetPropertyInt\n"); } if (!strcmp(key, "double_colons_are_names")) { // For ActivePerl return 1 return 1; } if (_DumpFoldLevels) { if (!strcmp(key, "fold") || !strcmp(key, "fold.comment") || !strcmp(key, "fold.compact")) { return 1; } } return 0; } char* BufferAccessor::GetProperties() { if (Verbose) { if (_DumpFoldLevels) return "dv"; else return "v"; } else if (_DumpFoldLevels) { return "d"; } else { return ""; } } //we won't implement the mask for now. void BufferAccessor::StartAt(unsigned int start, char chMask) { if (Verbose) { printf ("BufferAccessor::StartAt(%d,%d)\n",start,chMask); } return; } //flags? what to do with these? void BufferAccessor::SetFlags(char chFlags_, char chWhile_) { return; } unsigned int BufferAccessor::GetStartSegment() { //printf ("\nGetStartSeg"); return startSeg; } void BufferAccessor::StartSegment(unsigned int pos) { if (Verbose) { printf ("\nStartSeg set to %d",pos); } startSeg = pos; } void BufferAccessor::ColourTo(unsigned int pos, int chAttr) { if ((int) pos < 0) { return; } int i,max; //ColourTo seems to be receiving signed int data ??? max = pos+1; i = startSeg; for (; i < max; i++) { if ((i >= 0) && (i < size_p_buffer)) p_colors[i] = chAttr; } startSeg = pos + 1; } //full buffer cached in memory //buffer is known, so Fill can do nothing? void BufferAccessor::Fill(int position) { if (Verbose) { printf("\nBufferAccessor::Fill(%d)\n", position); } if ((position < (int) size_p_buffer) && (position >= 0)) { startPos = position; endPos = size_p_buffer < startPos + bufferSize ? size_p_buffer : startPos + bufferSize; if (Verbose) { printf("\nstartPos: %d\nendPos: %d\n", startPos, endPos); } memcpy( buf, &p_buffer[startPos], endPos - startPos ); } if (Verbose) { printf( "End fill\n" ); } } //full buffer cached in memory void BufferAccessor::Flush() { } void BufferAccessor::SetLevel(int line, int level) { if (_DumpFoldLevels) { if (line > NumLines) { if (Verbose) { printf ("SetLevel : can't allocate %d ints\n", NumLines); } return; } p_FoldLevels[line] = level; } } int BufferAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) { return 0; }