home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / sqdev200.zip / samples / sqconv.c < prev    next >
C/C++ Source or Header  |  1994-05-23  |  3KB  |  139 lines

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