home *** CD-ROM | disk | FTP | other *** search
/ CICA 1992 November / CICA_MS_Windows_CD-ROM_Walnut_Creek_November_1992.iso / bbs / sftool / convert.c < prev    next >
C/C++ Source or Header  |  1992-11-13  |  14KB  |  797 lines

  1. /*
  2.  * from SIMTEL20, CICA, ulowel, cug
  3.  * to:
  4.  * rbbs .dir
  5.  * opus files.bbs
  6.  * spitfire
  7.  * pcboard
  8.  */
  9.  
  10. #if 0
  11. 1234567890123456789012345678901 [Simtel]
  12. 4DOSANN.ZIP   B    6864  910819  4DOS runs on DOS 5.0 and Norton DOS info
  13. 1234567890123456789012345678901 [CICA]
  14. diskindx.txt    901031    Cumulative Index of the WRK Disks (below)
  15. diskroot.zip    920610    Windows Resource Kit 4/3/92
  16. ---------->
  17. opus:
  18. AD-FEBBS.ZIP Great Demo for Febbs made by OUNO-Soft.
  19. rbbs:
  20. 9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST modems.
  21. #endif
  22.  
  23. #include <assert.h>
  24. #include <ctype.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys\stat.h>
  30. #include <time.h>
  31.  
  32. #define SIMTEL    1
  33. #define CICA    2
  34. #define ULOWEL    3
  35. #define CUG        4
  36.  
  37. #define OPUS        1
  38. #define RBBS        2
  39. #define PCB            3
  40. #define SPITFIRE    4
  41. #define WILDCAT        5
  42.  
  43. struct data {
  44.     char filename[15];
  45.     long filesize;
  46.     int year;
  47.     int month;
  48.     int day;
  49.     char description[900];
  50. };
  51.  
  52. int pastheader = 0;
  53. int in_type;
  54. int out_type;
  55. struct data line;
  56. char path[128];
  57. FILE *logfile = NULL;
  58.  
  59. void    myexit(int i);
  60. void    print_err(const char *fmt, ...);
  61.  
  62. int
  63. readline(void) {
  64.     static char buf[1000] = "";
  65.     char workbuf[1000];
  66.     static char datebuf[5] = "12";
  67.     char *r;
  68.     
  69.     if (buf[0] == 0)
  70.         if (NULL == gets(buf))
  71.             return(1);
  72.     
  73.     line.filename[0] = 0;
  74.     line.description[0] = 0;
  75.         
  76.     switch (in_type) {
  77.     case ULOWEL:
  78.         if (0 != strnicmp(buf, "file:", 5)) {
  79.             buf[0] = 0;
  80.             return(0);
  81.         }
  82.         
  83.         r = strtok(buf, "\r\n\t ");
  84.         if (! r) {
  85.             print_err("unknown line:\n%s\n", buf);
  86.             myexit(1);
  87.             buf[0] = 0;
  88.             return(0);
  89.         }
  90.         
  91.         r += strlen(r) + 1;        /* skip 'file:' */
  92.         while (isspace(*r))        /* skip white */
  93.             ++r;
  94.         
  95.         r = strtok(r, "\r\n\t ");
  96.         if (! r) {
  97.             print_err("unknown line:\n%s\n", buf);
  98.             myexit(1);
  99.             buf[0] = 0;
  100.             return(0);
  101.         }
  102.         
  103.         strcpy(line.filename, r);
  104.  
  105.         while (1) {
  106.             if (NULL == gets(buf))
  107.                 return(1);
  108.             if (buf[0] == '\r')
  109.                 continue;
  110.             if (! buf[0])
  111.                 continue;
  112.             if (isspace(buf[0])) {
  113.                 strcat(line.description, " ");
  114.                 strcat(line.description, &buf[1]);
  115.             } else
  116.                 return(0);
  117.         }
  118.         
  119.     case SIMTEL:
  120.     case CICA:
  121.  
  122.         /* wait for header */
  123.         if (! pastheader) {
  124.             if (in_type == CICA) {
  125.                 if (0 == strlen(buf))
  126.                     ++pastheader;
  127.             } else {
  128.                 if (strstr(buf, "================"))
  129.                     ++pastheader;
  130.             }
  131.             buf[0] = 0;
  132.             return(0);
  133.         }
  134.     
  135.         if (strlen(buf) == 0) {
  136.             print_err("zero length line\n");
  137.             buf[0] = 0;
  138.             return(0);
  139.         }
  140.  
  141.         if (strlen(buf) < 20) {
  142.             print_err("warning: line too short:\n%s\n", buf);
  143.         }
  144.     
  145.         strcpy(workbuf, buf);
  146.     
  147.         r = strtok(buf, "\r\n\t ");
  148.         if (! r) {
  149.             print_err("unknown line:\n%s\n", buf);
  150.             myexit(1);
  151.             buf[0] = 0;
  152.             return(0);
  153.         }
  154.     
  155.         strcpy(line.filename, r);
  156.     
  157.         r += strlen(r) + 1;        /* skip name */
  158.         while (isspace(*r))        /* skip white */
  159.             ++r;
  160.  
  161.         if (in_type == SIMTEL) {
  162.             ++r;                    /* skip file type */
  163.     
  164.             while (isspace(*r))        /* skip white */
  165.                 ++r;
  166.     
  167.             r = strtok(r, "\r\n\t ");
  168.             if (! r) {
  169.                 print_err("unknown line:\n%s\n", buf);
  170.                 myexit(1);
  171.                 line.filename[0] = 0;
  172.                 buf[0] = 0;
  173.                 return(0);
  174.             }
  175.             
  176.             line.filesize = atol(r);
  177.             r += strlen(r) + 1;
  178.     
  179.             while (isspace(*r))        /* skip white */
  180.                 ++r;
  181.         }
  182.     
  183.         datebuf[0] = *r;
  184.         ++r;
  185.         datebuf[1] = *r;
  186.         ++r;
  187.         line.year = atoi(datebuf);
  188.     
  189.         datebuf[0] = *r;
  190.         ++r;
  191.         datebuf[1] = *r;
  192.         ++r;
  193.         line.month = atoi(datebuf);
  194.         datebuf[0] = *r;
  195.         ++r;
  196.         datebuf[1] = *r;
  197.         ++r;
  198.         line.day = atoi(datebuf);
  199.     
  200.         while (isspace(*r))        /* skip white */
  201.             ++r;
  202.     
  203.         if (*r)    
  204.             strcpy(line.description, r);
  205.         buf[0] = 0;
  206.         return(0);
  207.         
  208.     case CUG:
  209.         if (strlen(buf) == 0) {
  210.             print_err("zero length line\n");
  211.             buf[0] = 0;
  212.             return(0);
  213.         }
  214.  
  215.         if (strlen(buf) < 10) {
  216.             print_err("warning: line too short:\n%s\n", buf);
  217.         }
  218.     
  219.         strcpy(workbuf, buf);
  220.     
  221.         r = strtok(buf, "\r\n\t ");
  222.         if (! r) {
  223.             print_err("unknown line:\n%s\n", buf);
  224.             buf[0] = 0;
  225.             return(0);
  226.         }
  227.     
  228.         strcpy(line.filename, r);
  229.     
  230.         r += strlen(r) + 1;        /* skip name */
  231.         while (isspace(*r))        /* skip white */
  232.             ++r;
  233.  
  234.         if (*r)    
  235.             strcpy(line.description, r);
  236.         buf[0] = 0;
  237.         return(0);
  238.         
  239.     default:
  240.         print_err("bad input type\n");
  241.         myexit(1);
  242.     }
  243.     
  244.     return(0);
  245. }
  246.  
  247. void
  248. opus_writeline(void) {
  249.     FILE *foo;
  250.     char buf[128];
  251.     struct stat stat_buf;
  252.     struct tm *tm;
  253.     time_t t;
  254.     char *p, *r;
  255.  
  256.     if (! line.filename[0])
  257.         return;
  258.  
  259. #if 0    
  260. 9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST modems.
  261. #endif
  262.  
  263.     if (in_type == CUG) {
  264.         strupr(line.filename);
  265.         strrev(line.filename);
  266.         if (0 == strncmp("RID", line.filename, 3)) {
  267.             line.filename[0] = 0;
  268.             return;
  269.         }
  270.         if (0 == strncmp("SBB", line.filename, 3)) {
  271.             line.filename[0] = 0;
  272.             return;
  273.         }
  274.         strrev(line.filename);
  275.     }
  276.     
  277.     if (out_type == OPUS) {
  278.         printf("%s %s\n", line.filename, line.description);
  279.         return;
  280.     }
  281.  
  282.     switch (in_type) {
  283.     case CICA:
  284.     case ULOWEL:
  285.     case CUG:
  286.         sprintf(buf, "%s\\%s", path, line.filename);
  287.         if (NULL == (foo = fopen(buf, "r"))) {
  288.             print_err("failed opening '%s'\n   %s\n", buf, line.description);
  289.             return;
  290.         }
  291.  
  292.         stat(buf, &stat_buf);
  293.  
  294.         fclose(foo);
  295.         
  296.         line.filesize = stat_buf.st_size;
  297.         break;
  298.         
  299.     default:
  300.         break;
  301.     }
  302.  
  303.     switch (in_type) {
  304.     case ULOWEL:
  305.     case CUG:
  306.         t = stat_buf.st_ctime;
  307.         tm = localtime(&t);
  308.         line.month = tm->tm_mon;
  309.         line.day = tm->tm_mday;
  310.         line.year = tm->tm_year;
  311.         break;
  312.         
  313.     default:
  314.         break;
  315.     }
  316.     
  317.     printf("%-13s%8ld  %02d-%02d-%02d ",
  318.         line.filename,
  319.         line.filesize,
  320.         line.month,
  321.         line.day,
  322.         line.year);
  323.     
  324.     /* write wrapping description */
  325.     r = line.description;
  326.     p = r + strlen(r) - 1;
  327.     while (isspace(*p)) {
  328.         *p = 0;
  329.         --p;
  330.     }
  331.         
  332.     r = line.description;
  333.     if (strlen(r) <= 45) {        
  334.         printf("%s\n", r);
  335.         return;
  336.     }
  337.  
  338.     p = r + 45;
  339.     while (! isspace(*p))
  340.         --p;
  341.     
  342.     *p = 0;
  343.     printf("%s\n  ", r);
  344.     r = p + 1;
  345.         
  346.     while (1) {
  347.         if (strlen(r) <= 76) {        
  348.             printf("%s\n", r);
  349.             return;
  350.         }
  351.         p = r + 76;
  352.         while (! isspace(*p))
  353.             --p;
  354.  
  355. #if 0
  356. 9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST
  357.                                | modems.
  358. #endif
  359.         *p = 0;
  360.         printf("%s\n  ", r);
  361.         r = p + 1;
  362.     }
  363. }
  364.  
  365. void
  366. pcb_writeline(void) {
  367.     char *r;
  368.     char *p;
  369.     FILE *foo;
  370.     char buf[128];
  371.     struct stat stat_buf;
  372.     struct tm *tm;
  373.     time_t t;
  374.  
  375.     if (! line.filename[0])
  376.         return;
  377.  
  378. #if 0    
  379. 9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST
  380.                                | modems.
  381. #endif
  382.  
  383.     if (in_type == CUG) {
  384.         strupr(line.filename);
  385.         strrev(line.filename);
  386.         if (0 == strncmp("RID", line.filename, 3)) {
  387.             line.filename[0] = 0;
  388.             return;
  389.         }
  390.         if (0 == strncmp("SBB", line.filename, 3)) {
  391.             line.filename[0] = 0;
  392.             return;
  393.         }
  394.         strrev(line.filename);
  395.     }
  396.  
  397.     switch (in_type) {
  398.     case CICA:
  399.     case ULOWEL:
  400.     case CUG:
  401.         sprintf(buf, "%s\\%s", path, line.filename);
  402.         if (NULL == (foo = fopen(buf, "r"))) {
  403.             print_err("failed opening '%s'\n   %s\n", buf, line.description);
  404.             return;
  405.         }
  406.  
  407.         stat(buf, &stat_buf);
  408.  
  409.         fclose(foo);
  410.         
  411.         line.filesize = stat_buf.st_size;
  412.         
  413.     default:
  414.         break;
  415.     }
  416.  
  417.     switch (in_type) {
  418.     case ULOWEL:
  419.     case CUG:
  420.         t = stat_buf.st_ctime;
  421.         tm = localtime(&t);
  422.         line.month = tm->tm_mon;
  423.         line.day = tm->tm_mday;
  424.         line.year = tm->tm_year;
  425.         
  426.     default:
  427.         break;
  428.     }
  429.     
  430.     printf("%-13s%8ld  %02d-%02d-%02d  ",
  431.         line.filename,
  432.         line.filesize,
  433.         line.month,
  434.         line.day,
  435.         line.year);
  436.     
  437.     /* write wrapping description */
  438.  
  439.     r = line.description;
  440.     p = r + strlen(r) - 1;
  441.     while (isspace(*p)) {
  442.         *p = 0;
  443.         --p;
  444.     }
  445.         
  446.     while (1) {
  447.         if (strlen(r) <= 45) {        
  448.             printf("%s\n", r);
  449.             return;
  450.         }
  451.  
  452.         p = r + 45;
  453.         while (! isspace(*p))
  454.             --p;
  455.         
  456. #if 0    
  457. 9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST
  458.                                | modems.
  459. #endif
  460.         *p = 0;
  461.         printf("%s\n%31s| ", r, " ");
  462.         r = p + 1;
  463.     }
  464. }
  465.  
  466. void
  467. spitfire_writeline(void) {
  468.     char *r;
  469.     char *p;
  470.     FILE *foo;
  471.     char buf[128];
  472.     struct stat stat_buf;
  473.     struct tm *tm;
  474.     time_t t;
  475.     int x;
  476.     char buf2[20];
  477.  
  478.     if (! line.filename[0])
  479.         return;
  480.  
  481.     if (in_type == CUG) {
  482.         strupr(line.filename);
  483.         strrev(line.filename);
  484.         if (0 == strncmp("RID", line.filename, 3)) {
  485.             line.filename[0] = 0;
  486.             return;
  487.         }
  488.         if (0 == strncmp("SBB", line.filename, 3)) {
  489.             line.filename[0] = 0;
  490.             return;
  491.         }
  492.         strrev(line.filename);
  493.     }
  494.  
  495.     switch (in_type) {
  496.     case CICA:
  497.     case ULOWEL:
  498.     case CUG:
  499.         sprintf(buf, "%s\\%s", path, line.filename);
  500.         if (NULL == (foo = fopen(buf, "r"))) {
  501.             print_err("failed opening '%s'\n   %s\n", buf,
  502.                 line.description);
  503.             myexit(1);
  504.         }
  505.  
  506.         stat(buf, &stat_buf);
  507.  
  508.         fclose(foo);
  509.         
  510.         line.filesize = stat_buf.st_size;
  511.         break;
  512.         
  513.     default:
  514.         break;
  515.     }
  516.  
  517.     switch (in_type) {
  518.     case ULOWEL:
  519.     case CUG:
  520.         t = stat_buf.st_ctime;
  521.         tm = localtime(&t);
  522.         line.month = tm->tm_mon;
  523.         line.day = tm->tm_mday;
  524.         line.year = tm->tm_year;
  525.         break;
  526.         
  527.     default:
  528.         break;
  529.     }
  530.     
  531.     /* description -- wack off at 40 chars.*/
  532.     line.description[41] = 0;
  533.     
  534.     printf("%-12s",    line.filename);
  535.     
  536.     sprintf(buf, "%ld", line.filesize);
  537.     strrev(buf);
  538.     
  539.     p = buf;
  540.     r = buf2;
  541.     x = 0;
  542.     while (*p) {
  543.         if (x == 3) {
  544.             *r++ = ',';
  545.             x = 0;
  546.         }
  547.         ++x;
  548.         *r++ = *p++;
  549.     }
  550.     *r = 0;
  551.  
  552.     strrev(buf2);
  553.     printf("%9s ", buf2);
  554.  
  555.     printf(" %02d-%02d-%02d  %s\n",
  556.         line.month,
  557.         line.day,
  558.         line.year,
  559.         line.description);
  560. }
  561.  
  562.  
  563. void
  564. rbbs_writeline(void) {
  565.     FILE *foo;
  566.     char buf[128];
  567.     struct stat stat_buf;
  568.     struct tm *tm;
  569.     time_t t;
  570.     char *p, *r;
  571.  
  572.     if (! line.filename[0])
  573.         return;
  574.  
  575. #if 0    
  576. 9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST modems.
  577. #endif
  578.  
  579.     if (in_type == CUG) {
  580.         strupr(line.filename);
  581.         strrev(line.filename);
  582.         if (0 == strncmp("RID", line.filename, 3)) {
  583.             line.filename[0] = 0;
  584.             return;
  585.         }
  586.         if (0 == strncmp("SBB", line.filename, 3)) {
  587.             line.filename[0] = 0;
  588.             return;
  589.         }
  590.         strrev(line.filename);
  591.     }
  592.     
  593.     if (out_type == OPUS) {
  594.         printf("%s %s\n", line.filename, line.description);
  595.         return;
  596.     }
  597.  
  598.     switch (in_type) {
  599.     case CICA:
  600.     case ULOWEL:
  601.     case CUG:
  602.         sprintf(buf, "%s\\%s", path, line.filename);
  603.         if (NULL == (foo = fopen(buf, "r"))) {
  604.             print_err("failed opening '%s'\n   %s\n", buf, line.description);
  605.             return;
  606.         }
  607.  
  608.         stat(buf, &stat_buf);
  609.  
  610.         fclose(foo);
  611.         
  612.         line.filesize = stat_buf.st_size;
  613.         break;
  614.         
  615.     default:
  616.         break;
  617.     }
  618.  
  619.     switch (in_type) {
  620.     case ULOWEL:
  621.     case CUG:
  622.         t = stat_buf.st_ctime;
  623.         tm = localtime(&t);
  624.         line.month = tm->tm_mon;
  625.         line.day = tm->tm_mday;
  626.         line.year = tm->tm_year;
  627.         break;
  628.         
  629.     default:
  630.         break;
  631.     }
  632.     
  633.     printf("%-13s%8ld  %02d-%02d-%02d ",
  634.         line.filename,
  635.         line.filesize,
  636.         line.month,
  637.         line.day,
  638.         line.year);
  639.     
  640.     /* write wrapping description */
  641.     r = line.description;
  642.     p = r + strlen(r) - 1;
  643.     while (isspace(*p)) {
  644.         *p = 0;
  645.         --p;
  646.     }
  647.         
  648.     r = line.description;
  649.     if (strlen(r) <= 45) {        
  650.         printf("%s\n", r);
  651.         return;
  652.     }
  653.  
  654.     p = r + 45;
  655.     while (! isspace(*p))
  656.         --p;
  657.     
  658.     *p = 0;
  659.     printf("%s\n  ", r);
  660.     r = p + 1;
  661.         
  662.     while (1) {
  663.         if (strlen(r) <= 76) {        
  664.             printf("%s\n", r);
  665.             return;
  666.         }
  667.         p = r + 76;
  668.         while (! isspace(*p))
  669.             --p;
  670.  
  671. #if 0
  672. 9600TEXT.ZIP    16576  09-26-91  A pair of US Robotics text files about HST
  673.                                | modems.
  674. #endif
  675.         *p = 0;
  676.         printf("%s\n  ", r);
  677.         r = p + 1;
  678.     }
  679. }
  680.  
  681. void
  682. help(void) {
  683.     
  684.     print_err("usage: convert <in_type> <out_type> [directory path] < in > out\n");
  685.     print_err("       <in_type>   := cug; simtel; cica; ulowel\n");
  686.     print_err("       <out_type>  := opus, rbbs, spitfire, pcb\n");
  687.     print_err("       if <in_type> == cug, cica, ulowel\n");
  688.     print_err("       and <out_type> == rbbs, spitfire, pcb\n");
  689.     print_err("           [directory path] is required.\n");
  690.     
  691.     myexit(1);
  692. }
  693.  
  694. void _Cdecl
  695. main(int argc, char *argv[]) {
  696.     int rv;
  697.  
  698.     if (argc < 3)
  699.         help();
  700.  
  701.     if (0 == stricmp("simtel", argv[1])) {
  702.         in_type = SIMTEL;
  703.     } else if (0 == stricmp("cica", argv[1])) {
  704.         in_type = CICA;
  705.     } else if (0 == stricmp("cug", argv[1])) {
  706.         in_type = CUG;
  707.     } else if (0 == stricmp("ulowel", argv[1])) {
  708.         in_type = ULOWEL;
  709.     } else
  710.         help();
  711.  
  712.     if (0 == stricmp("opus", argv[2])) {
  713.         out_type = OPUS;
  714.     } else if (0 == stricmp("pcb", argv[2])) {
  715.         out_type = PCB;
  716.     } else if (0 == stricmp("rbbs", argv[2])) {
  717.         out_type = RBBS;
  718.     } else if (0 == stricmp("spitfire", argv[2])) {
  719.         out_type = SPITFIRE;
  720.     } else
  721.         help();
  722.  
  723.     switch (in_type) {
  724.         case ULOWEL:
  725.         case CICA:
  726.         case CUG:
  727.             switch (out_type) {
  728.             case OPUS:
  729.                 break;
  730.             case PCB:
  731.             case RBBS:
  732.             case SPITFIRE:
  733.                 if (argc != 4)
  734.                     help();
  735.                 strcpy(path, argv[3]);
  736.                 break;
  737.             default:
  738.                 fprintf(stderr, "bad switch\n");
  739.                 exit(1);
  740.             }
  741.             break;
  742.         case SIMTEL:
  743.         default:
  744.             break;
  745.     }
  746.     
  747.     if (NULL == (logfile = fopen("logfile", "at"))) {
  748.         print_err("erroring opening logfile\n");
  749.         myexit(1);
  750.     }
  751.             
  752.     while (1) {
  753.         rv = readline();
  754.  
  755.         switch (out_type) {
  756.         case OPUS:
  757.             opus_writeline();
  758.             break;
  759.         case PCB:
  760.             pcb_writeline();
  761.             break;
  762.         case RBBS:
  763.             rbbs_writeline();
  764.             break;
  765.         case SPITFIRE:
  766.             spitfire_writeline();
  767.             break;
  768.         default:
  769.             assert(0);
  770.             break;
  771.         }
  772.         if (rv)
  773.             myexit(0);
  774.     }
  775. }
  776.  
  777. void
  778. myexit(int i) {
  779.     
  780.     fclose(logfile);
  781.     
  782.     exit(i);
  783. }
  784.  
  785. void
  786. print_err(const char *fmt, ...) {
  787.     va_list args;
  788.     char str[0x100];
  789.  
  790.     va_start(args, fmt);
  791.     vsprintf(str, fmt, args);
  792.     va_end(args);
  793.  
  794.     if (logfile)
  795.         fprintf(logfile, "%s", str);
  796.     fprintf(stderr, "%s", str);
  797. }