diff --git a/main.cpp b/main.cpp index bac6c2b5..0fc283a4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,489 +1,14 @@ #include "pip.h" -template -class PIHash { - //template friend PIByteArray & operator >>(PIByteArray & s, PIHash & v); - //template friend PIByteArray & operator <<(PIByteArray & s, const PIHash & v); -public: - PIHash() {;} - PIHash(const PIHash & other) {*this = other;} - virtual ~PIHash() {;} - - PIHash & operator =(const PIHash & other) { - if (this == &other) return *this; - clear(); - pih_content = other.pih_content; - return *this; - } - - typedef T mapped_type; - typedef Key key_type; - typedef PIPair value_type; -/* - class iterator { - friend class PIHash; - private: - iterator(const PIHash * v, ssize_t p): parent(v), pos(p) {} - const PIHash * parent; - ssize_t pos; - public: - iterator(): parent(0), pos(0) {} - const Key & key() const {return const_cast * >(parent)->_key(pos);} - T & value() {return const_cast * >(parent)->_value(pos);} - void operator ++() {++pos;} - void operator ++(int) {++pos;} - void operator --() {--pos;} - void operator --(int) {--pos;} - bool operator ==(const iterator & it) const {return (pos == it.pos);} - bool operator !=(const iterator & it) const {return (pos != it.pos);} - }; - - class reverse_iterator { - friend class PIHash; - private: - reverse_iterator(const PIHash * v, ssize_t p): parent(v), pos(p) {} - const PIHash * parent; - ssize_t pos; - public: - reverse_iterator(): parent(0), pos(0) {} - const Key & key() const {return const_cast * >(parent)->_key(pos);} - T & value() const {return const_cast * >(parent)->_value(pos);} - void operator ++() {--pos;} - void operator ++(int) {--pos;} - void operator --() {++pos;} - void operator --(int) {++pos;} - bool operator ==(const reverse_iterator & it) const {return (pos == it.pos);} - bool operator !=(const reverse_iterator & it) const {return (pos != it.pos);} - }; -*/ - class const_iterator { - friend class PIHash; - private: - const_iterator(const PIHash * v, ssize_t p): parent(v), pos(p), bpos(0) { - if (pos == 0) { - pos = -1; - nextPos(); - } - } - void nextPos() { - while (++pos < parent->pih_content.size_s()) { - if (!parent->pih_content[pos].isEmpty()) - return; - } - } - const PIHash * parent; - ssize_t pos, bpos; - public: - const_iterator(): parent(0), pos(0) {} - //const value_type operator *() const {return parent->_pair(pos);} - //const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;} - const Key & key() const {return const_cast * >(parent)->pih_content[pos][bpos].key;} - const T & value() const {return const_cast * >(parent)->pih_content[pos][bpos].value;} - void operator ++() { - if (pos < parent->pih_content.size_s()) { - if (bpos >= parent->pih_content[pos].size_s() - 1) { - bpos = 0; - nextPos(); - } else - ++bpos; - } else { - bpos = 0; - ++pos; - } - printf(" ++: %d %d\n", pos, bpos); - } - //void operator ++(int) {++pos;} - void operator --() { - --pos; - } - //void operator --(int) {--pos;} - bool operator ==(const const_iterator & it) const {return (pos == it.pos && bpos == it.bpos);} - bool operator !=(const const_iterator & it) const {return !(*this == it);} - mutable value_type cval; - }; -/* - class const_reverse_iterator { - friend class PIHash; - private: - const_reverse_iterator(const PIHash * v, ssize_t p): parent(v), pos(p) {} - const PIHash * parent; - ssize_t pos; - public: - const_reverse_iterator(): parent(0), pos(0) {} - const value_type operator *() const {return parent->_pair(pos);} - const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;} - void operator ++() {--pos;} - void operator ++(int) {--pos;} - void operator --() {++pos;} - void operator --(int) {++pos;} - bool operator ==(const const_reverse_iterator & it) const {return (pos == it.pos);} - bool operator !=(const const_reverse_iterator & it) const {return (pos != it.pos);} - mutable value_type cval; - }; -*/ - //iterator begin() {return iterator(this, 0);} - //iterator end() {return iterator(this, size());} - const_iterator begin() const {return const_iterator(this, 0);} - const_iterator end() const {return const_iterator(this, size());} - const_iterator constBegin() const {return const_iterator(this, 0);} - const_iterator constEnd() const {return const_iterator(this, size());} - //reverse_iterator rbegin() {return reverse_iterator(this, size() - 1);} - //reverse_iterator rend() {return reverse_iterator(this, -1);} - //const_reverse_iterator rbegin() const {return const_reverse_iterator(this, size() - 1);} - //const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);} - //const_reverse_iterator constRbegin() const {return const_reverse_iterator(this, size() - 1);} - //const_reverse_iterator constRend() const {return const_reverse_iterator(this, -1);} - - size_t size() const {return pih_content.size();} - int size_s() const {return pih_content.size_s();} - size_t length() const {return pih_content.size();} - size_t capacity() const {return pih_content.size();} - bool isEmpty() const {return (pih_content.size() == 0);} - - T & operator [](const Key & key) { - if (pih_content.isEmpty()) _rehash(1); - uint k = piHash(key); - int i = _index(k); - if (i < pih_content.size_s()) { - PIVector & hv(pih_content[i]); - if (hv.size_s() == 1) { - if (hv[0].key == k) - return hv[0].value; - } - for (int j = 0; j < hv.size_s(); ++j) - if (hv[j].key == k) - return hv[j].value; - } - if (pih_content[i].size_s() >= 3) - _rehash(pih_content.size_s() * 2); - i = _index(k); - pih_content[i] << HashEntry(k); - return pih_content[i].back().value; - } - const T operator [](const Key & key) const {return value(key);} - T & at(const Key & key) {return (*this)[key];} - const T at(const Key & key) const {return (*this)[key];} - - PIHash & operator <<(const PIHash & other) { - if (other.isEmpty()) return *this; - for (int i = 0; i < other.pih_content.size_s(); ++i) - for (int j = 0; j < other.pih_content[i].size_s(); ++j) - insert(other.pih_content[i][j].key, other.pih_content[i][j].value); - return *this; - } - - bool operator ==(const PIHash & t) const {return (pih_content == t.pih_content);} - bool operator !=(const PIHash & t) const {return (pih_content != t.pih_content);} - bool contains(const Key & key) const { - bool f(false); - _find(key, f); - return f; - } - - PIHash & reserve(size_t new_size) {_rehash(new_size); return *this;} - - PIHash & remove(const Key & key) { - uint k = piHash(key); - int i = _index(k); - if (i >= pih_content.size_s()) return *this; - PIVector & hv(pih_content[i]); - for (int j = 0; j < hv.size_s(); ++j) - if (hv[j].key == k) { - hv.remove(j); - --j; - } - return *this; - } - PIHash & erase(const Key & key) {return remove(key);} - PIHash & clear() {pih_content.clear(); return *this;} - - void swap(PIHash & other) { - piSwapBinary > >(pih_content, other.pih_content); - } - - PIHash & insert(const Key & key, const T & value) { - (*this)[key] = value; - return *this; - } - const T value(const Key & key, const T & default_ = T()) const { - uint k = piHash(key); - int i = _index(k); - if (i >= pih_content.size_s()) return default_; - const PIVector & hv(pih_content[i]); - for (int j = 0; j < hv.size_s(); ++j) - if (hv[j].key == k) - return hv[j].value; - return default_; - } - PIVector values() const { - PIVector ret; - for (int i = 0; i < pih_content.size_s(); ++i) - for (int j = 0; j < pih_content[i].size_s(); ++j) - ret << pih_content[i][j].value; - return ret; - } - /*Key key(const T & value_, const Key & default_ = Key()) const { - for (int i = 0; i < pih_content.size_s(); ++i) - for (int j = 0; j < pih_content[i].size_s(); ++j) - if (pih_content[i][j].value == value_) - return pih_content[i][j].key; - return default_; - } - PIVector keys() const { - PIVector ret; - for (int i = 0; i < pih_content.size_s(); ++i) - for (int j = 0; j < pih_content[i].size_s(); ++j) - ret << pih_content[i][j].key; - return ret; - }*/ - - /*void dump() { - piCout << "PIHash" << size() << "entries" << PICoutManipulators::NewLine << "content:"; - for (size_t i = 0; i < pih_content.size(); ++i) - piCout << PICoutManipulators::Tab << i << ":" << pih_content[i]; - piCout << "index:"; - for (size_t i = 0; i < pim_index.size(); ++i) - piCout << PICoutManipulators::Tab << i << ":" << pim_index[i].key << "->" << pim_index[i].index; - }*/ - -protected: - struct HashEntry { - HashEntry(uint k = 0, const T & v = T()): key(k), value(v) {;} - uint key; - T value; - bool operator ==(const HashEntry & s) const {return key == s.key;} - bool operator !=(const HashEntry & s) const {return key != s.key;} - bool operator <(const HashEntry & s) const {return key < s.key;} - bool operator >(const HashEntry & s) const {return key > s.key;} - }; - /*template friend PIByteArray & operator >>(PIByteArray & s, PIDeque::HashEntry> & v); - template friend PIByteArray & operator <<(PIByteArray & s, const PIDeque::HashEntry> & v); - - const value_type _pair(ssize_t index) const { - if (index < 0 || index >= pim_index.size_s()) - return value_type(); - //piCout << "_pair" << index << pim_index[index].index; - return value_type(pim_index[index].key, pih_content[pim_index[index].index]); - } - Key & _key(ssize_t index) {return pim_index[index].key;} - T & _value(ssize_t index) {return pih_content[pim_index[index].index];}*/ - - inline size_t asize(size_t s) { - if (s == 0) return 0; - if (pih_content.size() + pih_content.size() >= s && pih_content.size() < s) - return pih_content.size() + pih_content.size(); - ssize_t t = 0, s_ = s - 1; - while (s_ >> t) ++t; - return (1 << t); - } - int _index(const uint & k) const { - return k % pih_content.size_s(); - } - void _rehash(int ns) { - ns = asize(ns); - if (pih_content.size_s() == ns) return; - PIVector > nhc; - nhc.resize(ns); - for (int i = 0; i < pih_content.size_s(); ++i) { - for (int j = 0; j < pih_content[i].size_s(); ++j) { - HashEntry & e(pih_content[i][j]); - int ni = e.key % ns; - nhc[ni] << e; - } - } - pih_content.swap(nhc); - } - - PIVector > pih_content; -}; - - -template -inline PICout operator <<(PICout s, const PIHash & v) { - s.space(); - s.setControl(0, true); - s << "{"; - bool first = true; - for (typename PIHash::const_iterator i = v.begin(); i != v.end(); ++i) { - if (!first) - s << ", "; - first = false; - s << i.key() << ": " << i.value(); - } - s << "}"; - s.restoreControl(); - return s; -} - - -struct HS { - HS() {i = 0; b0 = b1 = false;} - PIString str; - int i; - bool b0, b1; -}; -PICout operator <<(const PICout & s, const HS & v) {s << v.str << ", i =" << v.i << ", b =" << v.b0 << v.b1; return s;} - - - - -#include - -typedef PIVector MyType; -REGISTER_VARIANT(MyType) - - -class A: public PIObject { - PIOBJECT(A) -public: - EVENT1(e1, MyType, v) -}; - - -class B: public PIObject { - PIOBJECT(B) -public: - EVENT_HANDLER1(void, h1, MyType, v) {piCout << "handler h1" << v;} - EVENT_HANDLER(void, th) {piCout << "handler th";} -}; - - - int main() { - B b; - PIThread::runOnce([](){piCout << "thread 1"; piMSleep(1000);}, "first"); - PIThread::runOnce([](){piCout << "thread 2"; piMSleep(500);}, "second"); - PIThread::runOnce([](){piCout << "thread 3"; piMSleep(0);}, "third"); - PIThread::runOnce(&b, "th", "first th"); - PIThread::runOnce(&b, "th", "second th"); - PIThread::runOnce(&b, "th", "third th"); - piSleep(2); - return 0; - // - //PITimer timer([](){piCout << "timer";}); - //t.setSlot(test); - //timer.addDelimiter(5, [](void * d){piCout << "delim 5";}); - //timer.addDelimiter(2, [](){piCout << "delim 2";}); - //timer.start(100); - //piMSleep(2000); - - //A a; - //B b; - //int iii = 10; - //CONNECTL(&a, e1, [&](MyType v){piCout << "lambda" << v << iii; ++iii;}); - //std::function f = [&](MyType v){piCout << "lambda" << v << iii; ++iii;}; - //piCout << typeid([&](MyType v){piCout << "lambda" << v << iii; ++iii;}).name(); - //piCout << typeid(f).name(); - //a.e1(PIVector() << 1 << 2); - //piCout << iii; - // - //return 0; - - /*for (int i = 0; i < 0x2; ++i) { - PIByteArray ba, ba2; - writeOInt(ba, i); ba2 = ba; - uint u = readOInt(ba2); - piCout << "p =" << i << ", d =" << ba.toHex() << ", u =" << u; - } - for (int i = 0x7e; i < 0x82; ++i) { - PIByteArray ba, ba2; - writeOInt(ba, i); ba2 = ba; - uint u = readOInt(ba2); - piCout << "p =" << i << ", d =" << ba.toHex() << ", u =" << u; - } - for (int i = 0x3ffe; i < 0x4002; ++i) { - PIByteArray ba, ba2; - writeOInt(ba, i); ba2 = ba; - uint u = readOInt(ba2); - piCout << "p =" << i << ", d =" << ba.toHex() << ", u =" << u; - } - for (int i = 0x1ffffe; i < 0x200002; ++i) { - PIByteArray ba, ba2; - writeOInt(ba, i); ba2 = ba; - uint u = readOInt(ba2); - piCout << "p =" << i << ", d =" << ba.toHex() << ", u =" << u; - }*/ - HS hs; - hs.str = "123"; - hs.i = 321; - hs.b1 = true; - piCout << hs; - PIByteArray ba; - { - PIChunkStream cs(PIChunkStream::Version_2); - cs.add(1, hs.str).add(2, hs.i).add(3, hs.b0).add(4, hs.b1); - ba = cs.data(); - } - hs = HS(); - piCout << ba.size() << ba.toHex(); - piCout << hs; - { - PIChunkStream cs(ba); - cs.readAll(); - cs.get(1, hs.str).get(2, hs.i).get(3, hs.b0).get(4, hs.b1); - } - piCout << hs; - - return 0; - - PIConnection con; - PIString _str = "[connection]\ndevice.d = ser://COM2:9600\n"; - con.configureFromString(&_str); - con.start(); - PISerial * s = con.deviceByName("d")->cast(); - while (1) { - piMSleep(500); - piCout << s; - if (s) - piCout << s->isOpened(); - } - return 0; - - PIHash h; - h["1"] = "x"; - h["2"] = "bbb"; - h["3"] = "bbc"; - h["4"] = "aaa"; - h["5"] = "123"; - h["6"] = "321"; - h["7"] = "aaa"; - h["7"] = "aaa!!!!!"; - piCout << h.size() << h.capacity(); - //h.reserve(64); - //piCout << h.keys() << h.values(); - //piCout << (1 << 2); - PIHash h2; - PIMap m2; - PIString prefix = "1234567890"; - PITimeMeasurer tm; - double el = 0.; - - tm.reset(); - for (int i=0; i<10000; ++i) { - h2[prefix + PIString::fromNumber(i)+"1234567890"] = PIString::fromNumber(randomi()); - } - el = tm.elapsed_m(); piCout << el << h2.capacity(); - - tm.reset(); - for (int i=0; i<10000; ++i) { - m2[prefix + PIString::fromNumber(i)+"1234567890"] = PIString::fromNumber(randomi()); - } - el = tm.elapsed_m(); piCout << el; - piCout << "*********"; - - PIString _s; - tm.reset(); - for (int i=10000; i>=0; --i) { - _s = h2.value(prefix + PIString::fromNumber(i)+"1234567890"); - } - el = tm.elapsed_m(); piCout << el << h2.capacity(); - - tm.reset(); - for (int i=10000; i>=0; --i) { - _s = m2.value(prefix + PIString::fromNumber(i)+"1234567890"); - } - el = tm.elapsed_m(); piCout << el; - + //piCout << __sysoemname__; + const char * s = "Eng, Русский №!123"; + PIString str = PIString::fromUTF8(s); + piCout << str.size() << str; + str.forEach([](PIChar c){piCout << c; return c;}); + PIFile f("1.txt", PIIODevice::ReadWrite); + f.clear(); + f << str; return 0; } diff --git a/src_main/core/pichar.cpp b/src_main/core/pichar.cpp index 4dafe5c7..df23c67f 100644 --- a/src_main/core/pichar.cpp +++ b/src_main/core/pichar.cpp @@ -26,9 +26,14 @@ # define U_NOEXCEPT # include "unicode/ucnv.h" # include "unicode/ustring.h" +#else +# ifdef WINDOWS +# include +# endif +#endif char * __syslocname__ = 0; char * __sysoemname__ = 0; -#endif +char * __utf8name__ = 0; #ifdef BLACKBERRY # include #endif @@ -47,88 +52,64 @@ char * __sysoemname__ = 0; */ -PIChar::PIChar(const char * c, int * bytes) { +ushort charFromCodepage(const char * c, int size, const char * codepage, int * taken = 0) { + if (!c || size <= 0) return 0; + if (c[0] < 0x80) return c[0]; + int ret; #ifdef PIP_ICU UErrorCode e((UErrorCode)0); - UConverter * cc = ucnv_open(__syslocname__, &e); + UConverter * cc = ucnv_open(codepage, &e); if (cc) { UChar uc; e = (UErrorCode)0; - int ret = ucnv_toUChars(cc, &uc, 1, c, 4, &e); - if (bytes) * bytes = ret; + ret = ucnv_toUChars(cc, &uc, 1, c, size, &e); + if (taken) *taken = ret; ucnv_close(cc); - ch = uc; - return; + return ushort(uc); } +#else +# ifdef WINDOWS + wchar_t buffer; + ret = MultiByteToWideChar((uint)(uintptr_t)codepage, MB_ERR_INVALID_CHARS, c, size, &buffer, 1); + if (ret <= 0) return 0; + if (taken) *taken = ret; + return buffer; + //printf("request %d\n", sz); +# endif #endif - ch = *reinterpret_cast(c); + wchar_t wc(0); + mbtowc(0, 0, 0); // reset mbtowc + ret = mbtowc(&wc, c, sizeof(ushort)); + if (ret < 1 || ret > (int)sizeof(ushort)) return 0; + else return wc; + return c[0]; +} + + +PIChar::PIChar(const char * c, int * bytes) { + ch = charFromCodepage(c, 4, __syslocname__, bytes); } 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; + ret.ch = charFromCodepage(&c, 1, __sysoemname__); 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; + ret.ch = charFromCodepage(&c, 1, __syslocname__); 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 -//#ifdef ANDROID - wchar_t wc(0); - mbtowc(0, 0, 0); // reset mbtowc - //mbstate_t s; - //mbrtowc(&wc, c, 4, &s); - int sz = 0; - sz = mbtowc(&wc, c, sizeof(ushort)); - if (sz < 1 || sz > (int)sizeof(ushort)) ret.ch = 0; - else ret.ch = wc; - return ret; -//#endif -// ret.ch = *(ushort*)c; + int l = 0; + while (c[l] != '\0') ++l; + ret.ch = charFromCodepage(c, l, __utf8name__); return ret; } @@ -255,6 +236,11 @@ char PIChar::toConsole1Byte() const { ucnv_close(cc); return uc[0]; } +#endif +#ifdef WINDOWS + char ret[4] = {0,0,0,0}; + WideCharToMultiByte(CP_OEMCP, 0, (LPCWCH)&ch, 1, ret, 4, NULL, NULL); + return ret[0]; #endif return toAscii(); } @@ -272,6 +258,11 @@ char PIChar::toSystem() const { ucnv_close(cc); return uc[0]; } +#endif +#ifdef WINDOWS + char ret[4] = {0,0,0,0}; + WideCharToMultiByte(CP_ACP, 0, (LPCWCH)&ch, 1, ret, 4, NULL, NULL); + return ret[0]; #endif return toAscii(); } @@ -316,7 +307,14 @@ PICout operator <<(PICout s, const PIChar & v) { s << uc; } else #endif +#ifdef WINDOWS + //if (PICout::isBufferActive()) + s << v.toSystem(); + //else + // s << v.toConsole1Byte(); +#else s << v.toCharPtr(); +#endif s.restoreControl(); return s; } diff --git a/src_main/core/pichar.h b/src_main/core/pichar.h index 0436bfeb..07ba037c 100755 --- a/src_main/core/pichar.h +++ b/src_main/core/pichar.h @@ -25,10 +25,9 @@ #include "piincludes.h" -#ifdef PIP_ICU extern char * __syslocname__; extern char * __sysoemname__; -#endif +extern char * __utf8name__; class PIP_EXPORT PIChar { diff --git a/src_main/core/piinit.cpp b/src_main/core/piinit.cpp index fc29d548..5f2b223e 100644 --- a/src_main/core/piinit.cpp +++ b/src_main/core/piinit.cpp @@ -92,6 +92,7 @@ PRIVATE_DEFINITION_START(PIInit) #ifdef WINDOWS HMODULE ntlib; ULONG prev_res; + bool delete_locs; #endif PRIVATE_DEFINITION_END(PIInit) @@ -186,13 +187,16 @@ PIInit::PIInit() { actions.sa_handler = android_thread_exit_handler; sigaction(SIGTERM, &actions, 0); #endif -#ifdef PIP_ICU + PRIVATE->delete_locs = false; __syslocname__ = __sysoemname__ = 0; + __utf8name__ = "UTF-8"; +#ifdef PIP_ICU //__syslocname__ = new char[256]; //memset(__syslocname__, 0, 256); UErrorCode e((UErrorCode)0); u_init(&e); # ifdef WINDOWS + PRIVATE->delete_locs = true; CPINFOEX cpinfo; int l = 0; /*GetCPInfoEx(CP_ACP, 0, &cpinfo); @@ -218,6 +222,12 @@ PIInit::PIInit() { # endif //piCout << __syslocname__; //piCout << __sysoemname__; +#else +# ifdef WINDOWS + __syslocname__ = (char *)CP_ACP; + __sysoemname__ = (char *)CP_OEMCP; + __utf8name__ = (char *)CP_UTF8; +# endif #endif #ifdef MAC_OS host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &__pi_mac_clock); @@ -315,9 +325,11 @@ PIInit::~PIInit() { #ifdef MAC_OS mach_port_deallocate(mach_task_self(), __pi_mac_clock); #endif + if (PRIVATE->delete_locs) { + if (__syslocname__) delete __syslocname__; + if (__sysoemname__) delete __sysoemname__; + } #ifdef PIP_ICU - if (__syslocname__) delete __syslocname__; - if (__sysoemname__) delete __sysoemname__; u_cleanup(); #endif //if (currentLocale_t != 0) freelocale(currentLocale_t); diff --git a/src_main/core/pistring.cpp b/src_main/core/pistring.cpp index 8a50c485..c7d042ec 100755 --- a/src_main/core/pistring.cpp +++ b/src_main/core/pistring.cpp @@ -21,6 +21,10 @@ #ifdef PIP_ICU # define U_NOEXCEPT # include "unicode/ucnv.h" +#else +# ifdef WINDOWS +# include +# endif #endif #include #ifdef ANDROID @@ -205,6 +209,7 @@ llong PIString::toNumberBase(const PIString & value, int base, bool * ok) { void PIString::appendFromChars(const char * c, int s, const char * cp) { if (s <= 0) return; + int sz; #ifdef PIP_ICU UErrorCode e((UErrorCode)0); UConverter * cc = ucnv_open(cp, &e); @@ -212,16 +217,27 @@ void PIString::appendFromChars(const char * c, int s, const char * cp) { UChar * ucs = new UChar[s]; memset(ucs, 0, s * sizeof(UChar)); e = (UErrorCode)0; - int rs = ucnv_toUChars(cc, ucs, s, c, s, &e); - //printf("appendFromChars %d -> %d\n", s, rs); - for (int i = 0; i < rs; ++i) + int sz = ucnv_toUChars(cc, ucs, s, c, s, &e); + //printf("appendFromChars %d -> %d\n", s, sz); + for (int i = 0; i < sz; ++i) push_back(PIChar(ucs[i])); delete[] ucs; ucnv_close(cc); return; } +#else +# ifdef WINDOWS + sz = MultiByteToWideChar((uint)(uintptr_t)cp, MB_ERR_INVALID_CHARS, c, s, 0, 0); + if (sz <= 0) return; + wchar_t * buffer = new wchar_t[sz]; + MultiByteToWideChar((uint)(uintptr_t)cp, MB_ERR_INVALID_CHARS, c, s, buffer, sz); + for (int i = 0; i < sz; ++i) + push_back(PIChar((ushort)buffer[i])); + delete[] buffer; + return; + //printf("request %d\n", sz); +# endif #endif - int sz; wchar_t wc; mbtowc(0,0,0); // reset mbtowc //qDebug() << "FromChars ..."; @@ -242,11 +258,7 @@ 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 - ); + if (l > 0) ret.appendFromChars(s, l, __sysoemname__); return ret; } @@ -256,7 +268,7 @@ PIString PIString::fromSystem(const char * s) { int l = 0; while (s[l] != '\0') ++l; PIString ret; - if (l > 0) ret.appendFromChars(s, l); + if (l > 0) ret.appendFromChars(s, l, __syslocname__); return ret; } @@ -265,11 +277,7 @@ 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 - ); + if (l > 0) ret.appendFromChars(s, l, __utf8name__); return ret; } @@ -277,11 +285,7 @@ PIString PIString::fromUTF8(const char * s) { PIString PIString::fromUTF8(const PIByteArray & ba) { PIString ret; if (ba.isEmpty()) return ret; - ret.appendFromChars((const char*)ba.data(), ba.size() -#ifdef PIP_ICU - , "UTF-8" -#endif - ); + ret.appendFromChars((const char*)ba.data(), ba.size(), __utf8name__); return ret; } @@ -304,6 +308,10 @@ PIString PIString::fromCodepage(const char * s, const char * c) { if (l > 0) ret.appendFromChars(s, l #ifdef PIP_ICU , c +#else +# ifdef WINDOWS + , __utf8name__ +# endif #endif ); return ret; @@ -319,60 +327,53 @@ PIString PIString::readableSize(llong bytes) { void PIString::buildData(const char * cp) const { data_.clear(); + int sz = 0; #ifdef PIP_ICU UErrorCode e((UErrorCode)0); 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]; for (int i = 0; i < size_s(); ++i) { if (at(i).isAscii()) data_.push_back(uchar(at(i).unicode16Code())); else { e = (UErrorCode)0; - rs = ucnv_fromUChars(cc, uc, 8, (const UChar*)(PIDeque::data(i)), 1, &e); - //printf("conv %d\n", rs); - for (int j = 0; j < rs; ++j) + sz = ucnv_fromUChars(cc, uc, 8, (const UChar*)(PIDeque::data(i)), 1, &e); + for (int j = 0; j < sz; ++j) data_.push_back(uc[j]); } } - //printf("buildData %d -> %d\n", size_s(), data_.size_s()); ucnv_close(cc); data_.push_back('\0'); return; } +#else +# ifdef WINDOWS + sz = WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)PIDeque::data(), PIDeque::size_s(), 0, 0, NULL, NULL); + //printf("WideCharToMultiByte %d %d\n", (uint)(uintptr_t)cp, sz); + if (sz <= 0) { + //printf("WideCharToMultiByte erro %d\n", GetLastError()); + data_.push_back(uchar('\0')); + return; + } + data_.resize(sz); + WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)PIDeque::data(), PIDeque::size_s(), (LPSTR)data_.data(), data_.size_s(), NULL, NULL); + data_.push_back(uchar('\0')); + return; +# endif #endif wchar_t wc; char tc[8]; -// mbstate_t s; -// mbrlen(0,0,&s); wctomb(0, 0); - int sz(0); - //printf("PIString::data %d\n", size_s()); for (int i = 0; i < size_s(); ++i) { if (at(i).isAscii()) { data_.push_back(uchar(at(i).toAscii())); continue; } wc = at(i).toWChar(); -// sz = piClampi(wcrtomb(tc, wc, &s), 0, 4); sz = wctomb(tc, wc); for (int b = 0; b < sz; ++b) data_.push_back(uchar(tc[b])); - //printf("__%d_%d\n", i, wc); - /*tc = wc & 0xFF; - while (tc) { - data_.push_back(uchar(tc)); - wc >>= 8; - tc = wc & 0xFF; - //printf("____%d\n", wc); - }*/ - /*if (at(i).isAscii()) - data_.push_back(uchar(at(i).toAscii())); - else { - data_.push_back((at(i).toCharPtr()[0])); ++j; - data_.push_back((at(i).toCharPtr()[1])); - }*/ } data_.push_back(uchar('\0')); } @@ -390,21 +391,13 @@ void PIString::trimsubstr(int &st, int &fn) const { const char * PIString::dataConsole() const { - buildData( -#ifdef PIP_ICU - __sysoemname__ -#endif - ); + buildData(__sysoemname__ ); return (const char *)(data_.data()); } const char * PIString::dataUTF8() const { - buildData( -#ifdef PIP_ICU - "UTF-8" -#endif - ); + buildData(__utf8name__); return (const char *)(data_.data()); } @@ -425,11 +418,7 @@ uint PIString::hash() const { PIByteArray PIString::toUTF8() const { if (isEmpty()) return data_.resized(0); - buildData( -#ifdef PIP_ICU - "UTF-8" -#endif - ); + buildData(__utf8name__); return data_.resized(data_.size_s() - 1); } @@ -439,6 +428,10 @@ PIByteArray PIString::toCharset(const char * c) const { buildData( #ifdef PIP_ICU c +#else +# ifdef WINDOWS + __utf8name__ +# endif #endif ); return data_.resized(data_.size_s() - 1); @@ -450,7 +443,7 @@ PIString & PIString::operator +=(const char * str) { if (!str) return *this; int l = 0; while (str[l] != '\0') ++l; - appendFromChars(str, l); + appendFromChars(str, l, __syslocname__); return *this; } @@ -1119,17 +1112,11 @@ PICout operator <<(PICout s, const PIString & v) { s.space(); s.quote(); s.setControl(0, true); - if (PICout::isBufferActive()) + //if (PICout::isBufferActive()) s << v.data(); - else { -#ifdef WINDOWS - PIByteArray d = v.toByteArray(); - d.push_back(uchar('\0')); - s << (const char*)d.data(); -#else - s << v.dataConsole(); -#endif - } + //else { + // s << v.dataConsole(); + //} s.restoreControl(); s.quote(); return s; diff --git a/src_main/core/pistring.h b/src_main/core/pistring.h index f701263c..4c977f8b 100755 --- a/src_main/core/pistring.h +++ b/src_main/core/pistring.h @@ -439,7 +439,7 @@ public: * returned by function \a data() - 1, without terminating null-char \n * Example: \snippet pistring.cpp PIString::lengthAscii * \sa \a data() */ - int lengthAscii() const {buildData(); return data_.size_s() - 1;} + int lengthAscii() const {buildData(__syslocname__); return data_.size_s() - 1;} /*! \brief Return \c char * representation of this string in system codepage * \details This function fill buffer by sequence @@ -448,7 +448,7 @@ public: * execution of this function.\n * Example: \snippet pistring.cpp PIString::data * \sa \a dataConsole(), \a dataUTF8() */ - const char * data() const {buildData(); return (const char *)(data_.data());} + const char * data() const {buildData(__syslocname__); return (const char *)(data_.data());} /*! \brief Return \c char * representation of this string in terminal codepage * \details This function fill buffer by sequence @@ -477,7 +477,7 @@ public: uint hash() const; //! \brief Return \a PIByteArray contains \a data() of this string without terminating null-char - PIByteArray toByteArray() const {buildData(); return data_.resized(data_.size_s() - 1);} + PIByteArray toByteArray() const {buildData(__utf8name__); return data_.resized(data_.size_s() - 1);} //! \brief Return \a PIByteArray contains UTF-8 \a data() of this string without terminating null-char PIByteArray toUTF8() const; @@ -750,8 +750,8 @@ private: static PIString fromNumberBaseS(const llong value, int base = 10, bool * ok = 0); static PIString fromNumberBaseU(const ullong value, int base = 10, bool * ok = 0); static llong toNumberBase(const PIString & value, int base = -1, bool * ok = 0); - void appendFromChars(const char * c, int s, const char * cp = 0); - void buildData(const char * cp = 0) const; + void appendFromChars(const char * c, int s, const char * cp = __syslocname__); + void buildData(const char * cp = __syslocname__) const; void trimsubstr(int &st, int &fn) const; mutable PIByteArray data_; };