home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / bbs / rbbstool / to_rbbs.c < prev   
C/C++ Source or Header  |  1992-09-14  |  6KB  |  322 lines

  1. /*
  2.  * from SIMTEL20, CICA, ulowel:/games format to:
  3.  * rbbs .dir format
  4.  */
  5.  
  6. #if 0
  7. 1234567890123456789012345678901 [Simtel]
  8. 4DOSANN.ZIP   B    6864  910819  4DOS runs on DOS 5.0 and Norton DOS info
  9. 1234567890123456789012345678901 [CICA]
  10. diskindx.txt    901031    Cumulative Index of the WRK Disks (below)
  11. diskroot.zip    920610    Windows Resource Kit 4/3/92
  12. ---------->
  13. 9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST modems.
  14. #endif
  15.  
  16. #include <ctype.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys\stat.h>
  21. #include <time.h>
  22.  
  23. #define SIMTEL    1
  24. #define CICA    2
  25. #define ULOWEL    3
  26.  
  27. struct data {
  28.     char filename[15];
  29.     long filesize;
  30.     int year;
  31.     int month;
  32.     int day;
  33.     char description[900];
  34. };
  35.  
  36. int pastheader = 0;
  37. int in_type;
  38. struct data line;
  39. char path[128];
  40.  
  41. int
  42. readline(void) {
  43.     static char buf[1000];
  44.     char workbuf[1000];
  45.     static char datebuf[5] = "12";
  46.     char *r;
  47.     
  48.     if (buf[0] == 0)
  49.         if (NULL == gets(buf))
  50.             exit(0);
  51.     
  52.     line.filename[0] = 0;
  53.     line.description[0] = 0;
  54.         
  55.     if (in_type == ULOWEL) {
  56.         if (0 != strnicmp(buf, "file:", 5)) {
  57.             buf[0] = 0;
  58.             return(0);
  59.         }
  60.         
  61.         r = strtok(buf, "\r\n\t ");
  62.         if (! r) {
  63.             fprintf(stderr, "unknown line:\n%s\n", buf);
  64.             exit(1);
  65.             buf[0] = 0;
  66.             return(0);
  67.         }
  68.         
  69.         r += strlen(r) + 1;        /* skip 'file:' */
  70.         while (isspace(*r))        /* skip white */
  71.             ++r;
  72.         
  73.         r = strtok(r, "\r\n\t ");
  74.         if (! r) {
  75.             fprintf(stderr, "unknown line:\n%s\n", buf);
  76.             exit(1);
  77.             buf[0] = 0;
  78.             return(0);
  79.         }
  80.         
  81.         strcpy(line.filename, r);
  82.  
  83.         while (1) {
  84.             if (NULL == gets(buf))
  85.                 return(1);
  86.             if (buf[0] == '\r')
  87.                 continue;
  88.             if (! buf[0])
  89.                 continue;
  90.             if (isspace(buf[0])) {
  91.                 strcat(line.description, " ");
  92.                 strcat(line.description, &buf[1]);
  93.             } else
  94.                 return(0);
  95.         }
  96.     } else {
  97.  
  98.         /* wait for header */
  99.         if (! pastheader) {
  100.             if (in_type == CICA) {
  101.                 if (0 == strlen(buf))
  102.                     ++pastheader;
  103.             } else {
  104.                 if (strstr(buf, "================"))
  105.                     ++pastheader;
  106.             }
  107.             buf[0] = 0;
  108.             return(0);
  109.         }
  110.     
  111.         if (strlen(buf) == 0) {
  112.             fprintf(stderr, "zero length line\n");
  113.             exit(1);
  114.             buf[0] = 0;
  115.             return(0);
  116.         }
  117.  
  118.         if (strlen(buf) < 20) {
  119.             fprintf(stderr, "warning: line too short:\n%s\n", buf);
  120.             exit(1);
  121.         }
  122.     
  123.         strcpy(workbuf, buf);
  124.     
  125.         r = strtok(buf, "\r\n\t ");
  126.         if (! r) {
  127.             fprintf(stderr, "unknown line:\n%s\n", buf);
  128.             exit(1);
  129.             buf[0] = 0;
  130.             return(0);
  131.         }
  132.     
  133.         strcpy(line.filename, r);
  134.     
  135.         r += strlen(r) + 1;        /* skip name */
  136.         while (isspace(*r))        /* skip white */
  137.             ++r;
  138.  
  139.         if (in_type == SIMTEL) {
  140.             ++r;                    /* skip file type */
  141.     
  142.             while (isspace(*r))        /* skip white */
  143.                 ++r;
  144.     
  145.             r = strtok(r, "\r\n\t ");
  146.             if (! r) {
  147.                 fprintf(stderr, "unknown line:\n%s\n", buf);
  148.                 exit(1);
  149.                 line.filename[0] = 0;
  150.                 buf[0] = 0;
  151.                 return(0);
  152.             }
  153.             
  154.             line.filesize = atol(r);
  155.             r += strlen(r) + 1;
  156.     
  157.             while (isspace(*r))        /* skip white */
  158.                 ++r;
  159.         }
  160.     
  161.         datebuf[0] = *r;
  162.         ++r;
  163.         datebuf[1] = *r;
  164.         ++r;
  165.         line.year = atoi(datebuf);
  166.     
  167.         datebuf[0] = *r;
  168.         ++r;
  169.         datebuf[1] = *r;
  170.         ++r;
  171.         line.month = atoi(datebuf);
  172.         datebuf[0] = *r;
  173.         ++r;
  174.         datebuf[1] = *r;
  175.         ++r;
  176.         line.day = atoi(datebuf);
  177.     
  178.         while (isspace(*r))        /* skip white */
  179.             ++r;
  180.     
  181.         if (*r)    
  182.             strcpy(line.description, r);
  183.         buf[0] = 0;
  184.         return(0);
  185.     }
  186. }
  187.  
  188. void
  189. writeline(void) {
  190.     FILE *foo;
  191.     char buf[128];
  192.     struct stat stat_buf;
  193.     struct tm *tm;
  194.     time_t t;
  195.     char *p, *r;
  196.  
  197.     if (! line.filename[0])
  198.         return;
  199.  
  200. #if 0    
  201. 9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST modems.
  202. #endif
  203.  
  204.     if (in_type == CICA || in_type == ULOWEL) {
  205.         sprintf(buf, "%s\\%s", path, line.filename);
  206.         if (NULL == (foo = fopen(buf, "r"))) {
  207.             fprintf(stderr, "failed opening '%s'\n", buf);
  208.             exit(1);
  209.             exit(1);
  210.         }
  211.  
  212.         stat(buf, &stat_buf);
  213.  
  214.         fclose(foo);
  215.         
  216.         line.filesize = stat_buf.st_size;
  217.     }
  218.  
  219.     if (in_type == ULOWEL) {
  220.         t = stat_buf.st_ctime;
  221.         tm = localtime(&t);
  222.         line.month = tm->tm_mon;
  223.         line.day = tm->tm_mday;
  224.         line.year = tm->tm_year;
  225.     }
  226.     
  227.     printf("%-13s%8ld  %02d-%02d-%02d ",
  228.         line.filename,
  229.         line.filesize,
  230.         line.month,
  231.         line.day,
  232.         line.year);
  233.     
  234.     /* write wrapping description */
  235.  
  236.     r = line.description;
  237.     if (strlen(r) <= 45) {        
  238.         printf("%s\n", r);
  239.         return;
  240.     }
  241.  
  242.     p = r + 45;
  243.     while (! isspace(*p))
  244.         --p;
  245.     
  246.     *p = 0;
  247.     printf("%s\n  ", r);
  248.     r = p + 1;
  249.         
  250.     while (1) {
  251.         if (strlen(r) <= 76) {        
  252.             printf("%s\n", r);
  253.             return;
  254.         }
  255.         p = r + 76;
  256.         while (! isspace(*p))
  257.             --p;
  258.  
  259. #if 0
  260. 9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST
  261.                                | modems.
  262. #endif
  263.         *p = 0;
  264.         printf("%s\n  ", r);
  265.         r = p + 1;
  266.     }
  267. }
  268.  
  269. void
  270. help(void) {
  271.  
  272.     switch (in_type) {
  273.         case SIMTEL:
  274.             fprintf(stderr,
  275.                 "usage: to_rbbs simtel < in > out\n");
  276.             break;
  277.         case ULOWEL:
  278.             fprintf(stderr,
  279.                 "usage: to_rbbs ulowel [directory path] < in > out\n");
  280.             break;
  281.         case CICA:
  282.             fprintf(stderr,
  283.                 "usage: to_rbbs cica [directory path] < in > out\n");
  284.             break;
  285.         default:
  286.             fprintf(stderr,
  287.     "usage: to_max {simtel | cica | ulowel} [directory path] < in > out\n");
  288.             fprintf(stderr,
  289.     "               pick a input format\n");
  290.             break;
  291.     }
  292.     exit(1);
  293. }
  294.  
  295. void _Cdecl
  296. main(int argc, char *argv[]) {
  297.     int rv;
  298.  
  299.     if (argc == 1)
  300.         help();
  301.  
  302.     if (0 == stricmp("simtel", argv[1])) {
  303.         in_type = SIMTEL;
  304.     } else if (0 == stricmp("cica", argv[1])) {
  305.         in_type = CICA;
  306.     } else if (0 == stricmp("ulowel", argv[1])) {
  307.         in_type = ULOWEL;
  308.     } else
  309.         help();
  310.     
  311.     if (in_type == ULOWEL || in_type == CICA) {
  312.         strcpy(path, argv[2]);
  313.     }
  314.     
  315.     while (1) {
  316.         rv = readline();
  317.         writeline();
  318.         if (rv)
  319.             exit(0);
  320.     }
  321. }
  322.