home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / adapt_378.lzh / Adapt / adapt.c < prev    next >
C/C++ Source or Header  |  1990-10-10  |  3KB  |  137 lines

  1. #include <exec/types.h>
  2. #include <libraries/dos.h>
  3. #include <exec/memory.h>
  4. #include <clicodes.h>
  5.  
  6.  
  7. void *AllocMem();
  8.  
  9.  
  10. ULONG Length(name)
  11. UBYTE *name;
  12.   {
  13.   struct Lock *lock, *Lock();
  14.   ULONG length = 0;
  15.   struct FileInfoBlock *info;
  16.  
  17.   if(!(lock = Lock(name, ACCESS_READ))) return(0L);
  18.   if(!(info = (struct FileInfoBlock *)
  19.        AllocMem((LONG)(sizeof(struct FileInfoBlock)), 0L)))
  20.     {
  21.     UnLock(lock);
  22.     return(0L);
  23.     }
  24.   Examine(lock, info);
  25.   UnLock(lock);
  26.   length = info->fib_Size;
  27.   FreeMem(info, (LONG)(sizeof(struct FileInfoBlock)));
  28.   return(length);
  29.   }
  30.  
  31.  
  32. main(argc, argv)
  33. int argc;
  34. char *argv[];
  35.   {
  36.   struct FileHandle *source, *dest, *Open();
  37.   UBYTE *from, *qmem, *zmem, *to;
  38.   BOOL change = FALSE, strcmp();
  39.   int tabsize = 0, atoi();
  40.   ULONG qlength, zlength, tab = 0, loop;
  41.   LONG bytes, Read(), Write();
  42.  
  43.   if (argc < 3 || argc > 4 || !strcmp(argv[1], "?"))
  44.     {
  45.     CLITextColor3();
  46.     CLIStyleBold();
  47.     printf("\nAdapt ");
  48.     CLIStyleNormal();
  49.     CLITextColor1();
  50.     printf("- Version 2.2 by Lars Eggert in March 1990.\n");
  51.     printf("Converts the ASCII codes of German ´umlauts´ (äöüÄÖÜß)\n");
  52.     printf("in MS-DOS files into the right Amiga codes and can change\n");
  53.     printf("TABs into spaces.\n\n");
  54.     printf("Usage: ADAPT <source> <destination> [n]\n\n");
  55.     printf("Note: <source> and <destination> have to be given, whereas\n");
  56.     printf("      [n] is optional. The [n] parameter specifies the number\n");
  57.     printf("      of spaces one TAB shall have after converting.\n\n");
  58.     exit(0);
  59.     }
  60.   if(!strcmp(argv[1], argv[2]))
  61.     {
  62.     puts("Error: Same source and destination file!");
  63.     exit(20);
  64.     }
  65.   if(argc == 4)
  66.     {
  67.     change = TRUE;
  68.     tabsize = atoi(argv[3]);
  69.     }
  70.   if(!(qlength = Length(argv[1])))
  71.     {
  72.     puts("Error: Source not found or not enough memory to convert!");
  73.     exit(20);
  74.     }
  75.   if(!(source = Open(argv[1], MODE_OLDFILE)))
  76.     {
  77.     puts("Fehler: Source not found!");
  78.     exit(20);
  79.     }
  80.   if(!(qmem = AllocMem(qlength, 0L)))
  81.     {
  82.     puts("Fehler: Not enough memory!");
  83.     exit(20);
  84.     }
  85.   bytes = Read(source, qmem, qlength);
  86.   if(bytes < 0)
  87.     {
  88.     puts("Error: File read error!");
  89.     exit(20);
  90.     }
  91.   Close(source);
  92.   for(from = qmem; from <= (UBYTE *)(qmem+qlength); from++) if(*from == 9) ++tab;
  93.   zlength = qlength + tab*(tabsize-1);
  94.   if(!(to = zmem = AllocMem(zlength, 0L)))
  95.     {
  96.     puts("Fehler: Nicht genug Speicher!");
  97.     FreeMem(qmem, qlength);
  98.     exit(20);
  99.     }
  100.   for(from = qmem; from <= (UBYTE *)(qmem+qlength); from++)
  101.     {
  102.     switch(*from)
  103.       {
  104.       case 132: *(to++) = 'ä'; break;
  105.       case 148: *(to++) = 'ö'; break;
  106.       case 129: *(to++) = 'ü'; break;
  107.       case 142: *(to++) = 'Ä'; break;
  108.       case 153: *(to++) = 'Ö'; break;
  109.       case 154: *(to++) = 'Ü'; break;
  110.       case 225: *(to++) = 'ß'; break;
  111.       case   9: if(change) for(loop = 0; loop < tabsize; loop++) *(to++) = ' ';
  112.                 else *to++ = *from;
  113.                 break;
  114.       default:  *to++ = *from; break;
  115.       }
  116.     }
  117.   if(!(dest = Open(argv[2], MODE_NEWFILE)))
  118.     {
  119.     puts("Error: Could not open destination file!");
  120.     FreeMem(qmem, qlength);
  121.     FreeMem(zmem,zlength);
  122.     exit(20);
  123.     }
  124.   bytes = Write(dest, zmem, zlength);
  125.   if(bytes != zlength)
  126.     {
  127.     puts("Error: Disk write error!");
  128.     FreeMem(qmem, qlength);
  129.     FreeMem(zmem, zlength);
  130.     Close(dest);
  131.     exit(20);
  132.     }
  133.   FreeMem(qmem, qlength);
  134.   FreeMem(zmem, zlength);
  135.   Close(dest);
  136.   exit(0);
  137.   }