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 <string.h>
  19. #include <tchar.h>
  20. #include <stdio.h>
  21. #include <windows.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. UINT wFmt,
  89. WORD afCmd)
  90. {
  91.     register WORD 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,
  98.             hszItem, wFmt, afCmd);
  99.     if (hData)
  100.         hData = DdeAddData(hData, NULL, 0, cb + length);
  101.     cbData = cb;
  102.     SetMyRand(seed, mult, add);
  103.     while (hData && (length > 0)) {
  104.         psz = szT;
  105.         cb = BUFSZ;
  106.         while (cb--)
  107.             *psz++ = MyRand();
  108.         hData = DdeAddData(hData, (PBYTE)szT, min(length, BUFSZ), cbData);
  109.         cbData += BUFSZ;
  110.         length -= BUFSZ;
  111.     }
  112.     return(hData);
  113. }
  114. /****************************************************************************
  115.  *                                                                          *
  116.  *  FUNCTION   : CheckHugeData()                                            *
  117.  *                                                                          *
  118.  *  PURPOSE    : Verifies the correctness of a pseudo-random character      *
  119.  *               sequence generated by CreateHugeData.                      *
  120.  *                                                                          *
  121.  *  RETURNS    : TRUE if verified ok, FALSE otherwise.                      *
  122.  *                                                                          *
  123.  ****************************************************************************/
  124. BOOL CheckHugeData(
  125. HDDEDATA hData)
  126. {
  127.     LONG length;
  128.     LONG seed;
  129.     LONG mult;
  130.     LONG add;
  131.     TCHAR *psz;
  132.     DWORD cbOff;
  133.     WORD cb;
  134.     if (!DdeGetData(hData, (PBYTE)szT, BUFSZ, 0))
  135.         return(FALSE);
  136.     szT[BUFSZ - 1] = TEXT('');
  137.     psz = _tcschr(szT, TEXT(';'));
  138.     if (psz == NULL)
  139.         return(FALSE);
  140.     *psz = TEXT('');
  141.     if (_stscanf(szT, TEXT("%ld=%ld*%ld+%ld"), &length, &seed, &mult, &add) != 4)
  142.         return(FALSE);
  143.     if (length < 0)
  144.         return(FALSE);
  145.     SetMyRand(seed, mult, add);
  146.     cbOff = (_tcslen(szT) + 1) * sizeof(TCHAR);
  147.     while (length > 0) {
  148.         DdeGetData(hData, (PBYTE)szT, BUFSZ, cbOff);
  149.         psz = szT;
  150.         cb = BUFSZ;
  151.         while (length-- && cb--)
  152.             if (*psz++ != MyRand())
  153.                 return(FALSE);
  154.         cbOff += BUFSZ;
  155.         length -= BUFSZ;
  156.     }
  157.     return(TRUE);
  158. }
  159. #if 0
  160. /****************************************************************************
  161.  *                                                                          *
  162.  *  FUNCTION   : RandTest()                                                 *
  163.  *                                                                          *
  164.  *  PURPOSE    : Verifies the correctness of CreateHugeDataHandle() and     *
  165.  *               CheckHugeData().                                           *
  166.  *                                                                          *
  167.  *  RETURNS    :                                                            *
  168.  *                                                                          *
  169.  ****************************************************************************/
  170. BOOL RandTest(
  171. LONG length,
  172. LONG seed,
  173. LONG mult,
  174. LONG add)
  175. {
  176.     HDDEDATA hData;
  177.     BOOL fSuccess;
  178.     hData = CreateHugeDataHandle(length, seed, mult, add, 0, 1, 0);
  179.     if (!hData)
  180.         return(FALSE);
  181.     fSuccess = CheckHugeData(hData);
  182.     DdeFreeDataHandle(hData);
  183.     return(fSuccess);
  184. }
  185. #endif