HUGE.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:8k
源码类别:

Windows编程

开发平台:

Visual C++

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