?? mcbmain.c
字號:
// McbMain.c : Defines the entry point for the application.
//
/*
****************************************************************************
* Include all necessary include files
****************************************************************************
*/
#define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from headers */
#define STRICT
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
#include <windows.h> /* Windows Header Files */
#include <stdlib.h> /* C RunTime Header Files */
#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <commctrl.h>
#include <commdlg.h>
#include <assert.h>
#include <richedit.h>
#include <shellapi.h> /* for DragAcceptFiles */
#include "resource.h"
#include "McbXML.h"
/*
****************************************************************************
* Structure used for options
****************************************************************************
*/
typedef struct tagMcbOptions
{
TCHAR szLastFile[_MAX_PATH+1];
int bPanelsVertical;
int nFilterIndex;
} McbOptions;
/*
****************************************************************************
* Global Variables
****************************************************************************
*/
#define MAX_LOADSTRING 100
HINSTANCE g_hInst; /* Current instance */
TCHAR g_szTitle[MAX_LOADSTRING]; /* The title bar text */
TCHAR g_szWindowClass[MAX_LOADSTRING]; /* Windows class name */
McbOptions g_options;
TCHAR g_szOptFile[_MAX_PATH+1];
/**
****************************************************************************
* <P> Obtain the options filename. </P>
*
* @methodName McbGetOptionsFileName
*
* @param lpszFile
*
* @return void
*
* @exception none
*
* @author Martyn C Brown
*
* @changeHistory
* 4th February 2002 - (V1.0) Creation (MCB)
****************************************************************************
*/
void McbGetOptionsFileName(LPTSTR lpszFile)
{
TCHAR szFile[_MAX_PATH+1];
TCHAR szDrive[_MAX_DRIVE+1];
TCHAR szDir[_MAX_DIR+1];
GetModuleFileName(NULL, szFile, _MAX_PATH+1);
_tsplitpath(szFile, szDrive, szDir, NULL, NULL);
_tcscpy(lpszFile, szDrive);
lpszFile += _tcslen(szDrive);
_tcscpy(lpszFile, szDir);
lpszFile += _tcslen(szDir);
#define McbOPTIONSFILE _T("Options.xml")
_tcscpy(lpszFile, McbOPTIONSFILE);
}/* McbGetOptionsFileName */
/**
****************************************************************************
* <P> Helper function to load a file into allocated memory. </P>
*
* @methodName McbLoadFile
*
* @param lpszFile
*
* @return LPTSTR
*
* @exception none
*
* @author Martyn C Brown
*
* @changeHistory
* 4th February 2002 - (V1.0) Creation (MCB)
****************************************************************************
*/
LPTSTR McbLoadFile(LPCTSTR lpszFile)
{
LPTSTR lpszResult = NULL;
HANDLE hFile;
DWORD cbFile;
DWORD cbRead;
hFile = CreateFile(lpszFile, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
cbFile = GetFileSize(hFile, NULL);
if (cbFile)
{
lpszResult = (LPTSTR)malloc((cbFile+1) * sizeof(TCHAR));
lpszResult[cbFile] = 0;
if (!ReadFile(hFile, lpszResult, cbFile, &cbRead, NULL) ||
cbFile > cbRead)
{
free(lpszResult);
lpszResult = NULL;
}
}
CloseHandle(hFile);
}
return lpszResult;
}/* McbLoadFile */
/**
****************************************************************************
* <P> Helper function to save to a file. </P>
*
* @methodName McbLoadFile
*
* @param LPCTSTR lpszFile
* @param LPCTSTR lpszData
*
* @return LPTSTR
*
* @exception none
*
* @author Martyn C Brown
*
* @changeHistory
* 4th February 2002 - (V1.0) Creation (MCB)
****************************************************************************
*/
int McbSaveFile(LPCTSTR lpszFile, LPCTSTR lpszData)
{
int nResult = FALSE;
HANDLE hFile;
DWORD cbData;
DWORD cbWrite;
assert(lpszData && *lpszData);
hFile = CreateFile(lpszFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
cbData = _tcslen(lpszData);
if (WriteFile(hFile, lpszData, cbData, &cbWrite, NULL) &&
cbData <= cbWrite)
{
nResult = TRUE;
}
CloseHandle(hFile);
}
return nResult;
}/* McbSaveFile */
/**
****************************************************************************
* <P> Add quotes round a string so it can be used as an attribute value.
* </P>
*
* @methodName McbAddQuotes
*
* @param lpszData
*
* @return LPTSTR
*
* @exception none
*
* @author Martyn C Brown
*
* @changeHistory
* 4th February 2002 - (V1.0) Creation (MCB)
****************************************************************************
*/
LPTSTR McbAddQuotes(LPCTSTR lpszData)
{
LPTSTR lpszResult;
int cbData;
if (!lpszData || !(*lpszData))
{
lpszResult = (LPTSTR)malloc(3 * sizeof(TCHAR));
_tcscpy(lpszResult, _T("\"\""));
}
else
{
cbData = _tcslen(lpszData);
lpszResult = (LPTSTR)malloc((cbData + 3)* sizeof(TCHAR));
*lpszResult = _T('\"');
_tcscpy(lpszResult+1, lpszData);
lpszResult[cbData+1] = _T('\"');
lpszResult[cbData+2] = 0;
}
return lpszResult;
}/* McbAddQuotes */
/**
****************************************************************************
* <P> Remove quotes from an attribute value. </P>
*
* @methodName McbRemoveQuotes
*
* @param lpszData
*
* @return LPTSTR
*
* @exception none
*
* @author Martyn C Brown
*
* @changeHistory
* 4th February 2002 - (V1.0) Creation (MCB)
****************************************************************************
*/
LPTSTR McbRemoveQuotes(LPCTSTR lpszData)
{
LPTSTR lpszResult = NULL;
int cbData, cbTemp;
LPCTSTR lpszMarker;
if (lpszData && *lpszData)
{
cbData = _tcslen(lpszData);
cbTemp = cbData;
lpszMarker = lpszData;
if (lpszData[0] == _T('\"'))
{
lpszMarker++;
cbTemp--;
}
if (cbTemp && (lpszData[cbData-1] == _T('\"')))
{
cbTemp--;
}
if (cbTemp)
{
lpszResult = (LPTSTR)malloc((cbTemp + 1) *sizeof(TCHAR));
_tcsncpy(lpszResult, lpszMarker, cbTemp);
lpszResult[cbTemp] = 0;
}
}
return lpszResult;
}/* McbRemoveQuotes */
/**
****************************************************************************
* <P> Store or load options to an XML file. </P>
*
* @methodName McbArchiveOptions
*
* @param lpszFile
* @param * pOptions
* @param nStore
*
* @return int
*
* @exception none
*
* @author Martyn C Brown
*
* @changeHistory
* 4th February 2002 - (V1.0) Creation (MCB)
****************************************************************************
*/
int McbArchiveOptions(LPCTSTR lpszFile, McbOptions * pOptions, int nStore)
{
int nResult = 0;
McbXMLElement * pRoot;
McbXMLElement * pChild;
McbXMLAttribute * pAttr;
LPTSTR lpszName;
LPTSTR lpszValue;
LPTSTR lpszXML;
int cbXML;
assert(pOptions);
/*
*************************************************************************
* If we are storing to file.
*************************************************************************
*/
if (nStore)
{
/*
*********************************************************************
* Create the XML structure
*********************************************************************
*/
pRoot = McbCreateRoot();
if (pRoot)
{
/*
*****************************************************************
* Add the head element
*****************************************************************
*/
pChild = McbCreateElements(pRoot, _T("XMLOptions"));
if (pChild)
{
/*
*************************************************************
* Add vertical or horizontal panel
*************************************************************
*/
if (pOptions->bPanelsVertical)
{
lpszName = McbStrdup(_T("PanelsVertical"), 0);
}
else
{
lpszName = McbStrdup(_T("PanelsHorizontal"), 0);
}
McbAddAttribute(pChild, lpszName, NULL, 5);
/*
*************************************************************
* Add the file options element
*************************************************************
*/
pChild = McbCreateElements(pChild, _T("FileOptions"));
if (pChild)
{
/*
*********************************************************
* Add the filename attribute
*********************************************************
*/
lpszName = McbStrdup(_T("Filename"), 0);
lpszValue = McbAddQuotes(pOptions->szLastFile);
McbAddAttribute(pChild, lpszName, lpszValue, 5);
/*
*********************************************************
* Add the filter index attribute
*********************************************************
*/
lpszName = McbStrdup(_T("FilterIndex"), 0);
lpszValue = (LPTSTR)malloc(10 * sizeof(TCHAR));
_ltot(pOptions->nFilterIndex, lpszValue, 10);
McbAddAttribute(pChild, lpszName, lpszValue, 5);
}
}
/*
*****************************************************************
* Create string from the XML structure.
*****************************************************************
*/
lpszXML = McbCreateXMLString(pRoot, 1, &cbXML);
if (lpszXML)
{
/*
*************************************************************
* Attempt to save the XML to file.
*************************************************************
*/
if (McbSaveFile(lpszFile, lpszXML))
{
nResult = TRUE;
}
free(lpszXML);
}
McbDeleteElement(pRoot);
}
}
/*
*************************************************************************
* If we are reading from file
*************************************************************************
*/
else
{
/*
*********************************************************************
* Initialise the options structure.
*********************************************************************
*/
memset(pOptions, 0, sizeof(McbOptions));
/*
*********************************************************************
* Load the XML file.
*********************************************************************
*/
lpszXML = McbLoadFile(lpszFile);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -