home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / bbs / sftool / to_sf.c < prev   
Encoding:
C/C++ Source or Header  |  1992-09-15  |  5.1 KB  |  306 lines

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