home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / disk / cdrom / mkisofs / trans.c < prev    next >
C/C++ Source or Header  |  1994-11-21  |  5KB  |  216 lines

  1. /*
  2.  * File trans.c - scan TRANS.TBL file
  3.  *
  4.  * Written by Frank Munkert (1994).
  5.  *
  6.  */
  7.  
  8. /*
  9.  
  10.   TRANS.TBL contains lines of the kind
  11.   
  12.       amigafilename->isofilename
  13.  
  14.   amigafilename: any sequence of characters; special characters may be
  15.       escaped with `\`
  16.     
  17.   dosfilename: "8.3" style iso file name; only letters, digits and
  18.         underscore are allowed.
  19.  
  20.   All tabs and spaces will be ignored, unless they are escaped.
  21.             
  22.   Examples:
  23.   
  24.       drivers->DRIVERS
  25.     drivers  ->   DRIVERS.
  26.     file.c -> FILE.C
  27.     dow\ jones.txt -> DOWJONES.TXT
  28.  
  29. */
  30.  
  31. #include <ctype.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #include "trans.h"
  37. #include "unix/unixlib.h"
  38.  
  39. #ifdef LATTICE
  40. #include <proto/utility.h>
  41. #endif
  42. #if defined(__GNUC__) || defined(AZTEC_C)
  43. #include <clib/utility_protos.h>
  44. #endif
  45.  
  46.  
  47. void scan_trans_tbl (struct transtbl* tbl, const char *dir_name)
  48. {
  49.   char *tbl_file_name;
  50.   FILE *in;
  51.   static char buf[160];
  52.   char *cp;
  53.   char amiga_name[33];
  54.   char iso_name[13];
  55.   int i;
  56.   char *tmp;
  57.   struct translation* new;
  58.   int line = 0;
  59.   struct stat sbuf;
  60.  
  61.   tbl->number_of_entries = 0;
  62.   tbl->entries = NULL;
  63.  
  64.   tbl_file_name = malloc (strlen (dir_name) + strlen ("TRANS.TBL") + 2);
  65.   if (!tbl_file_name) {
  66.     fprintf (stderr, "out of memory!\n");
  67.     exit (1);
  68.   }
  69.  
  70.   strcpy (tbl_file_name, dir_name);
  71.   if (dir_name[strlen(dir_name)-1] != ':')
  72.     strcat (tbl_file_name, "/");
  73.  
  74.   strcat (tbl_file_name, "TRANS.TBL");
  75.   in = fopen (tbl_file_name, "r");
  76.   if (!in) {
  77.     free (tbl_file_name);
  78.     return;
  79.   }
  80.   
  81.   for (;;) {
  82.  
  83.     line++;
  84.     
  85.     cp = fgets (buf, sizeof (buf), in);
  86.     if (!cp)
  87.       break;
  88.     if (!(cp = strchr (buf, '\n'))) {
  89.       fprintf (stderr, "WARNING: line too long in line %d in file %s\n",
  90.                  line, tbl_file_name);
  91.       free (tbl_file_name);
  92.       return;
  93.     }
  94.     *cp = 0;
  95.     
  96.     /* skip blank lines: */
  97.     if (strspn (buf, " \t") == strlen (buf))
  98.       continue;
  99.       
  100.     /* read AmigaDOS name: */
  101.     for (cp=buf; isspace (*cp); cp++) ;
  102.     i=0;
  103.     while (!(cp[0] == '-' && cp[1] == '>')) {
  104.       if (*cp == 0) {
  105.         fprintf (stderr, "WARNING: missing '->' in line %d file %s\n",
  106.              line, tbl_file_name);
  107.     free (tbl_file_name);
  108.     return;
  109.       }
  110.       if (isspace (*cp)) {
  111.         cp++;
  112.       } else {
  113.         if (*cp == '\\')
  114.           cp++;
  115.  
  116.         if (i == 32)
  117.       break;
  118.         amiga_name[i++] = *cp++;
  119.       }
  120.     }
  121.     cp += 2;
  122.     amiga_name[i] = 0;
  123.     
  124.     /* read ISO name: */
  125.     for (; isspace (*cp); cp++) ;
  126.     i=0;
  127.     while (*cp) {
  128.       if (isspace (*cp)) {
  129.         cp++;
  130.       } else {
  131.         if (i == 12)
  132.       break;
  133.         iso_name[i++] = *cp++;
  134.       }
  135.     }
  136.     iso_name[i] = 0;
  137.  
  138.     /* check if the Amiga file really exists: */
  139.     tmp = malloc (strlen (dir_name) + strlen (amiga_name) + 2);
  140.     if (!tmp) {
  141.       fprintf (stderr, "out of memory!\n");
  142.       exit (1);
  143.     }
  144.     strcpy (tmp, dir_name);
  145.     if (dir_name[strlen(dir_name)-1] != ':')
  146.       strcat (tmp, "/");
  147.     strcat (tmp, amiga_name);
  148.     if (stat (tmp, &sbuf) == -1) {
  149.       fprintf (stderr, "WARNING: file %s (from %s) not found\n",
  150.                  tmp, tbl_file_name);
  151.     }
  152.     free (tmp);
  153.  
  154.     /* convert iso name to uppercase: */
  155.     for (cp=iso_name; *cp; cp++)
  156.       *cp = toupper (*cp);
  157.  
  158.     /* check if the iso name is valid: */
  159.     if (strspn (iso_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.")
  160.         != strlen (iso_name)) {
  161.       fprintf (stderr, "ERROR: invalid name '%s' in file %s\n",
  162.                  iso_name, tbl_file_name);
  163.       continue;
  164.     }
  165.     cp = strchr (iso_name, '.');
  166.     if ((cp && strchr (cp+1, '.')) ||
  167.         (!cp && strlen (iso_name) > 8) ||
  168.     (cp && cp-iso_name > 8) ||
  169.     (cp && strlen (iso_name) - (cp-iso_name+1) > 3) ||
  170.     (S_ISDIR(sbuf.st_mode) && cp) ||
  171.     (!S_ISDIR(sbuf.st_mode) && !cp)) {
  172.       fprintf (stderr, "ERROR: invalid name '%s' in file %s\n",
  173.                  iso_name, tbl_file_name);
  174.     }
  175.  
  176.     /* create table entry: */
  177.     new = (struct translation*) malloc (sizeof (*new));
  178.     if (!new) {
  179.       fprintf (stderr, "out of memory!\n");
  180.       exit (1);
  181.     }
  182.     strcpy (new->from, amiga_name);
  183.     strcpy (new->to, iso_name);
  184.     new->next = tbl->entries;
  185.     tbl->entries = new;
  186.     tbl->number_of_entries++;
  187.   }
  188.   
  189.   fclose (in);
  190.   free (tbl_file_name);
  191. }
  192.  
  193. void free_trans_tbl (struct transtbl* tbl)
  194. {
  195.   struct translation *ptr, *next;
  196.   
  197.   for (ptr = tbl->entries; ptr; ptr = next) {
  198.     next = ptr->next;
  199.     free (ptr);
  200.   }
  201.  
  202.   tbl->number_of_entries = 0;
  203. }
  204.  
  205. const char* translate (struct transtbl* tbl, const char* name)
  206. {
  207.   struct translation *ptr;
  208.   
  209.   for (ptr = tbl->entries; ptr; ptr = ptr->next) {
  210.     if (Stricmp ((UBYTE*) name, (UBYTE*) ptr->from) == 0) {
  211.       return ptr->to;
  212.     }
  213.   }
  214.   return NULL;
  215. }
  216.