home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / bcfamily / source / message.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-12  |  4.4 KB  |  148 lines

  1. //
  2. //      *******************************************************************
  3. //        JdeBP C++ Library Routines          General Public Licence v1.00
  4. //            Copyright (c) 1991,1992     Jonathan de Boyne Pollard
  5. //      *******************************************************************
  6. //
  7. // Part of FamAPI.LIB
  8. //
  9.  
  10. #include "famapi.h"
  11.  
  12. //
  13. //    This structure must _not_ be word-aligned
  14. //
  15. struct MsgSeg {
  16.     UCHAR ffbyte ;            // Byte, value 0xFF
  17.     UCHAR MkMsg[5] ;        // The letters "MKMSG"
  18.     UCHAR Unknwn1[2] ;        // Letter plus NUL
  19.     UCHAR Prefix[3] ;        // Three letter prefix for message number
  20.     USHORT NMessages ;        // Total number of messages
  21.     USHORT Unknwn2 ;        // Unknown
  22.     UCHAR Flag ;            // 0 = ULONG pointers, 1 = USHORT pointers
  23.     USHORT PtrTable ;        // 0 = No table offsets, 2 = Table offsets follow
  24.     USHORT FirstPtr ;        // Offset of start of pointer table
  25.     USHORT LastPtr ;        // Offset ot end of pointer table
  26.     ULONG FileLen ;            // Total length of file
  27.     UCHAR Unknwn4[5] ;        // Unknown
  28. } ;
  29.  
  30. //
  31. //    Retrive a message from a message file, expanding substitutions
  32. //
  33. USHORT _APICALL
  34. DosGetMessage    ( char far * far *PtrVTable,
  35.                   USHORT VCount,
  36.                   char far *PtrBuffer,
  37.                   USHORT BufferLength,
  38.                   USHORT MsgNumber,
  39.                   char far *PtrFileName,
  40.                   USHORT far *PtrMsgLength )
  41. {
  42.     USHORT Fh, NBytes, MsgLength = 0 ;
  43.     UINT Action ;
  44.     USHORT err = DosOpen(PtrFileName, &Fh, &Action, 0UL, 0L, 0x0001, 0x0040, 0UL) ;
  45.  
  46.     if (err) return err ;
  47.  
  48.     struct MsgSeg MsgSeg ;
  49.  
  50.     err = DosRead(Fh, (void far *)&MsgSeg, sizeof(MsgSeg), &NBytes) ;
  51.     if (!err) {
  52.         if (MsgNumber < MsgSeg.NMessages) {
  53.             ULONG PtrOffset, StartOffset, EndOffset ;
  54.             if (MsgSeg.PtrTable == 0x0002) {
  55.                 PtrOffset = MsgSeg.FirstPtr ;
  56.                 EndOffset = MsgSeg.FileLen ;
  57.             } else {
  58.                 PtrOffset = sizeof(MsgSeg) ;
  59.                 DosChgFilePtr(Fh, 0, 2, (LONG far *)&EndOffset) ;
  60.             }
  61.  
  62.             if (MsgSeg.Flag == 1) {
  63.                 PtrOffset += MsgNumber * 2 ;
  64.                 DosChgFilePtr(Fh, PtrOffset, 0, (LONG far *)&PtrOffset) ;
  65.                 DosRead(Fh, (void far *)&StartOffset, 2, &NBytes) ;
  66.                 if ((MsgNumber + 1) < MsgSeg.NMessages)
  67.                     DosRead(Fh, (void far *)&StartOffset, 2, &NBytes) ;
  68.             } else {
  69.                 PtrOffset += MsgNumber * 4 ;
  70.                 DosChgFilePtr(Fh, PtrOffset, 0, (LONG far *)&PtrOffset) ;
  71.                 DosRead(Fh, (void far *)&StartOffset, 4, &NBytes) ;
  72.                 if ((MsgNumber + 1) < MsgSeg.NMessages)
  73.                     DosRead(Fh, (void far *)&EndOffset, 4, &NBytes) ;
  74.             }
  75.  
  76.             //
  77.             //    We have the start and edd offsets, read the message in
  78.             //
  79.             UCHAR MsgByte ;
  80.             DosChgFilePtr(Fh, StartOffset, 0, (LONG far *)&StartOffset) ;
  81.             DosRead(Fh, (void far *)&MsgByte, sizeof(MsgByte), &NBytes) ;
  82.             if (MsgByte == 'E' || MsgByte == 'W') {
  83.                 if (MsgLength < BufferLength) PtrBuffer[MsgLength++] = MsgSeg.Prefix[0] ;
  84.                 if (MsgLength < BufferLength) PtrBuffer[MsgLength++] = MsgSeg.Prefix[1] ;
  85.                 if (MsgLength < BufferLength) PtrBuffer[MsgLength++] = MsgSeg.Prefix[2] ;
  86.                 USHORT Divisor = 1000U, ANumber = MsgNumber ;
  87.                 while (Divisor > 0) {
  88.                     UCHAR c = '0' ;
  89.                     while (ANumber >= Divisor) {
  90.                         c++ ;
  91.                         ANumber -= Divisor ;
  92.                     }
  93.                     if (MsgLength < BufferLength)
  94.                         PtrBuffer[MsgLength++] = c ;
  95.                     Divisor /= 10U ;
  96.                 }
  97.                 if (MsgLength < BufferLength) PtrBuffer[MsgLength++] = ':' ;
  98.                 if (MsgLength < BufferLength) PtrBuffer[MsgLength++] = ' ' ;
  99.             }
  100.             if (MsgByte == 'H'
  101.             ||  MsgByte == 'I'
  102.             ||  MsgByte == 'E'
  103.             ||  MsgByte == 'W') {
  104.                 USHORT ToGo = BufferLength - MsgLength ;
  105.                 if (ToGo >= (EndOffset - StartOffset))
  106.                     ToGo = EndOffset - StartOffset - 1 ;
  107.                 else if (ToGo < (EndOffset - StartOffset))
  108.                     err = ERROR_MR_MSG_TOO_LONG ;
  109.                 DosRead(Fh, (void far *)&PtrBuffer[MsgLength], ToGo, &NBytes) ;
  110.                 MsgLength += NBytes ;
  111.                 *PtrMsgLength = MsgLength ;
  112.             } else
  113.                 err = ERROR_MR_MID_NOT_FOUND ;
  114.         } else
  115.             err = ERROR_MR_MID_NOT_FOUND ;
  116.     }
  117.  
  118.     DosClose(Fh) ;
  119.  
  120.     return err ;
  121. }
  122.  
  123. //
  124. //    Copy a message, expanding substitutions
  125. //
  126. USHORT _APICALL
  127. DosInsMessage ( char far * far *PtrVTable,
  128.                 USHORT VCount,
  129.                 char far *PtrMessage,
  130.                 USHORT MessageLength,
  131.                 char far *PtrBuffer,
  132.                 USHORT BufferLength,
  133.                 USHORT far *PtrMsgLength )
  134. {
  135.     return ERROR_MR_UN_PERFORM ;
  136. }
  137.  
  138. //
  139. //    Put a message to a previously opened message file
  140. //
  141. USHORT _APICALL
  142. DosPutMessage ( int FileHandle,
  143.                 USHORT MessageLength,
  144.                 char far *PtrMessage )
  145. {
  146.     return ERROR_MR_UN_PERFORM ;
  147. }
  148.