home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / taman002.zip / TASKMANA.ZIP / src / kBase.cpp < prev    next >
C/C++ Source or Header  |  2000-04-29  |  11KB  |  343 lines

  1. /* $Id: kBase.cpp,v 1.1 2000/04/29 19:06:34 stknut Exp $
  2.  *
  3.  * kBase (kClassLib) - kBase class implementation and some basis stuff.
  4.  *
  5.  * Copyright (c) 1999-2000 knut st. osmundsen
  6.  *
  7.  */
  8.  
  9. /*******************************************************************************
  10. *   Defined Constants And Macros                                               *
  11. *******************************************************************************/
  12. #define INCL_DOSERRORS
  13. #define INCL_DOSPROCESS
  14. #define INCL_DOSMISC
  15. #define INCL_PM
  16.  
  17. #define USE_DOSCREATETHREAD 1
  18.  
  19.  
  20. /*******************************************************************************
  21. *   Header Files                                                               *
  22. *******************************************************************************/
  23. #include <os2.h>
  24. #ifdef USE_KLIB
  25.     #include <kAssert.h>
  26.     #include <kLog.h>
  27.     #include <kHeap.h>
  28. #else
  29.     #include <malloc.h>
  30. #endif
  31. #include <process.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34.  
  35. #include "kBase.h"
  36.  
  37.  
  38. /*******************************************************************************
  39. *   Structures and Typedefs                                                    *
  40. *******************************************************************************/
  41. #ifdef USE_DOSCREATETHREAD
  42.  
  43. struct THREADPARAM
  44. {
  45.    void *pthis;
  46.    void (_Optlink *pfn)(void*);
  47. };
  48.  
  49.  
  50.  
  51. #pragma handler( beginMemberThreadForwarder )
  52.  
  53. /**
  54.  * Forwarder function.
  55.  * @param     ulParam  struct THREADPARAM
  56.  * @remark
  57.  */
  58. VOID _System beginMemberThreadForwarder(ULONG ulParam)
  59. {
  60.    struct THREADPARAM *p = (struct THREADPARAM*)ulParam;
  61.  
  62.    (*p->pfn)(p->pthis);
  63.  
  64.    free(p);
  65. }
  66.  
  67. #endif
  68.  
  69.  
  70.  
  71. /**
  72.  * A hack!
  73.  * @returns   Thread id on success, -1 (0xffffffffL) on error
  74.  * @param     pfn
  75.  * @param     cbStack
  76.  * @param     pthis
  77.  * @remark
  78.  */
  79. TID _System beginMemberThread(void (_Optlink *pfn)(void *), ULONG ulDummy1, ULONG ulDummy2, ULONG ulDummy3, ULONG cbStack, void *pthis)
  80. {
  81.    ulDummy1 = ulDummy1;
  82.    ulDummy2 = ulDummy2;
  83.    ulDummy3 = ulDummy3;
  84.  
  85.    #ifdef USE_DOSCREATETHREAD
  86.       TID    tid;
  87.       APIRET rc;
  88.       struct THREADPARAM *p = (struct THREADPARAM*)malloc(sizeof(struct THREADPARAM));
  89.  
  90.       p->pthis =pthis;
  91.       p->pfn = pfn;
  92.       rc = DosCreateThread(&tid, beginMemberThreadForwarder, (ULONG)p, CREATE_READY | STACK_COMMITTED, cbStack);
  93.       return rc == NO_ERROR && tid != NULLHANDLE ? tid : 0xffffffffL;
  94.    #else
  95.       return (TID)_beginthread(pfn, NULL, (int)cbStack, pthis);
  96.    #endif
  97. }
  98.  
  99.  
  100.  
  101. /**
  102.  * Debug - get message description.
  103.  * @returns   Pointer to a const description string.
  104.  * @param     msg  Message id.
  105.  */
  106. const char *getWindowMsgDescription(ULONG msg)
  107. {
  108.    #define MSGCASE(a) case a: return #a
  109.    switch (msg)
  110.    {
  111.       MSGCASE(WM_NULL                  );
  112.       MSGCASE(WM_CREATE                );
  113.       MSGCASE(WM_DESTROY               );
  114.       MSGCASE(WM_ENABLE                );
  115.       MSGCASE(WM_SHOW                  );
  116.       MSGCASE(WM_MOVE                  );
  117.       MSGCASE(WM_SIZE                  );
  118.       MSGCASE(WM_ADJUSTWINDOWPOS       );
  119.       MSGCASE(WM_CALCVALIDRECTS        );
  120.       MSGCASE(WM_SETWINDOWPARAMS       );
  121.       MSGCASE(WM_QUERYWINDOWPARAMS     );
  122.       MSGCASE(WM_HITTEST               );
  123.       MSGCASE(WM_ACTIVATE              );
  124.       MSGCASE(WM_SETFOCUS              );
  125.       MSGCASE(WM_SETSELECTION          );
  126.       MSGCASE(WM_PPAINT                );
  127.       MSGCASE(WM_PSETFOCUS             );
  128.       MSGCASE(WM_PSYSCOLORCHANGE       );
  129.       MSGCASE(WM_PSIZE                 );
  130.       MSGCASE(WM_PACTIVATE             );
  131.       MSGCASE(WM_PCONTROL              );
  132.       MSGCASE(WM_COMMAND               );
  133.       MSGCASE(WM_SYSCOMMAND            );
  134.       MSGCASE(WM_HELP                  );
  135.       MSGCASE(WM_PAINT                 );
  136.       MSGCASE(WM_TIMER                 );
  137.       MSGCASE(WM_SEM1                  );
  138.       MSGCASE(WM_SEM2                  );
  139.       MSGCASE(WM_SEM3                  );
  140.       MSGCASE(WM_SEM4                  );
  141.       MSGCASE(WM_CLOSE                 );
  142.       MSGCASE(WM_QUIT                  );
  143.       MSGCASE(WM_SYSCOLORCHANGE        );
  144.       MSGCASE(WM_SYSVALUECHANGED       );
  145.       MSGCASE(WM_APPTERMINATENOTIFY    );
  146.       MSGCASE(WM_PRESPARAMCHANGED      );
  147.       MSGCASE(WM_CONTROL               );
  148.       MSGCASE(WM_VSCROLL               );
  149.       MSGCASE(WM_HSCROLL               );
  150.       MSGCASE(WM_INITMENU              );
  151.       MSGCASE(WM_MENUSELECT            );
  152.       MSGCASE(WM_MENUEND               );
  153.       MSGCASE(WM_DRAWITEM              );
  154.       MSGCASE(WM_MEASUREITEM           );
  155.       MSGCASE(WM_CONTROLPOINTER        );
  156.       MSGCASE(WM_QUERYDLGCODE          );
  157.       MSGCASE(WM_INITDLG               );
  158.       MSGCASE(WM_SUBSTITUTESTRING      );
  159.       MSGCASE(WM_MATCHMNEMONIC         );
  160.       MSGCASE(WM_SAVEAPPLICATION       );
  161.       MSGCASE(WM_SEMANTICEVENT         );
  162.       MSGCASE(WM_HELPBASE              );
  163.       MSGCASE(WM_HELPTOP               );
  164.       MSGCASE(WM_USER                  );
  165.       MSGCASE(WM_VRNDISABLED           );
  166.       MSGCASE(WM_VRNENABLED            );
  167.       MSGCASE(WM_MOUSEMOVE             );
  168.       MSGCASE(WM_BUTTON1DOWN           );
  169.       MSGCASE(WM_BUTTON1UP             );
  170.       MSGCASE(WM_BUTTON1DBLCLK         );
  171.       MSGCASE(WM_BUTTON2DOWN           );
  172.       MSGCASE(WM_BUTTON2UP             );
  173.       MSGCASE(WM_BUTTON2DBLCLK         );
  174.       MSGCASE(WM_BUTTON3DOWN           );
  175.       MSGCASE(WM_BUTTON3UP             );
  176.       MSGCASE(WM_BUTTON3DBLCLK         );
  177.       MSGCASE(WM_MOUSEMAP              );
  178.       MSGCASE(WM_EXTMOUSEFIRST         );
  179.       MSGCASE(WM_EXTMOUSELAST          );
  180.       MSGCASE(WM_BUTTON1MOTIONSTART    );
  181.       MSGCASE(WM_BUTTON1MOTIONEND      );
  182.       MSGCASE(WM_BUTTON1CLICK          );
  183.       MSGCASE(WM_BUTTON2MOTIONSTART    );
  184.       MSGCASE(WM_BUTTON2MOTIONEND      );
  185.       MSGCASE(WM_BUTTON2CLICK          );
  186.       MSGCASE(WM_BUTTON3MOTIONSTART    );
  187.       MSGCASE(WM_BUTTON3MOTIONEND      );
  188.       MSGCASE(WM_BEGINDRAG             );
  189.       MSGCASE(WM_ENDDRAG               );
  190.       MSGCASE(WM_SINGLESELECT          );
  191.       MSGCASE(WM_OPEN                  );
  192.       MSGCASE(WM_CONTEXTMENU           );
  193.       MSGCASE(WM_CONTEXTHELP           );
  194.       MSGCASE(WM_TEXTEDIT              );
  195.       MSGCASE(WM_BEGINSELECT           );
  196.       MSGCASE(WM_ENDSELECT             );
  197.       MSGCASE(WM_PICKUP                );
  198.       MSGCASE(WM_PENFIRST              );
  199.       MSGCASE(WM_PENLAST               );
  200.       MSGCASE(WM_MMPMFIRST             );
  201.       MSGCASE(WM_MMPMLAST              );
  202.       MSGCASE(WM_STDDLGFIRST           );
  203.       MSGCASE(WM_STDDLGLAST            );
  204.       MSGCASE(WM_BIDI_FIRST            );
  205.       MSGCASE(WM_BIDI_LAST             );
  206.       MSGCASE(WM_CHAR                  );
  207.       MSGCASE(WM_VIOCHAR               );
  208.       MSGCASE(WM_JOURNALNOTIFY         );
  209.       MSGCASE(WM_MSGBOXINIT            );
  210.       MSGCASE(WM_MSGBOXDISMISS         );
  211.       MSGCASE(WM_FLASHWINDOW           );
  212.       MSGCASE(WM_FORMATFRAME           );
  213.       MSGCASE(WM_UPDATEFRAME           );
  214.       MSGCASE(WM_FOCUSCHANGE           );
  215.       MSGCASE(WM_SETBORDERSIZE         );
  216.       MSGCASE(WM_TRACKFRAME            );
  217.       MSGCASE(WM_MINMAXFRAME           );
  218.       MSGCASE(WM_SETICON               );
  219.       MSGCASE(WM_QUERYICON             );
  220.       MSGCASE(WM_SETACCELTABLE         );
  221.       MSGCASE(WM_QUERYACCELTABLE       );
  222.       MSGCASE(WM_TRANSLATEACCEL        );
  223.       MSGCASE(WM_QUERYTRACKINFO        );
  224.       MSGCASE(WM_QUERYBORDERSIZE       );
  225.       MSGCASE(WM_NEXTMENU              );
  226.       MSGCASE(WM_ERASEBACKGROUND       );
  227.       MSGCASE(WM_QUERYFRAMEINFO        );
  228.       MSGCASE(WM_QUERYFOCUSCHAIN       );
  229.       MSGCASE(WM_OWNERPOSCHANGE        );
  230.       MSGCASE(WM_CALCFRAMERECT         );
  231.       MSGCASE(WM_WINDOWPOSCHANGED      );
  232.       MSGCASE(WM_ADJUSTFRAMEPOS        );
  233.       MSGCASE(WM_QUERYFRAMECTLCOUNT    );
  234.       MSGCASE(WM_QUERYHELPINFO         );
  235.       MSGCASE(WM_SETHELPINFO           );
  236.       MSGCASE(WM_ERROR                 );
  237.       MSGCASE(WM_REALIZEPALETTE        );
  238.       MSGCASE(WM_CTLCOLORCHANGE        );
  239.       MSGCASE(WM_QUERYCTLTYPE          );
  240.       MSGCASE(WM_RENDERFMT             );
  241.       MSGCASE(WM_RENDERALLFMTS         );
  242.       MSGCASE(WM_DESTROYCLIPBOARD      );
  243.       MSGCASE(WM_PAINTCLIPBOARD        );
  244.       MSGCASE(WM_SIZECLIPBOARD         );
  245.       MSGCASE(WM_HSCROLLCLIPBOARD      );
  246.       MSGCASE(WM_VSCROLLCLIPBOARD      );
  247.       MSGCASE(WM_DRAWCLIPBOARD         );
  248.       MSGCASE(WM_DDE_INITIATE          );
  249.       MSGCASE(WM_DDE_REQUEST           );
  250.       MSGCASE(WM_DDE_ACK               );
  251.       MSGCASE(WM_DDE_DATA              );
  252.       MSGCASE(WM_DDE_ADVISE            );
  253.       MSGCASE(WM_DDE_UNADVISE          );
  254.       MSGCASE(WM_DDE_POKE              );
  255.       MSGCASE(WM_DDE_EXECUTE           );
  256.       MSGCASE(WM_DDE_TERMINATE         );
  257.       MSGCASE(WM_DDE_INITIATEACK       );
  258.       MSGCASE(WM_DDE_LAST              );
  259.       MSGCASE(WM_QUERYCONVERTPOS       );
  260.    }
  261.    return "unknown msg";
  262. }
  263.  
  264.  
  265. /**************************/
  266. /* KLIB replacements.     */
  267. /**************************/
  268. #ifndef USE_KLIB
  269. void kDebugLog(char *pszFormat, ...)
  270. {
  271.    /* do nothing! */
  272.    pszFormat = pszFormat;
  273. }
  274.  
  275. BOOL _assertMsg(const char *pszExpr, const char *pszFile, const char *pszFunction,
  276.                 ULONG ulLine)
  277. {
  278.    static char szBuf[512];
  279.    PTIB        ptib;  /*  A pointer to the address of the TIB in which the current thread is returned. */
  280.    PPIB        ppib;  /*  The address of a pointer to the PIB in which the current process is returned. */
  281.    TID         tid = 0;
  282.    ULONG       ulType = 0;
  283.    int         rc = TRUE;
  284.  
  285.    if (DosGetInfoBlocks(&ptib, &ppib) == NO_ERROR)
  286.    {
  287.       tid    = ptib->tib_ptib2->tib2_ultid;
  288.       ulType = ppib->pib_ultype;
  289.    }
  290.  
  291.    /* log assert */
  292.    sprintf(szBuf,
  293.            "\r\n"
  294.            "Assertion failed - thread id %d:\r\n"
  295.            "  File: %s\r\n"
  296.            "  Function: %s\r\n"
  297.            "  Lineno: %d\r\n"
  298.            "  Expression: %s\r\n",
  299.            tid, pszFile, pszFunction, ulLine, pszExpr
  300.            );
  301.    kDebugLog(szBuf);
  302.  
  303.    /* display assert message */
  304.    if (ulType == 3)
  305.    {
  306.       HAB hab;
  307.       HMQ hmq;
  308.       hmq = WinCreateMsgQueue(hab = WinInitialize(0), 0);
  309.       sprintf(szBuf,
  310.               "Assertion failed - thread id %d:\r"
  311.               "  File: %s\r"
  312.               "  Function: %s\r"
  313.               "  Lineno: %d\r"
  314.               "  Expression: %s",
  315.               tid, pszFile, pszFunction, ulLine, pszExpr
  316.               );
  317.       if (WinMessageBox(HWND_DESKTOP,
  318.                         NULLHANDLE,
  319.                         szBuf,
  320.                         "Assert failed!",
  321.                         0,
  322.                         MB_ABORTRETRYIGNORE | MB_CRITICAL)
  323.           == MBID_IGNORE)
  324.          rc = FALSE;
  325.  
  326.       if (hmq != NULLHANDLE)
  327.       {
  328.          WinDestroyMsgQueue(hmq);
  329.          WinTerminate(hab);
  330.       }
  331.    }
  332.    else
  333.    {
  334.       DosPutMessage(0, strlen(szBuf), szBuf);
  335.       rc = TRUE;
  336.    }
  337.  
  338.    return rc;
  339. }
  340.  
  341.  
  342. #endif
  343.