[OSX][iOS]データ解析(2) | Cocoa練習帳

[OSX][iOS]データ解析(2)

現在のXcodeはビルドした結果の出力先が隠されている為、コマンドライン・ツールをビルドして、操作するという流れが面倒になっている為、標準入力から値を得る部分をデバッグ用に内部変数から値を得る様に修正する。




#ifndef DEBUG
        NSFileHandle    *fhi = [NSFileHandle fileHandleWithStandardInput];
#endif
        NSFileHandle    *fho = [NSFileHandle fileHandleWithStandardOutput];
 
#ifndef DEBUG
        NSData  *datainput = [fhi readDataToEndOfFile];
        NSString    *str = [[NSString alloc] initWithData:datainput encoding:NSUTF8StringEncoding];
#else
        NSString    *str = @"12:23:45 start\n1,10\n2,13";
#endif



行単位に切り出せたら、Perlのchompのように末尾の改行文字を削ろう。




NSCharacterSet  *charSet = [NSCharacterSet newlineCharacterSet];
            line = [line stringByTrimmingCharactersInSet:charSet];



 

著者はよくやっているのだが、ログのある時刻の抜き出しをやってみよう。




regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d\\d:\\d\\d:\\d\\d)"
                                                              options:NSRegularExpressionCaseInsensitive
                                                                error:&error];
            NSTextCheckingResult    *match = [regex firstMatchInString:line
                                                               options:0
                                                                 range:NSMakeRange(0, line.length)];
            if (0 < [match numberOfRanges]) {
                NSString    *tm = [line substringWithRange:[match rangeAtIndex:0]];
                NSLog(@"time: %@", tm);
            }



完成。




@autoreleasepool {        
#ifndef DEBUG
        NSFileHandle    *fhi = [NSFileHandle fileHandleWithStandardInput];
#endif
        NSFileHandle    *fho = [NSFileHandle fileHandleWithStandardOutput];
 
#ifndef DEBUG
        NSData  *datainput = [fhi readDataToEndOfFile];
        NSString    *str = [[NSString alloc] initWithData:datainput encoding:NSUTF8StringEncoding];
#else
        NSString    *str = @"12:23:45 start\n1,10\n2,13";
#endif
        
        NSError *error = NULL;
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(.+)\n|(.+)"
                                                                               options:NSRegularExpressionCaseInsensitive
                                                                                 error:&error];
        NSArray    *array = [regex matchesInString:str
                                           options:0
                                             range:NSMakeRange(0, str.length)];
        NSTextCheckingResult    *matches;
        for (matches in array) {
            NSString    *line = [str substringWithRange:[matches rangeAtIndex:0]];
 
#ifdef DEBUG
            NSData  *dataout = [[NSData alloc] initWithBytes:[line UTF8String]
                                                      length:[line lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
            [fho writeData:dataout];
#endif
            
            NSCharacterSet  *charSet = [NSCharacterSet newlineCharacterSet];
            line = [line stringByTrimmingCharactersInSet:charSet];
 
            regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d\\d:\\d\\d:\\d\\d)"
                                                              options:NSRegularExpressionCaseInsensitive
                                                                error:&error];
            NSTextCheckingResult    *match = [regex firstMatchInString:line
                                                               options:0
                                                                 range:NSMakeRange(0, line.length)];
            if (0 < [match numberOfRanges]) {
                NSString    *tm = [line substringWithRange:[match rangeAtIndex:0]];
                NSLog(@"time: %@", tm);
            }
        }
    }



ソースコード
GitHubからどうぞ。

https://github.com/murakami/workbook/tree/master/mac/analog - GitHub


関連情報
File System Programming Guide

NSRegularExpression Class Reference