409 lines
6.0 KiB
C++
409 lines
6.0 KiB
C++
//-----------------------------------------------------------------------------+
|
|
// Copyright (C), 1998-2007, SH Software Co. Ltd.
|
|
// = FileName : WFormatUnit.cpp
|
|
// = Version : ver2.0
|
|
// = Author : zjq
|
|
// = CreateDate : 2002-09-09
|
|
// = Description: 类的定义
|
|
// = Maintainers:
|
|
//
|
|
//-----------------------------------------------------------------------------+
|
|
#include "StdAfx.h"
|
|
#include "WFormatUnit.h"
|
|
#include "AString.h"
|
|
|
|
WFormatUnit::WFormatUnit()
|
|
{
|
|
bValid = false;
|
|
width = 0;
|
|
bLeftAlign = true;
|
|
type = WS_FORMAT;
|
|
}
|
|
WFormatUnit::WFormatUnit(const char *pszFormat, long &cur,const AString &s,long &sCur)
|
|
{
|
|
bValid = false;
|
|
width = 0;
|
|
bLeftAlign = true;
|
|
type = WS_FORMAT;
|
|
|
|
long begin = cur;
|
|
if (pszFormat[cur] == '%' && pszFormat[cur + 1] != '%')
|
|
{
|
|
cur++;
|
|
bool bFloat = false;
|
|
for (bool bContinue = true; pszFormat[cur] != NULL && bContinue; cur++)
|
|
{
|
|
switch (pszFormat[cur])
|
|
{
|
|
case '-':
|
|
if (cur - begin != 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
break;
|
|
case '.':
|
|
if (!bFloat)
|
|
{
|
|
bFloat = true;
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
|
|
break;
|
|
case '0':;
|
|
case '1':;
|
|
case '2':;
|
|
case '3':;
|
|
case '4':;
|
|
case '5':;
|
|
case '6':;
|
|
case '7':;
|
|
case '8':;
|
|
case '9':
|
|
break;
|
|
case 'c':
|
|
bContinue = false;
|
|
type = WS_CHAR;
|
|
break;
|
|
case 'd':
|
|
bContinue = false;
|
|
type = WS_INT;
|
|
break;
|
|
case 'f':
|
|
bContinue = false;
|
|
type = WS_DOUBLE;
|
|
break;
|
|
case 's':
|
|
bContinue = false;
|
|
type = WS_STRING;
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (cur - begin < 2)
|
|
{
|
|
return;
|
|
}
|
|
else if (cur - begin > 2)
|
|
{
|
|
Copy(pszFormat + begin + 1, 0, cur - begin - 2);
|
|
double setting;
|
|
*this >> setting;
|
|
width = (int)fabs(setting);
|
|
}
|
|
else if (type == WS_INT || type == WS_DOUBLE || type == WS_STRING)
|
|
{
|
|
if (pszFormat[cur] == NULL)
|
|
{
|
|
width = s.Length() - sCur;
|
|
}
|
|
else
|
|
{
|
|
width = s.Find(pszFormat[cur],sCur) - sCur;
|
|
}
|
|
|
|
if (width < 0)
|
|
{
|
|
width = 0;
|
|
}
|
|
}
|
|
|
|
Clear();
|
|
switch(type)
|
|
{
|
|
case WS_CHAR:
|
|
{
|
|
Insert(0,s[sCur]);
|
|
sCur++;
|
|
break;
|
|
}
|
|
case WS_INT:;
|
|
case WS_DOUBLE:;
|
|
case WS_STRING:
|
|
{
|
|
if (width <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
*this << s.Mid(sCur,sCur + width - 1);
|
|
sCur += width;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
type = WS_FORMAT;
|
|
if (pszFormat[cur] == '%' && pszFormat[cur + 1] == '%')
|
|
{
|
|
cur += 2;
|
|
}
|
|
|
|
while (pszFormat[cur] != NULL && pszFormat[cur] != '%')
|
|
{
|
|
cur++;
|
|
}
|
|
|
|
Copy(pszFormat + begin, 0, cur - begin);
|
|
|
|
AString s,d;
|
|
s = "%%";
|
|
d = "%";
|
|
|
|
Replace(s,d);
|
|
sCur += Length();
|
|
}
|
|
|
|
bValid = true;
|
|
}
|
|
|
|
WFormatUnit::WFormatUnit(const char *pszFormat,long &cur,char* &curP)
|
|
{
|
|
bValid = false;
|
|
bLeftAlign = true;
|
|
type = WS_FORMAT;
|
|
|
|
long begin = cur;
|
|
if (pszFormat[cur] == '%' && pszFormat[cur + 1] != '%')
|
|
{
|
|
cur++;
|
|
bool bFloat = false;
|
|
for (bool bContinue = true; pszFormat[cur] != NULL && bContinue; cur++)
|
|
{
|
|
switch (pszFormat[cur])
|
|
{
|
|
case '-':
|
|
if (cur - begin != 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
break;
|
|
case '.':
|
|
if (!bFloat)
|
|
{
|
|
bFloat = true;
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
|
|
break;
|
|
case '0':;
|
|
case '1':;
|
|
case '2':;
|
|
case '3':;
|
|
case '4':;
|
|
case '5':;
|
|
case '6':;
|
|
case '7':;
|
|
case '8':;
|
|
case '9':
|
|
break;
|
|
case 'c':
|
|
bContinue = false;
|
|
type = WS_CHAR;
|
|
|
|
break;
|
|
case 'd':
|
|
bContinue = false;
|
|
type = WS_INT;
|
|
break;
|
|
case 'f':
|
|
bContinue = false;
|
|
type = WS_DOUBLE;
|
|
break;
|
|
case 's':
|
|
bContinue = false;
|
|
type = WS_STRING;
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (cur - begin < 2)
|
|
{
|
|
return;
|
|
}
|
|
else if (cur - begin > 2)
|
|
{
|
|
Copy(pszFormat + begin + 1, 0, cur - begin - 2);
|
|
double setting;
|
|
*this >> setting;
|
|
bLeftAlign = setting >= 0;
|
|
setting = fabs(setting);
|
|
width = (int)setting;
|
|
|
|
if (Find('.') == NOT_FOUND)
|
|
{
|
|
preci = -1;
|
|
}
|
|
else
|
|
{
|
|
preci = (int)((setting - width) * 10 + 0.5);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
width = 0;
|
|
preci = -1;
|
|
}
|
|
|
|
Clear();
|
|
switch(type)
|
|
{
|
|
case WS_CHAR:
|
|
{
|
|
Insert(0,*curP);
|
|
curP += sizeof(int);
|
|
break;
|
|
}
|
|
case WS_INT:
|
|
{
|
|
*this << *((int*)curP);
|
|
if (width > 0)
|
|
{
|
|
if (bLeftAlign)
|
|
{
|
|
while (Length() < width)
|
|
{
|
|
Insert(m_nEnd,' ');
|
|
}
|
|
}
|
|
else
|
|
{
|
|
while (Length() < width)
|
|
{
|
|
Insert(0,' ');
|
|
}
|
|
}
|
|
}
|
|
|
|
curP += sizeof(int);
|
|
break;
|
|
}
|
|
case WS_DOUBLE:
|
|
{
|
|
*this << *((double*)curP);
|
|
int temp = Find('.');
|
|
if (preci != -1)
|
|
{
|
|
while (Length() - temp - 1 < preci)
|
|
{
|
|
Insert(m_nEnd,'0');
|
|
}
|
|
|
|
SetEnd(temp + preci + 1);
|
|
}
|
|
|
|
if (width > 0)
|
|
{
|
|
if (bLeftAlign)
|
|
{
|
|
while (Length() < width)
|
|
{
|
|
Insert(m_nEnd,' ');
|
|
}
|
|
}
|
|
else
|
|
{
|
|
while (Length() < width)
|
|
{
|
|
Insert(0,' ');
|
|
}
|
|
}
|
|
}
|
|
|
|
curP += sizeof(double);
|
|
break;
|
|
}
|
|
case WS_STRING:
|
|
{
|
|
*this << *(char**)(curP);
|
|
if (width > 0)
|
|
{
|
|
if (Length() < width)
|
|
{
|
|
if (bLeftAlign)
|
|
{
|
|
while (Length() < width)
|
|
{
|
|
Insert(m_nEnd,' ');
|
|
}
|
|
}
|
|
else
|
|
{
|
|
while (Length() < width)
|
|
{
|
|
Insert(0,' ');
|
|
}
|
|
}
|
|
}
|
|
else if (Length() > width)
|
|
{
|
|
DelRight(Length() - width);
|
|
}
|
|
}
|
|
|
|
curP += sizeof(char*);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
type = WS_FORMAT;
|
|
if (pszFormat[cur] == '%' && pszFormat[cur + 1] == '%')
|
|
{
|
|
cur += 2;
|
|
}
|
|
|
|
while (pszFormat[cur] != NULL && pszFormat[cur] != '%')
|
|
{
|
|
cur++;
|
|
}
|
|
|
|
Copy(pszFormat + begin, 0, cur - begin);
|
|
AString s,d;
|
|
s = "%%";
|
|
d = "%";
|
|
|
|
Replace(s,d);
|
|
}
|
|
|
|
bValid = true;
|
|
}
|
|
|
|
WFormatUnit::~WFormatUnit()
|
|
{
|
|
|
|
}
|
|
|
|
WFormatUnit& WFormatUnit::operator = (const WFormatUnit& src)
|
|
{
|
|
if (&src != this)
|
|
{
|
|
AString::operator = ((AString&)src);
|
|
width = src.width;
|
|
bLeftAlign = src.bLeftAlign;
|
|
preci = src.preci;
|
|
bValid = src.bValid;
|
|
type = src.type;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
|
|
bool WFormatUnit::IsValid()const
|
|
{
|
|
return bValid;
|
|
}
|