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

  1. /* $Id: kError.cpp,v 1.1 2000/04/29 19:06:34 stknut Exp $
  2.  *
  3.  * kError (kClassLib/kTaskMgr) - Implementation of the kError class
  4.  *
  5.  * Copyright (c) 1999-2000 knut st. osmundsen
  6.  *
  7.  */
  8.  
  9. /*******************************************************************************
  10. *   Defined Constants And Macros                                               *
  11. *******************************************************************************/
  12. #define INCL_DOSMISC
  13. #define INCL_DOSERRORS
  14. #define INCL_WINERRORS
  15. #define INCL_WINDIALOGS
  16.  
  17. /*******************************************************************************
  18. *   Header Files                                                               *
  19. *******************************************************************************/
  20. #include <os2.h>
  21. #ifdef USE_KLIB
  22.     #include <kAssert.h>
  23.     #include <kLog.h>
  24.     #include <kHeap.h>
  25. #endif
  26. #include <string.h>
  27. #include <stdio.h>
  28.  
  29. #include "kError.h"
  30. #undef   logError
  31.  
  32.  
  33.  
  34.  
  35. /**
  36.  * Sets the error number.
  37.  * @returns   Reference to this object.
  38.  * @param     ulError  Error number
  39.  */
  40. kMyError &kMyError::setError(ULONG ulError)
  41. {
  42.     ulMyErrorNo = ulError;
  43.     return *this;
  44. }
  45.  
  46.  
  47.  
  48. /**
  49.  * Creates and sets the error number.
  50.  * @param     Error number
  51.  */
  52. kMyError::kMyError()
  53. {
  54.     ulMyErrorNo = unused;
  55. }
  56.  
  57.  
  58.  
  59. /**
  60.  * Creates and sets the error number.
  61.  * @param     ulError  Error number
  62.  */
  63. kMyError::kMyError(ULONG ulError)
  64. {
  65.     ulMyErrorNo = ulError;
  66. }
  67.  
  68.  
  69.  
  70. /**
  71.  * Copy constructor.
  72.  * @param     obj  Object to copy.
  73.  */
  74. kMyError::kMyError(const kMyError &obj)
  75. {
  76.     ulMyErrorNo = obj.ulMyErrorNo;
  77. }
  78.  
  79.  
  80.  
  81. /**
  82.  * Gets the error number.
  83.  * @returns   Error number
  84.  */
  85. ULONG kMyError::getErrorNo() const
  86. {
  87.     return ulMyErrorNo;
  88. }
  89.  
  90.  
  91.  
  92. /**
  93.  * Get the error description.
  94.  * @returns   Pointer to a _read-only_ string.
  95.  */
  96. const char *kMyError::getDescription() const
  97. {
  98.     const char *pszRet;
  99.  
  100.     switch (ulMyErrorNo)
  101.     {
  102.         case unused:
  103.             pszRet = "...";
  104.             break;
  105.  
  106.         case new_failed:
  107.             pszRet = "new returned a null pointer.";
  108.             break;
  109.  
  110.         case WinCreateWindow_failed_frame:
  111.             pszRet = "WinCreateWindow frame failed.";
  112.             break;
  113.  
  114.         case WinCreateWindow_failed_client:
  115.             pszRet = "WinCreateWindow client failed.";
  116.             break;
  117.  
  118.         case user_cancel:
  119.             pszRet = "User canceld operation.";
  120.             break;
  121.  
  122.         case compressDiff_failed:
  123.             pszRet = "compressDiff failed.";
  124.             break;
  125.  
  126.         case unsupported_bitcount:
  127.             pszRet = "Unsupported bitcount.";
  128.             break;
  129.  
  130.         case dive_failed:
  131.             pszRet = "A kDive operation failed.";
  132.             break;
  133.  
  134.         case error_opening_movie:
  135.             pszRet = "Error opening movie.";
  136.             break;
  137.  
  138.         case getPalette_failed:
  139.             pszRet = "getPalette failed.";
  140.             break;
  141.  
  142.         case createPalette_failed:
  143.             pszRet = "createPalette failed.";
  144.             break;
  145.  
  146.         case getMousePos_failed:
  147.             pszRet = "getMousePos failed.";
  148.             break;
  149.  
  150.         case DosCreateEventSem_failed:
  151.             pszRet = "DosCreateEventSem failed.";
  152.             break;
  153.  
  154.         case WinRegisterClass_failed:
  155.             pszRet = "WinRegisterClass failed.";
  156.             break;
  157.  
  158.         case fopen_failed:
  159.             pszRet = "fopen failed.";
  160.             break;
  161.  
  162.         case ftell_failed:
  163.             pszRet = "ftell failed.";
  164.             break;
  165.  
  166.         case fread_failed:
  167.             pszRet = "fread failed.";
  168.             break;
  169.  
  170.         case fseek_failed:
  171.             pszRet = "fseek failed.";
  172.             break;
  173.  
  174.         case fwrite_failed:
  175.             pszRet = "fwrite failed.";
  176.             break;
  177.  
  178.         case beginthread_failed:
  179.             pszRet = "_beginthread failed";
  180.             break;
  181.  
  182.         case alloc_failed:
  183.             pszRet = "(c/m/re)alloc failed";
  184.             break;
  185.  
  186.         case invalid_class:
  187.             pszRet = "Invalid class (restoring objects from a file).";
  188.             break;
  189.  
  190.         case invalid_object:
  191.             pszRet = "Invalid object (restoring objects from a file).";
  192.             break;
  193.  
  194.         case invalid_fileformat:
  195.             pszRet = "Invalid fileformat (reading file header).";
  196.             break;
  197.  
  198.             /*
  199.          case :
  200.             pszRet = "";
  201.             break;
  202.          */
  203.  
  204.         default:
  205.             pszRet = "unknown error";
  206.     }
  207.     return pszRet;
  208. }
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217. /* * * * * * * * * * * */
  218. /* k D i v e E r r o r */
  219. /* k D i v e E r r o r */
  220. /* k D i v e E r r o r */
  221. /* * * * * * * * * * * */
  222.  
  223.  
  224. /**
  225.  * Set error, procteded.
  226.  * @returns   Reference to this object.
  227.  * @param     ulError  Error number.
  228.  */
  229. kDiveError &kDiveError::setError(ULONG ulError)
  230. {
  231.     ulDiveError = ulError;
  232.     return *this;
  233. }
  234.  
  235.  
  236.  
  237. /**
  238.  * "not used" - Constructor
  239.  * @param     ulError   Dive errornumber.
  240.  */
  241. kDiveError::kDiveError()
  242. {
  243.     ulDiveError = 0xdeadbeefL;
  244. }
  245.  
  246.  
  247.  
  248. /**
  249.  * Dive error class contstructor.
  250.  * @param     ulError   Dive errornumber.
  251.  */
  252. kDiveError::kDiveError(ULONG ulError)
  253. {
  254.     ulDiveError = ulError;
  255. }
  256.  
  257.  
  258.  
  259. /**
  260.  * Dive error class copy contstructor.
  261.  * @param     obj   Object to copy.
  262.  */
  263. kDiveError::kDiveError(const kDiveError &obj)
  264. {
  265.     ulDiveError = obj.ulDiveError;
  266. }
  267.  
  268.  
  269.  
  270. /**
  271.  * Gets the error number.
  272.  * @returns   Error number.
  273.  */
  274. ULONG kDiveError::getErrorNo() const
  275. {
  276.     return ulDiveError;
  277. }
  278.  
  279.  
  280.  
  281. /**
  282.  * Translates the ulError number into a short error description text. This text is
  283.  * staticly kept in the data segment, and therefor not necessary to free.
  284.  * NOTE: it must not be changed!
  285.  * @returns   Pointer to error description (text)
  286.  * @remark    Do not altern returned text!
  287.  */
  288. const char *kDiveError::getDescription() const
  289. {
  290.     const char *pszRet;
  291.  
  292.     switch (ulDiveError)
  293.     {
  294.         case DIVE_SUCCESS:
  295.             pszRet = "Command completed successfully.";
  296.             break;
  297.  
  298.         case DIVE_ERR_INVALID_INSTANCE:
  299.             pszRet = "The DIVE instance handle specified in the hDiveInst parameter is invalid.";
  300.             break;
  301.  
  302.         case DIVE_ERR_SOURCE_FORMAT:
  303.             pszRet = "The FOURCC of the source format is not a recognized FOURCC.";
  304.             break;
  305.  
  306.         case DIVE_ERR_DESTINATION_FORMAT:
  307.             pszRet = "The FOURCC of the destination format is not a recognized FOURCC.";
  308.             break;
  309.  
  310.         case DIVE_ERR_BLITTER_NOT_SETUP:
  311.             pszRet = "DiveSetupBlitter must be called before a call is made to DiveBlitImage.";
  312.             break;
  313.  
  314.         case DIVE_ERR_INSUFFICIENT_LENGTH:
  315.             pszRet = "The pFormatData of length ulFormatLength (specified in the DIVE_CAPS structure) is not large "
  316.                      "enough for the total number of input and output formats.";
  317.             break;
  318.  
  319.         case DIVE_ERR_TOO_MANY_INSTANCES:
  320.             pszRet = "There are not enough resources for another DIVE instance.";
  321.             break;
  322.  
  323.         case DIVE_ERR_NO_DIRECT_ACCESS:
  324.             pszRet = "The display adapter, display driver, or current video mode does not support direct-to-screen "
  325.                      "access.";
  326.             break;
  327.  
  328.         case DIVE_ERR_NOT_BANK_SWITCHED:
  329.             pszRet = "The display adapter is not a bank-switched adapter.";
  330.             break;
  331.  
  332.         case DIVE_ERR_INVALID_BANK_NUMBER:
  333.             pszRet = "The specified bank number is invalid.";
  334.             break;
  335.  
  336.         case DIVE_ERR_FB_NOT_ACQUIRED:
  337.             pszRet = "The frame buffer was not acquired in this instance.";
  338.             break;
  339.  
  340.         case DIVE_ERR_FB_ALREADY_ACQUIRED:
  341.             pszRet = "The frame buffer has already been acquired by this instance.";
  342.             break;
  343.  
  344.         case DIVE_ERR_ACQUIRE_FAILED:
  345.             pszRet = "The acquire action did not complete successfully.";
  346.             break;
  347.  
  348.         case DIVE_ERR_BANK_SWITCH_FAILED:
  349.             pszRet = "The bank could not be switched to the specified parameters.";
  350.             break;
  351.  
  352.         case DIVE_ERR_DEACQUIRE_FAILED:
  353.             pszRet = "The deacquire action did not complete successfully.";
  354.             break;
  355.  
  356.         case DIVE_ERR_INVALID_PALETTE:
  357.             pszRet = "The palette specified for the source or destination data is invalid.";
  358.             break;
  359.  
  360.         case DIVE_ERR_INVALID_DESTINATION_RECTL:
  361.             pszRet = "The rectangle of the output window is an invalid region.";
  362.             break;
  363.  
  364.         case DIVE_ERR_INVALID_BUFFER_NUMBER:
  365.             pszRet = "The buffer number must be a previously allocated or associated buffer number before it can "
  366.                      "be accessed.";
  367.             break;
  368.  
  369.         case DIVE_ERR_SSMDD_NOT_INSTALLED:
  370.             pszRet = "The device driver SSMDD.SYS is missing from CONFIG.SYS.";
  371.             break;
  372.  
  373.         case DIVE_ERR_BUFFER_ALREADY_ACCESSED:
  374.             pszRet = "The buffer has already been accessed by a previous call.";
  375.             break;
  376.  
  377.         case DIVE_ERR_BUFFER_NOT_ACCESSED:
  378.             pszRet = "The buffer has not been accessed (occurs on systems with accelerator-enabled hardware).";
  379.             break;
  380.  
  381.         case DIVE_ERR_TOO_MANY_BUFFERS:
  382.             pszRet = "There are not enough resources for another DIVE buffer.";
  383.             break;
  384.  
  385.         case DIVE_ERR_ALLOCATION_ERROR:
  386.             pszRet = "The hardware blitter memory allocation failed.";
  387.             break;
  388.  
  389.         case DIVE_ERR_INVALID_LINESIZE:
  390.             pszRet = "The scan line size specified in DiveAllocImageBuffer is invalid.";
  391.             break;
  392.  
  393.         case DIVE_ERR_FATAL_EXCEPTION:
  394.             pszRet = "DIVE is unable to register the exception handler to handle bank switching.";
  395.             break;
  396.  
  397.         case DIVE_ERR_INVALID_CONVERSION:
  398.             pszRet = "The source image format cannot be converted as requested to the destination image format.";
  399.             break;
  400.  
  401.         case DIVE_ERR_VSD_ERROR:
  402.             pszRet = "The vendor-specific driver (VSD) handle is invalid.";
  403.             break;
  404.  
  405.         case DIVE_ERR_COLOR_SUPPORT:
  406.             pszRet = "DIVE does not support any of the available hardware formats (occurs on overlay-accelerated"
  407.                      "systems).";
  408.             break;
  409.  
  410.         case DIVE_ERR_OUT_OF_RANGE:
  411.             pszRet = "Currently, DIVE_ERR_OUT_OF_RANGE is not returned by any function.";
  412.             break;
  413.  
  414.         case DIVE_WARN_NO_SIZE:
  415.             pszRet = "No blitting will occur because either the destination width or height is zero.";
  416.             break;
  417.  
  418.         case (ULONG)alloc_mem_failed:
  419.             pszRet = "DosAllocMem failed.";
  420.             break;
  421.  
  422.         case (ULONG)not_direct_access:
  423.             pszRet = "No Direct Screen access possible.";
  424.             break;
  425.  
  426.         case (ULONG)failed_to_create_framewnd:
  427.             pszRet = "Failed to create kDiveWnd frame window.";
  428.             break;
  429.  
  430.         case (ULONG)failed_to_create_mutex:
  431.             pszRet = "Failed to create kDiveWnd access mutex(hmtx).";
  432.             break;
  433.  
  434.         case (ULONG)failed_to_create_clientwnd:
  435.             pszRet = "Failed to create kDiveWnd client window.";
  436.             break;
  437.  
  438.         case (ULONG)queryRectangles_failed:
  439.             pszRet = "queryRectangles failed.";
  440.             break;
  441.  
  442.         case (ULONG)unsupported_colorencoding:
  443.             pszRet = "kDiveWnd: Unsuported colorencoding.";
  444.             break;
  445.  
  446.         default:
  447.             pszRet = "Unknown error.";
  448.     }
  449.  
  450.     return pszRet;
  451. }
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458. /* * * * * * * * * * */
  459. /* k W i n E r r o r */
  460. /* k W i n E r r o r */
  461. /* k W i n E r r o r */
  462. /* * * * * * * * * * */
  463.  
  464.  
  465. /**
  466.  * Gets the error info string.
  467.  * @param     ulLevel  Error info level. 0 based!
  468.  * @returns   Pointer to string. May be NULL-pointer.
  469.  */
  470. const char *kWinError::getErrorInfoText(ULONG ulLevel /*= 0*/) const
  471. {
  472.     char *pszRet = psz;
  473.  
  474.     if (pErrInfo != NULL)
  475.     {
  476.         if (ulLevel > pErrInfo->cDetailLevel)
  477.             ulLevel = pErrInfo->cDetailLevel - 1;
  478.  
  479.         pszRet = (char*)((int)pErrInfo + ((PUSHORT)((ULONG)pErrInfo + pErrInfo->offaoffszMsg))[ulLevel]);
  480.     }
  481.     return pszRet;
  482. }
  483.  
  484.  
  485.  
  486. /**
  487.  * Set the errid and pErrInfo.
  488.  * @returns   Reference to this object.
  489.  * @param     hab  Handle to anchorblock.
  490.  */
  491. kWinError &kWinError::setError(HAB hab /*= NULLHANDLE*/)
  492. {
  493.     /* cleanup */
  494.     if (pErrInfo != NULL)
  495.     {
  496.         WinFreeErrorInfo(pErrInfo);
  497.         pErrInfo = NULL;
  498.     }
  499.     if (psz != NULL)
  500.     {
  501.         delete psz;
  502.         psz = NULL;
  503.     }
  504.     /* get error info */
  505.     errid    = WinGetLastError(hab);
  506.     pErrInfo = WinGetErrorInfo(hab);
  507.     if (pErrInfo == NULL)
  508.     {
  509.         psz = new char[50];
  510.         if (psz != NULL)
  511.             sprintf(psz, "error no.0x%04x (serv. 0x%x)", (USHORT)errid, (USHORT)(errid >> 16));
  512.     }
  513.     return *this;
  514. }
  515.  
  516.  
  517.  
  518. /**
  519.  * Constructor.
  520.  * @param     hab        Handle to anchor block, NULLHANDLE is allowed and default.
  521.  * @param     fInitiate  TRUE:  Initiate error
  522.  *                       FALSE: do noting.
  523.  */
  524. kWinError::kWinError(HAB hab /*= NULLHANDLE*/, BOOL fInitiate /*= TRUE*/) : fErrInfoDelete(FALSE)
  525. {
  526.     if (fInitiate)
  527.         setError(hab);
  528.     else
  529.     {
  530.         psz = NULL;
  531.         pErrInfo = NULL;
  532.         errid = 0xdeadbeefL;
  533.     }
  534. }
  535.  
  536.  
  537.  
  538. /**
  539.  * Constructor.
  540.  * @param     hab        Handle to anchor block, NULLHANDLE is allowed and default.
  541.  * @param     fInitiate  TRUE:  Initiate error
  542.  *                       FALSE: do noting.
  543.  */
  544. kWinError::kWinError(const kWinError &obj) : fErrInfoDelete(TRUE)
  545. {
  546.     if (obj.psz != NULL)
  547.     {
  548.         psz = new char[strlen(obj.psz) + 1];
  549.         strcpy(psz, obj.psz);
  550.     }
  551.     else
  552.         psz = NULL;
  553.  
  554.     if (obj.pErrInfo != NULL)
  555.     {
  556.         pErrInfo = (PERRINFO)new char[obj.pErrInfo->cbFixedErrInfo];
  557.         memcpy(pErrInfo, obj.pErrInfo, (size_t)obj.pErrInfo->cbFixedErrInfo);
  558.     }
  559.     else
  560.         pErrInfo = NULL;
  561.  
  562.     errid = obj.errid;
  563. }
  564.  
  565. /**
  566.  * Destructor frees error info.
  567.  */
  568. kWinError::~kWinError()
  569. {
  570.     if (pErrInfo != NULL)
  571.     {
  572.         if (fErrInfoDelete)
  573.             delete pErrInfo;
  574.         else
  575.             WinFreeErrorInfo(pErrInfo);
  576.         pErrInfo = NULL;
  577.     }
  578.     if (psz != NULL)
  579.     {
  580.         delete psz;
  581.         psz = NULL;
  582.     }
  583. }
  584.  
  585.  
  586.  
  587. /**
  588.  * Get error description.
  589.  * @returns   Pointer to description text.
  590.  * @remark    return text should be used as read only data.
  591.  */
  592. const char *kWinError::getDescription() const
  593. {
  594.     const char *pszRet = getErrorInfoText(0);
  595.  
  596.     return pszRet == NULL ? "" : pszRet;
  597. }
  598.  
  599.  
  600.  
  601. /**
  602.  * Get error number. This does not include the serverity code.
  603.  * @returns   Error number. This does not include the serverity code.
  604.  */
  605. ULONG kWinError::getErrorNo() const
  606. {
  607.     return(ULONG)(USHORT)errid;
  608. }
  609.  
  610.  
  611.  
  612. /**
  613.  * Get the serverity code.
  614.  * @returns   The serverity code.
  615.  */
  616. ULONG kWinError::getServerity() const
  617. {
  618.     return(ULONG)(USHORT)(errid >> 16);
  619. }
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626. /* * * * * * * * * * */
  627. /* k D o s E r r o r */
  628. /* k D o s E r r o r */
  629. /* k D o s E r r o r */
  630. /* * * * * * * * * * */
  631.  
  632.  
  633. /**
  634.  * Prefetches the error message from the OS/2 message.
  635.  */
  636. VOID kDosError::prefetchMsg()
  637. {
  638.     /* not fetched from message file. */
  639.     if (ulRc == NO_ERROR
  640.         || (ulRc >= 40 && ulRc <= 49)
  641.        )
  642.         return;
  643.  
  644.     /* delete psz if allready allocated */
  645.     if (psz != NULL)
  646.         delete psz;
  647.  
  648.     psz = new char[4096];
  649.     if (psz != NULL)
  650.     {
  651.         ULONG ulRet;
  652.  
  653.         /* try DosGetMessage() first */
  654.         if (DosGetMessage(NULL, 0, psz, 4096, ulRc, "OSO001.MSG", &ulRet) == NO_ERROR
  655.             && ulRet != 0)
  656.         {
  657.             ULONG ulRet2;
  658.  
  659.             psz[ulRet] = '\0';
  660.             strcat(psz, "\n\n");
  661.             if (DosGetMessage(NULL, 0, &psz[strlen(psz)], 4096 - strlen(psz), ulRc, "OSO001H.MSG", &ulRet2) != NO_ERROR)
  662.                 psz[ulRet] = '\0';
  663.             else
  664.                 psz[ulRet+ulRet2] = '\0';
  665.         }
  666.         else
  667.             sprintf(psz, "Error description not found for error %d (0x%x).", ulRc, ulRc);
  668.     }
  669. }
  670.  
  671.  
  672. /**
  673.  * Sets error.
  674.  * @returns   Reference to this object.
  675.  * @param     ulRc  Error number
  676.  */
  677. kDosError &kDosError::setError(ULONG ulRc)
  678. {
  679.     this->ulRc = ulRc;
  680.     prefetchMsg();
  681.     return *this;
  682. }
  683.  
  684.  
  685.  
  686. /**
  687.  * Get error number.
  688.  * @param     ulRc  Error number
  689.  */
  690. kDosError::kDosError(ULONG ulRc) : ulRc(ulRc), psz(NULL)
  691. {
  692.     prefetchMsg();
  693. }
  694.  
  695.  
  696.  
  697. /**
  698.  * Default contructor.
  699.  */
  700. kDosError::kDosError() : ulRc(0xdeadbeefL), psz(NULL)
  701. {
  702. }
  703.  
  704.  
  705.  
  706. /**
  707.  * Copy contructor.
  708.  */
  709. kDosError::kDosError(const kDosError &obj) : ulRc(obj.ulRc)
  710. {
  711.     if (obj.psz != NULL)
  712.     {
  713.         psz = new char[strlen(obj.psz) + 1];
  714.         strcpy(psz, obj.psz);
  715.     }
  716.     else
  717.         psz = NULL;
  718. }
  719.  
  720.  
  721.  
  722. /**
  723.  * Destructor.
  724.  */
  725. kDosError::~kDosError()
  726. {
  727.     if (psz != NULL)
  728.     {
  729.         delete psz;
  730.         psz = NULL;
  731.     }
  732. }
  733.  
  734.  
  735.  
  736. /**
  737.  * Gets error number.
  738.  * @returns   Error number.
  739.  */
  740. const char *kDosError::getDescription() const
  741. {
  742.     const char *pszRet;
  743.  
  744.     switch (ulRc)
  745.     {
  746.         case NO_ERROR:       pszRet = "no error."; break;
  747.  
  748.             /* errornumbers which are not in message files: - remeber to exclude these in prefetchMsg()*/
  749.         case 40:             pszRet = "ERROR_BAD_COMMAND"; break;
  750.         case 41:             pszRet = "ERROR_CRC"; break;
  751.         case 42:             pszRet = "ERROR_BAD_LENGTH"; break;
  752.         case 43:             pszRet = "ERROR_SEEK"; break;
  753.         case 44:             pszRet = "ERROR_NOT_DOS_DISK"; break;
  754.         case 45:             pszRet = "ERROR_SECTOR_NOT_FOUND"; break;
  755.         case 46:             pszRet = "ERROR_OUT_OF_PAPER"; break;
  756.         case 47:             pszRet = "ERROR_WRITE_FAULT"; break;
  757.         case 48:             pszRet = "ERROR_READ_FAULT"; break;
  758.         case 49:             pszRet = "ERROR_GEN_FAILURE"; break;
  759.  
  760.  
  761.         default:
  762.             if (psz == NULL)
  763.                 return "IPE! Unknown error.";
  764.             else
  765.                 pszRet = psz;
  766.     }
  767.     return pszRet;
  768. }
  769.  
  770.  
  771. /**
  772.  * Gets error number.
  773.  * @returns   Error number.
  774.  */
  775. ULONG kDosError::getErrorNo() const
  776. {
  777.     return ulRc;
  778. }
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785. /* * * * * * * */
  786. /* k E r r o r */
  787. /* k E r r o r */
  788. /* k E r r o r */
  789. /* * * * * * * */
  790.  
  791.  
  792. /**
  793.  * Constructor.
  794.  * @param     ulErrorNo    Error number
  795.  * @param     ulErrorType  Error type.
  796.  */
  797. kError::kError(unsigned long ulErrorNo, unsigned long ulErrorType/* = kError::my*/) :
  798. kWinError(NULLHANDLE, FALSE),
  799. psz(NULL)
  800. {
  801.     setError(ulErrorNo, ulErrorType);
  802. }
  803.  
  804.  
  805.  
  806. /**
  807.  * Copy constructor.
  808.  * @param     obj  Object to copy.
  809.  */
  810. kError::kError(const kError &obj) : kMyError(obj), kDiveError(obj), kWinError(obj), kDosError(obj)
  811. {
  812.     if (obj.psz != NULL)
  813.     {
  814.         psz = new char[strlen(obj.psz) + 1];
  815.         strcpy(psz, obj.psz);
  816.     }
  817.     else
  818.         psz = NULL;
  819.     ulErrorType = obj.ulErrorType;
  820. }
  821.  
  822.  
  823.  
  824. /**
  825.  * Destructor - frees psz.
  826.  */
  827. kError::~kError()
  828. {
  829.     if (psz != NULL)
  830.     {
  831.         delete psz;
  832.         psz = NULL;
  833.     }
  834. }
  835.  
  836.  
  837.  
  838. /**
  839.  * Set the error.
  840.  * @param     ulErrorNo    Error number
  841.  * @param     ulErrorType  Error type.
  842.  * @returns   Reference to this object.
  843.  */
  844. kError &kError::setError(unsigned long ulErrorNo, unsigned long ulErrorType /*= kError::my*/)
  845. {
  846.     switch (ulErrorType)
  847.     {
  848.         case my:
  849.             kMyError::setError(ulErrorNo);
  850.             break;
  851.  
  852.         case dive:
  853.             kDiveError::setError(ulErrorNo);
  854.             break;
  855.  
  856.         case win:
  857.             kMyError::setError(ulErrorNo);
  858.             kWinError::setError();
  859.  
  860.             if (psz != NULL)
  861.                 delete psz;
  862.             psz = new char[strlen(kMyError::getDescription()) + strlen(kWinError::getDescription()) + 3 + 1];
  863.             if (psz != NULL)
  864.                 strcat(strcat(strcpy(psz, kMyError::getDescription()), " - "), kWinError::getDescription());
  865.             break;
  866.  
  867.         case dos:
  868.             kDosError::setError(ulErrorNo);
  869.             break;
  870.     }
  871.     this->ulErrorType = ulErrorType;
  872.     return *this;
  873. }
  874.  
  875.  
  876.  
  877. /**
  878.  * Get the error description.
  879.  * @returns   Pointer to a _read-only_ string.
  880.  */
  881. const char *kError::getDescription() const
  882. {
  883.     const char *pszRet;
  884.  
  885.     switch (ulErrorType)
  886.     {
  887.         case my:
  888.             pszRet = kMyError::getDescription();
  889.             break;
  890.  
  891.         case dive:
  892.             pszRet = kDiveError::getDescription();
  893.             break;
  894.  
  895.         case win:
  896.             if (psz != NULL)
  897.                 pszRet = psz;
  898.             else
  899.                 pszRet = "kError: new failed.";
  900.             break;
  901.  
  902.         case dos:
  903.             pszRet = kDosError::getDescription();
  904.             break;
  905.  
  906.         default:
  907.             pszRet = "IPE! Invalid error type.";
  908.     }
  909.  
  910.     return pszRet;
  911. }
  912.  
  913.  
  914.  
  915. /**
  916.  * Gets the error number.
  917.  * @returns   Error number
  918.  */
  919. unsigned long kError::getErrorNo() const
  920. {
  921.     switch (ulErrorType)
  922.     {
  923.         case my:
  924.             return kMyError::getErrorNo();
  925.  
  926.         case dive:
  927.             return kMyError::getErrorNo();
  928.  
  929.         case win:
  930.             return kMyError::getErrorNo();
  931.  
  932.         case dos:
  933.             return kMyError::getErrorNo();
  934.     }
  935.  
  936.     return 0xdeadbeefL;
  937. }
  938.  
  939.  
  940.  
  941. /**
  942.  * Gets error type.
  943.  * @returns   Error type.
  944.  */
  945. unsigned long  kError::getErrorType() const
  946. {
  947.     return ulErrorType;
  948. }
  949.  
  950.  
  951.  
  952. /**
  953.  * Shows the error description, using WinMessageBox();
  954.  * @returns   TRUE/FALSE according to result.
  955.  * @param     pszTitle  Message box title. May NOT be NULL.
  956.  * @remark    Should not be used in text mode.
  957.  */
  958. BOOL kError::showError(const char *pszTitle) const
  959. {
  960.     const char *pszDscr;
  961.  
  962.     pszDscr = getDescription();
  963.  
  964.     return WinMessageBox(HWND_DESKTOP,
  965.                          HWND_DESKTOP,
  966.                          pszDscr,
  967.                          pszTitle,
  968.                          0,
  969.                          MB_OK | MB_ERROR | MB_MOVEABLE )
  970.         != MBID_ERROR;
  971. }
  972.  
  973.  
  974.  
  975. /**
  976.  * Writes info to the log.
  977.  * @return    returns reference to this object.
  978.  * @param     pszFunction
  979.  * @param     ulLine
  980.  */
  981. const kError &kError::logError(const char *pszFunction, ULONG ulLine) const
  982. {
  983.     #ifdef USE_KLIB
  984.     kDebugLog("%s - %d: %s (errorno %d)\r\n", pszFunction, ulLine, getDescription(), getErrorNo());
  985.     #else
  986.     pszFunction = pszFunction;
  987.     ulLine = ulLine;
  988.     #endif
  989.     return *this;
  990. }
  991.  
  992.  
  993.  
  994. /**
  995.  * Writes info to the log.
  996.  * @return    returns reference to this object.
  997.  * @param     pszUser
  998.  * @param     pszFunction
  999.  * @param     ulLine
  1000.  */
  1001. const kError &kError::logErrorStr(const char *pszUser, const char *pszFunction, ULONG ulLine) const
  1002. {
  1003.     #ifdef USE_KLIB
  1004.     kDebugLog("%s - %d: %s - %s (errorno %d)\r\n", pszFunction, ulLine, pszUser, getDescription(), getErrorNo());
  1005.     #else
  1006.     pszUser = pszUser;
  1007.     pszFunction = pszFunction;
  1008.     ulLine = ulLine;
  1009.     #endif
  1010.     return *this;
  1011. }
  1012.  
  1013.