home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Troubleshooting Netware Systems
/
CSTRIAL0196.BIN
/
attach
/
msj
/
v10n04
/
multilin.exe
/
NUMBFORM.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-01
|
8KB
|
261 lines
// numbform.cpp : implementation file
//
#include "stdafx.h"
#include "global.h"
#include "numbform.h"
#include "picture.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
// GetNumberFormat does not provide picture definition
// as GetDateFormat or GetTimeFormat do. This file provides
// a simple implementation of such a facility. The implemen-
// tation is split into two parts, an overloaded function
// int GetNumberFormat(
// LCID lcid, // locale of interest
// DWORD dwFlags, // flags specifying function options
// LPCTSTR lpValue, // number to be formatted
// LPCTSTR lpFormat, // format string
// LPTSTR lpNumberStr, // buffer for storing formatted string
// int cchNumber // size of buffer
// );
// that will parse an arbitrary string and break it into
// pieces that can be handled individually by the built-in
// Win32 GetNumberFormat function, and a class CPicture
// that will transform the picture string pointed to by
// lpFormat into the NUMBERFMT structure required by the
// built-in GetNumberFormat.
int GetNumberFormat(LCID lcid, DWORD dwFlags, LPCTSTR lpValue, LPCTSTR lpFormat, LPTSTR lpNumber, UINT cchNumber)
{
// If no format, just use defaults
if(lpFormat==NULL)
{
return ::GetNumberFormat(lcid, dwFlags, lpValue, NULL, lpNumber, cchNumber);
}
// Loop over format string
UINT cchMax = cchNumber;
BOOL fInEscape = FALSE;
while(*lpFormat && cchNumber)
{
// strip ' escaped strings,
if( *lpFormat == _T('\'') )
{
// look ahead for '' to represent ' itself
if( fInEscape && *(lpFormat+1)==_T('\''))
{
// Copy ' character
*lpNumber++=*lpFormat++;
cchNumber--;
}
else
{
fInEscape = !fInEscape;
}
// Always eat at least one ' character
lpFormat++;
continue;
}
if( fInEscape )
{
// Copy one character
*lpNumber++=*lpFormat++;
cchNumber--;
continue;
}
// Try to parse this character as a number picture
CPicture pict(lpFormat, lcid, dwFlags );
UINT cchFormat=pict.m_Length;
if(cchFormat)
{
// Have a numeric picture in the lpInput stream
// Insert the formatted number
UINT cchOut=GetNumberFormat(lcid, 0, lpValue, &pict, lpNumber, cchNumber);
if( !cchOut )
{
// some error
return 0;
}
// Account for lpInput and output string used up
lpFormat+=cchFormat;
cchNumber-=(cchOut-1);
lpNumber+=(cchOut-1);
continue;
}
// Current character in lpInput string is not part of a
// numeric picture, echo directly
*lpNumber++=*lpFormat++;
cchNumber--;
}
// terminate
*lpNumber=_T('\0');
return cchMax - cchNumber + 1; // # of characters in output
}
/////////////////////////////////////////////////////////////////////////////
// CNumbFormat dialog
CNumbFormat::CNumbFormat(CWnd* pParent /*=NULL*/)
: CDialog(CNumbFormat::IDD, pParent)
{
//{{AFX_DATA_INIT(CNumbFormat)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CNumbFormat::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNumbFormat)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNumbFormat, CDialog)
//{{AFX_MSG_MAP(CNumbFormat)
ON_EN_CHANGE(IDC_CUSTOM, OnChangeCustom)
ON_LBN_DBLCLK(IDC_NUMBFORMATS, OnDblclkNumbformats)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNumbFormat message handlers
BOOL CNumbFormat::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
AppendNumberFormat( LOCALE_NOUSEROVERRIDE);
AppendNumberFormat( LOCALE_UNRESTRICTED);
SendDlgItemMessage( IDC_NUMBFORMATS, LB_SETCURSEL, 0, 0);
SendMessage( WM_COMMAND, IDC_NUMBFORMATS, 0);
// Format a sample date
OnDblclkNumbformats();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
#define STRINGLEN 128
/////////////////////////////////////////////////////
// EnumNumberFormatsProc
//
// Add the given date format string to the list box and
// set that item's data to LOCALE_NOUSEROVERRIDE or not
#define MAXSAMPLELEN 128
#define MAXNUMBERFORMAT 50
////////////////////////////////
// ShowSampleNumber
//
void CNumbFormat::ShowSampleNumber( CString& szNumberFormat, DWORD dwNumberFlags)
{
static TCHAR szSampleNumber[ MAXSAMPLELEN];
GetNumberFormat( GetThreadLocale(), // use this thread's locale
dwNumberFlags, // flags passed into this function
_T("0.7890"), // sample positive numeric value
szNumberFormat, // format seleted by user
szSampleNumber, // for formatted number
MAXSAMPLELEN); // number buffer length
SetDlgItemText( IDC_POSFORMAT, szSampleNumber);
GetNumberFormat( GetThreadLocale(), // use this thread's locale
dwNumberFlags, // flags passed into this function
_T("-123456.7890"), // sample numeric value
szNumberFormat, // format seleted by user
szSampleNumber, // for formatted number
MAXSAMPLELEN); // number buffer length
SetDlgItemText( IDC_NEGFORMAT, szSampleNumber);
}
BOOL CNumbFormat::AppendNumberFormat( DWORD dwFlags)
{
static TCHAR szTmp[STRINGLEN] = TEXT("");
LONG lTmp;
LONG lIndex = SendDlgItemMessage( IDC_NUMBFORMATS,
LB_GETCOUNT,
0, 0);
CPicture pix(GetThreadLocale(), dwFlags);
int nBufLen = pix.m_Length;
if ( nBufLen == 0 )
{
LONG lRC = GetLastError();
}
// Is this format string already in the list?
for ( lTmp = 0; lTmp < lIndex; ++lTmp )
{
SendDlgItemMessage( IDC_NUMBFORMATS,
LB_GETTEXT,
lTmp,
(LPARAM)szTmp);
if ( lstrcmp( pix, szTmp) == 0 )
return( TRUE); // Already in list
}
lIndex = SendDlgItemMessage( IDC_NUMBFORMATS,
LB_ADDSTRING,
0,
(LPARAM)(LPCTSTR)pix);
if ( ! (lIndex = LB_ERR || lIndex == LB_ERRSPACE) )
{
return( SendDlgItemMessage( IDC_NUMBFORMATS,
LB_SETITEMDATA,
lIndex,
(LPARAM)dwFlags) == LB_ERR ? FALSE : TRUE);
}
return( FALSE);
}
void CNumbFormat::OnChangeCustom()
{
// TODO: Add your control notification handler code here
CString szNumberFormat;
SendDlgItemMessage( IDC_CUSTOM,
WM_GETTEXT,
MAXNUMBERFORMAT,
(LPARAM)(szNumberFormat.GetBufferSetLength(MAXNUMBERFORMAT)));
DWORD dwNumberFlags = 0;
ShowSampleNumber( szNumberFormat, dwNumberFlags);
}
void CNumbFormat::OnDblclkNumbformats()
{
// TODO: Add your control notification handler code here
LONG lIndex = SendDlgItemMessage( IDC_NUMBFORMATS,
LB_GETCURSEL, 0, 0);
CString szNumberFormat;
SendDlgItemMessage( IDC_NUMBFORMATS,
LB_GETTEXT,
(WPARAM)lIndex,
(LPARAM)(szNumberFormat.GetBufferSetLength(MAXNUMBERFORMAT)));
DWORD dwNumberFlags = SendDlgItemMessage( IDC_NUMBFORMATS,
LB_GETITEMDATA,
(WPARAM)lIndex, 0);
ShowSampleNumber( szNumberFormat, dwNumberFlags);
}