PITimer important fix! ASCII-art start ...
git-svn-id: svn://db.shs.com.ru/pip@94 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -36,10 +36,79 @@ char * __sysoemname__ = 0;
|
||||
|
||||
|
||||
PIChar::PIChar(const char * c) {
|
||||
#ifdef PIP_ICU
|
||||
UErrorCode e((UErrorCode)0);
|
||||
UConverter * cc = ucnv_open(__syslocname__, &e);
|
||||
if (cc) {
|
||||
UChar uc;
|
||||
e = (UErrorCode)0;
|
||||
ucnv_toUChars(cc, &uc, 1, c, 4, &e);
|
||||
ucnv_close(cc);
|
||||
ch = uc;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
ch = *reinterpret_cast<const int * >(c);
|
||||
}
|
||||
|
||||
|
||||
PIChar PIChar::fromConsole(char c) {
|
||||
PIChar ret;
|
||||
#ifdef PIP_ICU
|
||||
UErrorCode e((UErrorCode)0);
|
||||
UConverter * cc = ucnv_open(__sysoemname__, &e);
|
||||
if (cc) {
|
||||
UChar uc;
|
||||
e = (UErrorCode)0;
|
||||
ucnv_toUChars(cc, &uc, 1, &c, 1, &e);
|
||||
ucnv_close(cc);
|
||||
ret.ch = uc;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
ret.ch = c;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIChar PIChar::fromSystem(char c) {
|
||||
PIChar ret;
|
||||
#ifdef PIP_ICU
|
||||
UErrorCode e((UErrorCode)0);
|
||||
UConverter * cc = ucnv_open(__syslocname__, &e);
|
||||
if (cc) {
|
||||
UChar uc;
|
||||
e = (UErrorCode)0;
|
||||
ucnv_toUChars(cc, &uc, 1, &c, 1, &e);
|
||||
ucnv_close(cc);
|
||||
ret.ch = uc;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
ret.ch = c;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIChar PIChar::fromUTF8(const char * c) {
|
||||
PIChar ret;
|
||||
#ifdef PIP_ICU
|
||||
UErrorCode e((UErrorCode)0);
|
||||
UConverter * cc = ucnv_open("UTF-8", &e);
|
||||
if (cc) {
|
||||
UChar uc;
|
||||
e = (UErrorCode)0;
|
||||
ucnv_toUChars(cc, &uc, 1, c, 8, &e);
|
||||
ucnv_close(cc);
|
||||
ret.ch = uc;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
ret.ch = *(ushort*)c;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
bool PIChar::operator ==(const PIChar & o) const {
|
||||
return strcmp(o.toCharPtr(), toCharPtr()) == 0;
|
||||
}
|
||||
|
||||
@@ -146,6 +146,10 @@ public:
|
||||
|
||||
//! Return symbol in lower case
|
||||
PIChar toLower() const;
|
||||
|
||||
static PIChar fromConsole(char c);
|
||||
static PIChar fromSystem(char c);
|
||||
static PIChar fromUTF8(const char * c);
|
||||
|
||||
private:
|
||||
ushort ch;
|
||||
|
||||
@@ -129,17 +129,18 @@ PIInit::PIInit() {
|
||||
# endif
|
||||
#endif
|
||||
#ifdef PIP_ICU
|
||||
__syslocname__ = new char[256];
|
||||
memset(__syslocname__, 0, 256);
|
||||
__syslocname__ = __sysoemname__ = 0;
|
||||
//__syslocname__ = new char[256];
|
||||
//memset(__syslocname__, 0, 256);
|
||||
# ifdef WINDOWS
|
||||
CPINFOEX cpinfo;
|
||||
GetCPInfoEx(CP_ACP, 0, &cpinfo);
|
||||
int l = 0;
|
||||
/*GetCPInfoEx(CP_ACP, 0, &cpinfo);
|
||||
for (l = 0; l < MAX_PATH; ++l)
|
||||
if (cpinfo.CodePageName[l] == '\0' || cpinfo.CodePageName[l] == ' ')
|
||||
break;
|
||||
memcpy(__syslocname__, "windows-", 8);
|
||||
memcpy(&(__syslocname__[8]), cpinfo.CodePageName, l);
|
||||
memcpy(&(__syslocname__[8]), cpinfo.CodePageName, l);*/
|
||||
GetCPInfoEx(CP_OEMCP, 0, &cpinfo);
|
||||
for (l = 0; l < MAX_PATH; ++l)
|
||||
if (cpinfo.CodePageName[l] == '\0' || cpinfo.CodePageName[l] == ' ')
|
||||
@@ -150,14 +151,14 @@ PIInit::PIInit() {
|
||||
memcpy(&(__sysoemname__[4]), cpinfo.CodePageName, l);
|
||||
//piCout << cpinfo.CodePageName;
|
||||
# else
|
||||
PIString en(getenv("LANG"));
|
||||
/*PIString en(getenv("LANG"));
|
||||
if (!en.isEmpty())
|
||||
en = en.mid(en.find(".") + 1);
|
||||
PIByteArray enba = en.toByteArray();
|
||||
memcpy(__syslocname__, enba.data(), enba.size_s());
|
||||
memcpy(__syslocname__, enba.data(), enba.size_s());*/
|
||||
# endif
|
||||
piCout << __syslocname__;
|
||||
piCout << __sysoemname__;
|
||||
//piCout << __syslocname__;
|
||||
//piCout << __sysoemname__;
|
||||
#endif
|
||||
#ifdef MAC_OS
|
||||
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &__pi_mac_clock);
|
||||
|
||||
@@ -97,11 +97,11 @@ PIString PIString::dtos(const double num) {pisprintf("%.8f", num); return PIStri
|
||||
#undef pisprintf
|
||||
|
||||
|
||||
void PIString::appendFromChars(const char * c, int s) {
|
||||
void PIString::appendFromChars(const char * c, int s, const char * cp) {
|
||||
if (s <= 0) return;
|
||||
#ifdef PIP_ICU
|
||||
UErrorCode e((UErrorCode)0);
|
||||
UConverter * cc = ucnv_open(__syslocname__, &e);
|
||||
UConverter * cc = ucnv_open(cp, &e);
|
||||
if (cc) {
|
||||
UChar * ucs = new UChar[s];
|
||||
memset(ucs, 0, s * sizeof(UChar));
|
||||
@@ -146,11 +146,47 @@ void PIString::appendFromChars(const char * c, int s) {
|
||||
}
|
||||
|
||||
|
||||
void PIString::buildData() const {
|
||||
PIString PIString::fromConsole(const char * s) {
|
||||
int l = 0;
|
||||
while (s[l] != '\0') ++l;
|
||||
PIString ret;
|
||||
if (l > 0) ret.appendFromChars(s, l
|
||||
#ifdef PIP_ICU
|
||||
, __sysoemname__
|
||||
#endif
|
||||
);
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::fromSystem(const char * s) {
|
||||
int l = 0;
|
||||
while (s[l] != '\0') ++l;
|
||||
PIString ret;
|
||||
if (l > 0) ret.appendFromChars(s, l);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::fromUTF8(const char * s) {
|
||||
int l = 0;
|
||||
while (s[l] != '\0') ++l;
|
||||
PIString ret;
|
||||
if (l > 0) ret.appendFromChars(s, l
|
||||
#ifdef PIP_ICU
|
||||
, "UTF-8"
|
||||
#endif
|
||||
);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void PIString::buildData(const char * cp) const {
|
||||
data_.clear();
|
||||
#ifdef PIP_ICU
|
||||
UErrorCode e((UErrorCode)0);
|
||||
UConverter * cc = ucnv_open(__syslocname__, &e);
|
||||
UConverter * cc = ucnv_open(cp, &e);
|
||||
if (cc) {
|
||||
int rs = 0;//, ol = UCNV_GET_MAX_BYTES_FOR_STRING(size_s(), ucnv_getMaxCharSize(cc));
|
||||
char uc[8];
|
||||
@@ -195,6 +231,26 @@ void PIString::buildData() const {
|
||||
}
|
||||
|
||||
|
||||
const char * PIString::dataConsole() const {
|
||||
buildData(
|
||||
#ifdef PIP_ICU
|
||||
__sysoemname__
|
||||
#endif
|
||||
);
|
||||
return (const char *)(data_.data());
|
||||
}
|
||||
|
||||
|
||||
const char * PIString::dataUTF8() const {
|
||||
buildData(
|
||||
#ifdef PIP_ICU
|
||||
"UTF-8"
|
||||
#endif
|
||||
);
|
||||
return (const char *)(data_.data());
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const char * str) {
|
||||
int l = 0;
|
||||
while (str[l] != '\0') ++l;
|
||||
@@ -810,33 +866,10 @@ PICout operator <<(PICout s, const PIString & v) {
|
||||
s.space();
|
||||
s.quote();
|
||||
s.setControl(0, true);
|
||||
#ifdef PIP_ICU
|
||||
if (__sysoemname__) {
|
||||
PIByteArray oba;
|
||||
UErrorCode e((UErrorCode)0);
|
||||
UConverter * cc = ucnv_open(__sysoemname__, &e);
|
||||
if (cc) {
|
||||
int rs = 0;
|
||||
char uc[8];
|
||||
for (int i = 0; i < v.size_s(); ++i) {
|
||||
if (v[i].isAscii())
|
||||
oba.push_back(uchar(v[i].unicode16Code()));
|
||||
else {
|
||||
e = (UErrorCode)0;
|
||||
rs = ucnv_fromUChars(cc, uc, 8, (const UChar*)(((PIDeque<PIChar>*)(&v))->data(i)), 1, &e);
|
||||
//printf("conv %d\n", rs);
|
||||
for (int j = 0; j < rs; ++j)
|
||||
oba.push_back(uc[j]);
|
||||
}
|
||||
}
|
||||
ucnv_close(cc);
|
||||
oba.push_back('\0');
|
||||
s << (const char *)oba.data();
|
||||
} else
|
||||
s << v.data();
|
||||
} else
|
||||
#endif
|
||||
if (PICout::isBufferActive())
|
||||
s << v.data();
|
||||
else
|
||||
s << v.dataConsole();
|
||||
s.restoreControl();
|
||||
s.quote();
|
||||
return s;
|
||||
|
||||
@@ -458,14 +458,30 @@ public:
|
||||
* \sa \a data() */
|
||||
int lengthAscii() const {buildData(); return data_.size_s() - 1;}
|
||||
|
||||
/*! \brief Return \c char * representation of this string
|
||||
/*! \brief Return \c char * representation of this string in system codepage
|
||||
* \details This function fill buffer by sequence
|
||||
* of chars. Minimum length of this buffer is count
|
||||
* of symbols. Returned \c char * is valid until next
|
||||
* execution of this function.\n
|
||||
* Example: \snippet pistring.cpp PIString::data
|
||||
* \sa \a lengthAscii() */
|
||||
* \sa \a dataConsole(), \a dataUTF8() */
|
||||
const char * data() const {buildData(); return (const char *)(data_.data());}
|
||||
|
||||
/*! \brief Return \c char * representation of this string in terminal codepage
|
||||
* \details This function fill buffer by sequence
|
||||
* of chars. Minimum length of this buffer is count
|
||||
* of symbols. Returned \c char * is valid until next
|
||||
* execution of this function.\n
|
||||
* \sa \a data(), \a dataUTF8() */
|
||||
const char * dataConsole() const;
|
||||
|
||||
/*! \brief Return \c char * representation of this string in UTF-8
|
||||
* \details This function fill buffer by sequence
|
||||
* of chars. Minimum length of this buffer is count
|
||||
* of symbols. Returned \c char * is valid until next
|
||||
* execution of this function.\n
|
||||
* \sa \a data(), \a dataConsole() */
|
||||
const char * dataUTF8() const;
|
||||
|
||||
//! \brief Return \c std::string representation of this string
|
||||
std::string stdString() const {return convertToStd();}
|
||||
@@ -692,6 +708,15 @@ public:
|
||||
//! \brief Return "true" or "false"
|
||||
static PIString fromBool(const bool value) {return PIString(value ? "true" : "false");}
|
||||
|
||||
//! \brief Return string constructed from terminal codepage
|
||||
static PIString fromConsole(const char * s);
|
||||
|
||||
//! \brief Return string constructed from system codepage
|
||||
static PIString fromSystem(const char * s);
|
||||
|
||||
//! \brief Return string constructed from UTF-8
|
||||
static PIString fromUTF8(const char * s);
|
||||
|
||||
//! \brief Return string contains human readable size in B/kB/MB/GB/TB
|
||||
//! \details Example: \snippet pistring.cpp PIString::readableSize
|
||||
static PIString readableSize(llong bytes) {PIString s; s.setReadableSize(bytes); return s;}
|
||||
@@ -779,8 +804,8 @@ private:
|
||||
}*/
|
||||
return ret;
|
||||
}
|
||||
void appendFromChars(const char * c, int s);
|
||||
void buildData() const;
|
||||
void appendFromChars(const char * c, int s, const char * cp = 0);
|
||||
void buildData(const char * cp = 0) const;
|
||||
std::string convertToStd() const;
|
||||
#ifdef HAS_LOCALE
|
||||
wstring convertToWString() const {wstring s; for (int i = 0; i < length(); ++i) s.push_back(at(i).toWChar()); return s;}
|
||||
|
||||
Reference in New Issue
Block a user