home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / warptlk3.zip / TOOLKIT / SAMPLES / PM / PORTING / IMG_UTIL.C < prev    next >
C/C++ Source or Header  |  1995-08-24  |  11KB  |  328 lines

  1. /**************************************************************************
  2.  *  File name  :  img_util.c
  3.  *
  4.  *  Description:  This module contains the code for allocating/freeing
  5.  *                memory for the image data, and for image file-related
  6.  *                options.  It contains these functions:
  7.  *
  8.  *                UtilMemoryAllocate(usBufSiz, ppByte)
  9.  *                UtilMemoryFree(pByte)
  10.  *                UtilFindFileSize(pszFilename)
  11.  *                UtilGetFileHandle(pszFilename, phfile)
  12.  *                UtilUpdateTitleText(hab, hwnd, pszFullFile)
  13.  *
  14.  *  Concepts   :  32-bit memory allocation
  15.  *
  16.  *  API's      :  DosAllocSeg  [in 16 bit]  or DosAllocMem  [in 32 bit]
  17.  *                DosFreeSeg   [in 16 bit]  or DosFreeMem   [in 32 bit]
  18.  *                DosFindFirst
  19.  *                DosOpen
  20.  *                WinLoadString
  21.  *                WinSetWindowText
  22.  *                WinWindowFromID
  23.  *
  24.  *  Required
  25.  *    Files    :  OS2.H, STRING.H, IMG_MAIN.H, IMG_XTRN.H
  26.  *
  27.  *  Copyright (C) 1991, 1994 IBM Corporation
  28.  *
  29.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  30.  *      sample code created by IBM Corporation. This sample code is not
  31.  *      part of any standard or IBM product and is provided to you solely
  32.  *      for  the purpose of assisting you in the development of your
  33.  *      applications.  The code is provided "AS IS", without
  34.  *      warranty of any kind.  IBM shall not be liable for any damages
  35.  *      arising out of your use of the sample code, even if they have been
  36.  *      advised of the possibility of such damages.                                                    *
  37.  *************************************************************************/
  38.  
  39. /*
  40.  *  Include files, macros, defined constants, and externs
  41.  */
  42.  
  43. #define INCL_DOSERRORS
  44. #define INCL_WINFRAMEMGR
  45. #define INCL_WINSWITCHLIST
  46. #define INCL_WINSTDFILE
  47. #include <os2.h>
  48. #include <string.h>
  49.  
  50. #include "img_main.h"
  51. #include "img_xtrn.h"
  52.  
  53. #ifdef PORT_S116
  54. LONG FAR pascal UtilFindFileSize(PSZ pszFilename);
  55. BOOL FAR pascal UtilMemoryAllocate(USHORT cbSize, PBYTE FAR *ppByte);
  56. BOOL FAR pascal UtilGetFileHandle(PSZ pszFilename, PHFILE phfile);
  57. VOID FAR pascal UtilUpdateTitleText(HAB hab, HWND hwnd, PSZ pszFullFile);
  58. VOID FAR pascal UtilMemoryFree(SEL sel);
  59. #endif
  60. /**************************************************************************
  61.  *
  62.  *  Name       : UtilMemoryAllocate(usBufSiz, ppByte)
  63.  *
  64.  *  Description: Allocates memory for the image
  65.  *
  66.  *  Concepts   : Routine is called whenever memory needs to be
  67.  *               allocated to store a new image.
  68.  *
  69.  *  API's      :  DosAllocSeg  [in 16 bit]  or DosAllocMem  [in 32 bit]
  70.  *
  71.  *  Parameters : usBufSiz = how many bytes to allocate
  72.  *               ppByte   = pointer to the first byte of the memory block
  73.  *
  74.  *  Return     : TRUE  - if memory successfully allocated
  75.  *               FALSE - if it failed to allocate memory
  76.  *
  77.  *************************************************************************/
  78. #if (defined(PORT_16) || defined(PORT_32))
  79. BOOL UtilMemoryAllocate(USHORT usBufSiz, PBYTE FAR *ppByte)
  80. #else
  81. BOOL FAR pascal UtilMemoryAllocate(USHORT usBufSiz, PBYTE FAR *ppByte)
  82. #endif
  83. {
  84. #if (defined(PORT_16) || defined(PORT_S116))
  85.     SEL sel;
  86.  
  87.     if (DosAllocSeg(usBufSiz, &sel, SEG_NONSHARED) == NO_ERROR)
  88.     {
  89.         *ppByte = (BYTE *)MAKEP(sel, 0);
  90.         return TRUE;
  91.     }
  92.     else
  93.         return FALSE;
  94. #else
  95.  
  96.     return (BOOL)(!DosAllocMem((PPVOID)ppByte,
  97.                     (ULONG)usBufSiz, PAG_READ | PAG_WRITE | PAG_COMMIT));
  98. #endif
  99.  
  100. }  /* End of UtilMemoryAllocate  */
  101.  
  102. /**************************************************************************
  103.  *
  104.  *  Name       : UtilMemoryFree(pByte)
  105.  *
  106.  *  Description: Frees memory of the image
  107.  *
  108.  *  Concepts   : Routine is called at cleanup time during the processing
  109.  *               of a WM_DESTROY message, and before loading a new image
  110.  *
  111.  *  API's      :  DosFreeSeg  [in 16 bit]  or DosFreeMem  [in 32 bit]
  112.  *
  113.  *  Parameters : pByte   = pointer to the first byte of the memory block
  114.  *
  115.  *  Return     : [none]
  116.  *
  117.  *************************************************************************/
  118. #ifdef PORT_32
  119. VOID UtilMemoryFree(pByte)
  120. PBYTE pByte;
  121. {
  122.    DosFreeMem(pByte);
  123. }   /* End of UtilMemoryFree */
  124. #else
  125. #ifdef PORT_16
  126. VOID UtilMemoryFree(SEL sel)
  127. #else
  128. VOID FAR pascal UtilMemoryFree(SEL sel)
  129. #endif
  130. {
  131.    DosFreeSeg(sel);
  132. }   /* End of UtilMemoryFree */
  133. #endif
  134.  
  135. /**************************************************************************
  136.  *
  137.  *  Name       : UtilFindFileSize(pszFilename)
  138.  *
  139.  *  Description: Finds the size of a loaded image
  140.  *
  141.  *  Concepts   : Called to determine the size of the image in order
  142.  *               to allocate sufficient storage for it
  143.  *
  144.  *  API's      : DosFindFirst
  145.  *
  146.  *  Parameters : pszFilename = a pointer to a string, the file name
  147.  *
  148.  *  Return     : filesize = max(0xFFFF, filesize)
  149.  *
  150.  *************************************************************************/
  151. #if (defined(PORT_16) || defined(PORT_32))
  152. LONG UtilFindFileSize(pszFilename)
  153. #else
  154. LONG FAR pascal UtilFindFileSize(pszFilename)
  155. #endif
  156. PSZ pszFilename;
  157. {
  158.     FILEFINDBUF ffb;           /* DOS file-search results buffer    */
  159.     HDIR hdir = 0xffffffff;    /* allocate a handle to caller       */
  160. #if (defined(PORT_16) || defined(PORT_S116))
  161.     USHORT usAttrib  = 0;
  162.     USHORT cSearch   = 1;
  163.     if (DosFindFirst(pszFilename,  /* filename from Open Dialog */
  164.                      &hdir,
  165.                      usAttrib,
  166.                      &ffb,
  167.                      (USHORT)(sizeof(ffb) * cSearch),
  168.                      &cSearch,
  169.                      0L) == NO_ERROR)  /* reserved must be NULL */
  170. #else
  171.     ULONG ulAttrib  = 0;
  172.     ULONG cSearch   = 1;
  173.     if (DosFindFirst(pszFilename,  /* filename from Open Dialog */
  174.                      &hdir,
  175.                      ulAttrib,
  176.                      &ffb,
  177.                      sizeof(ffb) * cSearch,
  178.                      &cSearch,
  179.                      1L) == NO_ERROR)  /* number of files to locate */
  180. #endif
  181.        /*
  182.         * Obtain the size of the image. The amount of storage
  183.         * required depends on the size of the image, which is
  184.         * recorded in the image-file header.
  185.         * If the image is greater than 64KB, then only the first 64KB of
  186.         * image data is loaded.
  187.         */
  188.         return (ffb.cbFileAlloc > 0xFFFFL) ? 0xFFFFL : ffb.cbFileAlloc;
  189.     else
  190.         return 0L;
  191.  
  192. }   /* End of UtilFindFileSize */
  193.  
  194. /**************************************************************************
  195.  *
  196.  *  Name       : UtilGetFileHandle(pszFilename, phfile)
  197.  *
  198.  *  Description: Opens the file passed in and returns its file handle
  199.  *
  200.  *  Concepts   : Called in order the read in the image data from a file
  201.  *               so that it can be displayed
  202.  *
  203.  *  API's      : DosOpen
  204.  *
  205.  *  Parameters : pszFilename = a pointer to a string, the file name
  206.  *               phfile      = a pointer to the file handle
  207.  *
  208.  *  Return     : the value of the DosOpen return
  209.  *
  210.  *************************************************************************/
  211. #if (defined(PORT_16) || defined(PORT_32))
  212. BOOL UtilGetFileHandle(pszFilename, phfile)
  213. #else
  214. BOOL FAR pascal UtilGetFileHandle(pszFilename, phfile)
  215. #endif
  216. PSZ pszFilename;
  217. PHFILE phfile;
  218. {
  219. #if (defined(PORT_16) || defined(PORT_S116))
  220.     USHORT usAction;
  221.  
  222.     return DosOpen(pszFilename,           /* filename from open dialog */
  223.                    phfile,                     /* file handle returned */
  224.                    (PUSHORT)&usAction,
  225.                    0L,
  226.                    FILE_NORMAL,
  227.                    FILE_OPEN,
  228.                    OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
  229.                    0L) == NO_ERROR;                    /* must be zero */
  230. #else
  231.     ULONG ulAction;
  232.  
  233.     return (BOOL)(!DosOpen(pszFilename,       /* filename from open dialog */
  234.                    phfile,                    /* file handle returned */
  235.                    &ulAction,
  236.                    0L,
  237.                    FILE_NORMAL,
  238.                    FILE_OPEN,
  239.                    OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
  240.                    (PEAOP2)NULL)) ;                    /* must be zero */
  241. #endif
  242.  
  243. }   /* End of UtilGetFileHandle  */
  244.  
  245. /**************************************************************************
  246.  *
  247.  *  Name       : UtilUpdateTitleText(hab, hwnd, pszFullFile)
  248.  *
  249.  *  Description: Updates the text in the main window's title bar to
  250.  *               display the app name, followed by the separator,
  251.  *               followed by the file name
  252.  *
  253.  *  Concepts   : Called at init time and when the text file is changed
  254.  *
  255.  *  API's      : WinLoadString
  256.  *               WinSetWindowText
  257.  *               WinWindowFromID
  258.  *
  259.  *  Parameters : hab         = anchor block handle
  260.  *               hwnd        = window handle
  261.  *               pszFullFile = a string:  the path and filename
  262.  *
  263.  *  Return     : [none]
  264.  *
  265.  *************************************************************************/
  266. #if (defined(PORT_16) || defined(PORT_32))
  267. VOID UtilUpdateTitleText(hab, hwnd, pszFullFile)
  268. #else
  269. VOID FAR pascal UtilUpdateTitleText(hab, hwnd, pszFullFile)
  270. #endif
  271. HAB  hab;
  272. HWND hwnd;
  273. PSZ  pszFullFile;
  274. {
  275.     CHAR szBuf[MAXNAMEL];
  276.     CHAR szSeparator[TITLESEPARATORLEN+1];
  277.     CHAR szUntitled[MESSAGELEN];
  278.     PSZ pszFileName;
  279. #ifdef PORT_S116
  280.     PCHAR pch;
  281.     USHORT cch;
  282. #endif
  283.  
  284.     WinLoadString(hab, (HMODULE)NULL, IDS_APPNAME, MAXNAMEL, szBuf);
  285.     WinLoadString(hab, (HMODULE)NULL, IDS_TITLEBARSEPARATOR,
  286.                   TITLESEPARATORLEN, szSeparator);
  287.  
  288.     strcat(szBuf, szSeparator);
  289.  
  290. #if (defined(PORT_16) || defined(PORT_32))
  291.    /*
  292.     * if filename is valid it will be F.Q so search for
  293.     * final backslash, otherwise use the Untitled string.
  294.     */
  295.     if (NULL != (pszFileName = strrchr(pszFullFile, (int)'\\')))
  296.         pszFileName++;
  297. #else
  298.     /* set pointer to end of string and count characters in string */
  299.     pch = pszFullFile;
  300.     cch = 0;
  301.     while (*pch++)
  302.         cch++;
  303.  
  304.     /* must cater to the null string in this test */
  305.     if (cch > 0)
  306.         pch--;
  307.  
  308.     /* search for final backslash in F.Q name if it exists */
  309.     while (*--pch && *pch != '\\' && cch-- >= 0)  ;
  310.  
  311.    /*
  312.     * if filename is valid (ie '\' is found in F.Q name,
  313.     * then use filename, otherwise use the Untitled string.
  314.     */
  315.     if (*pch == '\\')
  316.         pszFileName = ++pch;
  317. #endif
  318.     else
  319.     {
  320.         /* load "untitled" string */
  321.         WinLoadString(hab, 0, IDS_UNTITLED, MESSAGELEN, szUntitled);
  322.         pszFileName = (PSZ)szUntitled;
  323.     }
  324.     strcat(szBuf, pszFileName);  /* szBuf = Image Sample Program - IMAGE.IMG */
  325.     WinSetWindowText(WinWindowFromID(hwnd, FID_TITLEBAR), szBuf);
  326. }   /* End of UtilUpdateTitleText  */
  327. /***************************  End of img_util.c  *************************/
  328.