Lefora Free Forum
login join
Loading
957 views

Parsing textfile to NSTextview and keep updates

Page 1
1–2
rookie - member
1 posts

Hi everyone

I have a problem with something that should be fairly simple. I have a C++ background and did similar things in C++/STL and the Win32 APi, but I can't get very far in Cocoa and Objective C.

What I want to do is read a log file line by line, check for keywoards (such as INFO, ERROR, WARNING) in each line, and color the full line accordingly. Also the logfile updates frequently and it would be costly (i think) to read the whole file over and over again, so something like the terminal "tail -f" command would be good (though this is for a later stage of the project).

My code now is as such:

NSTextStorage *ts;
    NSArray *lines;
    NSAttributedString *line;
   
    // store the textstorage of the textview
    ts = [textView textStorage];
   
    // load the file into an array
    lines = [[NSString stringWithContentsOfFile:@"/Volumes/data/Users/jeroenb/Library/Preferences/Macromedia/Flash Player/Logs/flashlog.txt"] componentsSeparatedByString:@"\n"];


    // create attributes for the strings   
    NSFont *font = [NSFont fontWithName:@"Monaco" size:14.0];
    NSMutableDictionary *attrsDictionary =    [NSMutableDictionary dictionaryWithObject:font    forKey:NSFontAttributeName];
    [attrsDictionary setObject: [NSColor blackColor] forKey: NSForegroundColorAttributeName];
   
    // loop the file and add the strings to the textstorage
    for (NSString *element in lines) {
        line = [[NSAttributedString alloc] initWithString:element attributes:attrsDictionary];
        [ts appendAttributedString:line];   
    }

It works to load the file, and eventually I have a NSTextview with all the text (but all on one line :( newlines are lost) and it already takes a long time to do this (so I am thinking I do it very wrong, it takes about 1 second to parse the file, which is only about 10kb);

Furthermore, I have no clue on how to search the line string properply and setting the colour, though this can be found in the documentation I am sure. I have more problems with the newlines that are gone and the slow appending to the textstorage.

Hopefully it is clear what I want to do, it's so basic and simple, and still I can't doing, even after digging into the xcode cocoa/objective c documentation.

Thank you already!

PS: the log file is something like:
[11:31:00 - INFO new app inited
[11:31:05 - INFO] engine started
[11:31:07 - WARNING] engine assets not found
[11:31:12 - ERROR] asset 'background.png' is null

And it should become in the textview
[11:31:00 - INFO new app inited (blue line)
[11:31:05 - INFO] engine started (blue line)
[11:31:07 - WARNING] engine assets not found (yellow line)
[11:31:12 - ERROR] asset 'background.png' is null (red line)

?
288 posts


Ah this is very interesting and I've got good news for you!  Your STL skills will do you proud here.  There are many ways to create an 'attributed string', however one of the simplest is to write in HTML !  So, read the file and parse it into lines with STL and convert the lines to:

<pre>this is <span style='color:red'>INFO: </span> something dead boring</pre><pre>...</pre>

Ask cocoa to convert create the attributed string and insert it into the NSTextView control.

I'm not sure off-hand why it's taking 1 second to parse 10k - I'd expect it to be at least 100x faster and possibly 1000x faster.  Remember that Obj/C is C and Obj/C++ is C++ - so all your STL skills will come in handy.  Get your file parser to create HTML code in the terminal.  Then move to XCode/Cocoa to finish the job (change the wizard's .m files to have the extension .mm)  You'll be done in no time.

The tail -f feature is very simple.  You'll want a timer (say every couple of seconds) and you inspect the length of your 'target' file.  Read the new data, dance it through your attributed string code and you'll be happy.  With a little cunning slight of hand (hint: never write partial lines), this should be very fast and smooth as you will simply be creating additional HTML to insert at the bottom of the control.

__________________
Page 1
1–2

Locked Topic


You must be a member to post in this forum

Join Now!