Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HiRez

macrumors 603
Original poster
Jan 6, 2004
6,265
2,630
Western US
Hi, I could use some suggestions for a program I'm writing. Basically I want to build a table from an XML source (this part works fine), and then allow the user to build up a string format to output the contents of the table, but with some flexibility. For example, they might want each row simply comma-delimited. Or they might want a common string prefix, then column 3, then a colon, then column 1 formatted as a percentage, then a string suffix. I can't know the count or names of any of the columns beforehand, it could be anything.

What's the best way to go about allowing the user to specify such a thing? I was thinking maybe an NSTokenField where the tokens would be column names and surrounded by any amount of text? I don't know if that would work because I've never used NSTokenField before. Ideally I think I'd want drop-down lists for selecting columns and then text fields for filling in delimeters. Would NSRuleEditor or NSPredicateEditor work? Or should I just enter an awk expression and send the output through there to an NSTask? That would probably be fairly powerful, but not as user-friendly as I'd like.

Thanks for any suggestions/tips!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
It sounds like all you need to do is define a grammar, and this one sounds simple, then implement a parser for it and a way to execute it.

You could allow any string in your grammar, and parse the input using:
$$ - $ literal
$[0-9][0-9]*
$[^0-9$]
[^$][^$]*

In terms of finding this, it seems like:
NSString's
Code:
rangeOfCharacterFromSet:options:range:
will do the trick, with
You can use
Code:
 [NSCharacterSet characterSetWithCharactersInString:@"$"]
to generate the character set, and it's return will have a location element you can use. The range will start from 0 to length, but you'll need to track the current position for the range in subsequent iterations. You'll need to check the next character with characterAtIndex:. If you find any token other than $[0-9][0-9]*, you can simply append it to your output string. When you do find this token, you'll just need to grab the Nth data item of the input, and append that to your output. Remember bounds checking. Normally in schemes like this, if the number requested is greater than the fields available, empty string is used in its place.

There are probably more concise ways of doing this, but using NSString stuff, this seems like the easiest way to do it.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.