home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / tools / genrb / genrb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  6.9 KB  |  284 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1998, 1999     *
  6. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  7. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  8. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  9. *                                                                             *
  10. *******************************************************************************
  11. *
  12. * File genrb.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   05/25/99    stephen        Creation.
  18. *******************************************************************************
  19. */
  20.  
  21. #include <stdio.h>
  22.  
  23. #include "utypes.h"
  24. #include "cmemory.h"
  25. #include "cstring.h"
  26. #include "filestrm.h"
  27. #include "error.h"
  28. #include "parse.h"
  29. #include "write.h"
  30. #include "util.h"
  31.  
  32.  
  33. /* Protos */
  34. static void usage();
  35. static void version();
  36. static void processFile(const char *filename, UErrorCode *status);
  37. static char* make_res_filename(const char *filename, UErrorCode *status);
  38. static char* make_col_filename(const char *filename, UErrorCode *status);
  39. int main(int argc, char **argv);
  40.  
  41. /* File suffixes */
  42. #define RES_SUFFIX ".res"
  43. #define COL_SUFFIX ".col"
  44.  
  45. /* The version of genrb */
  46. #define GENRB_VERSION "1.0"
  47.  
  48.  
  49. int
  50. main(int argc,
  51.      char **argv)
  52. {
  53.   int printUsage = 0;
  54.   int printVersion = 0;
  55.   int optind = 1;
  56.   int i;
  57.   char *arg;
  58.   UErrorCode status;
  59.  
  60.  
  61.   /* parse the options */
  62.   for(optind = 1; optind < argc; ++optind) {
  63.     arg = argv[optind];
  64.     
  65.     /* version info */
  66.     if(icu_strcmp(arg, "-v") == 0 || icu_strcmp(arg, "--version") == 0) {
  67.       printVersion = 1;
  68.     }
  69.     /* usage info */
  70.     else if(icu_strcmp(arg, "-h") == 0 || icu_strcmp(arg, "--help") == 0) {
  71.       printUsage = 1;
  72.     }
  73.     /* POSIX.1 says all arguments after -- are not options */
  74.     else if(icu_strcmp(arg, "--") == 0) {
  75.       /* skip the -- */
  76.       ++optind;
  77.       break;
  78.     }
  79.     /* unrecognized option */
  80.     else if(icu_strncmp(arg, "-", icu_strlen("-")) == 0) {
  81.       printf("genrb: invalid option -- %s\n", arg+1);
  82.       printUsage = 1;
  83.     }
  84.     /* done with options, start file processing */
  85.     else {
  86.       break;
  87.     }
  88.   }
  89.  
  90.   /* print usage info */
  91.   if(printUsage) {
  92.     usage();
  93.     return 0;
  94.   }
  95.  
  96.   /* print version info */
  97.   if(printVersion) {
  98.     version();
  99.     return 0;
  100.   }
  101.  
  102.   /* generate the binary files */
  103.   for(i = optind; i < argc; ++i) {
  104.     status = U_ZERO_ERROR;
  105.     processFile(argv[i], &status);
  106.     if(U_FAILURE(status)) {
  107.       printf("genrb: %s processing file \"%s\"\n", errorName(status), argv[i]);
  108.       if(getErrorText() != 0)
  109.     printf("       (%s)\n", getErrorText());
  110.     }
  111.   }
  112.  
  113.   return 0;
  114. }
  115.  
  116. /* Usage information */
  117. static void
  118. usage()
  119. {  
  120.   puts("Usage: genrb [OPTIONS] [FILES]");
  121.   puts("Options:");
  122.   puts("  -h, --help        Print this message and exit.");
  123.   puts("  -v, --version     Print the version number of genrb and exit.");
  124. }
  125.  
  126. /* Version information */
  127. static void
  128. version()
  129. {
  130.   printf("genrb version %s (ICU version %s), by Stephen F. Booth.\n", 
  131.      GENRB_VERSION, ICU_VERSION);
  132.   puts("(C) Copyright International Business Machines Corporation, 1998, 1999");
  133.   puts("Licensed Material - Program-Property of IBM - All Rights Reserved.");
  134.   puts("US Government Users Restricted Rights - Use, duplication, or disclosure");
  135.   puts("restricted by GSA ADP Schedule Contract with IBM Corp.");
  136. }
  137.  
  138. /* Process a file */
  139. static void
  140. processFile(const char *filename,
  141.         UErrorCode *status)
  142. {
  143.   FileStream *in;
  144.   FileStream *rb_out;
  145.   struct SRBItemList *data;
  146.   char *rbname;
  147.  
  148.   if(U_FAILURE(*status)) return;
  149.  
  150.   /* Setup */
  151.   in = rb_out = 0;
  152.  
  153.   /* Open the input file for reading */
  154.   in = T_FileStream_open(filename, "r");
  155.   if(in == 0) {
  156.     *status = U_FILE_ACCESS_ERROR;
  157.     setErrorText("File not found");
  158.     return;
  159.   }
  160.  
  161.   /* Parse the data into an SRBItemList */
  162.   data = parse(in, status);
  163.  
  164.   /* Determine the target rb filename */
  165.   rbname = make_res_filename(filename, status);
  166.   if(U_FAILURE(*status)) {
  167.     goto finish;
  168.   }
  169.  
  170.   /* Open the target file  for writing */
  171.   rb_out = T_FileStream_open(rbname, "wb");
  172.   if(rb_out == 0 || T_FileStream_error(rb_out) != 0) {
  173.     *status = U_FILE_ACCESS_ERROR;
  174.     setErrorText("Could not open file for writing");
  175.     goto finish;
  176.   }
  177.  
  178.   /* Write the data to the file */
  179.   rb_write(rb_out, data, status);
  180.  
  181.  finish:
  182.  
  183.   /* Clean up */
  184.   rblist_close(data, status);
  185.   T_FileStream_close(in);
  186.   T_FileStream_close(rb_out);
  187.  
  188.   icu_free(rbname);
  189. }
  190.  
  191. /* Generate the target .res file name from the input file name */
  192. static char*
  193. make_res_filename(const char *filename,
  194.          UErrorCode *status)
  195. {
  196.   char *basename;
  197.   char *dirname;
  198.   char *resName;
  199.  
  200.   if(U_FAILURE(*status)) return 0;
  201.  
  202.   /* setup */
  203.   basename = dirname = resName = 0;
  204.  
  205.   /* determine basename, and compiled file names */
  206.   basename = (char*) icu_malloc(sizeof(char) * (icu_strlen(filename) + 1));
  207.   if(basename == 0) {
  208.     *status = U_MEMORY_ALLOCATION_ERROR;
  209.     goto finish;
  210.   }
  211.   get_basename(basename, filename);
  212.   
  213.   dirname = (char*) icu_malloc(sizeof(char) * (icu_strlen(filename) + 1));
  214.   if(dirname == 0) {
  215.     *status = U_MEMORY_ALLOCATION_ERROR;
  216.     goto finish;
  217.   }
  218.   get_dirname(dirname, filename);
  219.  
  220.   resName = (char*) icu_malloc(sizeof(char) * (icu_strlen(dirname)
  221.                            + icu_strlen(basename) 
  222.                            + icu_strlen(RES_SUFFIX) + 1));
  223.   if(resName == 0) {
  224.     *status = U_MEMORY_ALLOCATION_ERROR;
  225.     goto finish;
  226.   }
  227.   icu_strcpy(resName, dirname);
  228.   icu_strcat(resName, basename);
  229.   icu_strcat(resName, RES_SUFFIX);
  230.  
  231.  finish:
  232.   icu_free(basename);
  233.   icu_free(dirname);
  234.  
  235.   return resName;
  236. }
  237.  
  238. /* Generate the target .col file name from the input file name */
  239. static char*
  240. make_col_filename(const char *filename,
  241.          UErrorCode *status)
  242. {
  243.   char *basename;
  244.   char *dirname;
  245.   char *colName;
  246.  
  247.   if(U_FAILURE(*status)) return 0;
  248.  
  249.   /* setup */
  250.   basename = dirname = colName = 0;
  251.  
  252.   /* determine basename, and compiled file names */
  253.   basename = (char*) icu_malloc(sizeof(char) * (icu_strlen(filename) + 1));
  254.   if(basename == 0) {
  255.     *status = U_MEMORY_ALLOCATION_ERROR;
  256.     goto finish;
  257.   }
  258.   get_basename(basename, filename);
  259.   
  260.   dirname = (char*) icu_malloc(sizeof(char) * (icu_strlen(filename) + 1));
  261.   if(dirname == 0) {
  262.     *status = U_MEMORY_ALLOCATION_ERROR;
  263.     goto finish;
  264.   }
  265.   get_dirname(dirname, filename);
  266.  
  267.   colName = (char*) icu_malloc(sizeof(char) * (icu_strlen(dirname)
  268.                            + icu_strlen(basename) 
  269.                            + icu_strlen(COL_SUFFIX) + 1));
  270.   if(colName == 0) {
  271.     *status = U_MEMORY_ALLOCATION_ERROR;
  272.     goto finish;
  273.   }
  274.   icu_strcpy(colName, dirname);
  275.   icu_strcat(colName, basename);
  276.   icu_strcat(colName, COL_SUFFIX);
  277.  
  278.  finish:
  279.   icu_free(basename);
  280.   icu_free(dirname);
  281.  
  282.   return colName;
  283. }
  284.