size_t os_fread_mbs(FILE *file, char **pstr) { size_t size = 0; size_t len = 0; fseek(file, 0, SEEK_END); size = (size_t)os_ftelli64(file); *pstr = NULL; if (size > 0) { char *mbstr = bmalloc(size+1); fseek(file, 0, SEEK_SET); size = fread(mbstr, 1, size, file); if (size == 0) { bfree(mbstr); return 0; } mbstr[size] = 0; len = os_mbs_to_utf8_ptr(mbstr, size, pstr); bfree(mbstr); } return len; }
std::string getWindowAtom(Window win, const char *atom) { Atom netWmName = XInternAtom(disp(), atom, false); int n; char **list = 0; XTextProperty tp; std::string res = "unknown"; XGetTextProperty(disp(), win, &tp, netWmName); if (!tp.nitems) XGetWMName(disp(), win, &tp); if (!tp.nitems) return "error"; if (tp.encoding == XA_STRING) { res = (char*)tp.value; } else { int ret = XmbTextPropertyToTextList(disp(), &tp, &list, &n); if (ret >= Success && n > 0 && *list) { res = *list; XFreeStringList(list); } } char *conv = nullptr; if (os_mbs_to_utf8_ptr(res.c_str(), 0, &conv)) res = conv; bfree(conv); XFree(tp.value); return res; }
char *dstr_to_mbs(const struct dstr *str) { char *dst; os_mbs_to_utf8_ptr(str->array, str->len, &dst); return dst; }
void dstr_from_mbs(struct dstr *dst, const char *mbstr) { dstr_free(dst); dst->len = os_mbs_to_utf8_ptr(mbstr, 0, &dst->array); }