home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / programming / asm_programming / MJRDEVEL.ARC / MSGUTL.C < prev    next >
Text File  |  1989-03-01  |  7KB  |  210 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *   MSGUTL.C                                                              *
  4.  *                                                                         *
  5.  *   Copyright (C) 1987-1989 GALACTICOMM, Inc.    All Rights Reserved.     *
  6.  *                                                                         *
  7.  *   This file contains a library of routines for dealing with .MCV files  *
  8.  *   (output from MSGIDX).  These routines allow the converted messages    *
  9.  *   to be retrieved by message number, which will generally be done thru  *
  10.  *   the symbolic names "#defined" in MSGIDX's output header file.         *
  11.  *                                                                         *
  12.  *   There are also some utility routines for extracting options or        *
  13.  *   parameters from these messages files, of type:  numeric, yes/no,      *
  14.  *   and keyword identified.                                               *
  15.  *                                                                         *
  16.  *                                            - T. Stryker 6/10/86         *
  17.  *                                            - R. Stein 1/31/88           *
  18.  *                                                                         *
  19.  ***************************************************************************/
  20.  
  21. #include "stdio.h"
  22. #include "ctype.h"
  23. #include "portable.h"
  24.  
  25. #define MBFSIZ 2001                /* maximum size of each msg (incl '\0') */
  26.  
  27. struct msgblk {                    /* named-message file block             */
  28.      FILE *msgfp;                  /*   fopen()-style file block pointer   */
  29.      int msgcnt;                   /*   count of messages in this file     */
  30.      long *msgloc;                 /*   dynam-alloc'd fseek() msg offsets  */
  31.      char *filnam;                 /*   message file name for error reports*/
  32. };
  33.  
  34. static char *msgbuf=NULL;          /* message text buffer                  */
  35. struct msgblk *curmbk=NULL;        /* set by setmbk()                      */
  36. int lstmsg=-1;
  37.  
  38. struct msgblk *opnmsg(mcvfil)      /* open a message file                  */
  39. char *mcvfil;
  40. {
  41.      char *alcmem();
  42.  
  43.      curmbk=(struct msgblk *)alcmem(sizeof(struct msgblk));
  44.      if ((curmbk->msgfp=fopen(mcvfil,FOPRB)) == NULL) {
  45.           catastro("INIMSG: CAN'T OPEN \"%s\" FOR INPUT",mcvfil);
  46.      }
  47.      if (fread(&(curmbk->msgcnt),sizeof(int),1,curmbk->msgfp) != 1) {
  48.           catastro("INIMSG: CAN'T READ COUNT IN \"%s\"",mcvfil);
  49.      }
  50.      curmbk->msgloc=(long *)alcmem(curmbk->msgcnt*sizeof(long));
  51.      if (fread(curmbk->msgloc,sizeof(long),curmbk->msgcnt,curmbk->msgfp) != curmbk->msgcnt) {
  52.           catastro("INIMSG: CAN'T READ POINTERS IN \"%s\"",mcvfil);
  53.      }
  54.      curmbk->msgcnt-=1;
  55.      curmbk->filnam=alcmem(strlen(mcvfil)+1);
  56.      strcpy(curmbk->filnam,mcvfil);
  57.      lstmsg=-1;
  58.      return(curmbk);
  59. }
  60.  
  61. setmbk(mb)                         /* set message block                    */
  62. struct msgblk *mb;
  63. {
  64.      curmbk=mb;
  65.      lstmsg=-1;
  66. }
  67.  
  68. char *getmsg(msgnum)               /* get a message (by number)            */
  69. int msgnum;
  70. {
  71.      char *alcmem();
  72.      long msgbgn;
  73.  
  74.      if (msgbuf == NULL) {
  75.           msgbuf=alcmem(MBFSIZ);
  76.      }
  77.      if (msgnum < 0 || msgnum >= curmbk->msgcnt) {
  78.           catastro("GETMSG: MSG NO. %d OUT OF RANGE IN %s",msgnum,curmbk->filnam);
  79.      }
  80.      msgbgn=curmbk->msgloc[msgnum];
  81.      if (fseek(curmbk->msgfp,msgbgn,0) != 0) {
  82.           catastro("GETMSG: ERROR SEEKING MSG NO. %d IN %s",msgnum,curmbk->filnam);
  83.      }
  84.      if (fread(msgbuf,(int)(curmbk->msgloc[msgnum+1]-msgbgn),1,curmbk->msgfp) != 1) {
  85.           catastro("GETMSG: ERROR READING MSG NO. %d IN %s",msgnum,curmbk->filnam);
  86.      }
  87.      lstmsg=msgnum;
  88.      return(msgbuf);
  89. }
  90.  
  91. char *catfix2()                    /* catfix2() for catamsg()              */
  92. {
  93.      static char mfbuff[30]={""};
  94.  
  95.      if (curmbk != NULL && curmbk->filnam != NULL) {
  96.           sprintf(mfbuff,"MSG:%s/%d",curmbk->filnam,lstmsg);
  97.      }
  98.      return(mfbuff);
  99. }
  100.  
  101. clsmsg(mb)                         /* close message file                   */
  102. struct msgblk *mb;
  103. {
  104.      if (mb != NULL && mb->filnam != NULL) {
  105.           curmbk=mb;
  106.           fclose(mb->msgfp);
  107.           free(mb->msgloc);
  108.           free(mb->filnam);
  109.           mb->filnam=NULL;
  110.           free(mb);
  111.           if (msgbuf != NULL) {
  112.                free(msgbuf);
  113.                msgbuf=NULL;
  114.           }
  115.      }
  116. }
  117.  
  118. sameas(stg1,stg2)             /* Are strings identical? (ignoring U/L case) */
  119. char *stg1,*stg2;
  120. {
  121.      while (*stg1 != '\0') {
  122.           if (tolower(*stg1) != tolower(*stg2)) {
  123.                return(0);
  124.           }
  125.           stg1+=1;
  126.           stg2+=1;
  127.      }
  128.      return(*stg2 == '\0');
  129. }
  130.  
  131. char *lastwd(string)                          /* Find last word of a string */
  132. char *string;               /* (does not remove trailing blanks after word) */
  133. {
  134.      char *cp;
  135.  
  136.      for (cp=string+strlen(string)-1 ; cp >= string && isspace(*cp) ; cp--) {
  137.      }
  138.      for ( ; cp >= string && !isspace(*cp) ; cp--) {
  139.      }
  140.      return(cp+1);
  141. }
  142.  
  143. long lngopt(msgnum,floor,ceiling)       /* Get numeric option from msg file */
  144. int msgnum;
  145. long floor,ceiling;                          /* specified limits for number */
  146. {
  147.      long ln;
  148.  
  149.      if (sscanf(lastwd(getmsg(msgnum)),"%ld",&ln)) {
  150.           if (ln >= floor && ln <= ceiling) {
  151.                return(ln);
  152.           }
  153.           catastro("NUMERIC OPTION %d IN %s OUT OF RANGE",msgnum,curmbk->filnam);
  154.      }
  155.      catastro("BAD FORMAT FOR NUMERIC OPTION %d IN %s",msgnum,curmbk->filnam);
  156. }
  157.  
  158. unsigned hexopt(msgnum,floor,ceiling)/* Get hexadecimal option from msg file */
  159. int msgnum;
  160. unsigned floor,ceiling;                      /* specified limits for number */
  161. {
  162.      long ln;
  163.  
  164.      if (sscanf(lastwd(getmsg(msgnum)),"%lx",&ln)) {
  165.           if (ln >= floor && ln <= ceiling) {
  166.                return((unsigned)ln);
  167.           }
  168.           catastro("HEX OPTION %d IN %s OUT OF RANGE",msgnum,curmbk->filnam);
  169.      }
  170.      catastro("BAD FORMAT FOR HEX OPTION %d IN %s",msgnum,curmbk->filnam);
  171. }
  172.  
  173. int numopt(msgnum,floor,ceiling)        /* Get numeric option from msg file */
  174. int msgnum;
  175. int floor,ceiling;                           /* specified limits for number */
  176. {
  177.      return((int)lngopt(msgnum,floor+0L,ceiling+0L));
  178. }
  179.  
  180. int ynopt(msgnum)                     /* Get yes/no option from msg file */
  181. int msgnum;
  182. {
  183.      int rc=0;
  184.  
  185.      switch (toupper(*lastwd(getmsg(msgnum)))) {
  186.      case 'Y':
  187.           rc=1;
  188.      case 'N':
  189.           return(rc);
  190.      }
  191.      catastro("BAD FORMAT FOR YES/NO OPTION %d IN %s",msgnum,curmbk->filnam);
  192. }
  193.  
  194. chropt(msgnum)                /* Get single-character option from msg file */
  195. int msgnum;
  196. {
  197.      return(*lastwd(getmsg(msgnum)));
  198. }
  199.  
  200. char *stgopt(msgnum)               /* get a string from a message file     */
  201. int msgnum;
  202. {
  203.      char *cp,*mp,*alcmem();
  204.  
  205.      cp=alcmem(strlen(mp=getmsg(msgnum))+1);
  206.      strcpy(cp,mp);
  207.      return(cp);
  208. }
  209.  
  210.