home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / QPROC.ZIP / QPUTIL.C < prev    next >
C/C++ Source or Header  |  1990-10-18  |  7KB  |  294 lines

  1. /*****************************************************************/ 
  2. /**             Microsoft LAN Manager            **/ 
  3. /**           Copyright(c) Microsoft Corp., 1985-1990        **/ 
  4. /*****************************************************************/ 
  5. /****************************** Module Header ******************************\
  6.  
  7. Module Name: QPUTIL.C
  8.  
  9. Utility functions for PM Print Queue Processor
  10.  
  11. History:
  12.  10-Sep-88 [stevewo]  Created.
  13.  14-Feb-90 [thomaspa] DBCS fixes.
  14.  
  15. \***************************************************************************/
  16.  
  17. #define INCL_WINATOM
  18. #include "pmprint.h"
  19. #include <netlib.h>
  20. #include <strapi.h>
  21. #include <makenear.h>
  22.  
  23. #define NEXTCHAR( psz )        if( IS_LEAD_BYTE( *psz ) )    \
  24.                 {                \
  25.                     psz += 2;        \
  26.                 }                \
  27.                 else                \
  28.                 {                \
  29.                     psz++;            \
  30.                 }
  31. /*
  32.  * MakeNearPtr
  33.  * Runtime test for a pointer outside the default data segment
  34.  *
  35.  * In non-debugging versions, this is replaced by a simple macro
  36.  * that returns the offset-portion of the pointer.
  37.  *
  38.  */
  39. #if defined(DEBUG)
  40. void near * pascal far
  41. MakeNearPtr( void far * fp )
  42. {
  43.     /* HIWORD and LOWORD both generate better code
  44.        and work on rvalues */
  45.     if(SELECTOROF(fp) != HIUSHORT(fp))
  46.         SplWarning( "MakeNearPtr: SELECTOROF(fp) != HIUSHORT(fp)", 0, 0);
  47.     if(OFFSETOF(fp) != LOUSHORT(fp))
  48.     SplWarning( "MakeNearPtr: OFFSETOF(fp) != LOUSHORT(fp)", 0, 0);
  49.     /* assumption: SS == DS */
  50.     if(fp != NULL && HIUSHORT(fp) != HIUSHORT((char far *)&fp))
  51.     SplWarning( "MakeNearPtr: HIUSHORT(fp) != HIUSHORT((char far *)&fp)", 0,0);
  52.     return LOUSHORT(fp);
  53. }
  54. #endif // DEBUG
  55.  
  56.  
  57.  
  58. VOID FARENTRY EnterSplSem( VOID )
  59. {
  60.     if (!bInitDone)
  61.         SplPanic( "Spooler not initialized", 0, 0 );
  62.     else
  63.         FSRSemEnter( &semPMPRINT );
  64. }
  65.  
  66. VOID FARENTRY LeaveSplSem( VOID )
  67. {
  68.     FSRSemLeave( &semPMPRINT );
  69. }
  70.  
  71. VOID FARENTRY ExitSplSem( VOID )
  72. {
  73.     FSRSemExit( &semPMPRINT );
  74. }
  75.  
  76.  
  77. /* AllocSplMem -- allocates memory in default DS
  78.  *
  79.  * in:  cb - size of memory requested
  80.  * out: -> new memory, NULL if error
  81.  */
  82. NPVOID FARENTRY AllocSplMem(USHORT cb)
  83. {
  84.     NPVOID p;
  85.  
  86.     p = WinAllocMem( hSplHeap, cb );
  87. #ifdef LATER
  88.     SplWarning( "AllocSplMem( %0d ) = %2x", cb, p );
  89. #endif
  90.     if (p) {
  91. #ifdef DEBUG
  92.         memsetf(p, 0xbc, cb);  /* put in signature */
  93. #endif
  94.         SplInSem();
  95.         return( p );
  96.     } else {
  97.         SplErrNoMemory( cb );
  98.     }
  99. }
  100.  
  101.  
  102. /* AllocSplStr -- Allocates mem for a string and copies it into
  103.  *
  104.  * in:  pszSrc - -> new string, may be NULL or null string
  105.  * out: new string, NULL if source was empty
  106.  */
  107. NPSZ FARENTRY AllocSplStr(PSZ pszSrc)
  108. {
  109.     NPSZ p = NULL;
  110.     USHORT cbSrc;
  111.                                         /* change only if src != NULL */
  112.     if (pszSrc && (cbSrc = SafeStrlen(pszSrc))) {
  113.         if (p = AllocSplMem(cbSrc + 1))
  114.             strcpyf(p, pszSrc);
  115.     }
  116.     return p;
  117. }
  118.  
  119.  
  120. /* FreeSplMem -- frees memory allocated by AllocSplMem
  121.  *
  122.  * in:  p - -> memory allocated by AllocSplMem or NULL
  123.  *      cb - size specified on AllocSplMem
  124.  * out: -> memory if memory couldn't freed, otherwise NULL
  125.  */
  126. NPVOID FARENTRY FreeSplMem( p, cb )
  127. NPVOID p;
  128. USHORT cb;
  129. {
  130. #ifdef DEBUG
  131.     if (p) {
  132.         SplInSem();
  133.         memsetf(p, 0xab, cb);  /* put in signature */
  134.     }
  135. #endif
  136.     if (p) {
  137. #ifdef LATER
  138.         SplWarning( "FreeSplMem( %0x, %2d )", p, cb );
  139. #endif
  140.         if (p = WinFreeMem( hSplHeap, p, cb ))
  141.             SplPanic( "SplFreeMem - invalid pointer = %0x", p, 0 );
  142.     }
  143.  
  144.     return( p );
  145. }
  146.  
  147.  
  148. /* FreeSplStr -- frees memory allocated by AllocSplStr
  149.  *
  150.  * in:  psz - -> memory allocated by AllocSplMem or NULL
  151.  * out: -> memory if memory couldn't freed, otherwise NULL
  152.  */
  153. NPSZ FARENTRY FreeSplStr(NPSZ psz)
  154. {
  155.     return psz ? FreeSplMem(psz, SafeStrlen(psz) + 1) : NULL;
  156. }
  157.  
  158.  
  159. USHORT FARENTRY AsciiToInt( psz )
  160. PSZ psz;
  161. {
  162.     USHORT n;
  163.     UCHAR c;
  164.     BOOL bNegative = FALSE;
  165.     BOOL bDigitSeen= FALSE;
  166.  
  167.     while (*psz == ' ')
  168.         psz++;
  169.  
  170.     c = *psz;
  171.     if (c == '-')
  172.         bNegative = TRUE;
  173.     else
  174.     if (c != '+')
  175.         c = 0;
  176.  
  177.     if (c)
  178.         psz++;
  179.  
  180.     n = 0;
  181.     while (c = *psz++) {
  182.         c -= '0';
  183.         if (c > 9)
  184.             break;
  185.  
  186.         else {
  187.             bDigitSeen = TRUE;
  188.             n = (n*10) + c;
  189.             }
  190.         }
  191.  
  192.     if (bNegative)
  193.         return( -n );
  194.     else
  195.         return( n );
  196. }
  197.  
  198.  
  199.  
  200.  
  201. /*
  202.  * ParseKeyData -- parses string for a separator character and
  203.  *                 allocates and builds an array of strings out of it,
  204.  *                 each string is zero-terminated, followed by an extra \0
  205.  * in:  pKeyData - -> string to parse
  206.  *      chSep - separator character
  207.  *      cbKeyData - length of the string
  208.  * out: -> string array allocated on near heap, must be freed by caller
  209.  */
  210. PKEYDATA FARENTRY ParseKeyData( pKeyData, chSep, cbKeyData )
  211. PSZ pKeyData;
  212. UCHAR chSep;
  213. USHORT cbKeyData;
  214. {
  215.     USHORT cTokens, cb, i;
  216.     register PKEYDATA pResult;
  217.     register NPSZ pDst;
  218.     PSZ psz;
  219.  
  220.     cTokens = 0;
  221.     cb = cbKeyData;
  222.     if (psz = pKeyData) {
  223.         while (cb-- && *psz)
  224.         {
  225.             if (*psz == chSep)
  226.                 cTokens++;
  227.             NEXTCHAR(psz);  
  228.         }
  229.  
  230.         if (psz[-1] != chSep)
  231.             if (chSep != ';')
  232.                 cTokens++;
  233.             else
  234.                 cTokens = 0;
  235.         }
  236.  
  237.     if (cb || *psz || !cTokens)
  238.         return( NULL );
  239.  
  240.     cb = sizeof( KEYDATA ) + (cTokens-1) * sizeof( NPSZ ) + cbKeyData;
  241.     if (!(pResult = (PKEYDATA)AllocSplMem( cb )))
  242.         return( NULL );
  243.  
  244.     pResult->cb = cb;
  245.     pResult->cTokens = cTokens;
  246.     pDst = (NPSZ)pResult + (cb-cbKeyData);
  247.     i = 0;
  248.     while (cTokens--) {
  249.         pResult->pTokens[ i ] = pDst;
  250.         while (*pDst = *pKeyData++) {
  251.             if (*pDst != chSep)
  252.         {
  253.                 NEXTCHAR(pDst);
  254.             }
  255.             else {
  256.                 *pDst = '\0';
  257.                 break;
  258.                 }
  259.             }
  260.  
  261.         if (pResult->pTokens[ i ] == pDst)
  262.             pResult->pTokens[ i ] = NULL;
  263.         else
  264.         {
  265.             NEXTCHAR(pDst);
  266.         }
  267.         i++;
  268.     }
  269.  
  270.     return( pResult );
  271. }
  272.  
  273.  
  274. PSZ FARENTRY MyItoa(USHORT wNumber, PSZ pszRes)
  275. {
  276.     PSZ  pch,pchEnd;
  277.     CHAR  ch;
  278.  
  279.     pch = pszRes;                     /* pch points on 1rst CHAR */
  280.     do {
  281.         *pszRes++ = (CHAR)(wNumber % 10 + '0');
  282.     } while ((wNumber /= 10) > 0);
  283.  
  284.     pchEnd=pszRes;
  285.     *pszRes-- = '\0';                  /* pszRes points on last CHAR */
  286.  
  287.     while (pch < pszRes) {
  288.         ch = *pch;
  289.         *pch++ = *pszRes;
  290.         *pszRes-- = ch;
  291.     }
  292.     return (pchEnd);      /* return pointer to terminating 0 byte */
  293. }
  294.