diff options
Diffstat (limited to 'src/formatstring.cpp')
-rw-r--r-- | src/formatstring.cpp | 52 |
1 files changed, 32 insertions, 20 deletions
diff --git a/src/formatstring.cpp b/src/formatstring.cpp index 579bcba0..0ff416b0 100644 --- a/src/formatstring.cpp +++ b/src/formatstring.cpp @@ -10,45 +10,48 @@ namespace newsbeuter { void fmtstr_formatter::register_fmt(char f, const std::string& value) { - GetLogger().log(LOG_DEBUG, "fmtstr_formatter::register_fmt: char = %c value = %s", f, value.c_str()); + // GetLogger().log(LOG_DEBUG, "fmtstr_formatter::register_fmt: char = %c value = %s", f, value.c_str()); fmts[f] = utils::utf8str2wstr(value); - GetLogger().log(LOG_DEBUG, "fmtstr_formatter::register_fmt: char = %c done", f); + // GetLogger().log(LOG_DEBUG, "fmtstr_formatter::register_fmt: char = %c done", f); } std::string fmtstr_formatter::do_format(const std::string& fmt, unsigned int width) { - std::wstring wfmt(utils::str2wstr(fmt)); - std::string result = utils::wstr2str(do_wformat(wfmt, width)); - GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_format: after conversion: `%s'", result.c_str()); + std::string result; + if (fmt.length() > 0) { + std::wstring wfmt(utils::str2wstr(fmt)); + std::wstring w = do_wformat(wfmt, width); + result = utils::wstr2str(w); + } return result; } std::wstring fmtstr_formatter::do_wformat(const std::wstring& wfmt, unsigned int width) { - GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_format: fmt = `%ls' width = %u", wfmt.c_str(), width); + GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_wformat: fmt = `%ls' width = %u", wfmt.c_str(), width); std::wstring result; unsigned int i; unsigned int fmtlen = wfmt.length(); - GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_format: fmtlen = %u", fmtlen); + GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_wformat: fmtlen = %u", fmtlen); for (i=0;i<fmtlen;++i) { if (wfmt[i] == L'%') { if (i<(fmtlen-1)) { if (wfmt[i+1] == L'-' || iswdigit(wfmt[i+1])) { - std::string number; + std::wstring number; wchar_t c; while ((wfmt[i+1] == L'-' || iswdigit(wfmt[i+1])) && i<(fmtlen-1)) { number.append(1,wfmt[i+1]); ++i; } - GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_format: number = %s", number.c_str()); + GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_wformat: number = %ls", number.c_str()); if (i<(fmtlen-1)) { c = wfmt[i+1]; ++i; - std::istringstream is(number); + std::wistringstream is(number); int align; is >> align; - if (abs(align) > fmts[c].length()) { + if (static_cast<unsigned int>(abs(align)) > fmts[c].length()) { wchar_t buf[256]; swprintf(buf,sizeof(buf)/sizeof(*buf),L"%*ls", align, fmts[c].c_str()); - GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_format: swprintf result = %ls", buf); + GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_wformat: swprintf result = %ls", buf); result.append(buf); } else { result.append(fmts[c].substr(0,abs(align))); @@ -56,7 +59,7 @@ std::wstring fmtstr_formatter::do_wformat(const std::wstring& wfmt, unsigned int } } else if (wfmt[i+1] == L'%') { result.append(1, L'%'); - GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_format: appending %"); + GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_wformat: appending %"); ++i; } else if (wfmt[i+1] == L'>') { if (wfmt[i+2]) { @@ -65,9 +68,9 @@ std::wstring fmtstr_formatter::do_wformat(const std::wstring& wfmt, unsigned int i += 2; } else { std::wstring rightside = do_wformat(&wfmt[i+3], 0); - GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_format: aligning, right side = %ls", rightside.c_str()); + GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_wformat: aligning, right side = %ls", rightside.c_str()); int diff = width - wcswidth(result.c_str(),result.length()) - wcswidth(rightside.c_str(), rightside.length()); - GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_format: diff = %d char = %lc", diff, wfmt[i+2]); + GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_wformat: diff = %d char = %lc", diff, wfmt[i+2]); if (diff > 0) { result.append(diff, wfmt[i+2]); } @@ -80,21 +83,29 @@ std::wstring fmtstr_formatter::do_wformat(const std::wstring& wfmt, unsigned int while (wfmt[j] && wfmt[j] != L'?') j++; if (wfmt[j]) { - std::wstring cond = wfmt.substr(i+2, j - i - 1); + std::wstring cond = wfmt.substr(i+2, j - i - 2); unsigned int k = j + 1; while (wfmt[k] && wfmt[k] != L'?') k++; if (wfmt[k]) { std::wstring values = wfmt.substr(j+1, k - j - 1); - std::vector<std::string> pair = utils::tokenize(utils::wstr2str(values),"&"); + std::vector<std::wstring> pair = utils::wtokenize(values,L"&"); while (pair.size() < 2) - pair.push_back(""); + pair.push_back(L""); + GetLogger().log(LOG_DEBUG, "fmtstr_formatter::do_wformat: values = `%ls' cond = `%ls' cond[0] = `%lc' fmts[cond[0]] = `%ls'", values.c_str(), cond.c_str(), cond[0], fmts[cond[0]].c_str()); + GetLogger().log(LOG_DEBUG, "YYY pair0 = `%ls' pair1 = `%ls'", pair[0].c_str(), pair[1].c_str()); + std::wstring subresult; if (fmts[cond[0]].length() > 0) { - result.append(do_wformat(utils::str2wstr(pair[0]), width)); + if (pair[0].length() > 0) + subresult = do_wformat(pair[0], width); } else { - result.append(do_wformat(utils::str2wstr(pair[1]), width)); + if (pair[1].length() > 0) + subresult = do_wformat(pair[1], width); } + GetLogger().log(LOG_DEBUG, "YYY result = `%ls'", subresult.c_str()); + result.append(subresult); + i = k; } else { @@ -112,6 +123,7 @@ std::wstring fmtstr_formatter::do_wformat(const std::wstring& wfmt, unsigned int result.append(1, wfmt[i]); } } + GetLogger().log(LOG_DEBUG, "end of do_wformat"); return result; } |