Annotation of os232sdk/toolkt20/c/samples/image/img_util.c, revision 1.1.1.1

1.1       root        1: /*==============================================================*\
                      2:  *  img_util.c - Utility routines for the IMAGE sample
                      3:  *
                      4:  *      Created 1989, 1990 IBM, Microsoft Corp.
                      5:  *--------------------------------------------------------------*
                      6:  *
                      7:  *  This module contains the code for allocating/freeing memory
                      8:  *  for the image data, and for image file-related options
                      9:  *
                     10:  *--------------------------------------------------------------*
                     11:  *
                     12:  *  This source file contains the following functions:
                     13:  *
                     14:  *      UtilMemoryAllocate()
                     15:  *      UtilMemoryFree()
                     16:  *      UtilFindFileSize()
                     17:  *      UtilGetFileHandle()
                     18:  *      UtilUpdateTitleText()
                     19:  *
                     20: \*==============================================================*/
                     21: 
                     22: /*--------------------------------------------------------------*\
                     23:  *  Include files, macros, defined constants, and externs       *
                     24: \*--------------------------------------------------------------*/
                     25: 
                     26: #define INCL_DOSERRORS
                     27: #define INCL_WINFRAMEMGR
                     28: #define INCL_WINSWITCHLIST
                     29: #include <os2.h>
                     30: #include <string.h>
                     31: #include "img_main.h"
                     32: #include "img_xtrn.h"
                     33: 
                     34: #ifdef PORT_S116
                     35: LONG FAR pascal UtilFindFileSize(PSZ pszFilename);
                     36: BOOL FAR pascal UtilMemoryAllocate(USHORT cbSize, PBYTE FAR *ppByte);
                     37: BOOL FAR pascal UtilGetFileHandle(PSZ pszFilename, PHFILE phfile);
                     38: VOID FAR pascal UtilUpdateTitleText(HAB hab, HWND hwnd, PSZ pszFullFile);
                     39: VOID FAR pascal UtilMemoryFree(SEL sel);
                     40: #endif
                     41: /****************************************************************\
                     42:  *  Allocate memory routine                                     *
                     43:  *--------------------------------------------------------------*
                     44:  *                                                              *
                     45:  *  Name:    UtilMemoryAllocate(usBufSiz, ppByte)               *
                     46:  *                                                              *
                     47:  *  Purpose: To allocate memory for the image                   *
                     48:  *                                                              *
                     49:  *  Usage:  Routine is called whenever memory needs to be       *
                     50:  *          allocated to store a new image.                     *
                     51:  *                                                              *
                     52:  *  Method:                                                     *
                     53:  *                                                              *
                     54:  *  Returns:                                                    *
                     55:  *           TRUE  - if memory successfully allocated           *
                     56:  *           FALSE - if failed to allocate memory               *
                     57: \****************************************************************/
                     58: #if (defined(PORT_16) || defined(PORT_32))
                     59: BOOL UtilMemoryAllocate(usBufSiz, ppByte)
                     60: #else
                     61: BOOL FAR pascal UtilMemoryAllocate(usBufSiz, ppByte)
                     62: #endif
                     63: USHORT usBufSiz;
                     64: PBYTE FAR *ppByte;
                     65: {
                     66: #if (defined(PORT_16) || defined(PORT_S116))
                     67:     SEL sel;
                     68: 
                     69:     if (DosAllocSeg(usBufSiz, &sel, SEG_NONSHARED) == NO_ERROR) {
                     70:         *ppByte = (BYTE *)MAKEP(sel, 0);
                     71:         return TRUE;
                     72:     } else
                     73:         return FALSE;
                     74: #else
                     75: 
                     76:     return DosAllocMem(ppByte,
                     77:                        usBufSiz,
                     78:                        PAG_READ | PAG_WRITE | PAG_COMMIT) == NO_ERROR;
                     79: #endif
                     80: 
                     81: }   /* UtilMemoryAllocate() */
                     82: 
                     83: /****************************************************************\
                     84:  *  Free up allocated memory
                     85:  *--------------------------------------------------------------
                     86:  *
                     87:  *  Name:   UtilMemoryFree()
                     88:  *
                     89:  *  Purpose: Releases the memory held for the image
                     90:  *
                     91:  *  Usage:  called at cleanup time during the processing of the
                     92:  *          WM_DESTROY message & during loading of new image
                     93:  *
                     94:  *  Method:
                     95:  *
                     96:  *  Returns:
                     97:  *
                     98: \****************************************************************/
                     99: 
                    100: #ifdef PORT_32
                    101: VOID UtilMemoryFree(pByte)
                    102: PBYTE pByte;
                    103: {
                    104:    DosFreeMem(pByte);
                    105: 
                    106: }   /* UtilMemoryFree() */
                    107: #else
                    108: #ifdef PORT_16
                    109: VOID UtilMemoryFree(sel)
                    110: #else
                    111: VOID FAR pascal UtilMemoryFree(sel)
                    112: #endif
                    113: SEL sel;
                    114: {
                    115:     DosFreeSeg(sel);
                    116: 
                    117: }   /* UtilMemoryFree() */
                    118: #endif
                    119: 
                    120: /****************************************************************\
                    121:  *  Find File Size
                    122:  *--------------------------------------------------------------
                    123:  *
                    124:  *  Name:   UtilFindFileSize()
                    125:  *
                    126:  *  Purpose: To find the size of the file passed in
                    127:  *
                    128:  *  Usage:  Called to determine the size of the image in order
                    129:  *          to allocate sufficient storage for it
                    130:  *
                    131:  *  Method: filesize = min(0xFFFF, filesize)
                    132:  *
                    133:  *  Returns:
                    134:  *
                    135: \****************************************************************/
                    136: #if (defined(PORT_16) || defined(PORT_32))
                    137: LONG UtilFindFileSize(pszFilename)
                    138: #else
                    139: LONG FAR pascal UtilFindFileSize(pszFilename)
                    140: #endif
                    141: PSZ pszFilename;
                    142: {
                    143:     FILEFINDBUF ffb;       /* DOS file-search results buffer    */
                    144:     HDIR hdir = 0xffff;    /* allocate a handle to caller       */
                    145: #if (defined(PORT_16) || defined(PORT_S116))
                    146:     USHORT usAttrib  = 0;
                    147:     USHORT cSearch   = 1;
                    148:     if (DosFindFirst(pszFilename,  /* filename from Open Dialog */
                    149:                      &hdir,
                    150:                      usAttrib,
                    151:                      &ffb,
                    152:                      (USHORT)(sizeof(ffb) * cSearch),
                    153:                      &cSearch,
                    154:                      0L) == NO_ERROR)  /* reserved must be NULL */
                    155: #else
                    156:     ULONG ulAttrib  = 0;
                    157:     ULONG cSearch   = 1;
                    158:     if (DosFindFirst(pszFilename,  /* filename from Open Dialog */
                    159:                      &hdir,
                    160:                      ulAttrib,
                    161:                      &ffb,
                    162:                      (USHORT)(sizeof(ffb) * cSearch),
                    163:                      &cSearch,
                    164:                      1L) == NO_ERROR)  /* no of files to locate */
                    165: #endif
                    166:        /*
                    167:         * Obtain the size of the image. The amount of storage
                    168:         * required depends on the size of the image, which is
                    169:         * recorded in the image-file header.
                    170:         * If the image is greater than 64KB, then only the first 64KB of
                    171:         * image data is loaded.
                    172:         */
                    173:         return (ffb.cbFile > 0xFFFFL) ? 0xFFFFL : ffb.cbFile;
                    174:     else
                    175:         return 0L;
                    176: 
                    177: }   /* UtilFindFileSize() */
                    178: 
                    179: /****************************************************************\
                    180:  *  Get File Handle
                    181:  *--------------------------------------------------------------
                    182:  *
                    183:  *  Name:   UtilGetFileHandle(pszFilename, phfile)
                    184:  *
                    185:  *  Purpose: To open the file passed in and return the file handle
                    186:  *
                    187:  *  Usage:  Called in order the read in the image data from a file
                    188:  *          so that it can be displayed
                    189:  *
                    190:  *  Method:
                    191:  *
                    192:  *  Returns:
                    193:  *
                    194: \****************************************************************/
                    195: #if (defined(PORT_16) || defined(PORT_32))
                    196: BOOL UtilGetFileHandle(pszFilename, phfile)
                    197: #else
                    198: BOOL FAR pascal UtilGetFileHandle(pszFilename, phfile)
                    199: #endif
                    200: PSZ pszFilename;
                    201: PHFILE phfile;
                    202: {
                    203: #if (defined(PORT_16) || defined(PORT_S116))
                    204:     USHORT usAction;
                    205:     return DosOpen(pszFilename,           /* filename from open dialog */
                    206:                    phfile,                     /* file handle returned */
                    207:                    (PUSHORT)&usAction,
                    208:                    0L,
                    209:                    FILE_NORMAL,
                    210:                    FILE_OPEN,
                    211:                    OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
                    212:                    0L) == NO_ERROR;                    /* must be zero */
                    213: #else
                    214:     ULONG ulAction;
                    215:     return DosOpen(pszFilename,           /* filename from open dialog */
                    216:                    phfile,                     /* file handle returned */
                    217:                    (PULONG)&ulAction,
                    218:                    0L,
                    219:                    FILE_NORMAL,
                    220:                    FILE_OPEN,
                    221:                    OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
                    222:                    0L) == NO_ERROR;                    /* must be zero */
                    223: #endif
                    224: 
                    225: }   /* UtilGetFileHandle() */
                    226: 
                    227: /****************************************************************\
                    228:  *  Appends the app name to the title bar text
                    229:  *--------------------------------------------------------------
                    230:  *
                    231:  *  Name:   UtilUpdateTitleText(hab, hwnd, pszFullFile)
                    232:  *
                    233:  *  Purpose: Updates the text in the main window's title bar to
                    234:  *          display the app name, followed by the separator,
                    235:  *          followed by the file name
                    236:  *
                    237:  *  Usage:  called at init time and when the text file is changed
                    238:  *
                    239:  *  Method:  gets the program name, appends the separator, and
                    240:  *          appends the file name.
                    241:  *
                    242:  *  Returns:
                    243:  *
                    244: \****************************************************************/
                    245: #if (defined(PORT_16) || defined(PORT_32))
                    246: VOID UtilUpdateTitleText(hab, hwnd, pszFullFile)
                    247: #else
                    248: VOID FAR pascal UtilUpdateTitleText(hab, hwnd, pszFullFile)
                    249: #endif
                    250: HAB  hab;
                    251: HWND hwnd;
                    252: PSZ  pszFullFile;
                    253: {
                    254:     CHAR szBuf[MAXNAMEL];
                    255:     CHAR szSeparator[TITLESEPARATORLEN+1];
                    256:     CHAR szUntitled[MESSAGELEN];
                    257:     PSZ pszFileName;
                    258: #ifdef PORT_S116
                    259:     PCHAR pch;
                    260:     USHORT cch;
                    261: #endif
                    262: 
                    263:     /* get current task title */
                    264:     WinQueryTaskTitle(NULL, szBuf, MAXNAMEL);    /* szBuf = IMAGE.EXE */
                    265: 
                    266:     WinLoadString(hab, NULL, IDS_TITLEBARSEPARATOR,
                    267:                   TITLESEPARATORLEN, szSeparator);
                    268: 
                    269:     strcat(szBuf, szSeparator);                /* szBuf = IMAGE.EXE - */
                    270:    /*
                    271:     * ??? ADG comment here ???
                    272:     */
                    273: #if (defined(PORT_16) || defined(PORT_32))
                    274:    /*
                    275:     * if filename is valid it will be F.Q so search for
                    276:     * final backslash, otherwise use the Untitled string.
                    277:     */
                    278:     if (pszFileName = strrchr(pszFullFile, '\\'))
                    279:         pszFileName++;
                    280: #else
                    281:     /* set pointer to end of string and count characters in string */
                    282:     pch = pszFullFile;
                    283:     cch = 0;
                    284:     while (*pch++)
                    285:         cch++;
                    286: 
                    287:     /* must cater for the null string in this test */
                    288:     if (cch > 0)
                    289:         pch--;
                    290: 
                    291:     /* search for final backslash in F.Q name if it exists */
                    292:     while (*--pch && *pch != '\\' && cch-- >= 0);
                    293: 
                    294:    /*
                    295:     * if filename is valid (ie '\' is found in F.Q name,
                    296:     * then use filename, otherwise use the Untitled string.
                    297:     */
                    298:     if (*pch == '\\')
                    299:         pszFileName = ++pch;
                    300: #endif
                    301:     else {
                    302:         /* load "untitled" string */
                    303:         WinLoadString(hab, NULL, IDS_UNTITLED, MESSAGELEN, szUntitled);
                    304:         pszFileName = (PSZ)szUntitled;
                    305:     }
                    306:     strcat(szBuf, pszFileName);      /* szBuf = IMAGE.EXE - IMAGE.IMG */
                    307:     WinSetWindowText(WinWindowFromID(hwnd, FID_TITLEBAR), szBuf);
                    308: 
                    309: }   /* UtilUpdateTitleText() */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.