RichString: return number of written characters on write/append functions

This commit is contained in:
Christian Göttsche 2020-12-23 12:20:57 +01:00
parent 86d2931255
commit 94d7f0b585
2 changed files with 31 additions and 25 deletions

View File

@ -48,25 +48,29 @@ static void RichString_setLen(RichString* this, int len) {
#ifdef HAVE_LIBNCURSESW #ifdef HAVE_LIBNCURSESW
static inline void RichString_writeFromWide(RichString* this, int attrs, const char* data_c, int from, int len) { static inline int RichString_writeFromWide(RichString* this, int attrs, const char* data_c, int from, int len) {
wchar_t data[len + 1]; wchar_t data[len + 1];
len = mbstowcs(data, data_c, len); len = mbstowcs(data, data_c, len);
if (len < 0) if (len <= 0)
return; return 0;
int newLen = from + len; int newLen = from + len;
RichString_setLen(this, newLen); RichString_setLen(this, newLen);
for (int i = from, j = 0; i < newLen; i++, j++) { for (int i = from, j = 0; i < newLen; i++, j++) {
this->chptr[i] = (CharType) { .attr = attrs & 0xffffff, .chars = { (iswprint(data[j]) ? data[j] : '?') } }; this->chptr[i] = (CharType) { .attr = attrs & 0xffffff, .chars = { (iswprint(data[j]) ? data[j] : '?') } };
} }
return len;
} }
static inline void RichString_writeFromAscii(RichString* this, int attrs, const char* data, int from, int len) { static inline int RichString_writeFromAscii(RichString* this, int attrs, const char* data, int from, int len) {
int newLen = from + len; int newLen = from + len;
RichString_setLen(this, newLen); RichString_setLen(this, newLen);
for (int i = from, j = 0; i < newLen; i++, j++) { for (int i = from, j = 0; i < newLen; i++, j++) {
this->chptr[i] = (CharType) { .attr = attrs & 0xffffff, .chars = { (isprint(data[j]) ? data[j] : '?') } }; this->chptr[i] = (CharType) { .attr = attrs & 0xffffff, .chars = { (isprint(data[j]) ? data[j] : '?') } };
} }
return len;
} }
inline void RichString_setAttrn(RichString* this, int attrs, int start, int finish) { inline void RichString_setAttrn(RichString* this, int attrs, int start, int finish) {
@ -91,17 +95,19 @@ int RichString_findChar(RichString* this, char c, int start) {
#else /* HAVE_LIBNCURSESW */ #else /* HAVE_LIBNCURSESW */
static inline void RichString_writeFromWide(RichString* this, int attrs, const char* data_c, int from, int len) { static inline int RichString_writeFromWide(RichString* this, int attrs, const char* data_c, int from, int len) {
int newLen = from + len; int newLen = from + len;
RichString_setLen(this, newLen); RichString_setLen(this, newLen);
for (int i = from, j = 0; i < newLen; i++, j++) { for (int i = from, j = 0; i < newLen; i++, j++) {
this->chptr[i] = (((unsigned char)data_c[j]) >= 32 ? ((unsigned char)data_c[j]) : '?') | attrs; this->chptr[i] = (((unsigned char)data_c[j]) >= 32 ? ((unsigned char)data_c[j]) : '?') | attrs;
} }
this->chptr[newLen] = 0; this->chptr[newLen] = 0;
return len;
} }
static inline void RichString_writeFromAscii(RichString* this, int attrs, const char* data_c, int from, int len) { static inline int RichString_writeFromAscii(RichString* this, int attrs, const char* data_c, int from, int len) {
RichString_writeFromWide(this, attrs, data_c, from, len); return RichString_writeFromWide(this, attrs, data_c, from, len);
} }
void RichString_setAttrn(RichString* this, int attrs, int start, int finish) { void RichString_setAttrn(RichString* this, int attrs, int start, int finish) {
@ -145,26 +151,26 @@ void RichString_setAttr(RichString* this, int attrs) {
RichString_setAttrn(this, attrs, 0, this->chlen - 1); RichString_setAttrn(this, attrs, 0, this->chlen - 1);
} }
void RichString_appendWide(RichString* this, int attrs, const char* data) { int RichString_appendWide(RichString* this, int attrs, const char* data) {
RichString_writeFromWide(this, attrs, data, this->chlen, strlen(data)); return RichString_writeFromWide(this, attrs, data, this->chlen, strlen(data));
} }
void RichString_appendnWide(RichString* this, int attrs, const char* data, int len) { int RichString_appendnWide(RichString* this, int attrs, const char* data, int len) {
RichString_writeFromWide(this, attrs, data, this->chlen, len); return RichString_writeFromWide(this, attrs, data, this->chlen, len);
} }
void RichString_writeWide(RichString* this, int attrs, const char* data) { int RichString_writeWide(RichString* this, int attrs, const char* data) {
RichString_writeFromWide(this, attrs, data, 0, strlen(data)); return RichString_writeFromWide(this, attrs, data, 0, strlen(data));
} }
void RichString_appendAscii(RichString* this, int attrs, const char* data) { int RichString_appendAscii(RichString* this, int attrs, const char* data) {
RichString_writeFromAscii(this, attrs, data, this->chlen, strlen(data)); return RichString_writeFromAscii(this, attrs, data, this->chlen, strlen(data));
} }
void RichString_appendnAscii(RichString* this, int attrs, const char* data, int len) { int RichString_appendnAscii(RichString* this, int attrs, const char* data, int len) {
RichString_writeFromAscii(this, attrs, data, this->chlen, len); return RichString_writeFromAscii(this, attrs, data, this->chlen, len);
} }
void RichString_writeAscii(RichString* this, int attrs, const char* data) { int RichString_writeAscii(RichString* this, int attrs, const char* data) {
RichString_writeFromAscii(this, attrs, data, 0, strlen(data)); return RichString_writeFromAscii(this, attrs, data, 0, strlen(data));
} }

View File

@ -52,16 +52,16 @@ void RichString_setAttr(RichString* this, int attrs);
void RichString_appendChr(RichString* this, char c, int count); void RichString_appendChr(RichString* this, char c, int count);
void RichString_appendWide(RichString* this, int attrs, const char* data); int RichString_appendWide(RichString* this, int attrs, const char* data);
void RichString_appendnWide(RichString* this, int attrs, const char* data, int len); int RichString_appendnWide(RichString* this, int attrs, const char* data, int len);
void RichString_writeWide(RichString* this, int attrs, const char* data); int RichString_writeWide(RichString* this, int attrs, const char* data);
void RichString_appendAscii(RichString* this, int attrs, const char* data); int RichString_appendAscii(RichString* this, int attrs, const char* data);
void RichString_appendnAscii(RichString* this, int attrs, const char* data, int len); int RichString_appendnAscii(RichString* this, int attrs, const char* data, int len);
void RichString_writeAscii(RichString* this, int attrs, const char* data); int RichString_writeAscii(RichString* this, int attrs, const char* data);
#endif #endif