From c3a7bcfd5b39a6d2999373f481935bfffb705c13 Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Fri, 12 Aug 2016 19:19:24 +0200 Subject: [PATCH] src/system/helpers.cpp: Add cutString() function Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/71674d0f8aab183ad93d7220d5e6a8bfa6505588 Author: Michael Liebmann Date: 2016-08-12 (Fri, 12 Aug 2016) ------------------ No further description and justification available within origin commit message! ------------------ This commit was generated by Migit --- src/system/helpers.cpp | 21 +++++++++++++++++++++ src/system/helpers.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index d77efa230..a45fcfc48 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -438,6 +438,27 @@ std::string trim(std::string &str, const std::string &trimChars /*= " \n\r\t"*/) return result.erase(0, result.find_first_not_of(trimChars)); } +std::string cutString(const std::string str, Font *msgFont, const int width) +{ + std::string ret = str; + ret = trim(ret); + int sw = msgFont->getRenderWidth(ret); + if (sw <= width) + return ret; + else { + std::string z = "..."; + int zw = msgFont->getRenderWidth(z); + if (width <= 2*zw) + return ret; + do { + ret = ret.substr(0, ret.length()-1); + sw = msgFont->getRenderWidth(ret); + } while (sw+zw > width); + ret = trim(ret) + z; + } + return ret; +} + std::string strftime(const char *format, const struct tm *tm) { char buf[4096]; diff --git a/src/system/helpers.h b/src/system/helpers.h index 486d1e8e6..5d9efa8d9 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -37,6 +37,8 @@ #include #include +#include + int my_system(const char * cmd); int my_system(int argc, const char *arg, ...); /* argc is number of arguments including command */ @@ -67,6 +69,7 @@ std::string getFileName(std::string &file); std::string getFileExt(std::string &file); std::string getNowTimeStr(const char* format); std::string trim(std::string &str, const std::string &trimChars = " \n\r\t"); +std::string cutString(const std::string str, Font *msgFont, const int width); std::string strftime(const char *format, const struct tm *tm); std::string strftime(const char *format, time_t when, bool gm = false); time_t toEpoch(std::string &date);