home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / msgapi38.zip / SAMPLE / SQCONVER.C
C/C++ Source or Header  |  1994-03-19  |  3KB  |  131 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "prog.h"
  5. #include "alc.h"
  6. #include "msgapi.h"
  7.  
  8.  
  9. #define BUFSIZE 4096
  10.  
  11. int _stdc main(int argc,char *argv[])
  12. {
  13.   XMSG msg;
  14.   MSG *in_area, *out_area;
  15.   MSGH *in_msg, *out_msg;
  16.   char *ctrl, *buffer;
  17.   dword offset, msgn;
  18.   long got;
  19.   int t1, t2;
  20.   int ctrllen;
  21.   struct _minf mi;
  22.  
  23.   if (argc < 6)
  24.   {
  25.     printf("format:  SQCONVERT <from_name> <from_type>  <to_name> <to_type> <default_zone>\n");
  26.     printf("example: SQCONVERT \\msg\\foo     *.msg     \\msg\\foosq  squish       1\n");
  27.     exit(1);
  28.   }
  29.  
  30.   printf("Converting area %s...\n",argv[1]);
  31.  
  32.   if (eqstri(argv[2],"*.msg"))
  33.     t1=MSGTYPE_SDM;
  34.   else if (eqstri(argv[2],"squish"))
  35.     t1=MSGTYPE_SQUISH;
  36.   else t1=atoi(argv[2]);
  37.  
  38.   mi.def_zone=atoi(argv[5]);
  39.  
  40.   MsgOpenApi(&mi);
  41.  
  42.   if ((in_area=MsgOpenArea(argv[1], MSGAREA_NORMAL, t1))==NULL)
  43.   {
  44.     printf("Error opening area `%s' (type %d) for read!\n",
  45.            argv[1], t1);
  46.     exit(1);
  47.   }
  48.  
  49.   MsgLock(in_area);
  50.  
  51.   if (eqstri(argv[4],"*.msg"))
  52.     t2=MSGTYPE_SDM;
  53.   else if (eqstri(argv[4],"squish"))
  54.     t2=MSGTYPE_SQUISH;
  55.   else t2=atoi(argv[4]);
  56.  
  57.   if ((out_area=MsgOpenArea(argv[3], MSGAREA_CRIFNEC, t2))==NULL)
  58.   {
  59.     printf("Error opening area `%s' (type %d) for write!\n",
  60.            argv[3], t2);
  61.     exit(1);
  62.   }
  63.  
  64.   MsgLock(out_area);
  65.  
  66.   if ((buffer=malloc(BUFSIZE))==NULL)
  67.   {
  68.     printf("Error!  Ran out of memory...\n");
  69.     exit(1);
  70.   }
  71.  
  72.   for (msgn=1L; msgn <= MsgHighMsg(in_area); msgn++)
  73.   {
  74.     if ((msgn % 5)==0)
  75.       printf("Msg: %ld\r",msgn);
  76.  
  77.     if ((in_msg=MsgOpenMsg(in_area,MOPEN_READ,msgn))==NULL)
  78.       continue;
  79.  
  80.     if ((out_msg=MsgOpenMsg(out_area, MOPEN_CREATE, 0L))==NULL)
  81.     {
  82.       printf("Error writing to output area; msg#%ld (%d).\n", msgn, msgapierr);
  83.       MsgCloseMsg(in_msg);
  84.       continue;
  85.     }
  86.  
  87.     ctrllen=(int)MsgGetCtrlLen(in_msg);
  88.  
  89.     if ((ctrl=malloc(ctrllen))==NULL)
  90.       ctrllen=0;
  91.  
  92.     MsgReadMsg(in_msg, &msg, 0L, 0L, NULL, ctrllen, ctrl);
  93.  
  94.     msg.attr |= MSGSCANNED;
  95.  
  96.     msg.replyto=0L;
  97.     memset(msg.replies, '\0', sizeof(msg.replies));
  98.  
  99.     MsgWriteMsg(out_msg, FALSE, &msg, NULL, 0L,
  100.                 MsgGetTextLen(in_msg), ctrllen, ctrl);
  101.  
  102.     for (offset=0L; offset < MsgGetTextLen(in_msg);)
  103.     {
  104.       got=MsgReadMsg(in_msg, NULL, offset, BUFSIZE, buffer, 0L, NULL);
  105.  
  106.       if (got <= 0)
  107.         break;
  108.  
  109.       MsgWriteMsg(out_msg, TRUE, NULL, buffer, got,
  110.                   MsgGetTextLen(in_msg), 0L, NULL);
  111.  
  112.       offset += got;
  113.     }
  114.  
  115.     if (ctrl)
  116.       free(ctrl);
  117.  
  118.     MsgCloseMsg(out_msg);
  119.     MsgCloseMsg(in_msg);
  120.   }
  121.  
  122.   free(buffer);
  123.   MsgCloseArea(out_area);
  124.   MsgCloseArea(in_area);
  125.   MsgCloseApi();
  126.  
  127.   printf("\nDone!\n");
  128.   return 0;
  129. }
  130.  
  131.