Merge branch 'master' of https://git.shs.tools/SHS/pip
This commit is contained in:
@@ -812,11 +812,12 @@ bool PITerminal::initialize() {
|
||||
//piCoutObj << fr << PRIVATE->fd << pty;
|
||||
if (fr == 0) {
|
||||
char ** argv = new char*[2];
|
||||
argv[0] = new char[PRIVATE->shell.lengthAscii() + 1];
|
||||
memcpy(argv[0], PRIVATE->shell.dataAscii(), PRIVATE->shell.lengthAscii());
|
||||
argv[0][PRIVATE->shell.lengthAscii()] = 0;
|
||||
PIByteArray shell = PRIVATE->shell.toByteArray();
|
||||
argv[0] = new char[shell.size() + 1];
|
||||
memcpy(argv[0], shell.data(), shell.size());
|
||||
argv[0][shell.size()] = 0;
|
||||
argv[1] = 0;
|
||||
execvp(PRIVATE->shell.dataAscii(), argv);
|
||||
execvp(shell.data(), argv);
|
||||
delete[] argv[0];
|
||||
delete[] argv;
|
||||
exit(0);
|
||||
|
||||
@@ -540,7 +540,7 @@ PICout & PICout::writePIString(const PIString & s) {
|
||||
buffer_->append(s);
|
||||
} else {
|
||||
if (PICout::isOutputDeviceActive(PICout::StdOut)) {
|
||||
for (const PIChar & c: s) std::wcout.put(c.toWChar());
|
||||
for (PIChar c: s) std::wcout.put(c.toWChar());
|
||||
}
|
||||
if (PICout::isOutputDeviceActive(PICout::Buffer)) PICout::__string__().append(s);
|
||||
}
|
||||
|
||||
@@ -121,7 +121,6 @@ public:
|
||||
|
||||
virtual uint classNameID() const {static uint ret = PIStringAscii("PIObject").hash(); return ret;}
|
||||
|
||||
static const PIString __classNameS() {return PIStringAscii("PIObject");}
|
||||
static const char * __classNameCC() {return "PIObject";}
|
||||
static uint __classNameIDS() {static uint ret = PIStringAscii("PIObject").hash(); return ret;}
|
||||
|
||||
|
||||
@@ -392,7 +392,6 @@
|
||||
protected: \
|
||||
typedef name __PIObject__; \
|
||||
public: \
|
||||
static const PIString __classNameS() {static PIString ret = PIStringAscii(#name); return ret;} \
|
||||
static const char * __classNameCC() {return #name;} \
|
||||
static uint __classNameIDS() {static uint ret = PIStringAscii(#name).hash(); return ret;} \
|
||||
virtual const char * className() const {return #name;} \
|
||||
|
||||
@@ -28,12 +28,6 @@
|
||||
# include <stringapiset.h>
|
||||
#endif
|
||||
#include <wchar.h>
|
||||
#ifdef ANDROID
|
||||
# if __ANDROID_API__ < 21
|
||||
# define wctomb(s, wc) wcrtomb(s, wc, NULL)
|
||||
# define mbtowc(pwc, s, n) mbrtowc(pwc, s, n, NULL)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
//! \addtogroup Core
|
||||
//! \{
|
||||
@@ -235,14 +229,15 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
return;
|
||||
//printf("request %d\n", sz);
|
||||
# else
|
||||
mbstate_t state;
|
||||
memset(&state, 0, sizeof(state));
|
||||
wchar_t wc;
|
||||
mbtowc(0,0,0); // reset mbtowc
|
||||
//qDebug() << "FromChars ...";
|
||||
while (s>0) {
|
||||
while (sz = mbrtowc(&wc, c, s, &state) > 0) {
|
||||
//qDebug() << "0" << s;
|
||||
sz = mbtowc(&wc, c, s);
|
||||
// sz = mbrtowc(&wc, c, s, &state);
|
||||
//qDebug() << "1" << sz;
|
||||
if (sz < 1) break;
|
||||
// if (sz < 1) break;
|
||||
push_back(PIChar(wc));
|
||||
c += sz; s -= sz;
|
||||
//qDebug() << "2" << c;
|
||||
@@ -254,37 +249,33 @@ void PIString::appendFromChars(const char * c, int s, const char * codepage) {
|
||||
|
||||
|
||||
PIString PIString::fromConsole(const char * s) {
|
||||
int l = 0;
|
||||
while (s[l] != '\0') ++l;
|
||||
PIString ret;
|
||||
if (l > 0) ret.appendFromChars(s, l, __sysoemname__);
|
||||
if (!s) return ret;
|
||||
if (s[0] != '\0') ret.appendFromChars(s, strlen(s), __sysoemname__);
|
||||
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, __syslocname__);
|
||||
if (!s) return ret;
|
||||
if (s[0] != '\0') ret.appendFromChars(s, strlen(s), __syslocname__);
|
||||
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, __utf8name__);
|
||||
if (!s) return ret;
|
||||
if (s[0] != '\0') ret.appendFromChars(s, strlen(s), __utf8name__);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::fromUTF8(const PIByteArray & ba) {
|
||||
PIString ret;
|
||||
if (ba.isEmpty()) return ret;
|
||||
ret.appendFromChars((const char*)ba.data(), ba.size(), __utf8name__);
|
||||
if (ba.isNotEmpty()) ret.appendFromChars((const char*)ba.data(), ba.size(), __utf8name__);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -296,19 +287,17 @@ PIString PIString::fromAscii(const char * s) {
|
||||
|
||||
PIString PIString::fromAscii(const char * s, int len) {
|
||||
PIString ret;
|
||||
ret.reserve(len);
|
||||
ret.resize(len);
|
||||
for (int l = 0; l < len; ++l) {
|
||||
ret.push_back(PIChar(s[l]));
|
||||
ret[l] = s[l];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIString PIString::fromCodepage(const char * s, const char * c) {
|
||||
int l = 0;
|
||||
while (s[l] != '\0') ++l;
|
||||
PIString ret;
|
||||
if (l > 0) ret.appendFromChars(s, l
|
||||
if (s[0] > '\0') ret.appendFromChars(s, strlen(s)
|
||||
#ifdef PIP_ICU
|
||||
, c
|
||||
#else
|
||||
@@ -342,7 +331,8 @@ PIString PIString::readableSize(llong bytes) {
|
||||
|
||||
|
||||
void PIString::buildData(const char * cp) const {
|
||||
data_.clear();
|
||||
//data_.clear();
|
||||
deleteData();
|
||||
int sz = 0;
|
||||
#ifdef PIP_ICU
|
||||
UErrorCode e((UErrorCode)0);
|
||||
@@ -371,34 +361,45 @@ void PIString::buildData(const char * cp) const {
|
||||
//printf("WideCharToMultiByte %d %d\n", (uint)(uintptr_t)cp, sz);
|
||||
if (sz <= 0) {
|
||||
//printf("WideCharToMultiByte erro %d\n", GetLastError());
|
||||
data_.push_back(uchar('\0'));
|
||||
data_ = (char *)malloc(1);
|
||||
data_[0] = '\0';
|
||||
return;
|
||||
}
|
||||
data_.resize(sz);
|
||||
WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)PIDeque<PIChar>::data(), PIDeque<PIChar>::size_s(), (LPSTR)data_.data(), data_.size_s(), NULL, NULL);
|
||||
data_.push_back(uchar('\0'));
|
||||
data_ = (char *)malloc(sz+1);
|
||||
//data_.resize(sz);
|
||||
WideCharToMultiByte((uint)(uintptr_t)cp, 0, (LPCWCH)PIDeque<PIChar>::data(), PIDeque<PIChar>::size_s(), (LPSTR)data_, sz, NULL, NULL);
|
||||
data_[sz] = '\0';
|
||||
return;
|
||||
# else
|
||||
wchar_t wc;
|
||||
char tc[8];
|
||||
wctomb(0, 0);
|
||||
//char tc[MB_CUR_MAX];
|
||||
mbstate_t state;
|
||||
memset(&state, 0, sizeof(state));
|
||||
data_ = (char *)malloc(MB_CUR_MAX*size()+1);
|
||||
char *p = data_;
|
||||
for (int i = 0; i < size_s(); ++i) {
|
||||
if (at(i).isAscii()) {
|
||||
data_.push_back(uchar(at(i).toAscii()));
|
||||
continue;
|
||||
}
|
||||
// if (at(i).isAscii()) {
|
||||
// data_.push_back(uchar(at(i).toAscii()));
|
||||
// continue;
|
||||
// }
|
||||
wc = at(i).toWChar();
|
||||
sz = wctomb(tc, wc);
|
||||
for (int b = 0; b < sz; ++b) {
|
||||
data_.push_back(uchar(tc[b]));
|
||||
}
|
||||
sz = wcrtomb(p, wc, &state);
|
||||
if (sz < 0) break;
|
||||
p += sz;
|
||||
}
|
||||
data_.push_back(uchar('\0'));
|
||||
p[0] = '\0';
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void PIString::deleteData() const {
|
||||
if (!data_) return;
|
||||
free(data_);
|
||||
data_ = nullptr;
|
||||
}
|
||||
|
||||
|
||||
void PIString::trimsubstr(int &st, int &fn) const {
|
||||
for (int i = 0; i < length(); ++i) {
|
||||
if (at(i) != ' ' && at(i) != '\t' && at(i) != '\n' && at(i) != '\r' && at(i) != char(12) && at(i) != uchar(0)) {
|
||||
@@ -422,14 +423,14 @@ uint PIString::hash() const {
|
||||
|
||||
|
||||
PIByteArray PIString::toUTF8() const {
|
||||
if (isEmpty()) return data_.resized(0);
|
||||
if (isEmpty()) return PIByteArray(1,'\0');
|
||||
buildData(__utf8name__);
|
||||
return data_.resized(data_.size_s() - 1);
|
||||
return PIByteArray(data_, strlen(data_));
|
||||
}
|
||||
|
||||
|
||||
PIByteArray PIString::toCharset(const char * c) const {
|
||||
if (isEmpty()) return data_.resized(0);
|
||||
if (isEmpty()) return PIByteArray(1,'\0');
|
||||
buildData(
|
||||
#ifdef PIP_ICU
|
||||
c
|
||||
@@ -439,20 +440,22 @@ PIByteArray PIString::toCharset(const char * c) const {
|
||||
# endif
|
||||
#endif
|
||||
);
|
||||
return data_.resized(data_.size_s() - 1);
|
||||
|
||||
return PIByteArray(data_, strlen(data_));
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const char * str) {
|
||||
if (!str) return *this;
|
||||
int l = 0;
|
||||
while (str[l] != '\0') ++l;
|
||||
appendFromChars(str, l, __syslocname__);
|
||||
appendFromChars(str, strlen(str), __syslocname__);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
PIString::~PIString() {
|
||||
deleteData();
|
||||
}
|
||||
|
||||
|
||||
PIString & PIString::operator +=(const wchar_t * str) {
|
||||
if (!str) return *this;
|
||||
int i = -1;
|
||||
@@ -1350,20 +1353,6 @@ PIString PIString::inBrackets(const PIChar start, const PIChar end) const {
|
||||
}
|
||||
|
||||
|
||||
//! \~\details
|
||||
//! \~english It`s equivalent length of char sequence returned by function \a data() - 1, without terminating null-char
|
||||
//! \~russian Эквивалентно длине данных, возвращаемых \a data() - 1, без завершающего нулевого байта
|
||||
//! \~\code
|
||||
//! piCout << PIString("0123456789").lengthAscii(); // 10
|
||||
//! piCout << PIString("№1").lengthAscii(); // 3
|
||||
//! \endcode
|
||||
//! \~\sa \a data()
|
||||
int PIString::lengthAscii() const {
|
||||
buildData(__syslocname__);
|
||||
return data_.size_s() - 1;
|
||||
}
|
||||
|
||||
|
||||
//! \~\details
|
||||
//! \~english
|
||||
//! This function fill internal buffer by sequence
|
||||
@@ -1380,8 +1369,9 @@ int PIString::lengthAscii() const {
|
||||
//! \endcode
|
||||
//! \~\sa \a dataConsole(), \a dataUTF8()
|
||||
const char * PIString::data() const {
|
||||
if (isEmpty()) return "";
|
||||
buildData(__syslocname__);
|
||||
return (const char *)(data_.data());
|
||||
return data_;
|
||||
}
|
||||
|
||||
|
||||
@@ -1397,8 +1387,9 @@ const char * PIString::data() const {
|
||||
//! действителен до следующего вызова этого метода
|
||||
//! \~\sa \a data(), \a dataUTF8()
|
||||
const char * PIString::dataConsole() const {
|
||||
if (isEmpty()) return "";
|
||||
buildData(__sysoemname__ );
|
||||
return (const char *)(data_.data());
|
||||
return data_;
|
||||
}
|
||||
|
||||
|
||||
@@ -1414,8 +1405,9 @@ const char * PIString::dataConsole() const {
|
||||
//! действителен до следующего вызова этого метода
|
||||
//! \~\sa \a data(), \a dataConsole()
|
||||
const char * PIString::dataUTF8() const {
|
||||
if (isEmpty()) return "";
|
||||
buildData(__utf8name__);
|
||||
return (const char *)(data_.data());
|
||||
return data_;
|
||||
}
|
||||
|
||||
|
||||
@@ -1431,12 +1423,14 @@ const char * PIString::dataUTF8() const {
|
||||
//! действителен до следующего вызова этого метода
|
||||
//! \~\sa \a dataConsole(), \a dataUTF8()
|
||||
const char * PIString::dataAscii() const {
|
||||
data_.clear();
|
||||
if (isEmpty()) return "";
|
||||
deleteData();
|
||||
data_ = (char*)malloc(size()+1);
|
||||
for (int i = 0; i < size_s(); ++i) {
|
||||
data_.push_back(uchar(at(i).ch));
|
||||
data_[i] = uchar(at(i).ch);
|
||||
}
|
||||
data_.push_back(uchar('\0'));
|
||||
return (const char *)data_.data();
|
||||
data_[size()] = '\0';
|
||||
return data_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -131,7 +131,7 @@ public:
|
||||
//! \endcode
|
||||
PIString(const int len, const PIChar c): PIDeque<PIChar>() {for (int i = 0; i < len; ++i) PIDeque<PIChar>::push_back(c);}
|
||||
|
||||
~PIString() {}
|
||||
~PIString();
|
||||
|
||||
|
||||
//! \~english Assign operator
|
||||
@@ -749,10 +749,6 @@ public:
|
||||
//! \~russian Возвращает строку между символами "start" и "end" с начала строки
|
||||
PIString inBrackets(const PIChar start, const PIChar end) const;
|
||||
|
||||
//! \~english Returns bytes count of this string in system codepage
|
||||
//! \~russian Возвращает объем строки в системной кодировке
|
||||
int lengthAscii() const;
|
||||
|
||||
//! \~english Returns \c char * representation of this string in system codepage
|
||||
//! \~russian Возвращает \c char * представление строки в системной кодировке
|
||||
const char * data() const;
|
||||
@@ -773,9 +769,9 @@ public:
|
||||
//! \~russian Возвращает хэш
|
||||
uint hash() const;
|
||||
|
||||
//! \~english Returns \a PIByteArray contains \a data() of this string without terminating null-char
|
||||
//! \~russian Возвращает \a PIByteArray содержащий \a data() строки без завершающего нулевого байта
|
||||
PIByteArray toByteArray() const {buildData(__utf8name__); return data_.resized(data_.size_s() - 1);}
|
||||
//! \~english Same as \a toUTF8().
|
||||
//! \~russian Тоже самое, что \a toUTF8().
|
||||
PIByteArray toByteArray() const {return toUTF8();}
|
||||
|
||||
//! \~english Returns \a PIByteArray contains \a dataUTF8() of this string without terminating null-char
|
||||
//! \~russian Возвращает \a PIByteArray содержащий \a dataUTF8() строки без завершающего нулевого байта
|
||||
@@ -1401,8 +1397,10 @@ private:
|
||||
static llong toNumberBase(const PIString & value, int base = -1, bool * ok = 0);
|
||||
void appendFromChars(const char * c, int s, const char * cp = __syslocname__);
|
||||
void buildData(const char * cp = __syslocname__) const;
|
||||
void deleteData() const;
|
||||
void trimsubstr(int &st, int &fn) const;
|
||||
mutable PIByteArray data_;
|
||||
|
||||
mutable char * data_ = nullptr;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -67,8 +67,9 @@ int PIIOString::readDevice(void * read_to, int max_size) {
|
||||
PIString rs = str->mid(pos, max_size);
|
||||
pos += max_size;
|
||||
if (pos > str->size_s()) pos = str->size_s();
|
||||
int ret = rs.lengthAscii();
|
||||
memcpy(read_to, rs.data(), rs.lengthAscii());
|
||||
const char * cc = rs.data();
|
||||
int ret = strlen(cc);
|
||||
memcpy(read_to, cc, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -81,7 +82,7 @@ int PIIOString::writeDevice(const void * data, int max_size) {
|
||||
if (rs.size_s() > max_size) rs.resize(max_size);
|
||||
str->insert(pos, rs);
|
||||
pos += rs.size_s();
|
||||
return rs.lengthAscii();
|
||||
return strlen((const char *)data);
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +91,7 @@ int PIIOString::writeString(const PIString & string) {
|
||||
if (pos > str->size_s()) pos = str->size_s();
|
||||
str->insert(pos, string);
|
||||
pos += string.size_s();
|
||||
return string.lengthAscii();
|
||||
return string.size_s();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -115,44 +115,37 @@ void PIProcess::startProc(bool detached) {
|
||||
//cout << "run" << endl;
|
||||
PIString str;
|
||||
/// arguments convertion
|
||||
#ifdef WINDOWS
|
||||
int as = 0;
|
||||
piForeachC (PIString & i, args)
|
||||
as += i.lengthAscii() + 3;
|
||||
const char * argscc[args.size()+1];
|
||||
int argsl[args.size()];
|
||||
for (int i = 0; i < args.size_s(); ++i) {
|
||||
argscc[i] = args[i].data();
|
||||
argsl[i] = strlen(argscc[i]);
|
||||
as += argsl[i] + 3;
|
||||
}
|
||||
argscc[args.size()] = 0;
|
||||
#ifdef WINDOWS
|
||||
char * a = new char[as];
|
||||
memset(a, ' ', as - 1);
|
||||
as = 0;
|
||||
for (int i = 0; i < args.size_s(); ++i) {
|
||||
str = args[i];
|
||||
a[as] = '"';
|
||||
memcpy(&(a[as + 1]), str.data(), str.lengthAscii());
|
||||
a[as + str.lengthAscii() + 1] = '"';
|
||||
as += str.lengthAscii() + 3;
|
||||
memcpy(&(a[as + 1]), argscc[i], argsl[i]);
|
||||
a[as + argsl[i] + 1] = '"';
|
||||
as += argsl[i] + 3;
|
||||
}
|
||||
a[as - 1] = 0;
|
||||
//piCout << a;
|
||||
#else
|
||||
//piCout << "#" << args;
|
||||
char * a[args.size_s() + 1];
|
||||
for (int i = 0; i < args.size_s(); ++i) {
|
||||
str = args[i];
|
||||
//piCout << i << str << str.size() << str.lengthAscii() << str.lengthAscii() << str.lengthAscii();
|
||||
a[i] = new char[str.lengthAscii() + 1];
|
||||
memcpy(a[i], str.data(), str.lengthAscii());
|
||||
a[i][str.lengthAscii()] = 0;
|
||||
}
|
||||
a[args.size_s()] = 0;
|
||||
#endif
|
||||
#ifndef WINDOWS
|
||||
/// environment convertion
|
||||
char ** e = new char*[env.size_s() + 1];
|
||||
const char * envcc[env.size()+1];
|
||||
envcc[env.size_s()] = 0;
|
||||
for (int i = 0; i < env.size_s(); ++i) {
|
||||
str = env[i];
|
||||
e[i] = new char[str.lengthAscii() + 1];
|
||||
memcpy(e[i], str.data(), str.lengthAscii());
|
||||
e[i][str.lengthAscii()] = 0;
|
||||
//cout << e[i] << endl;
|
||||
envcc[i] = env[i].data();
|
||||
}
|
||||
e[env.size_s()] = 0;
|
||||
#endif
|
||||
/// files for stdin/out/err
|
||||
t_in = t_out = t_err = false;
|
||||
if (f_in.path().isEmpty()) {
|
||||
@@ -196,7 +189,7 @@ void PIProcess::startProc(bool detached) {
|
||||
0, // Thread handle not inheritable
|
||||
false, // Set handle inheritance to FALSE
|
||||
detached ? DETACHED_PROCESS/*CREATE_NEW_CONSOLE*/ : 0, // Creation flags
|
||||
0,//e, // Use environment
|
||||
0,//envcc, // Use environment
|
||||
wd.isEmpty() ? 0 : wd.data(), // Use working directory
|
||||
&(PRIVATE->si), // Pointer to STARTUPINFO structure
|
||||
&(PRIVATE->pi))) // Pointer to PROCESS_INFORMATION structure
|
||||
@@ -209,7 +202,7 @@ void PIProcess::startProc(bool detached) {
|
||||
#else
|
||||
|
||||
//cout << "exec " << tf_in << ", " << tf_out << ", " << tf_err << endl;
|
||||
if (execve(str.data(), a, e) < 0)
|
||||
if (execve(str.data(), argscc, envcc) < 0)
|
||||
piCoutObj << "\"execve" << str << args << "\" error :" << errorString();
|
||||
} else {
|
||||
piMinSleep;
|
||||
@@ -224,14 +217,8 @@ void PIProcess::startProc(bool detached) {
|
||||
#endif
|
||||
if (!detached) execFinished(str, exit_code);
|
||||
is_exec = false;
|
||||
for (int i = 0; i < env.size_s(); ++i)
|
||||
delete e[i];
|
||||
delete[] e;
|
||||
#ifdef WINDOWS
|
||||
delete a;
|
||||
#else
|
||||
for (int i = 0; i < args.size_s(); ++i)
|
||||
delete a[i];
|
||||
delete[] a;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user