home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / ddeml / dmgstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  8.4 KB  |  312 lines

  1. /****************************** Module Header ******************************\
  2. * Module Name: DMGSTR.C
  3. *
  4. * DDE manager string handling routines
  5. *
  6. * Created: 1/31/88 Sanford Staab
  7. *
  8. * Copyright (c) 1988, 1989  Microsoft Corporation
  9. \***************************************************************************/
  10. #include "ddemlp.h"
  11. #include "ctype.h"
  12.  
  13.  
  14.  
  15. /***************************** Private Function ****************************\
  16. *
  17. * returns string length not counting null terminator.
  18. *
  19. * History:  1/1/89  created     sanfords
  20. \***************************************************************************/
  21. int lstrlen(psz)
  22. PSZ psz;
  23. {
  24.     int c = 0;
  25.  
  26.     while (*psz != 0) {
  27.         psz++;
  28.         c++;
  29.     }
  30.     return(c);
  31. }
  32.  
  33. /***************************** Public  Function ****************************\
  34. * Concatonates psz1 and psz2 into psz1.
  35. * returns psz pointing to end of concatonated string.
  36. * pszLast marks point at which copying must stop.  This makes this operation
  37. * safe for limited buffer sizes.
  38. *
  39. * History:  1/1/89  created sanfords
  40. \***************************************************************************/
  41. PSZ lstrcat(psz1, psz2, pszLast)
  42. PSZ psz1, psz2, pszLast;
  43. {
  44.     psz1 += lstrlen(psz1);
  45.     while (*psz2 != '\0' && psz1 < pszLast) {
  46.         *psz1++ = *psz2++;
  47.     }
  48.     *psz1 = '\0';
  49.     return(psz1);
  50. }
  51.  
  52. /***************************** Private Function ****************************\
  53. * DESCRIPTION: ASCII dependent converter of DDE structure data to a string.
  54. * returns psz pointing to end of copy.
  55. * During monitoring we allocate segments as gettable so we can monitor them.
  56. *
  57. * History:      Created 1/31/89         sanfords
  58. \***************************************************************************/
  59. PSZ pddesToPsz(msg, pddes, psz, pszLast)
  60. USHORT msg;
  61. PDDESTRUCT pddes;
  62. PSZ psz;
  63. PSZ pszLast;
  64. {
  65.     USHORT cb;
  66.     PBYTE pData;
  67. #define pDdeInit ((PDDEINIT)pddes)
  68.  
  69.     *psz = '\0';
  70.     switch (msg) {
  71.     case WM_DDE_REQUEST:
  72.     case WM_DDE_ACK:
  73.     case WM_DDE_DATA:
  74.     case WM_DDE_ADVISE:
  75.     case WM_DDE_UNADVISE:
  76.     case WM_DDE_POKE:
  77.     case WM_DDE_EXECUTE:
  78.         psz = lstrcat(psz, "S:", pszLast);
  79.         psz = Status(pddes->fsStatus, psz, pszLast);
  80.         psz = lstrcat(psz, " F:", pszLast);
  81.         psz = Format(pddes->usFormat, psz, pszLast);
  82.         psz = lstrcat(psz, " I:", pszLast);
  83.         psz = lstrcat(psz, DDES_PSZITEMNAME(pddes), pszLast);
  84.         if (pddes->cbData)
  85.             psz = lstrcat(psz, "\n\r  Data:", pszLast);
  86.         pData = DDES_PABDATA(pddes);
  87.  
  88.         for (cb = 0; (ULONG)cb < pddes->cbData && psz < pszLast; cb++, pData++) {
  89.             /*
  90.              * new line every 64 chars
  91.              */
  92.             if ((cb & 0x3F) == 0) {
  93.                 *psz = '\0';
  94.                 psz = lstrcat(psz, "\n\r    ", pszLast);
  95.             }
  96.             if (*pData > 0x20)
  97.                 *psz = *pData;
  98.             else
  99.                 *psz = '.';
  100.                 
  101.             *psz++ = *psz & 0x7f;
  102.         }
  103.         CopyBlock("\n\r", pszLast - 3, 3L);
  104.         break;
  105.  
  106.     case WM_DDE_INITIATEACK:
  107.     case WM_DDE_INITIATE:
  108.         if (CheckSel(SELECTOROF(pDdeInit))) {
  109.             psz = lstrcat(psz, "A:", pszLast);
  110.             psz = lstrcat(psz, pDdeInit->pszAppName, pszLast);
  111.             psz = lstrcat(psz, " T:", pszLast);
  112.             psz = lstrcat(psz, pDdeInit->pszTopic, pszLast);
  113.         }
  114.         break;
  115.  
  116.     case WM_DDE_TERMINATE:
  117.         break;
  118.     }
  119.     *psz = '\0';
  120.     return(psz);
  121.     
  122. #undef pDdeInit
  123.  
  124. }
  125.  
  126.  
  127. PSZ Status(fs, psz, pszLast)
  128. USHORT fs;
  129. PSZ psz;
  130. PSZ pszLast;
  131. {
  132.     if (fs & DDE_FACK) {
  133.         psz = lstrcat(psz, "ACK ", pszLast);
  134.     }
  135.     if (fs & DDE_FBUSY) {
  136.         psz = lstrcat(psz, "BUSY ", pszLast);
  137.     }
  138.     if (fs & DDE_FNODATA) {
  139.         psz = lstrcat(psz, "NODATA ", pszLast);
  140.     }
  141.     if (fs & DDE_FACKREQ) {
  142.         psz = lstrcat(psz, "ACKREQ ", pszLast);
  143.     }
  144.     if (fs & DDE_FRESPONSE) {
  145.         psz = lstrcat(psz, "RESPONSE ", pszLast);
  146.     }
  147.     if (fs & DDE_NOTPROCESSED) {
  148.         psz = lstrcat(psz, "NOTPROCESSED ", pszLast);
  149.     }
  150.     if (fs & DDE_FAPPSTATUS) {
  151.         psz = lstrcat(psz, "APPSTAT=", pszLast);
  152.         psz = itoa(fs & DDE_FAPPSTATUS, psz, pszLast);
  153.         *psz++ = ' ';
  154.         *psz++ = '\0';
  155.     }
  156.     if (fs & DDE_FRESERVED) {
  157.         psz = lstrcat(psz, "RESERVED=", pszLast);
  158.         psz = itoa(fs & DDE_FRESERVED, psz, pszLast);
  159.     }
  160.     return(psz);
  161. }
  162.  
  163.  
  164. PSZ Format(fmt, psz, pszLast)
  165. USHORT fmt;
  166. PSZ psz;
  167. PSZ pszLast;
  168. {
  169.     if (fmt > 0xbfff) {
  170.         *psz++ = '"';
  171.         psz += WinQueryAtomName(WinQuerySystemAtomTable(), fmt, psz, pszLast - psz);
  172.         *psz++ = '"';
  173.         *psz = '\0';
  174.     } else if (fmt == DDEFMT_TEXT) {
  175.         psz = lstrcat(psz, "TEXT", pszLast);
  176.     } else {
  177.         psz = itoa(fmt, psz, pszLast);
  178.     }
  179.     return(psz);
  180. }
  181.  
  182.  
  183.  
  184.  
  185. /***************************** Private Function ****************************\
  186. * DESCRIPTION: puts an apropriate string for a DDE message into psz. pszLast
  187. * specifies the last spot to copy.  Returns a psz pointing to the end of
  188. * the copyed data.
  189. *
  190. * History:      Created 1/31/89         sanfords
  191. \***************************************************************************/
  192. PSZ ddeMsgToPsz(msg, psz, pszLast)
  193. USHORT msg;
  194. PSZ psz;
  195. PSZ pszLast;
  196. {
  197.     psz = lstrcat(psz, " ", pszLast);
  198.     if (msg < WM_DDE_FIRST || msg > WM_DDE_LAST) {
  199.         psz = itoa(msg, psz, pszLast);
  200.     } else {
  201.         WinLoadString(DMGHAB, hmodDmg, msg, pszLast - psz + 1, psz);
  202.         psz += lstrlen(psz);
  203.     }
  204.     return(lstrcat(psz, "(", pszLast));
  205. }
  206.  
  207. /***************************** Private Function ****************************\
  208. * DESCRIPTION:
  209. *   fills psz with a hex string "0xdddd" and returns psz pointing to the 0
  210. *   terminator at the end.  copying will never go beyond pszLast.
  211. *
  212. * History:      Created 1/31/89        sanfords
  213. \***************************************************************************/
  214. PSZ itoa(us, psz, pszLast)
  215. USHORT us;
  216. PSZ psz;
  217. PSZ pszLast;
  218. {
  219.     if (psz > pszLast - 7)
  220.         return(psz);
  221.     *psz++ = '0';
  222.     *psz++ = 'x';
  223.     return(stoa(psz, us));
  224. }
  225.  
  226. /***************************** Private Function ****************************\
  227. * DESCRIPTION:
  228. *   fills psz with a hex string "0xdddddddd" and returns psz pointing to the 0
  229. *   terminator at the end.  copying will never go beyond pszLast.
  230. *
  231. * History:      Created 1/31/89        sanfords
  232. \***************************************************************************/
  233. PSZ ltoa(ul, psz, pszLast)
  234. ULONG ul;
  235. PSZ psz;
  236. PSZ pszLast;
  237. {
  238.     if (psz > pszLast - 11)
  239.         return(psz);
  240.     *psz++ = '0';
  241.     *psz++ = 'x';
  242.     psz = stoa(psz, HIUSHORT(ul));
  243.     return(stoa(psz, LOUSHORT(ul)));
  244. }
  245.  
  246.  
  247. /***************************** Private Function ****************************\
  248. * DESCRIPTION:
  249. *   fills psz with a hex string "dddd" and returns psz pointing to the 0
  250. *   terminator at the end.
  251. *
  252. * History:      Created 1/31/89        sanfords
  253. \***************************************************************************/
  254. PSZ stoa(psz, us)
  255. PSZ psz;
  256. USHORT us;
  257. {
  258.     static char dtoa[] = "0123456789abcdef";
  259.  
  260.     *psz++ = dtoa[(us & 0xf000) >> 12];
  261.     *psz++ = dtoa[(us & 0xf00) >> 8];
  262.     *psz++ = dtoa[(us & 0xf0) >> 4];
  263.     *psz++ = dtoa[us & 0xf];
  264.     *psz = '\0';
  265.     return(psz);
  266. }
  267.  
  268.  
  269. /*
  270.  * Decimal to ascii
  271.  */
  272. PSZ dtoa(psz, us, fRecurse)
  273. PSZ psz;
  274. USHORT us;
  275. BOOL fRecurse;
  276. {
  277.     if (us > 9) {
  278.         psz = dtoa(psz, us / 10, TRUE);
  279.         *psz++ = (UCHAR)(us % 10) + '0';
  280.     } else if (us > 0)
  281.         *psz++ = (UCHAR)us + '0';
  282.     else if (!fRecurse)
  283.         *psz++ = '0';
  284.     *psz = '\000';
  285.     return(psz);
  286. }
  287.    
  288.  
  289. /***************************** Private Function ****************************\
  290. * DESCRIPTION:
  291. *   fills psz with a hex time stamp and returns psz pointing to the 0
  292. *   terminator at the end.
  293. *
  294. * History:      Created 5/9/89        sanfords
  295. \***************************************************************************/
  296. PSZ timestamp(psz, pszLast)
  297. PSZ psz;
  298. PSZ pszLast;
  299. {
  300.     DATETIME dt;
  301.     static USHORT prevTime = 0;
  302.     USHORT Time;
  303.  
  304.     DosGetDateTime(&dt);
  305.     Time = MAKESHORT(dt.hundredths, dt.seconds);
  306.     psz = lstrcat(psz, "----------- dTime=", pszLast);
  307.     psz = itoa(Time - prevTime, psz, pszLast);
  308.     psz = lstrcat(psz, " ", pszLast);
  309.     prevTime = Time;
  310.     return(psz + lstrlen(psz));
  311. }
  312.