Files
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

408 lines
6.0 KiB
C++

//-----------------------------------------------------------------------------+
// Copyright (C), 1998-2022, Beijing SH Software Co. Ltd.
// = FileName : TWFormatUnit.cpp
// = Version : ver2.0
// = Author :
// = CreateDate : 2002-09-09
// = Description: 类的定义
// = Maintainers:
//
//-----------------------------------------------------------------------------+
#include "..\StdAfx.h"
#include "TWFormatUnit.h"
#include "TAString.h"
TWFormatUnit::TWFormatUnit()
{
bValid = false;
width = 0;
bLeftAlign = true;
type = WS_FORMAT;
}
TWFormatUnit::TWFormatUnit(const char *pszFormat, long &cur,const TAString &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);
TAString s,d;
s = "%%";
d = "%";
Replace(s,d);
sCur += Length();
}
bValid = true;
}
TWFormatUnit::TWFormatUnit(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);
TAString s,d;
s = "%%";
d = "%";
Replace(s,d);
}
bValid = true;
}
TWFormatUnit::~TWFormatUnit()
{
}
TWFormatUnit& TWFormatUnit::operator = (const TWFormatUnit& src)
{
if (&src != this)
{
TAString::operator = ((TAString&)src);
width = src.width;
bLeftAlign = src.bLeftAlign;
preci = src.preci;
bValid = src.bValid;
type = src.type;
}
return *this;
}
bool TWFormatUnit::IsValid()const
{
return bValid;
}