home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / MISC / CDROM.ZIP / ALDEKIT.ZIP / ALDEKIT.C next >
Encoding:
C/C++ Source or Header  |  1989-12-16  |  8.0 KB  |  272 lines

  1. /*---------------------------------------------------------------------------*/
  2. /*                                  ALDEkit                                  */
  3. /*                               Version 1.00                                */
  4. /*                            December 16th, 1989                            */
  5. /*                           (C) Copyright 1989 CMI                          */
  6. /*---------------------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <process.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12. #include <ctype.h>
  13. #include <string.h>
  14. #include <dos.h>
  15. #include <io.h>
  16.  
  17. #define BELL    0x07                        /* bell */
  18. #define BS      0x08                        /* backspace */
  19. #define CR      0x0D                        /* carriage return */
  20. #define SPACE   0x20                        /* space */
  21. #define LINELEN 83                          /* char/line plus CR-LF-NULL */
  22. #define BUFSIZE 13                          /* input buffer size */
  23. #define SLPTIME 3                           /* sleep time in seconds */
  24.  
  25. /* Function prototypes */
  26.  
  27. void Print_Menu(void);
  28. void Get_Filename(void);
  29. void Erase_Line(void);
  30. char Prompt(int type);
  31. int Transfer(void);
  32.  
  33. /* Global variables */
  34.  
  35. const char *version = "1.00";               /* current version number */
  36. const char *menu = "MENU.TXT";              /* full name of menu file */
  37. const char *preset_src_path = "J:";         /* preset source path */
  38. const char *preset_cat_ext = ".CAT";        /* preset catalog file ext */
  39. const char *preset_dest_path = "I:\\CDROM"; /* preset destination path */
  40. const char *bbs_file = "FILES.BBS";         /* BBS download file list */
  41.  
  42. char dir_mark[] = {'\\', '\0'}, line_buffer[LINELEN], input_buffer[BUFSIZE];
  43. char mst_path[80], cat_path[80], src_path[80], dest_path[80];
  44. FILE *cat_file, *src_file, *dest_file;
  45.  
  46. main()
  47. {
  48.     char ch;
  49.     int done = 0, line, prompt;
  50.  
  51.     while (!done) {
  52.         Print_Menu();                       /* display main menu */
  53.  
  54.         do {                                /* make selection */
  55.             printf(" Enter file area number to read (return to exit) -> ");
  56.             Get_Filename();
  57.  
  58.             if (!input_buffer[0]) {
  59.                 Erase_Line();
  60.                 printf(" ALDEkit Version %s ", version);
  61.                 printf("(C) Copyright 1989 CMI\n");
  62.                 exit(0);
  63.             }
  64.             strset(mst_path, '\0');             /* clear master path */
  65.             strcat(mst_path, preset_src_path);  /* create master path */
  66.             strcat(mst_path, dir_mark);
  67.             strcat(mst_path, input_buffer);
  68.             strcpy(src_path, mst_path);         /* create source path */
  69.             strcpy(cat_path, mst_path);         /* create catalog path */
  70.             strcat(cat_path, dir_mark);
  71.             strcat(cat_path, input_buffer);
  72.             strcat(cat_path, preset_cat_ext);   /* complete catalog path */
  73.  
  74.             if ((cat_file = fopen(cat_path, "r")) == NULL) {
  75.                 Erase_Line();
  76.                 printf(" Cannot open catalog file ... try again. ");
  77.                 sleep(SLPTIME);
  78.                 Erase_Line();
  79.             }
  80.         } while (cat_file == NULL);
  81.  
  82.         clrscr();
  83.         line = prompt = 0;
  84.         ch = '\0';
  85.  
  86.         do {
  87.             if (fgets(line_buffer, LINELEN, cat_file) == NULL) {
  88.                 fclose(cat_file);
  89.                 prompt = 1;
  90.             }
  91.             else printf("%s", line_buffer);
  92.  
  93.             if (line++ == 23) {
  94.                 line = 0;
  95.  
  96.                 do {
  97.                     ch = Prompt(prompt);
  98.                     if (ch == 'T') Transfer();
  99.                 } while (ch == 'T');
  100.             }
  101.         } while (ch != 'E');
  102.     }
  103.     return 0;                               /* normal program termination */
  104. }
  105.  
  106. void Print_Menu(void)                       /* display main menu */
  107. {
  108.     FILE *menu_file;
  109.  
  110.     clrscr();
  111.  
  112.     /* make sure file exists otherwise terminate program */
  113.     if ((menu_file = fopen(menu, "r")) == NULL) {
  114.         Erase_Line();
  115.         printf(" Cannot open menu file ... program terminated.\n");
  116.         exit(1);
  117.     }
  118.     while (fgets(line_buffer, LINELEN, menu_file))
  119.         printf("%s", line_buffer);
  120.     fclose(menu_file);
  121. }
  122.  
  123. void Get_Filename(void)                     /* get filename */
  124. {
  125.     char ch;
  126.     int i, done;
  127.  
  128.     strset(input_buffer, '\0');             /* clear buffer */
  129.     done = i = 0;
  130.  
  131.     while (!done) {
  132.         ch = getch();
  133.  
  134.         if (!ch) {                          /* skip extended keys */
  135.             getch();
  136.             putchar(BELL);
  137.             continue;
  138.         }
  139.         switch (ch) {
  140.             case CR:
  141.                 done = 1;
  142.                 break;
  143.             case BS:
  144.                 if (i > 0) {
  145.                     putchar(BS);
  146.                     putchar(SPACE);
  147.                     putchar(BS);
  148.                     input_buffer[--i] = '\0';
  149.                 }
  150.                 break;
  151.             default:
  152.                 if (ch > ' ' && ch <= '~')
  153.                     if (i < BUFSIZE - 1) {
  154.                         putchar(ch);
  155.                         input_buffer[i++] = toupper(ch);
  156.                         break;
  157.                     }
  158.                 putchar(BELL);
  159.                 break;
  160.         }
  161.     }
  162. }
  163.  
  164. void Erase_Line(void)
  165. {
  166.     printf("\r%79s\r", " ");
  167. }
  168.  
  169. char Prompt(int type)               /* prompt and get response during list */
  170. {
  171.     char c, ch;
  172.     int done = 0;
  173.  
  174.     if (!type) printf(" (M)ore,");
  175.     printf(" (T)ransfer to download area, (E)xit to main menu -> ");
  176.  
  177.     while (!done) {
  178.         ch = toupper(c = getch());
  179.  
  180.         if (!ch) {                          /* skip extended keys */
  181.             getch();
  182.             putchar(BELL);
  183.             continue;
  184.         }
  185.         if (c >= ' ' && c <= '~') {
  186.             putchar(c);
  187.             putchar(BS);
  188.             switch (ch) {
  189.                 case 'M':
  190.                     if (!type) {
  191.                         done = 1;
  192.                         break;
  193.                     }
  194.                     else putchar(BELL);
  195.                     break;
  196.                 case 'T':
  197.                 case 'E':
  198.                     done = 1;
  199.                     break;
  200.                 default:
  201.                     putchar(BELL);
  202.                     break;
  203.             }
  204.         }
  205.         else putchar(BELL);
  206.     }
  207.     Erase_Line();
  208.     return ch;
  209. }
  210.  
  211. int Transfer(void)
  212. {
  213.     int ch;
  214.     struct ftime date;
  215.  
  216.     Erase_Line();
  217.     printf(" Enter filename to transfer to download area -> ");
  218.     Get_Filename();
  219.     strset(src_path, '\0');
  220.     strcat(src_path, mst_path);
  221.     strcat(src_path, dir_mark);
  222.     strcat(src_path, input_buffer);
  223.  
  224.     if ((src_file = fopen(src_path, "rb")) == NULL) {
  225.         Erase_Line();
  226.         printf(" Cannot open source file for transfer ... try again. ");
  227.         sleep(SLPTIME);
  228.         Erase_Line();
  229.         return 1;
  230.     }
  231.     getftime(fileno(src_file), &date);
  232.  
  233.     strset(dest_path, '\0');
  234.     strcat(dest_path, preset_dest_path);
  235.     strcat(dest_path, dir_mark);
  236.     strcat(dest_path, input_buffer);
  237.  
  238.     if (!access(dest_path, 0)) {
  239.         fclose(src_file);
  240.         Erase_Line();
  241.         printf(" File already exists ... try again. ");
  242.         sleep(SLPTIME);
  243.         Erase_Line();
  244.         return 2;
  245.     }
  246.     dest_file = fopen(dest_path, "w+b");
  247.     Erase_Line();
  248.     printf(" One moment please ... transferring file to download area. ");
  249.  
  250.     while ((ch = fgetc(src_file)) != EOF)
  251.         fputc(ch, dest_file);
  252.  
  253.     fclose(src_file);
  254.     fclose(dest_file);
  255.     dest_file = fopen(dest_path, "rb");
  256.     setftime(fileno(dest_file), &date);
  257.     fclose(dest_file);
  258.  
  259.     strset(dest_path, '\0');
  260.     strcat(dest_path, preset_dest_path);
  261.     strcat(dest_path, dir_mark);
  262.     strcat(dest_path, bbs_file);
  263.  
  264.     strcat(input_buffer, "\r\n");
  265.     dest_file = fopen(dest_path, "ab");
  266.     fwrite(input_buffer, strlen(input_buffer), 1, dest_file);
  267.     fclose(dest_file);
  268.  
  269.     Erase_Line();
  270.     return 0;
  271. }
  272.