|
|
Microsoft OS/2 SDK 2.0 05-30-1990
/*==============================================================*\
* img_util.c - Utility routines for the IMAGE sample
*
* Created 1989, 1990 IBM, Microsoft Corp.
*--------------------------------------------------------------*
*
* This module contains the code for allocating/freeing memory
* for the image data, and for image file-related options
*
*--------------------------------------------------------------*
*
* This source file contains the following functions:
*
* UtilMemoryAllocate()
* UtilMemoryFree()
* UtilFindFileSize()
* UtilGetFileHandle()
* UtilUpdateTitleText()
*
\*==============================================================*/
/*--------------------------------------------------------------*\
* Include files, macros, defined constants, and externs *
\*--------------------------------------------------------------*/
#define INCL_DOSERRORS
#define INCL_WINFRAMEMGR
#define INCL_WINSWITCHLIST
#include <os2.h>
#include <string.h>
#include "img_main.h"
#include "img_xtrn.h"
#ifdef PORT_S116
LONG FAR pascal UtilFindFileSize(PSZ pszFilename);
BOOL FAR pascal UtilMemoryAllocate(USHORT cbSize, PBYTE FAR *ppByte);
BOOL FAR pascal UtilGetFileHandle(PSZ pszFilename, PHFILE phfile);
VOID FAR pascal UtilUpdateTitleText(HAB hab, HWND hwnd, PSZ pszFullFile);
VOID FAR pascal UtilMemoryFree(SEL sel);
#endif
/****************************************************************\
* Allocate memory routine *
*--------------------------------------------------------------*
* *
* Name: UtilMemoryAllocate(usBufSiz, ppByte) *
* *
* Purpose: To allocate memory for the image *
* *
* Usage: Routine is called whenever memory needs to be *
* allocated to store a new image. *
* *
* Method: *
* *
* Returns: *
* TRUE - if memory successfully allocated *
* FALSE - if failed to allocate memory *
\****************************************************************/
#if (defined(PORT_16) || defined(PORT_32))
BOOL UtilMemoryAllocate(usBufSiz, ppByte)
#else
BOOL FAR pascal UtilMemoryAllocate(usBufSiz, ppByte)
#endif
USHORT usBufSiz;
PBYTE FAR *ppByte;
{
#if (defined(PORT_16) || defined(PORT_S116))
SEL sel;
if (DosAllocSeg(usBufSiz, &sel, SEG_NONSHARED) == NO_ERROR) {
*ppByte = (BYTE *)MAKEP(sel, 0);
return TRUE;
} else
return FALSE;
#else
return DosAllocMem(ppByte,
usBufSiz,
PAG_READ | PAG_WRITE | PAG_COMMIT) == NO_ERROR;
#endif
} /* UtilMemoryAllocate() */
/****************************************************************\
* Free up allocated memory
*--------------------------------------------------------------
*
* Name: UtilMemoryFree()
*
* Purpose: Releases the memory held for the image
*
* Usage: called at cleanup time during the processing of the
* WM_DESTROY message & during loading of new image
*
* Method:
*
* Returns:
*
\****************************************************************/
#ifdef PORT_32
VOID UtilMemoryFree(pByte)
PBYTE pByte;
{
DosFreeMem(pByte);
} /* UtilMemoryFree() */
#else
#ifdef PORT_16
VOID UtilMemoryFree(sel)
#else
VOID FAR pascal UtilMemoryFree(sel)
#endif
SEL sel;
{
DosFreeSeg(sel);
} /* UtilMemoryFree() */
#endif
/****************************************************************\
* Find File Size
*--------------------------------------------------------------
*
* Name: UtilFindFileSize()
*
* Purpose: To find the size of the file passed in
*
* Usage: Called to determine the size of the image in order
* to allocate sufficient storage for it
*
* Method: filesize = min(0xFFFF, filesize)
*
* Returns:
*
\****************************************************************/
#if (defined(PORT_16) || defined(PORT_32))
LONG UtilFindFileSize(pszFilename)
#else
LONG FAR pascal UtilFindFileSize(pszFilename)
#endif
PSZ pszFilename;
{
FILEFINDBUF ffb; /* DOS file-search results buffer */
HDIR hdir = 0xffff; /* allocate a handle to caller */
#if (defined(PORT_16) || defined(PORT_S116))
USHORT usAttrib = 0;
USHORT cSearch = 1;
if (DosFindFirst(pszFilename, /* filename from Open Dialog */
&hdir,
usAttrib,
&ffb,
(USHORT)(sizeof(ffb) * cSearch),
&cSearch,
0L) == NO_ERROR) /* reserved must be NULL */
#else
ULONG ulAttrib = 0;
ULONG cSearch = 1;
if (DosFindFirst(pszFilename, /* filename from Open Dialog */
&hdir,
ulAttrib,
&ffb,
(USHORT)(sizeof(ffb) * cSearch),
&cSearch,
1L) == NO_ERROR) /* no of files to locate */
#endif
/*
* Obtain the size of the image. The amount of storage
* required depends on the size of the image, which is
* recorded in the image-file header.
* If the image is greater than 64KB, then only the first 64KB of
* image data is loaded.
*/
return (ffb.cbFile > 0xFFFFL) ? 0xFFFFL : ffb.cbFile;
else
return 0L;
} /* UtilFindFileSize() */
/****************************************************************\
* Get File Handle
*--------------------------------------------------------------
*
* Name: UtilGetFileHandle(pszFilename, phfile)
*
* Purpose: To open the file passed in and return the file handle
*
* Usage: Called in order the read in the image data from a file
* so that it can be displayed
*
* Method:
*
* Returns:
*
\****************************************************************/
#if (defined(PORT_16) || defined(PORT_32))
BOOL UtilGetFileHandle(pszFilename, phfile)
#else
BOOL FAR pascal UtilGetFileHandle(pszFilename, phfile)
#endif
PSZ pszFilename;
PHFILE phfile;
{
#if (defined(PORT_16) || defined(PORT_S116))
USHORT usAction;
return DosOpen(pszFilename, /* filename from open dialog */
phfile, /* file handle returned */
(PUSHORT)&usAction,
0L,
FILE_NORMAL,
FILE_OPEN,
OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
0L) == NO_ERROR; /* must be zero */
#else
ULONG ulAction;
return DosOpen(pszFilename, /* filename from open dialog */
phfile, /* file handle returned */
(PULONG)&ulAction,
0L,
FILE_NORMAL,
FILE_OPEN,
OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
0L) == NO_ERROR; /* must be zero */
#endif
} /* UtilGetFileHandle() */
/****************************************************************\
* Appends the app name to the title bar text
*--------------------------------------------------------------
*
* Name: UtilUpdateTitleText(hab, hwnd, pszFullFile)
*
* Purpose: Updates the text in the main window's title bar to
* display the app name, followed by the separator,
* followed by the file name
*
* Usage: called at init time and when the text file is changed
*
* Method: gets the program name, appends the separator, and
* appends the file name.
*
* Returns:
*
\****************************************************************/
#if (defined(PORT_16) || defined(PORT_32))
VOID UtilUpdateTitleText(hab, hwnd, pszFullFile)
#else
VOID FAR pascal UtilUpdateTitleText(hab, hwnd, pszFullFile)
#endif
HAB hab;
HWND hwnd;
PSZ pszFullFile;
{
CHAR szBuf[MAXNAMEL];
CHAR szSeparator[TITLESEPARATORLEN+1];
CHAR szUntitled[MESSAGELEN];
PSZ pszFileName;
#ifdef PORT_S116
PCHAR pch;
USHORT cch;
#endif
/* get current task title */
WinQueryTaskTitle(NULL, szBuf, MAXNAMEL); /* szBuf = IMAGE.EXE */
WinLoadString(hab, NULL, IDS_TITLEBARSEPARATOR,
TITLESEPARATORLEN, szSeparator);
strcat(szBuf, szSeparator); /* szBuf = IMAGE.EXE - */
/*
* ??? ADG comment here ???
*/
#if (defined(PORT_16) || defined(PORT_32))
/*
* if filename is valid it will be F.Q so search for
* final backslash, otherwise use the Untitled string.
*/
if (pszFileName = strrchr(pszFullFile, '\\'))
pszFileName++;
#else
/* set pointer to end of string and count characters in string */
pch = pszFullFile;
cch = 0;
while (*pch++)
cch++;
/* must cater for the null string in this test */
if (cch > 0)
pch--;
/* search for final backslash in F.Q name if it exists */
while (*--pch && *pch != '\\' && cch-- >= 0);
/*
* if filename is valid (ie '\' is found in F.Q name,
* then use filename, otherwise use the Untitled string.
*/
if (*pch == '\\')
pszFileName = ++pch;
#endif
else {
/* load "untitled" string */
WinLoadString(hab, NULL, IDS_UNTITLED, MESSAGELEN, szUntitled);
pszFileName = (PSZ)szUntitled;
}
strcat(szBuf, pszFileName); /* szBuf = IMAGE.EXE - IMAGE.IMG */
WinSetWindowText(WinWindowFromID(hwnd, FID_TITLEBAR), szBuf);
} /* UtilUpdateTitleText() */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.