Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* plistconvert: convert plist files.
- Copyright 2015
- Trusted Software Foundation.
- This file is part of openIOS utils.
- See more on openios.net
- This program is totally free software,
- you can do with it whatever you want.
- */
- #import <Foundation/Foundation.h>
- #include <stdio.h>
- #define REPORT_BUGS_TO "root@openios.net"
- #define VERSION_STRING "v0.3"
- #define BIN 1
- #define XML 0
- int plistConvert(const char *, int, char *);
- void print_version (const char *name)
- {
- printf (" %s %s\n", name, VERSION_STRING);
- printf ("Copyright 2015 Trusted Software Foundation\n");
- printf ("licensed: as is.\n");
- exit (0);
- }
- static void usage (int help, const char *name)
- {
- FILE *s;
- s = help ? stdout : stderr;
- fprintf (s, "Usage: %s -options plist_files\n", name);
- fprintf (s, " Convert *.plist files to readable xml format or back to binary.\n");
- fprintf (s, " Saves files with the same name.\n");
- // fprintf (s, " If there are no files tries to read stdin(piping)\n");
- fprintf (s, " The options are:\n\
- -b convert to binary format\n\
- -t -x convert to text(xml) format\n\
- -h -H --help print this help message\n\
- -v -V --version print version information\n");
- if (help)
- fprintf (s, "Report bugs to %s\n", REPORT_BUGS_TO);
- exit (help ? 0 : 1);
- }
- int main(int argc, char *argv[])
- {
- //if (argc < 1) usage (0, program_name);
- const char *program_name = argv[0];
- int i = 2;
- int targetFormat;
- const char *status[80];
- if (argc < 2) usage (0, program_name);
- if (argv[1][0] == '-')
- {
- if (strcmp (argv[1], "--help") == 0
- || strcmp (argv[1], "-h") == 0
- || strcmp (argv[1], "-H") == 0)
- usage (1, program_name);
- else if (strcmp (argv[1], "--version") == 0
- || strcmp (argv[1], "-V") == 0
- || strcmp (argv[1], "-v") == 0)
- print_version (program_name);
- else
- {
- if (strcmp (argv[1], "-b") == 0) targetFormat = BIN;
- else if (strcmp (argv[1], "-t") == 0
- || strcmp (argv[1], "-x") == 0) targetFormat = XML;
- else usage(0, program_name);
- while(i < argc)
- {
- printf("%s: ", argv[i]);
- if(0 == plistConvert(argv[i], targetFormat, status))
- printf("Ok\n");
- else printf("failed with error: %s \n", status);
- i++;
- }
- exit(0);
- }
- }
- usage (0, program_name);
- }
- plistError(NSError *error, char *status)
- {
- char *hint = "";
- char *notFound = "(file not found?)";
- char *notPlist = "(file not *.plist?)";
- char *chmod = "(can't write, not enough rights?)";
- char *cError = [[error domain] UTF8String];
- int code = [error code];
- if(strcmp (cError, "NSCocoaErrorDomain") == 0)
- {
- switch (code)
- {
- case 260: hint = notFound; break;
- case 3840: hint = notPlist; break;
- case 513: hint = chmod; break;
- }
- }
- snprintf(status, 80, "%s: code %d, %s", cError, code, hint);
- return 1;
- }
- int plistConvert(const char *fileName, int targetFormat, char *status)
- {
- NSError *error;
- NSPropertyListFormat format;
- NSString *path = [NSString stringWithCString: fileName
- encoding: NSASCIIStringEncoding];
- NSData *plistData = [NSData dataWithContentsOfFile:path
- options:NSDataReadingUncached
- error:&error];
- if(error != NULL) return plistError(error, status);
- id plist = [NSPropertyListSerialization propertyListWithData:plistData
- options:NSPropertyListImmutable
- format:&format
- error:&error];
- if(error != NULL) return plistError(error, status);
- NSString *xmlData = [NSPropertyListSerialization dataWithPropertyList:plist
- format:(targetFormat==XML?
- NSPropertyListXMLFormat_v1_0:
- NSPropertyListBinaryFormat_v1_0)
- options:0
- error:&error];
- if(error == NULL)
- {
- [xmlData writeToFile:path
- options:NSDataWritingAtomic
- error:&error];
- if(error != NULL) return plistError(error, status);
- return 0;
- }
- else return plistError(error, status);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement