home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / vsoup128.zip / convsoup.cc < prev    next >
C/C++ Source or Header  |  1997-02-06  |  5KB  |  204 lines

  1. //  $Id: convsoup.cc 1.5 1997/02/06 09:49:47 hardy Exp $
  2. //
  3. //  This progam/module was written by Hardy Griech based on ideas and
  4. //  pieces of code from Chin Huang (cthuang@io.org).  Bug reports should
  5. //  be submitted to rgriech@ibm.net.
  6. //
  7. //  This file is part of soup++ for OS/2.  Soup++ including this file
  8. //  is freeware.  There is no warranty of any kind implied.  The terms
  9. //  of the GNU Gernal Public Licence are valid for this piece of software.
  10. //
  11. //
  12. //  Small programm to convert SOUP formats from "bn"/"Bn" to "mn"/"un"
  13. //
  14. //  The program must be started in the directory contining the areas file
  15. //  and the corresponding msg files
  16. //
  17.  
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23.  
  24.  
  25. #define AREAS      "AREAS"
  26. #define AREASTMP   "AREAS.TMP"
  27. #define MSGTMP     "msg.tmp"
  28. #define MSGPOSTFIX ".MSG"
  29.  
  30.  
  31.  
  32. const char *progname;
  33.  
  34.  
  35.  
  36. static void openMsgs( const char *prefix, FILE **msg, FILE **msgTmp )
  37. {
  38.     char msgName[200];
  39.     
  40.     sprintf( msgName,"%s%s",prefix,MSGPOSTFIX );
  41.     *msg = fopen( msgName,"rb" );
  42.     if (*msg == NULL)
  43.     fprintf( stderr,"%s: cannot open %s\n", progname,msgName );
  44.  
  45.     *msgTmp = fopen( MSGTMP,"wb" );
  46.     if (*msgTmp == NULL)
  47.     fprintf( stderr,"%s: cannot create %s\n", progname,MSGTMP );
  48. }   // openMsgs
  49.  
  50.  
  51.  
  52. static void closeMsgs( const char *prefix, FILE *msg, FILE *msgTmp )
  53. {
  54.     if (msg != NULL  &&  msgTmp != NULL) {
  55.     char msgName[200];
  56.  
  57.     sprintf( msgName,"%s%s",prefix,MSGPOSTFIX );
  58.     fclose( msg );
  59.     if (remove(msgName) == -1) {
  60.         fprintf( stderr,"%s:  cannot delete %s\n",progname,msgName );
  61.         exit( 3 );
  62.     }
  63.     fclose( msgTmp );
  64.     rename( MSGTMP, msgName );
  65.     }
  66.     else {
  67.     if (msg)
  68.         fclose( msg );
  69.     if (msgTmp) {
  70.         fclose( msgTmp );
  71.         remove( MSGTMP );
  72.     }
  73.     }
  74. }   // closeMsgs
  75.  
  76.  
  77.  
  78. int main( int, char *argv[] )
  79. {
  80.     FILE *areas, *areasTmp, *msg, *msgTmp;
  81.     char buf[200];
  82.     int res;
  83.     char prefix[100],areaname[100],encoding[100],description[100];
  84.  
  85.     progname = strrchr(argv[0], '\\');
  86.     if (progname == NULL)
  87.     progname = argv[0];
  88.     else
  89.     ++progname;
  90.  
  91.     fprintf( stderr,"%s - v0.02 rg230197\n", progname );
  92.     fprintf( stderr,"%s: convert binary SOUP (b/B) to mailbox (m) and USENET (u) format\n", progname );
  93.  
  94.     areas = fopen( AREAS,"rb" );
  95.     if (areas == NULL) {
  96.     fprintf( stderr,"%s: %s does not exist\n", progname,AREAS );
  97.     exit( 3 );
  98.     }
  99.  
  100.     areasTmp = fopen( AREASTMP,"wb" );
  101.     if (areasTmp == NULL) {
  102.     fprintf( stderr,"%s: cannot create %s\n", progname,AREASTMP );
  103.     exit( 3 );
  104.     }
  105.  
  106.     while (fgets(buf,sizeof(buf),areas) != NULL) {
  107.     *description = '\0';
  108.     res = sscanf( buf,"%s%s%s%[^\n]",prefix,areaname,encoding,description );
  109.     if (res < 3) {
  110.         fprintf( stderr,"%s: ill line in areas: %s", progname,buf );
  111.         fputs( buf,areasTmp );
  112.     }
  113.     else {
  114.         fprintf( stderr,"%s: %8s%s %3s: ",progname,prefix,MSGPOSTFIX,encoding );
  115.         
  116.         if (*encoding == 'b') {
  117.         //
  118.         //  8-bit binary mail to UNIX mailbox
  119.         //
  120.         fprintf( stderr,"8-bit binary mail to UNIX mailbox\n" );
  121.         openMsgs( prefix, &msg, &msgTmp );
  122.         if (msg != NULL  &&  msgTmp != NULL) {
  123.             for (;;) {
  124.             unsigned char lenbuf[4];
  125.             size_t artSize;
  126.             
  127.             if (fread( lenbuf,sizeof(lenbuf),1,msg ) != 1)
  128.                 break;
  129.             artSize = (lenbuf[0] << 24) +
  130.                 (lenbuf[1] << 16) +
  131.                 (lenbuf[2] <<  8) +
  132.                 (lenbuf[3] <<  0);
  133.             fprintf( msgTmp,"From ConvSoup Wed Oct 23 09:15 GMT 1996\n" );   // dummy
  134.             while (artSize > 0) {
  135.                 char line[BUFSIZ];
  136.                 
  137.                 if (fgets( line, (artSize < sizeof(line)) ? artSize+1 : sizeof(line),msg ) == NULL) {
  138.                 perror( "gets()" );
  139.                 exit( 3 );
  140.                 }
  141.                 artSize -= strlen( line );
  142.                 if (strncmp(line,"From ",5) == 0)
  143.                 fputc( '>',msgTmp );
  144.                 fputs( line,msgTmp );
  145.             }
  146.             }
  147.             *encoding = 'm';
  148.         }
  149.         closeMsgs( prefix, msg, msgTmp );
  150.         }
  151.         else if (*encoding == 'B') {
  152.         //
  153.         //  8-bit binary news to UNIX mailbox
  154.         //
  155.         fprintf( stderr,"8-bit binary news to USENET news\n" );
  156.         openMsgs( prefix, &msg, &msgTmp );
  157.         if (msg != NULL  &&  msgTmp != NULL) {
  158.             for (;;) {
  159.             unsigned char lenbuf[4];
  160.             size_t artSize;
  161.             
  162.             if (fread( lenbuf,sizeof(lenbuf),1,msg ) != 1)
  163.                 break;
  164.             artSize = (lenbuf[0] << 24) +
  165.                 (lenbuf[1] << 16) +
  166.                 (lenbuf[2] <<  8) +
  167.                 (lenbuf[3] <<  0);
  168.             fprintf( msgTmp,"#! rnews %lu\n", artSize );
  169.             while (artSize > 0) {
  170.                 char buffer[4096];
  171.                 size_t rd;
  172.  
  173.                 rd = fread( buffer,1,(artSize < sizeof(buffer)) ? artSize : sizeof(buffer),msg );
  174.                 if (rd == 0) {
  175.                 perror( "fread()" );
  176.                 exit( 3 );
  177.                 }
  178.                 artSize -= rd;
  179.                 fwrite( buffer,1,rd, msgTmp );
  180.             }
  181.             }
  182.             *encoding = 'u';
  183.         }
  184.         closeMsgs( prefix, msg, msgTmp );
  185.         }
  186.         else
  187.         fprintf( stderr,"skipped\n" );
  188.         
  189.         fprintf( areasTmp,"%s\t%s\t%s", prefix,areaname,encoding );
  190.         if (*description)
  191.         fprintf( areasTmp,"\t%s",description );
  192.         fprintf( areasTmp,"\n" );
  193.     }
  194.     }
  195.  
  196.     fclose( areas );
  197.     fclose( areasTmp );
  198.     if (remove(AREAS) == -1) {
  199.     fprintf( stderr,"%s:  cannot delete %s\n",progname,AREAS );
  200.     exit( 3 );
  201.     }
  202.     rename( AREASTMP, AREAS );
  203. }   // main
  204.