Guest User

plistconvert

a guest
Feb 10th, 2015
435
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* plistconvert: convert plist files.
  2.    Copyright 2015
  3.    Trusted Software Foundation.
  4.  
  5.    This file is part of openIOS utils.
  6.    See more on openios.net
  7.    
  8.    This program is totally free software,
  9.    you can do with it whatever you want.
  10. */
  11.  
  12. #import <Foundation/Foundation.h>
  13. #include <stdio.h>
  14.  
  15. #define REPORT_BUGS_TO "root@openios.net"
  16. #define VERSION_STRING "v0.3"
  17.  
  18. #define BIN 1
  19. #define XML 0
  20.  
  21. int plistConvert(const char *, int, char *);
  22.  
  23. void print_version (const char *name)
  24. {
  25.   printf (" %s %s\n", name, VERSION_STRING);
  26.   printf ("Copyright 2015 Trusted Software Foundation\n");
  27.   printf ("licensed: as is.\n");
  28.   exit (0);
  29. }
  30.  
  31. static void usage (int help, const char *name)
  32. {
  33.   FILE *s;
  34.  
  35.   s = help ? stdout : stderr;
  36.  
  37.   fprintf (s, "Usage: %s -options plist_files\n", name);
  38.   fprintf (s, " Convert *.plist files to readable xml format or back to binary.\n");
  39.   fprintf (s, " Saves files with the same name.\n");
  40. //  fprintf (s, " If there are no files tries to read stdin(piping)\n");
  41.   fprintf (s, " The options are:\n\
  42.  -b                              convert to binary format\n\
  43.  -t -x                           convert to text(xml) format\n\
  44.  -h -H --help                    print this help message\n\
  45.  -v -V --version                 print version information\n");
  46.  
  47.  
  48.   if (help)
  49.     fprintf (s, "Report bugs to %s\n", REPORT_BUGS_TO);
  50.  
  51.   exit (help ? 0 : 1);
  52. }
  53.  
  54. int main(int argc, char *argv[])
  55. {
  56.     //if (argc < 1) usage (0, program_name);
  57.    
  58.     const char *program_name = argv[0];
  59.     int i = 2;
  60.     int targetFormat;
  61.     const char *status[80];
  62.  
  63.     if (argc < 2) usage (0, program_name);
  64.  
  65.     if (argv[1][0] == '-')
  66.     {
  67.         if (strcmp (argv[1], "--help") == 0
  68.              || strcmp (argv[1], "-h") == 0
  69.              || strcmp (argv[1], "-H") == 0)
  70.                
  71.             usage (1, program_name);
  72.        
  73.         else if (strcmp (argv[1], "--version") == 0
  74.                      || strcmp (argv[1], "-V") == 0
  75.                      || strcmp (argv[1], "-v") == 0)
  76.    
  77.                  print_version (program_name);
  78.              else
  79.              {
  80.                  if (strcmp (argv[1], "-b") == 0) targetFormat = BIN;
  81.                  else if (strcmp (argv[1], "-t") == 0
  82.                        || strcmp (argv[1], "-x") == 0) targetFormat = XML;
  83.                       else usage(0, program_name);
  84.                  
  85.                  while(i < argc)
  86.                  {      
  87.                      printf("%s: ", argv[i]);
  88.  
  89.                      if(0 == plistConvert(argv[i], targetFormat, status))
  90.                          printf("Ok\n");
  91.                      else printf("failed with error: %s \n", status);
  92.                      
  93.                      i++;
  94.                  }
  95.                  exit(0);  
  96.              }
  97.  
  98.     }
  99.  
  100.     usage (0, program_name);
  101. }
  102.  
  103. plistError(NSError *error, char *status)
  104. {    
  105.     char *hint = "";
  106.     char *notFound = "(file not found?)";
  107.     char *notPlist = "(file not *.plist?)";
  108.     char *chmod = "(can't write, not enough rights?)";
  109.  
  110.     char *cError = [[error domain] UTF8String];
  111.     int code = [error code];
  112.    
  113.     if(strcmp (cError, "NSCocoaErrorDomain") == 0)
  114.     {
  115.         switch (code)
  116.         {
  117.             case 260:  hint = notFound; break;
  118.             case 3840: hint = notPlist; break;
  119.             case 513:  hint = chmod;    break;
  120.         }
  121.     }
  122.  
  123.     snprintf(status, 80, "%s: code %d, %s", cError, code, hint);
  124.    
  125.     return 1;
  126.  
  127. }
  128.  
  129. int plistConvert(const char *fileName, int targetFormat, char *status)
  130. {
  131.     NSError *error;
  132.     NSPropertyListFormat format;
  133.  
  134.     NSString *path = [NSString stringWithCString: fileName
  135.                                encoding: NSASCIIStringEncoding];
  136.                      
  137.     NSData *plistData = [NSData dataWithContentsOfFile:path
  138.                                 options:NSDataReadingUncached
  139.                                 error:&error];
  140.  
  141.     if(error != NULL) return plistError(error, status);
  142.  
  143.     id plist = [NSPropertyListSerialization propertyListWithData:plistData
  144.                     options:NSPropertyListImmutable
  145.                     format:&format
  146.                     error:&error];
  147.  
  148.     if(error != NULL) return plistError(error, status);
  149.  
  150.     NSString *xmlData = [NSPropertyListSerialization dataWithPropertyList:plist
  151.                             format:(targetFormat==XML?
  152.                                         NSPropertyListXMLFormat_v1_0:
  153.                                         NSPropertyListBinaryFormat_v1_0)
  154.                             options:0
  155.                             error:&error];
  156.     if(error == NULL)
  157.     {
  158.         [xmlData writeToFile:path
  159.                  options:NSDataWritingAtomic
  160.                  error:&error];
  161.  
  162.         if(error != NULL) return plistError(error, status);
  163.  
  164.         return 0;
  165.     }
  166.     else return plistError(error, status);
  167.  
  168. }
RAW Paste Data