First, I renamed your string.h to fbstring_compat.h and changed ustring to a proper class.
fbstring_compat.h
Code: Select all
#include <cstdlib>
#include <cstdio>
#include <string.h>
#include <iostream>
#include <locale>
/*
* Класс для работы с динамическими строками (внутренний формат UTF8)
*
* доступные операторы:
* + объединяет две строки ustring. Так же можно в один из параметров отправлять char*
* += объединяет текущую строку с другой строкой ustring. Так же можно в параметр отправлять char*
* == проверка строк на равенство.
* != проверка строк на неравенство.
* * возвращает указатель на fbstring*
* = присвоение строки ustring или const char*
* [] индексация (возвращает отдельный байт по индексу)
*
* конструкторы (примеры)
* ustring s; // пустая строка , содержит только символ конца строки
* ustring s1 = s; // создание новой строки из существующей ustring
* ustring s2 = "one"; // создание новой строки с помощью типа const char*
* ustring s2 = 5; // создание строки с выделенной памятью на 5 байт
* ustring s2 = {8,'Q'}; // создание строки с выделенной памятью на 8 байт и символом по умолчанию (во 2 параметре передается только один байт char)
*
* функции
* lensymbols кол-во символов в строке
* len кол-во байт в строке
* in_unicode возвращает unicode представление строки wchar_t*
* in_utf8 создает обычную строку ustring из unicode строки wchar_t*
* mid возвращает часть строки. По умолчанию работает с символами , но может работать с отдельными байтами
* instr поиск подстроки. По умолчанию работает с символами , но может работать с отдельными байтами
* trim обрезка ненужных символов с обих сторон
* ltrim обрезка ненужных символов слева
* ltrim обрезка ненужных символов справа
* chr возвращает строку ustring из отдельного символа char
* asc возвращает байт из символа
* val преобразует строку в число double
* valint преобразует строку в число int
* str преобразует число (double или int) в строку
* right возвращает часть строки справа (работает только с символами)
* left возвращает часть строки слева (работает только с символами)
* space создает строку ustring c нужным кол-вом пробелов
* string создает строку ustring c нужным кол-вом любых символов в диапазоне char
* lcase возвращает строку в нижнем регистре (желательно использовать в программе setlocale(LC_ALL, ""))
* lcase возвращает строку в верхнем регистре (желательно использовать в программе setlocale(LC_ALL, ""))
* replace перезапись части строки
* insert вставка в любую часть строки
* clear очистка от ненужных символов
*/
using namespace std;
namespace fbc
{
struct fbstring
{
char *sBuf;
long iLen;
long iSize;
};
class ustring;
// operator overloading
ustring operator + (const char* psz , const ustring &s);
ustring operator + (const ustring &s , const char* psz);
ustring operator + (const ustring &s1 , const ustring &s2);
}
using fbc::ustring;
class ustring
{
//private:
//fbstring* obj;
public:
// everything must be public for this code to work
fbstring* obj;
// constructor
ustring();
ustring(const char*);
ustring(const ustring &s);
ustring(int iCount);
ustring(int iCount , char ch);
// destructor
~ustring();
// public methods
void createnullstring();
int lensymbols();
wchar_t* in_unicode();
void in_utf8(const wchar_t *s);
ustring mid(unsigned int iStart , unsigned int iNcount = 0x7FFFFFFF , int iUni = 1);
int instr(int iStart , const ustring & sSearch , int iUni = 1);
int instr(int iStart , const char* pszSearch , int iUni = 1);
ustring trim(const char* s = " ");
ustring ltrim(const char* s = " ");
ustring rtrim(const char* s = " ");
ustring chr(char i);
int asc();
double val();
int valint();
ustring str(double i);
ustring str(int i);
ustring right(int iCount);
ustring left(int iCount);
ustring space(int iCount);
ustring string(int iCount , char ch);
int len();
ustring lcase();
ustring ucase();
ustring replace(ustring & sSearch , ustring & sReplace , int iPos = 1 , int iParam = 0 , int iReg = 0 , int iUni = 1);
ustring insert(ustring & sInsert , int iPos , int iUni = 1);
ustring clear(ustring & sTrim);
// operator overloading
unsigned char & operator [] (int i)
{
return reinterpret_cast<unsigned char&>((obj->sBuf)[i]);
};
bool operator == (const ustring &s)
{
return !strcmp(s.obj->sBuf , obj->sBuf);
};
bool operator != (const ustring &s)
{
return strcmp(s.obj->sBuf , obj->sBuf);
};
fbstring* operator * ()
{
return obj;
};
ustring& operator = (const ustring &s)
{
if (&s == this) return *this;
if (obj->sBuf) free(obj->sBuf);
if (!s.obj->iLen)
{
createnullstring();
return *this;
}
else
{
char *ps = static_cast<char*>(malloc(s.obj->iSize));
if (s.obj->sBuf)
obj->sBuf = strcpy(ps , s.obj->sBuf);
else
obj->sBuf = ps;
obj->iLen = s.obj->iLen;
(obj->sBuf)[obj->iLen] = 0;
obj->iSize = s.obj->iSize;
return *this;
};
};
ustring& operator = (const char* psz)
{
if (!psz) return *this;
if (obj->sBuf) free(obj->sBuf);
int iLen =strlen(psz);
if (!iLen)
{
createnullstring();
return *this;
}
else
{
int iAlloc = (iLen + 1);
char *ps = static_cast<char*>(malloc(iAlloc));
obj->sBuf = strcpy(ps , psz);
this->obj->iLen = iLen;
(obj->sBuf)[iLen] = 0;
obj->iSize = iAlloc;
return *this;
};
};
ustring& operator = (fbstring* psz)
{
if (!psz) return *this;
if (obj->sBuf) free(obj->sBuf);
if (!(psz->iLen))
{
createnullstring();
return *this;
}
else
{
char *ps = static_cast<char*>(malloc(psz->iSize));
obj->sBuf = strcpy(ps , psz->sBuf);
this->obj->iLen = psz->iLen;
(obj->sBuf)[this->obj->iLen] = 0;
obj->iSize = psz->iSize;
return *this;
};
};
ustring& operator += (const ustring &s)
{
if (!(s.obj->iLen)) return *this;
int iAlloc = s.obj->iSize + obj->iSize;
char *ps = static_cast<char*>(malloc(iAlloc));
sprintf(ps , "%s%s", obj->sBuf , s.obj->sBuf);
obj->iLen = s.obj->iLen+obj->iLen;
obj->iSize = iAlloc;
free(obj->sBuf);
obj->sBuf = ps;
return *this;
};
ustring& operator += (const char* psz)
{
if (!psz) return *this;
int iLen = strlen(psz);
if (!iLen) return *this;
int iAlloc = (iLen + 1 + obj->iSize);
char *ps = static_cast<char*>(malloc(iAlloc));
sprintf(ps , "%s%s", obj->sBuf , psz);
this->obj->iLen = this->obj->iLen+iLen;
obj->iSize = iAlloc;
free(obj->sBuf);
obj->sBuf = ps;
return *this;
};
};
// implement missing methods
// put everything inside a header is bad practice
// but it's C++ anyway
void ustring::createnullstring()
{
char *ps = static_cast<char*>(malloc(sizeof(char)));
if (ps)
{
obj->sBuf = ps;
ps[0] = 0;
obj->iLen = 0;
obj->iSize = sizeof(char);
};
};
// constructors
ustring::ustring()
{
obj = static_cast<fbstring*>(malloc(sizeof(fbstring)));
createnullstring();
};
ustring::ustring(const char* psz)
{
if (!psz)
{
obj = static_cast<fbstring*>(malloc(sizeof(fbstring)));
createnullstring();
return;
};
int iLen = strlen(psz);
if (!iLen)
{
obj = static_cast<fbstring*>(malloc(sizeof(fbstring)));
createnullstring();
return;
};
int iAlloc = (iLen+1);
char *ps = static_cast<char*>(malloc(iAlloc));
if (ps)
{
obj = static_cast<fbstring*>(malloc(sizeof(fbstring)));
obj->sBuf = strcpy(ps , psz);
this->obj->iLen = iLen;
this->obj->iSize = iAlloc;
(obj->sBuf)[iLen] = 0;
}
};
ustring::ustring(const ustring &s)
{
if (&s == this) return;
char *ps = static_cast<char*>(malloc(s.obj->iSize));
if (ps)
{
obj = static_cast<fbstring*>(malloc(sizeof(fbstring)));
obj->sBuf = strcpy(ps , s.obj->sBuf);
obj->iLen = s.obj->iLen;
obj->iSize = s.obj->iSize;
(obj->sBuf)[obj->iLen] = 0;
}
};
ustring::ustring(int iCount)
{
int iAlloc = (iCount+1);
char *ps = static_cast<char*>(malloc(iAlloc));
if (ps)
{
obj = static_cast<fbstring*>(malloc(sizeof(fbstring)));
obj->sBuf = ps;
obj->iLen = iCount;
obj->iSize = iAlloc;
(obj->sBuf)[iCount] = 0;
};
};
ustring::ustring(int iCount , char ch)
{
int iAlloc = (iCount+1);
char *ps = static_cast<char*>(malloc(iAlloc));
memset(ps , ch , iCount);
if (ps)
{
obj = static_cast<fbstring*>(malloc(sizeof(fbstring)));
obj->sBuf = ps;
obj->iLen = iCount;
obj->iSize = iAlloc;
(obj->sBuf)[iCount] = 0;
};
};
// destructor
ustring::~ustring()
{
if (obj->sBuf) free(obj->sBuf);
obj->sBuf = 0;
obj->iLen = 0;
obj->iSize = 0;
free(obj);
};
// public methods
int ustring::lensymbols()
{
int iIndex = 0;
for (int i = 0; i < obj->iLen; i++)
{
if ((((obj->sBuf)[i] & 0xF0) == 0xE0) && (((obj->sBuf)[i+1] & 0xC0) == 0x80) && (((obj->sBuf)[i+2] & 0xC0) == 0x80))
i+=2;
else if (((obj->sBuf)[i] & 0xC0) == 0xC0 && ((obj->sBuf)[i+1] & 0xC0) == 0x80)
i++;
iIndex++;
};
return iIndex;
};
wchar_t* ustring::in_unicode()
{
int iIndex = 0;
int shUnicodeSymbol = 0;
int iAlloc = (lensymbols()+1) * sizeof(wchar_t);
wchar_t *ws = static_cast<wchar_t*>(calloc(1, iAlloc));
for (int i = 0; i < obj->iLen; i++)
{
if ((((obj->sBuf)[i] & 0xF0) == 0xE0) && (((obj->sBuf)[i+1] & 0xC0) == 0x80) && (((obj->sBuf)[i+2] & 0xC0) == 0x80))
{
shUnicodeSymbol = (((obj->sBuf)[i] & 0xF) << 12) | ((((obj->sBuf)[i+1] & 0x3F) << 6) | ((obj->sBuf)[i+2] & 0x3F));
i+=2;
}
else if (((obj->sBuf)[i] & 0xC0) == 0xC0 && ((obj->sBuf)[i+1] & 0xC0) == 0x80)
{
shUnicodeSymbol = (((obj->sBuf)[i] & 0x1F) << 6) | (((obj->sBuf)[i+1] & 0x3F));
i++;
}
else if (((obj->sBuf)[i] & 0x80) == 0 )
shUnicodeSymbol = (obj->sBuf)[i];
else
shUnicodeSymbol = 0xFFFD;
(ws)[iIndex] = shUnicodeSymbol;
iIndex++;
};
(ws)[iIndex] = 0;
return ws;
};
void ustring::in_utf8(const wchar_t *s)
{
int iSimbol = 0;
int j = 0;
int iLen = wcslen(s);
int iAlloc = (iLen+1)*6;
char *ps = static_cast<char*>(calloc(1 , iAlloc));
for (int i = 0; i < iLen ; i++)
{
iSimbol = s[i];
if (iSimbol < 0x80)
ps[j++] = iSimbol;
else if (iSimbol < 0x800)
{
ps[j++] = (0xC0 | (iSimbol >> 6));
ps[j++] = (0x80 | (iSimbol & 0x3f));
}
else if (iSimbol < 0x10000 && (iSimbol < 0xD800 || iSimbol > 0xDFFF))
{
ps[j++] = (0xE0 | (0xF & (iSimbol >> 12)));
ps[j++] = 0x80 | (0x3F & (iSimbol >> 6));
ps[j++] = 0x80 | (iSimbol & 0x3F);
}
else
{
ps[j++] = 0xEF;
ps[j++] = 0xBF;
ps[j++] = 0xBD;
};
};
if (obj->sBuf) free(obj->sBuf);
obj->sBuf = ps;
this->obj->iLen = strlen(ps);
this->obj->iSize = iAlloc;
};
ustring ustring::mid(unsigned int iStart , unsigned int iNcount , int iUni)
{
ustring sRet;
if (iUni)
{
wchar_t *ws = in_unicode();
if (!ws) return sRet;
unsigned int iLenws = wcslen(ws);
if (iStart > iLenws || iStart <=0 || !iNcount)
{
free(ws);
return sRet;
};
wchar_t *pAddr = ws + iStart - 1;
unsigned int iCount = ((iNcount+iStart-1) > iLenws) ? (iLenws-iStart+1) : iNcount;
pAddr[iCount] = 0;
sRet.in_utf8(pAddr);
free(ws);
return sRet;
}
else
{
if (iStart > obj->iLen || iStart <=0 || !iNcount) return sRet;
char *pAddr = obj->sBuf + iStart - 1;
unsigned int iCount = ((iNcount+iStart-1) > obj->iLen) ? (obj->iLen-iStart+1) : iNcount;
int iAlloc = (iCount+1);
char *ps = static_cast<char*>(malloc(iAlloc));
if (!ps) return sRet;
memcpy(ps , pAddr , iCount);
ps[iCount] = 0;
if (sRet.obj->sBuf) free(sRet.obj->sBuf);
sRet.obj->sBuf = ps;
sRet.obj->iLen = iCount;
sRet.obj->iSize = iAlloc;
return sRet;
};
};
int ustring::instr(int iStart , const ustring & sSearch , int iUni)
{
if (iUni)
{
wchar_t *ws = in_unicode();
if (!ws) return 0;
wchar_t *wsSearch = ((ustring &)sSearch).in_unicode();
if (!wsSearch)
{free(ws); return 0;};
int iLenws = wcslen(ws);
if (!iStart || iStart > iLenws)
{free(ws); free(wsSearch); return 0;};
wchar_t* pAddr = ws + iStart - 1;
wchar_t* pRet = wcsstr(pAddr , wsSearch);
if (!pRet)
{free(ws); free(wsSearch); return 0;};
int iRet = static_cast<int>(pRet-pAddr);
free(ws);
free(wsSearch);
return iRet+iStart;
}
else
{
if (!iStart || iStart > obj->iLen) return 0;
char* pAddr = obj->sBuf + iStart - 1;
char* pRet = strstr(pAddr , sSearch.obj->sBuf);
if (!pRet) return 0;
int iRet = static_cast<int>(pRet-pAddr);
return iRet+iStart;
};
};
int ustring::instr(int iStart , const char* pszSearch , int iUni)
{
if (iUni)
{
wchar_t *ws = in_unicode();
if (!ws) return 0;
ustring sSearch = pszSearch;
wchar_t *wsSearch = sSearch.in_unicode();
if (!wsSearch)
{free(ws); return 0;};
int iLenws = wcslen(ws);
if (!iStart || iStart > iLenws)
{free(ws); free(wsSearch); return 0;};
wchar_t* pAddr = ws + iStart - 1;
wchar_t* pRet = wcsstr(pAddr , wsSearch);
if (!pRet)
{free(ws); free(wsSearch); return 0;};
int iRet = static_cast<int>(pRet-pAddr);
free(ws);
free(wsSearch);
return iRet+iStart;
}
else
{
if (!iStart || iStart > obj->iLen) return 0;
char* pAddr = obj->sBuf + iStart - 1;
char* pRet = strstr(pAddr , pszSearch);
if (!pRet) return 0;
int iRet = static_cast<int>(pRet-pAddr);
return iRet+iStart;
};
};
ustring ustring::trim(const char* s)
{
int iLenchar = strlen(s);
ustring sRet;
if (!(obj->iLen)) return sRet;
if (iLenchar == 1)
{
char *ps = obj->sBuf;
int i = 0, j = obj->iLen-1;
while (ps[i] == s[0] && i != obj->iLen) i++;
while (ps[j] == s[0] && j>=0) j--;
sRet = ps+i;
sRet.obj->iLen = j-i+1;
(sRet.obj->sBuf)[sRet.obj->iLen] = 0;
return sRet;
}
else if (iLenchar > 1)
{
wchar_t *ws = in_unicode();
ustring sSymb = s;
wchar_t *wsSymb = sSymb.in_unicode();
if (!ws) return sRet;
int iLenws = wcslen(ws);
int i = 0, j = iLenws-1;
while (ws[i] == wsSymb[0] && i != iLenws) i++;
while (ws[j] == wsSymb[0] && j>=0) j--;
ws[j-i+1+i] = 0;
sRet.in_utf8(ws+i);
free(ws);
free(wsSymb);
return sRet;
}
else
return sRet;
};
ustring ustring::ltrim(const char* s)
{
int iLenchar = strlen(s);
ustring sRet;
if (!(obj->iLen)) return sRet;
if (iLenchar == 1)
{
char *ps = obj->sBuf;
int i = 0;
while (ps[i] == s[0] && i != obj->iLen) i++;
sRet = ps+i;
sRet.obj->iLen = obj->iLen - i;
return sRet;
}
else if (iLenchar > 1)
{
wchar_t *ws = in_unicode();
ustring sSymb = s;
wchar_t *wsSymb = sSymb.in_unicode();
if (!ws) return sRet;
int iLenws = wcslen(ws);
int i = 0;
while (ws[i] == wsSymb[0] && i != iLenws) i++;
sRet.in_utf8(ws+i);
free(ws);
free(wsSymb);
return sRet;
}
else
return sRet;
};
ustring ustring::rtrim(const char* s)
{
int iLenchar = strlen(s);
ustring sRet;
if (!obj->iLen) return sRet;
if (iLenchar == 1)
{
char *ps = obj->sBuf;
int j = obj->iLen-1;
while (ps[j] == s[0] && j>=0) j--;
sRet = ps;
sRet.obj->iLen = j+1;
(sRet.obj->sBuf)[sRet.obj->iLen] = 0;
return sRet;
}
else if (iLenchar > 1)
{
wchar_t *ws = in_unicode();
ustring sSymb = s;
wchar_t *wsSymb = sSymb.in_unicode();
if (!ws) return sRet;
int iLenws = wcslen(ws);
int j = iLenws-1;
while (ws[j] == wsSymb[0] && j>=0) j--;
ws[j+1] = 0;
sRet.in_utf8(ws);
free(ws);
free(wsSymb);
return sRet;
}
else
return sRet;
};
ustring ustring::chr(char i)
{
ustring sRet = " ";
(sRet.obj->sBuf)[0] = i;
return sRet;
};
int ustring::asc()
{
return (obj->sBuf)[0];
};
double ustring::val()
{
return atof(obj->sBuf);
};
int ustring::valint()
{
return atoi(obj->sBuf);
};
ustring ustring::str(double d)
{
char* ps = static_cast<char*>(calloc(1, 80));
sprintf(ps , "%f" , d);
ustring sRet = ps;
free(ps);
return sRet;
}
ustring ustring::str(int i)
{
char* ps = static_cast<char*>(calloc(1, 15));
sprintf(ps , "%d" , i);
ustring sRet = ps;
free(ps);
return sRet;
}
ustring ustring::right(int iCount)
{
ustring sRet;
wchar_t *ws = in_unicode();
if (!ws) return *this;
int iLenws = wcslen(ws);
if (iCount > iLenws) iCount = iLenws;
wchar_t *ps = ws +(iLenws - iCount);
sRet.in_utf8(ps);
free(ws);
return sRet;
};
ustring ustring::left(int iCount)
{
ustring sRet;
wchar_t *ws = in_unicode();
if (!ws) return *this;
int iLenws = wcslen(ws);
if (iCount<=0)
{
free(ws);
return *this;
};
if (iCount > iLenws) iCount = iLenws;
ws[iCount] = 0;
sRet.in_utf8(ws);
free(ws);
return sRet;
};
ustring ustring::space(int iCount)
{
ustring sRet(iCount);
memset(sRet.obj->sBuf , 32 , iCount);
return sRet;
};
ustring ustring::string(int iCount , char ch)
{
ustring sRet(iCount);
memset(sRet.obj->sBuf , ch , iCount);
return sRet;
};
int ustring::len()
{
return obj->iLen;
};
ustring ustring::lcase()
{
ustring sRet;
wchar_t *ws = in_unicode();
if (!ws) return sRet;
int iLenws = wcslen(ws);
for (int i = 0 ; i < iLenws ; i++)
ws[i] = towlower(ws[i]);
sRet.in_utf8(ws);
free(ws);
return sRet;
};
ustring ustring::ucase()
{
ustring sRet;
wchar_t *ws = in_unicode();
if (!ws) return sRet;
int iLenws = wcslen(ws);
for (int i = 0 ; i < iLenws ; i++)
ws[i] = towupper(ws[i]);
sRet.in_utf8(ws);
free(ws);
return sRet;
};
ustring ustring::replace(ustring & sSearch , ustring & sReplace , int iPos , int iParam , int iReg , int iUni)
{
if (iUni)
{
if (!(obj->iLen) || !(sSearch.obj->iLen)) return *this;
wchar_t *ws_S = sSearch.in_unicode();
if (!ws_S) return *this;
int iLen_S = wcslen(ws_S);
int iPosSearch , iPosSearchNext = iPos;
ustring sReturnString = *this;
if (!iReg)
{
for (int i = 0; i < iLen_S; i++)
ws_S[i] = towlower(ws_S[i]);
};
while (1)
{
if (!iReg)
{
wchar_t *ws = sReturnString.in_unicode();
if (ws)
{
int iLenWS = wcslen(ws);
for (int i = 0; i < iLenWS; i++)
ws[i] = towlower(ws[i]);
wchar_t* pAddr = ws + iPosSearchNext - 1;
wchar_t* pRet = wcsstr(pAddr , ws_S);
if (pRet)
iPosSearch = (pRet-pAddr)+iPosSearchNext;
else
iPosSearch = 0;
free(ws);
}
else
iPosSearch = 0;
}
else
iPosSearch = sReturnString.instr( iPosSearchNext , sSearch , iUni);
if (iPosSearch)
{
sReturnString = sReturnString.mid( 1 , iPosSearch - 1 , iUni) + sReplace + sReturnString.mid( iPosSearch + sSearch.lensymbols() , 0x7fffffff , iUni);
if (iParam)
iPosSearchNext = iPosSearch + sReplace.lensymbols();
else
{
if (!iReg) free(ws_S);
return sReturnString;
};
}
else
{
if (!iReg) free(ws_S);
return sReturnString;
};
};
}
else
{
ustring sReturnString = *this;
int iPosSearch , iPosSearchNext = iPos;
while (1)
{
if (!iReg)
iPosSearch = (sReturnString.lcase()).instr( iPosSearchNext , sSearch.lcase() , 0);
else
iPosSearch = sReturnString.instr( iPosSearchNext , sSearch , 0);
if (iPosSearch)
{
sReturnString = sReturnString.mid( 1 , iPosSearch - 1 , 0) + sReplace + sReturnString.mid( iPosSearch + sSearch.len() , 0x7fffffff , 0);
if (iParam)
iPosSearchNext = iPosSearch + sReplace.len();
else
return sReturnString;
}
else
return sReturnString;
};
};
};
ustring ustring::insert(ustring & sInsert , int iPos , int iUni)
{
int iLenDest;
if (iUni)
iLenDest = lensymbols();
else
iLenDest = obj->iLen;
if (iPos == -1) iPos = iLenDest+1;
if ((iLenDest+1 < iPos) || (iPos == 0) || (iPos < -1)) return *this;
return mid(1,iPos-1 , iUni) + sInsert + mid(iPos , 0x7fffffff , iUni);
};
ustring ustring::clear(ustring & sTrim)
{
if (!(obj->iLen) || !(sTrim.obj->iLen)) return *this;
wchar_t* ws = in_unicode();
wchar_t* wtr = sTrim.in_unicode();
int iIndex = 0 , iResult = 0;
for (int i = 0 ; i < static_cast<int>(wcslen(ws)) ; i++)
{
for (int j = 0 ; j < static_cast<int>(wcslen(wtr)) ; j++)
if (ws[i] == wtr[j])
iResult = 1;
if (iResult)
iResult = 0;
else
ws[iIndex++] = ws[i];
};
ws[iIndex] = 0;
ustring sRet;
sRet.in_utf8(ws);
free(ws); free(wtr);
return sRet;
};
// outside class operator overloading
ustring fbc::operator + (const char* psz , const ustring &s)
{
int iLen = 0;
if (psz) iLen = strlen(psz);
ustring sRet;
int iAlloc = (iLen + 1 + s.obj->iSize);
sRet.obj->sBuf = static_cast<char*>(malloc(iAlloc));
sprintf(sRet.obj->sBuf , "%s%s", psz , s.obj->sBuf );
sRet.obj->iLen = s.obj->iLen + iLen;
sRet.obj->iSize = iAlloc;
return sRet;
};
ustring fbc::operator + (const ustring &s , const char* psz)
{
int iLen = 0;
if (psz) iLen = strlen(psz);
ustring sRet;
int iAlloc = (iLen + 1 + s.obj->iSize);
sRet.obj->sBuf = static_cast<char*>(malloc(iAlloc));
sprintf(sRet.obj->sBuf , "%s%s", s.obj->sBuf , psz );
sRet.obj->iLen = s.obj->iLen + iLen;
sRet.obj->iSize = iAlloc;
return sRet;
};
ustring fbc::operator + (const ustring &s1 , const ustring &s2)
{
ustring sRet;
int iAlloc = (s1.obj->iSize + s2.obj->iSize);
sRet.obj->sBuf = static_cast<char*>(malloc(iAlloc));
sprintf(sRet.obj->sBuf , "%s%s", s1.obj->sBuf , s2.obj->sBuf );
sRet.obj->iLen = s1.obj->iLen + s2.obj->iLen;
sRet.obj->iSize = iAlloc;
return sRet;
};
Instead of using a whole fbc namespace, I changed to only using fbc::fbstring in window9.h.
Your example w9.cpp compiled fine on MSYS2 (of course can't link because the dynamic library is of Linux, but it proves my refactored code is error-free).
On MSYS2, for no known reason pkg-config doesn't work right. So I have to add all include paths manually.
g++ -c w9.cpp -I. -I/mingw64/include/gtk-3.0 -I/mingw64/include/glib-2.0 -I/mingw64/lib/glib-2.0/include -mms-bitfields -I/mingw64/include -I/mingw64/include/pango-1.0 -I/mingw64/include/harfbuzz -I/mingw64/include/cairo -I/mingw64/include/gdk-pixbuf-2.0 -I/mingw64/include/atk-1.0