home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / DDEML / CLIENT / HUGE.C_ / HUGE.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  7.0 KB  |  191 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  MODULE      : huge.c                                                   *
  4.  *                                                                         *
  5.  *  PURPOSE     : This contains functions useful for generating and        *
  6.  *                verifying huge text data blocks.                         *
  7.  *                                                                         *
  8.  ***************************************************************************/
  9.  
  10. #include "ddemlcl.h"
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include "huge.h"
  14.  
  15. extern DWORD idInst;
  16. #define BUFSZ   435
  17.  
  18. LONG lseed, lmult, ladd;
  19. char szT[BUFSZ];
  20.  
  21. VOID SetMyRand(LONG seed, LONG mult, LONG add);
  22. char MyRand(VOID);
  23. BOOL RandTest(LONG length, LONG seed, LONG mult, LONG add);
  24.  
  25. /****************************************************************************
  26.  *                                                                          *
  27.  *  FUNCTION   : SetMyRand()                                                *
  28.  *                                                                          *
  29.  *  PURPOSE    : Transfers random sequence generation variables to globals. *
  30.  *                                                                          *
  31.  ****************************************************************************/
  32. VOID SetMyRand(
  33. LONG seed,
  34. LONG mult,
  35. LONG add)
  36. {
  37.     lseed = seed;
  38.     lmult = mult;
  39.     ladd = add;
  40. }
  41.  
  42.  
  43. /****************************************************************************
  44.  *                                                                          *
  45.  *  FUNCTION   : MyRand()                                                   *
  46.  *                                                                          *
  47.  *  PURPOSE    : Generates the next random character in a sequence.         *
  48.  *                                                                          *
  49.  *  RETURNS    : the character generated                                    *
  50.  *                                                                          *
  51.  ****************************************************************************/
  52. char MyRand()
  53. {
  54.     char c;
  55.  
  56.     lseed = lseed * lmult + ladd;
  57.     c = (char)(LOWORD(lseed) ^ HIWORD(lseed));
  58.     return((char)((c & (char)0x4f) + ' '));   // 0x20 - 0x6f - all printable
  59. }
  60.  
  61.  
  62. /*
  63.  * This function allocates and fills a HUGE data handle with a verifiable
  64.  * text string.
  65.  *
  66.  * The format of the text string is:
  67.  * "<length>=<seed>*<mult>+<add>;---data of length <length>---\0"
  68.  * all values are stored in base 16 numbers.
  69.  */
  70. /****************************************************************************
  71.  *                                                                          *
  72.  *  FUNCTION   : CreateHugeDataHandle()                                     *
  73.  *                                                                          *
  74.  *  PURPOSE    : Generates a huge pseudo-random sequence of printable       *
  75.  *               characters of the length given and places then into        *
  76.  *               a DDEML data handle.                                       *
  77.  *                                                                          *
  78.  *  RETURNS    : The data handle created or 0 on failure.                   *
  79.  *                                                                          *
  80.  ****************************************************************************/
  81. HDDEDATA CreateHugeDataHandle(
  82. LONG length,
  83. LONG seed,
  84. LONG mult,
  85. LONG add,
  86. HSZ hszItem,
  87. WORD wFmt,
  88. WORD afCmd)
  89. {
  90.     register WORD cb;
  91.     HDDEDATA hData;
  92.     DWORD cbData;
  93.     char *psz;
  94.  
  95.     wsprintf(szT, "%ld=%ld*%ld+%ld;", length, seed, mult, add);
  96.     cb = strlen(szT);
  97.     hData = DdeCreateDataHandle(idInst, szT, cb + 1, 0, hszItem, wFmt, afCmd);
  98.     if (hData)
  99.         hData = DdeAddData(hData, NULL, 0, cb + length + 1);
  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, szT, min(length, BUFSZ), cbData);
  108.         cbData += BUFSZ;
  109.         length -= BUFSZ;
  110.     }
  111.     return(hData);
  112. }
  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.     char *psz;
  132.     DWORD cbOff;
  133.     WORD cb;
  134.  
  135.     if (!DdeGetData(hData, szT, BUFSZ, 0))
  136.         return(FALSE);
  137.     szT[BUFSZ - 1] = '\0';
  138.     psz = strchr(szT, ';');
  139.     if (psz == NULL)
  140.         return(FALSE);
  141.     *psz = '\0';
  142.  
  143.     if (sscanf(szT, "%ld=%ld*%ld+%ld", &length, &seed, &mult, &add) != 4)
  144.         return(FALSE);
  145.  
  146.     if (length < 0)
  147.         return(FALSE);
  148.     SetMyRand(seed, mult, add);
  149.     cbOff = strlen(szT) + 1;
  150.     while (length > 0) {
  151.         DdeGetData(hData, szT, BUFSZ, cbOff);
  152.         psz = szT;
  153.         cb = BUFSZ;
  154.         while (length-- && cb--)
  155.             if (*psz++ != MyRand())
  156.                 return(FALSE);
  157.         cbOff += BUFSZ;
  158.         length -= BUFSZ;
  159.     }
  160.     return(TRUE);
  161. }
  162.  
  163. #if 0
  164. /****************************************************************************
  165.  *                                                                          *
  166.  *  FUNCTION   : RandTest()                                                 *
  167.  *                                                                          *
  168.  *  PURPOSE    : Verifies the correctness of CreateHugeDataHandle() and     *
  169.  *               CheckHugeData().                                           *
  170.  *                                                                          *
  171.  *  RETURNS    :                                                            *
  172.  *                                                                          *
  173.  ****************************************************************************/
  174. BOOL RandTest(
  175. LONG length,
  176. LONG seed,
  177. LONG mult,
  178. LONG add)
  179. {
  180.     HDDEDATA hData;
  181.     BOOL fSuccess;
  182.  
  183.     hData = CreateHugeDataHandle(length, seed, mult, add, 0, 1, 0);
  184.     if (!hData)
  185.         return(FALSE);
  186.     fSuccess = CheckHugeData(hData);
  187.     DdeFreeDataHandle(hData);
  188.     return(fSuccess);
  189. }
  190. #endif
  191.