Parsing a Java Properties file in Objective-C for iPhone

后端 未结 6 910
情话喂你
情话喂你 2021-01-18 19:09

I\'m looking for a way in the iPhone SDK to read in a Properties file (not the XML flavor) for example this one:

# a comment
! a comment

a = a string
b = a          


        
相关标签:
6条回答
  • 2021-01-18 19:34

    Take a look at this PropertyParser

    NSString *text = @"sample key = sample value";
    
    PropertyParser *propertyParser = [[PropertyParser alloc] init];
    
    NSMutableDictionary *keyValueMap = [propertyParser parse:text];
    
    0 讨论(0)
  • 2021-01-18 19:36

    It's a perfectly simple parsing problem. Read a line. Ignore if comment. Check for continuation, and read/append continuation lines as needed. Look for the "=". Make the left side of the "=" (after trimming white space) the key. Either parse the right side yourself or put it into an NSString and use stringWithFormat on it to "reduce" any escapes to pure character form. Return key and reduced right side.

    (But refreshing my memory on the properties file format reminds me that:

    The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character;

    So a little scanning of the line is required to separate the key from the rest. Nothing particularly difficult, though.)

    0 讨论(0)
  • 2021-01-18 19:42

    You can now use NSRegularExpression class to do this.

    0 讨论(0)
  • 2021-01-18 19:58

    Have you considered using lex/yacc or flex/bison to generate your own compiler code from a description of the grammar for properties files? I'm not sure if there are any existing grammars defined for a Java properties file, but it seems like it would be a pretty simple grammar to write.

    Here's another SO post that mentions this approach for general purpose parsing

    0 讨论(0)
  • 2021-01-18 19:59

    I've ended with this solution if anybody is interested :

    @interface NSDictionary (PropertiesFile)
    
    + (NSDictionary *)dictionaryWithPropertiesFile:(NSString *)file;
    
    @end
    
    @implementation NSDictionary (PropertiesFile)
    
    + (NSDictionary *)dictionaryWithPropertiesFile:(NSString *)file {
        NSError *error = nil;
        NSString *propertyFileContent = [[NSString alloc] initWithContentsOfFile:file encoding:NSUTF8StringEncoding error:&error];
        if (error) return nil;
    
        NSArray *properties = [propertyFileContent componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
        if (properties.count == 0) return nil;
    
        NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithCapacity:properties.count];
        for (NSString *propertySting in properties) {
            NSArray *property = [propertySting componentsSeparatedByString:@"="];
    
            if (property.count != 2) continue;
    
            [result setObject:property[1] forKey:property[0]];
        }
        return result.allKeys.count > 0 ? result : nil;
    }
    
    @end
    
    0 讨论(0)
  • 2021-01-18 20:00

    I would take a look at ParseKit http://parsekit.com/. Otherwise you could use RegexKitLite and create some regular expressions.

    0 讨论(0)
提交回复
热议问题