home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / ipc / ddeml / server / huge.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  8KB  |  205 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples.
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved.
  6. *       This source code is only intended as a supplement to
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /***************************************************************************
  13.  *                                                                         *
  14.  *  MODULE      : huge.c                                                   *
  15.  *                                                                         *
  16.  *  PURPOSE     : This contains functions useful for generating and        *
  17.  *                verifying huge text data blocks.                         *
  18.  *                                                                         *
  19.  ***************************************************************************/
  20.  
  21. #include <string.h>
  22. #include <tchar.h>
  23. #include <stdio.h>
  24. #include <windows.h>
  25. #include <ddeml.h>
  26. #include "huge.h"
  27.  
  28. extern DWORD idInst;
  29. #define BUFSZ   435
  30.  
  31. LONG lseed, lmult, ladd;
  32. TCHAR szT[BUFSZ];
  33.  
  34. VOID SetMyRand(LONG seed, LONG mult, LONG add);
  35. TCHAR MyRand(VOID);
  36. BOOL RandTest(LONG length, LONG seed, LONG mult, LONG add);
  37.  
  38. /****************************************************************************
  39.  *                                                                          *
  40.  *  FUNCTION   : SetMyRand()                                                *
  41.  *                                                                          *
  42.  *  PURPOSE    : Transfers random sequence generation variables to globals. *
  43.  *                                                                          *
  44.  ****************************************************************************/
  45. VOID SetMyRand(
  46. LONG seed,
  47. LONG mult,
  48. LONG add)
  49. {
  50.     lseed = seed;
  51.     lmult = mult;
  52.     ladd = add;
  53. }
  54.  
  55.  
  56. /****************************************************************************
  57.  *                                                                          *
  58.  *  FUNCTION   : MyRand()                                                   *
  59.  *                                                                          *
  60.  *  PURPOSE    : Generates the next random character in a sequence.         *
  61.  *                                                                          *
  62.  *  RETURNS    : the character generated                                    *
  63.  *                                                                          *
  64.  ****************************************************************************/
  65. TCHAR MyRand()
  66. {
  67.     TCHAR c;
  68.  
  69.     lseed = lseed * lmult + ladd;
  70.     c = (TCHAR)(LOWORD(lseed) ^ HIWORD(lseed));
  71.     return((TCHAR)((c & (TCHAR)0x4f) + TEXT(' ')));   // 0x20 - 0x6f - all printable
  72. }
  73.  
  74.  
  75. /*
  76.  * This function allocates and fills a HUGE data handle with a verifiable
  77.  * text string.
  78.  *
  79.  * The format of the text string is:
  80.  * "<length>=<seed>*<mult>+<add>;---data of length <length>---\0"
  81.  * all values are stored in base 16 numbers.
  82.  */
  83. /****************************************************************************
  84.  *                                                                          *
  85.  *  FUNCTION   : CreateHugeDataHandle()                                     *
  86.  *                                                                          *
  87.  *  PURPOSE    : Generates a huge pseudo-random sequence of printable       *
  88.  *               characters of the length given and places then into        *
  89.  *               a DDEML data handle.                                       *
  90.  *                                                                          *
  91.  *  RETURNS    : The data handle created or 0 on failure.                   *
  92.  *                                                                          *
  93.  ****************************************************************************/
  94. HDDEDATA CreateHugeDataHandle(
  95. LONG length,
  96. LONG seed,
  97. LONG mult,
  98. LONG add,
  99. HSZ hszItem,
  100. UINT wFmt,
  101. WORD afCmd)
  102. {
  103.     register WORD cb;
  104.     HDDEDATA hData;
  105.     DWORD cbData;
  106.     TCHAR *psz;
  107.  
  108.     wsprintf(szT, TEXT("%ld=%ld*%ld+%ld;"), length, seed, mult, add);
  109.     cb = (_tcslen(szT) + 1) * sizeof(TCHAR);
  110.     hData = DdeCreateDataHandle(idInst, (PBYTE)szT, cb, 0,
  111.             hszItem, wFmt, afCmd);
  112.     if (hData)
  113.         hData = DdeAddData(hData, NULL, 0, cb + length);
  114.     cbData = cb;
  115.     SetMyRand(seed, mult, add);
  116.     while (hData && (length > 0)) {
  117.         psz = szT;
  118.         cb = BUFSZ;
  119.         while (cb--)
  120.             *psz++ = MyRand();
  121.         hData = DdeAddData(hData, (PBYTE)szT, min(length, BUFSZ), cbData);
  122.         cbData += BUFSZ;
  123.         length -= BUFSZ;
  124.     }
  125.     return(hData);
  126. }
  127.  
  128. /****************************************************************************
  129.  *                                                                          *
  130.  *  FUNCTION   : CheckHugeData()                                            *
  131.  *                                                                          *
  132.  *  PURPOSE    : Verifies the correctness of a pseudo-random character      *
  133.  *               sequence generated by CreateHugeData.                      *
  134.  *                                                                          *
  135.  *  RETURNS    : TRUE if verified ok, FALSE otherwise.                      *
  136.  *                                                                          *
  137.  ****************************************************************************/
  138. BOOL CheckHugeData(
  139. HDDEDATA hData)
  140. {
  141.     LONG length;
  142.     LONG seed;
  143.     LONG mult;
  144.     LONG add;
  145.     TCHAR *psz;
  146.     DWORD cbOff;
  147.     WORD cb;
  148.  
  149.     if (!DdeGetData(hData, (PBYTE)szT, BUFSZ, 0))
  150.         return(FALSE);
  151.     szT[BUFSZ - 1] = TEXT('\0');
  152.     psz = _tcschr(szT, TEXT(';'));
  153.     if (psz == NULL)
  154.         return(FALSE);
  155.     *psz = TEXT('\0');
  156.  
  157.     if (_stscanf(szT, TEXT("%ld=%ld*%ld+%ld"), &length, &seed, &mult, &add) != 4)
  158.         return(FALSE);
  159.  
  160.     if (length < 0)
  161.         return(FALSE);
  162.     SetMyRand(seed, mult, add);
  163.     cbOff = (_tcslen(szT) + 1) * sizeof(TCHAR);
  164.     while (length > 0) {
  165.         DdeGetData(hData, (PBYTE)szT, BUFSZ, cbOff);
  166.         psz = szT;
  167.         cb = BUFSZ;
  168.         while (length-- && cb--)
  169.             if (*psz++ != MyRand())
  170.                 return(FALSE);
  171.         cbOff += BUFSZ;
  172.         length -= BUFSZ;
  173.     }
  174.     return(TRUE);
  175. }
  176.  
  177. #if 0
  178. /****************************************************************************
  179.  *                                                                          *
  180.  *  FUNCTION   : RandTest()                                                 *
  181.  *                                                                          *
  182.  *  PURPOSE    : Verifies the correctness of CreateHugeDataHandle() and     *
  183.  *               CheckHugeData().                                           *
  184.  *                                                                          *
  185.  *  RETURNS    :                                                            *
  186.  *                                                                          *
  187.  ****************************************************************************/
  188. BOOL RandTest(
  189. LONG length,
  190. LONG seed,
  191. LONG mult,
  192. LONG add)
  193. {
  194.     HDDEDATA hData;
  195.     BOOL fSuccess;
  196.  
  197.     hData = CreateHugeDataHandle(length, seed, mult, add, 0, 1, 0);
  198.     if (!hData)
  199.         return(FALSE);
  200.     fSuccess = CheckHugeData(hData);
  201.     DdeFreeDataHandle(hData);
  202.     return(fSuccess);
  203. }
  204. #endif
  205.