[haiku-commits] Re: haiku: hrev48969 - src/apps/text_search

  • From: Jérôme Duval <jerome.duval@xxxxxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Wed, 1 Apr 2015 19:55:20 +0200

2015-04-01 19:36 GMT+02:00 <waddlesplash@xxxxxxxxx>:

+ BString contents;
+
+ BFile file(&ref, B_READ_WRITE);
+ off_t size;
+ file.GetSize(&size);
+ file.Seek(0, SEEK_SET);
+ char* buffer = new char[size];
+ file.Read(buffer, size);
+ if (fEncoding > 0) {
+ char* temp = strdup_to_utf8(fEncoding, buffer, size);
+ contents.SetTo(temp, size);
+ free(temp);
+ } else
+ contents.SetTo(buffer, size);
+ delete[] buffer;
+
+ int32 index = 0, lines = 1; // First line is 1 not 0
+ while (true) {
+ int32 newPos;
+ if (fCaseSensitive)
+ newPos = contents.FindFirst(fPattern, index);
+ else
+ newPos = contents.IFindFirst(fPattern, index);
+ if (newPos == B_ERROR)
+ break;
+
+ lines += _CountLines(contents, index, newPos);
+ BString linenoAndLine;
+ linenoAndLine.SetToFormat("%" B_PRId32 ":%s", lines,
_GetLine(contents, newPos).String());
+ message.AddString("text", linenoAndLine);
+
+ index = newPos + 1;
}

This looks a bit rough:
* B_READ_WRITE doesn't look good to grep on readonly medium.
* Checking BFile::InitCheck() plus the return values of GetSize(),
Seek() Read() would be helpful
* new char[size] will probably need to swap memory to grep a 2GB file.
It's not reasonable to fully read a file in memory.

Bye,
Jérôme

Other related posts: