home *** CD-ROM | disk | FTP | other *** search
/ Brotikasten / BROTCD01.iso / amiga / unp00.lha / UnP00.c < prev    next >
C/C++ Source or Header  |  1994-10-01  |  3KB  |  166 lines

  1. /***********************/
  2. /*               */
  3. /* UnP00 v1.0           */
  4. /* ⌐ 1994 David Kinder */
  5. /*               */
  6. /* Compile with:       */
  7. /* DCC -mi -r UnP00.c  */
  8. /*               */
  9. /***********************/
  10.  
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <exec/memory.h>
  14. #include <proto/dos.h>
  15. #include <proto/exec.h>
  16. #include <proto/intuition.h>
  17.  
  18. struct RDArgs *rdargs;
  19. struct Remember *memkey;
  20. struct List *list;
  21. struct AnchorPath *anchor;
  22.  
  23. extern struct Library *IntuitionBase;
  24.  
  25. LONG args[1];
  26. UBYTE ver[] = "$VER:UnP00 1.0 (1.10.94)";
  27.  
  28. void quit(void);
  29. void error(LONG code);
  30. void storename(void);
  31. void convert(void);
  32.  
  33. main()
  34. {
  35. LONG match;
  36.  
  37.   if (((struct DosLibrary *)DOSBase)->dl_lib.lib_Version < 36) quit();
  38.   if ((list = AllocRemember(&memkey,sizeof(struct List),MEMF_CLEAR)) == NULL)
  39.     quit();
  40.   NewList(list);
  41.   if ((anchor = AllocRemember(&memkey,sizeof(struct AnchorPath)+256,
  42.     MEMF_CLEAR|MEMF_PUBLIC)) == NULL) quit();
  43.   anchor->ap_BreakBits = SIGBREAKF_CTRL_C;
  44.   anchor->ap_Strlen = 255;
  45.   if ((rdargs = AllocDosObject(DOS_RDARGS,NULL)) == NULL) quit();
  46.   if (ReadArgs("C64FILE/A",args,rdargs) == NULL) error(IoErr());
  47.  
  48.   match = MatchFirst((STRPTR)args[0],anchor);
  49.   while (match == 0)
  50.   {
  51.     storename();
  52.     match = MatchNext(anchor);
  53.   }
  54.   MatchEnd(anchor);
  55.   if (match != ERROR_NO_MORE_ENTRIES) error(match);
  56.   convert();
  57.   quit();
  58. }
  59.  
  60. void error(LONG code)
  61. {
  62.   PrintFault(code,NULL);
  63.   quit();
  64. }
  65.  
  66. void quit()
  67. {
  68.   FreeRemember(&memkey,TRUE);
  69.   if (rdargs)
  70.   {
  71.     FreeArgs(rdargs);
  72.     FreeDosObject(DOS_RDARGS,rdargs);
  73.   }
  74.   exit(0);
  75. }
  76.  
  77. void storename()
  78. {
  79. struct Node *node;
  80. UBYTE *name;
  81.  
  82.   if (node = AllocRemember(&memkey,sizeof(struct Node),MEMF_CLEAR))
  83.   {
  84.     if (name = AllocRemember(&memkey,strlen(anchor->ap_Buf),MEMF_CLEAR))
  85.     {
  86.       strcpy(name,anchor->ap_Buf);
  87.       node->ln_Name = name;
  88.       AddTail(list,node);
  89.     }
  90.   }
  91. }
  92.  
  93. void convert()
  94. {
  95. char buffer[17];
  96. struct Node *node;
  97. LONG ch,i,all;
  98. BPTR from,to,lock;
  99.  
  100.   Printf("UnP00 v1.0 ⌐ 1994 David Kinder\n\n");
  101.   all = FALSE;
  102.   node = list->lh_Head;
  103.   while (node->ln_Succ != NULL)
  104.   {
  105.     from = 0; to = 0;
  106.     if (CheckSignal(SIGBREAKF_CTRL_C) != 0) error(ERROR_BREAK);
  107.     Printf("Opening \"%s\"... ",node->ln_Name);
  108.     Flush(Output());
  109.     if ((from = Open(node->ln_Name,MODE_OLDFILE)) == 0)
  110.     {
  111.       PrintFault(IoErr(),NULL);
  112.       goto end;
  113.     }
  114.     FRead(from,buffer,8,1);
  115.     if (strcmp(buffer,"C64File") != 0)
  116.     {
  117.       Printf("not a C64Neu .P00 data file\n");
  118.       goto end;
  119.     }
  120.     FRead(from,buffer,18,1);
  121.     Printf("\n");
  122.     for (i = 0; i < 18; i++) *(buffer + i) = tolower(*(buffer + i));
  123.     if (all == FALSE)
  124.     {
  125.       if ((lock = Lock(buffer,ACCESS_READ)) != 0)
  126.       {
  127.     UnLock(lock);
  128.     Printf("Output file \"%s\" exists. Overwrite (y/n/a)? ",buffer);
  129.     Flush(Output());
  130.     ch = FGetC(Input());
  131.     Flush(Input());
  132.     switch (ch)
  133.     {
  134.       case 'y':
  135.         break;
  136.       case 'a':
  137.         all = TRUE;
  138.         break;
  139.       default:
  140.         goto end;
  141.         break;
  142.     }
  143.       }
  144.     }
  145.     Printf("Writing \"%s\"... ",buffer);
  146.     Flush(Output());
  147.     if ((to = Open(buffer,MODE_NEWFILE)) == 0)
  148.     {
  149.       PrintFault(IoErr(),NULL);
  150.       goto end;
  151.     }
  152.     do
  153.     {
  154.       ch = FGetC(from);
  155.       if (ch != -1) FPutC(to,ch);
  156.     }
  157.     while (ch != -1);
  158.     Printf("\n");
  159.  
  160. end:
  161.     if (to) Close(to);
  162.     if (from) Close(from);
  163.     node = node->ln_Succ;
  164.   }
  165. }
  166.