Need UI Advice for String Buildup

Discussion in 'Mac Programming' started by HiRez, Jul 29, 2008.

  1. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #1
    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!
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. HiRez thread starter macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US

Share This Page