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 / client / huge.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  8KB  |  204 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 <windows.h>
  22. #include <string.h>
  23. #include <tchar.h>
  24. #include <stdio.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. DWORD wFmt,
  101. DWORD afCmd)
  102. {
  103.     register DWORD 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, hszItem, wFmt, afCmd);
  111.     if (hData)
  112.         hData = DdeAddData(hData, NULL, 0, cb + length);
  113.     cbData = cb;
  114.     SetMyRand(seed, mult, add);
  115.     while (hData && (length > 0)) {
  116.         psz = szT;
  117.         cb = BUFSZ;
  118.         while (cb--)
  119.             *psz++ = MyRand();
  120.         hData = DdeAddData(hData, (PBYTE)szT, min(length, BUFSZ), cbData);
  121.         cbData += BUFSZ;
  122.         length -= BUFSZ;
  123.     }
  124.     return(hData);
  125. }
  126.  
  127. /****************************************************************************
  128.  *                                                                          *
  129.  *  FUNCTION   : CheckHugeData()                                            *
  130.  *                                                                          *
  131.  *  PURPOSE    : Verifies the correctness of a pseudo-random character      *
  132.  *               sequence generated by CreateHugeData.                      *
  133.  *                                                                          *
  134.  *  RETURNS    : TRUE if verified ok, FALSE otherwise.                      *
  135.  *                                                                          *
  136.  ****************************************************************************/
  137. BOOL CheckHugeData(
  138. HDDEDATA hData)
  139. {
  140.     LONG length;
  141.     LONG seed;
  142.     LONG mult;
  143.     LONG add;
  144.     TCHAR *psz;
  145.     DWORD cbOff;
  146.     DWORD cb;
  147.  
  148.     if (!DdeGetData(hData, (PBYTE)szT, BUFSZ, 0))
  149.         return(FALSE);
  150.     szT[BUFSZ - 1] = TEXT('\0');
  151.     psz = _tcschr(szT, TEXT(';'));
  152.     if (psz == NULL)
  153.         return(FALSE);
  154.     *psz = TEXT('\0');
  155.  
  156.     if (_stscanf(szT, TEXT("%ld=%ld*%ld+%ld"), &length, &seed, &mult, &add) != 4)
  157.         return(FALSE);
  158.  
  159.     if (length < 0)
  160.         return(FALSE);
  161.     SetMyRand(seed, mult, add);
  162.     cbOff = _tcslen(szT) + 1;
  163.     while (length > 0) {
  164.         DdeGetData(hData, (PBYTE)szT, BUFSZ, cbOff);
  165.         psz = szT;
  166.         cb = BUFSZ;
  167.         while (length-- && cb--)
  168.             if (*psz++ != MyRand())
  169.                 return(FALSE);
  170.         cbOff += BUFSZ;
  171.         length -= BUFSZ;
  172.     }
  173.     return(TRUE);
  174. }
  175.  
  176. #if 0
  177. /****************************************************************************
  178.  *                                                                          *
  179.  *  FUNCTION   : RandTest()                                                 *
  180.  *                                                                          *
  181.  *  PURPOSE    : Verifies the correctness of CreateHugeDataHandle() and     *
  182.  *               CheckHugeData().                                           *
  183.  *                                                                          *
  184.  *  RETURNS    :                                                            *
  185.  *                                                                          *
  186.  ****************************************************************************/
  187. BOOL RandTest(
  188. LONG length,
  189. LONG seed,
  190. LONG mult,
  191. LONG add)
  192. {
  193.     HDDEDATA hData;
  194.     BOOL fSuccess;
  195.  
  196.     hData = CreateHugeDataHandle(length, seed, mult, add, 0, 1, 0);
  197.     if (!hData)
  198.         return(FALSE);
  199.     fSuccess = CheckHugeData(hData);
  200.     DdeFreeDataHandle(hData);
  201.     return(fSuccess);
  202. }
  203. #endif
  204.