static int convert_command()

in Other Sources/plcrashutil/main.m [52:117]


static int convert_command (int argc, char *argv[]) {
    const char *format = "iphone";
    const char *input_file;
    FILE *output = stdout;

    /* options descriptor */
    static struct option longopts[] = {
        { "format",     required_argument,      NULL,          'f' },
        { NULL,         0,                      NULL,           0 }
    };    

    /* Read the options */
    char ch;
    while ((ch = getopt_long(argc, argv, "f:", longopts, NULL)) != -1) {
        switch (ch) {
            case 'f':
                format = optarg;
                break;
            default:
                print_usage();
                return 1;
        }
    }
    argc -= optind;
    argv += optind;

    /* Ensure there's an input file specified */
    if (argc < 1) {
        fprintf(stderr, "No input file supplied\n");
        print_usage();
        return 1;
    } else {
        input_file = argv[0];
    }
    
    /* Verify that the format is supported. Only one is actually supported currently */
    PLCrashReportTextFormat textFormat;
    if (strcasecmp(format, "iphone") == 0 || strcasecmp(format, "ios")) {
        textFormat = PLCrashReportTextFormatiOS;
    } else {
        fprintf(stderr, "Unsupported format requested\n");
        print_usage();
        return 1;
    }

    /* Try reading the file in */
    NSError *error;
    NSData *data = [NSData dataWithContentsOfFile: [NSString stringWithUTF8String: input_file] 
                                          options: NSMappedRead error: &error];
    if (data == nil) {
        fprintf(stderr, "Could not read input file: %s\n", [[error localizedDescription] UTF8String]);
        return 1;
    }
    
    /* Decode it */
    PLCrashReport *crashLog = [[PLCrashReport alloc] initWithData: data error: &error];
    if (crashLog == nil) {
        fprintf(stderr, "Could not decode crash log: %s\n", [[error localizedDescription] UTF8String]);
        return 1;
    }

    /* Format the report */
    NSString* report = [PLCrashReportTextFormatter stringValueForCrashReport: crashLog withTextFormat: textFormat];
    fprintf(output, "%s", [report UTF8String]);     
    return 0;
}