I'm still in the early coding phase of this task and its forcing me to
dig into the C# details and thus *learn* some things which is always a
good thing. The principal areas of my confusion >>
Area Status
1) The Unicode usage. Somewhat figured out.
2) The need for a pointer. Somewhat figured out.
3) Memory management. Please help. Beyond my skills.
4) Scroll/Thumb logic. Any advice appreciated.
Comments on the above >>
#1) One important observation is that StreamReader correctly imports
ASCII text data files into a RichTextBox because it defaults to UTF-8.
Why is this confusing? My texts state that .Net uses Unicode(UTF-16)?
Some digging in the doco explains the usage of the "preamble" Byte
Order Mark(BOM) tag. Interesting how the default behavior differs from
some very popular textbook teachings.
#2) StreamReader calls File.OpenText that has attribute CanSeek =
false. Thus the only other C# option I have found so far is to use
FileStream. Unfortunately FileStream supports the reading of bytes and
not lines. There was some happy dancing here when I figured out the
byte_array -> string -> RichTexBox conversion and input. Key
statements follow:
#using System.Text;
public class ModifiedRichTextBox : RichTextBox
FileInfo myFile = new FileInfo(path);
using (FileStream fs = File.OpenRead(myFile.FullName))
fs.Position = myFile.Length / 2;
int byteArraySize = 500;
byte[] byteArray = new byte[byteArraySize];
string charBlock = "";
if( fs.Read(byteArray, 0, byteArraySize) > 0 )
{
charBlock = Encoding.UTF8.GetString(byteArray); // <- tricky 4 me
this.Text += charBlock; // "this" is the RichTextBox
}
#3) Using the thumb to "Position" the pointer within the file,
clearing the RichTextBox and performing another RichTextBox input is
level one performance. Being able to smoothly scroll in both
directions using a buffer would kick it up several notches.
Inputting the small byte array into the RichTextBox allows some local
scrolling. Controlling the buffer expertly would be VERY nice ... but
way beyond my current skill level. Perhaps this is what is known as
"virtualization" or "memory mapping"? I hate to throw words around out
of ignorance ... but I also have no problems recognizing my lack of
skill. A quote from Wikipedia >>
"The Microsoft .Net runtime environment does not natively include
managed access to memory mapped files, but there are third-party
libraries which do so."
Wiki references the following >>
http://www.winterdom.com/dev/dotnet/index.html
The above has a FileMap and Patch that looks promising ... I hope to
explore them soon and hopefully understand them. Yikes! Most likely
over my head.
#4) A crude scrolling/thumb implementation simple scales the percent
thumb travel to that of the pointer being Positioned to that same
percentage of the file.Length. Some clever scroll and cursor event
trapping within the RichTextBox might can effectively reload the
RichTextBox ... but I suspect less than smooth operation. Even so,
viewing portions of a huge file quickly and without hogging resources
is a good thing.
I do have a need for such a file viewer and am also using it as a
learning task. Any direction I receive here will hasten that learning
experience and keep me out of the all too many mined fields. LOL ..
perhaps I should say mind fields?
All guidance appreciated!!
Thanks.
-- Tom