PARSPARM.C
上传用户:yuandong
上传日期:2022-08-08
资源大小:954k
文件大小:8k
源码类别:

Delphi控件源码

开发平台:

C++ Builder

  1. /*++
  2. Module Name:
  3.     windowsspoolerprtprocswinprintformfeed.c
  4. Abstract:
  5.     Table and routine to send formfeed to a printer.
  6. Revision History:
  7. --*/
  8. #include <windows.h>
  9. #include <winspool.h>
  10. #include <winsplp.h>
  11. #include <wchar.h>
  12. #include "winprint.h"
  13. /** Constants for our various states **/
  14. #define ST_KEY      0x01        /** Looking for a key **/
  15. #define ST_VALUE    0x02        /** Looking for a value **/
  16. #define ST_EQUAL    0x04        /** Looking for an = sign **/
  17. #define ST_EQNODATA 0x08        /** Looking for equal w/ no data **/
  18. #define ST_DELIM    0x10        /** Looking for a ; **/
  19. #define ST_DMNODATA 0x20        /** Looking for a ; w/ no data **/
  20. /*++
  21. *******************************************************************
  22.     G e t K e y V a l u e
  23.     Routine Description:
  24.         Returns the value for a given key in the given
  25.         parameter string.  The key/values are in the order of
  26.         KEY = VALUE;.  The spaces are optional, the ';' is
  27.         required and MUST be present, directly after the value.
  28.         If the call fails, the return length will be 0 and the
  29.         return code will give the error.  This routine is written
  30.         as a state machine, driven by the current character. 
  31.     Arguments:
  32.         pParmString => Parameter string to parse
  33.         pKeyName    => Key to search for
  34.         ValueType   =  type of value to return, string or ULONG
  35.         pDestLength => length of dest buffer on enter,
  36.                        new length on exit.
  37.         pDestBuffer => area to store the key value
  38.     Return Value:
  39.         0 if okay
  40.         error if failed (from winerror.h)
  41. *******************************************************************
  42. --*/
  43. USHORT
  44. GetKeyValue(
  45.     IN      PWCHAR  pParmString,
  46.     IN      PWCHAR  pKeyName,
  47.     IN      USHORT  ValueType,
  48.     IN OUT  PUSHORT pDestLength,
  49.     OUT     PVOID   pDestBuffer)
  50. {
  51.     PWCHAR  pKey, pVal, pValEnd = NULL;
  52.     WCHAR   HoldChar;
  53.     USHORT  State = ST_KEY;    /** Start looking for a key **/
  54.     ULONG   length;
  55.     /** If any of the pointers are bad, return error **/
  56.     if ((pParmString == NULL) ||
  57.         (pKeyName == NULL)    ||
  58.         (pDestLength == NULL) ||
  59.         (pDestBuffer == NULL)) {
  60.         *pDestLength = 0;
  61.         return ERROR_INVALID_PARAMETER;
  62.     }
  63.     /**
  64.         If we are looking for a ULONG, make sure they passed
  65.         in a big enough buffer.
  66.     **/
  67.     if (ValueType == VALUE_ULONG) {
  68.         if (*pDestLength < sizeof(ULONG)) {
  69.             *(PULONG)pDestBuffer = 0;
  70.             return ERROR_INSUFFICIENT_BUFFER;
  71.         }
  72.     }
  73.         
  74.     while (pParmString && *pParmString) {
  75.         /**
  76.             Update our state, if necessary, depending on
  77.             the current character.
  78.         **/
  79.         switch (*pParmString) {
  80.         /**
  81.             We got a white space.  If we were looking for an equal
  82.             sign or delimiter, then note that we got a space.  If
  83.             we run across more data, then we have an error.
  84.         **/
  85.         case (WCHAR)' ':
  86.         case (WCHAR)'t':
  87.             /**
  88.                 If we were looking for an equal sign,
  89.                 check to see if this is the key they
  90.                 wanted.  If not, jump to the next key.
  91.             **/
  92.             if (State == ST_EQUAL) {
  93.                 if (_wcsnicmp(pKey, pKeyName, lstrlen(pKeyName))) {
  94.                     if (pParmString = wcschr(pParmString, (WCHAR)';')) {
  95.                         pParmString++;
  96.                     }
  97.                     State = ST_KEY;
  98.                     pValEnd = NULL;
  99.                     break;
  100.                 }
  101.                 /** Looking for an equal sign with no more data **/
  102.                 State = ST_EQNODATA;
  103.             }
  104.             else if (State == ST_DELIM) {
  105.                 /** If this is the end of the value, remember it **/
  106.                 if (!pValEnd) {
  107.                     pValEnd = pParmString;
  108.                 }
  109.                 /** Now looking for a delimiter with no more data **/
  110.                 State = ST_DMNODATA;
  111.             }
  112.             pParmString++;
  113.             break;
  114.         /**
  115.             Found an equal sign.  If we were looking for one,
  116.             then great - we will then be looking for a value.
  117.             We will check to see if this is the key they wanted.
  118.             Otherwise, this is an error and we will start over
  119.             with the next key.
  120.         **/
  121.         case (WCHAR)'=':
  122.             if (State == ST_EQUAL) {
  123.                 if (_wcsnicmp(pKey, pKeyName, lstrlen(pKeyName))) {
  124.                     /** Error - go to next key **/
  125.                     if (pParmString = wcschr(pParmString, (WCHAR)';')) {
  126.                         pParmString++;
  127.                     }
  128.                     State = ST_KEY;
  129.                     pValEnd = NULL;
  130.                     break;
  131.                 }
  132.                 pParmString++;
  133.                 State = ST_VALUE;
  134.             }
  135.             else {
  136.                 /** Error - go to next key **/
  137.                 if (pParmString = wcschr(pParmString, (WCHAR)';')) {
  138.                     pParmString++;
  139.                 }
  140.                 State = ST_KEY;
  141.                 pValEnd = NULL;
  142.             }
  143.             break;
  144.         /**
  145.             Found a delimeter.  If this is what we were looking
  146.             for, great - we have a complete key/value pair.
  147.         **/
  148.         case (WCHAR)';':    
  149.             if (State == ST_DELIM) {
  150.                 if (!pValEnd) {
  151.                     pValEnd = pParmString;
  152.                 }
  153.                 if (ValueType == VALUE_ULONG) {
  154.                     if (!iswdigit(*pVal)) {
  155.                         if (pParmString = wcschr(pParmString, (WCHAR)';')) {
  156.                             pParmString++;
  157.                         }
  158.                         State = ST_KEY;
  159.                         pValEnd = NULL;
  160.                         break;
  161.                     }
  162.                     *(PULONG)pDestBuffer = wcstoul(pVal, NULL, 10);
  163.                     return 0;
  164.                 }
  165.                 else if (ValueType == VALUE_STRING) {
  166.                     /**
  167.                         ASCIIZ the value to copy it out without
  168.                         any trailing spaces.
  169.                     **/
  170.                     HoldChar = *pValEnd;
  171.                     *pValEnd = (WCHAR)0;
  172.                     /** Make sure the buffer is big enough **/
  173.                     length = lstrlen(pVal);
  174.                     if (*pDestLength < length) {
  175.                         *pDestLength = 0;
  176.                         return ERROR_INSUFFICIENT_BUFFER;
  177.                     }
  178.                     /**
  179.                         Copy the data, restore the character where
  180.                         we ASCIIZ'd the string, set up the length
  181.                         and return.
  182.                     **/
  183.                     lstrcpy(pDestBuffer, pVal);
  184.                     *pValEnd = HoldChar;
  185.                     *(PULONG)pDestLength = length;
  186.                     return 0;
  187.                 }
  188.             }
  189.             else {
  190.                 /** We weren't looking for a delimiter - next key **/
  191.                 State = ST_KEY;
  192.                 pValEnd = NULL;
  193.                 pParmString++;
  194.             }
  195.             break;
  196.         /**
  197.             Found some data.  If we had hit a space,
  198.             and were expecting a equal sign or delimiter,
  199.             this is an error.
  200.         **/
  201.         default:
  202.             if ((State == ST_EQNODATA) ||
  203.                 (State == ST_DMNODATA)) {
  204.                 if (pParmString = wcschr(pParmString, (WCHAR)';')) {
  205.                     pParmString++;
  206.                 }
  207.                 State = ST_KEY;
  208.                 pValEnd = NULL;
  209.                 break;
  210.             }
  211.             else if (State == ST_KEY) {
  212.                 pKey = pParmString;
  213.                 State = ST_EQUAL;
  214.             }
  215.             else if (State == ST_VALUE) {
  216.                 pVal = pParmString;
  217.                 State = ST_DELIM;
  218.             }
  219.             pParmString++;
  220.             break;
  221.         } /* End switch */
  222.     } /* While parms data */
  223.     *pDestLength = 0;
  224.     return ERROR_NO_DATA;
  225. }