Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=fwsetup-ng.git;a=commitdiff;h=8adf5a1a557f4e3f6e65c36ef1328c79cbca72f5
commit 8adf5a1a557f4e3f6e65c36ef1328c79cbca72f5 Author: James Buren <[email protected]> Date: Sat Aug 18 23:12:07 2012 -0500 add text measuring function for newt diff --git a/Utility.cc b/Utility.cc index a0fd4c4..728a1c3 100644 --- a/Utility.cc +++ b/Utility.cc @@ -1,5 +1,6 @@ #include <fcntl.h> #include <unistd.h> +#include <string.h> #include "Utility.hh" #define LOGFILE "fwsetup.log" @@ -22,11 +23,74 @@ pid_t execute(const string &cmd) dup2(fd,STDERR_FILENO); close(fd); - + execl("/bin/sh","/bin/sh","-c",cmd.c_str(),(void *) 0); - + _exit(250); } return pid; } + +#ifdef NEWT +bool get_text_size(const string &text,int &width,int &height) +{ + wchar_t wc = 0; + size_t n = 0; + size_t off = 0; + mbstate_t mbs; + int cw = 0; + int w = 0; + int h = 0; + int i = 0; + + memset(&mbs,0,sizeof(mbstate_t)); + + while(true) + { + n = mbrtowc(&wc,text.c_str()+off,text.length()-off,&mbs); + + if(n == (size_t) -1 || n == (size_t) -2) + return false; + + if(n == 0) + break; + + switch(wc) + { + case L'\t': + cw += 8; + break; + + case L'\n': + if(cw > w) + w = cw; + cw = 0; + ++h; + break; + + default: + i = wcwidth(wc); + if(i > 0) + cw += i; + break; + } + + off += n; + } + + if(w == 0 && cw > 0) + w = cw; + else if(w == 0) + w = 1; + + if(h == 0) + h = 1; + + width = w; + + height = h; + + return true; +} +#endif diff --git a/Utility.hh b/Utility.hh index 12bf460..e4e1b95 100644 --- a/Utility.hh +++ b/Utility.hh @@ -6,3 +6,7 @@ using std::string; pid_t execute(const string &cmd); + +#ifdef NEWT +bool get_text_size(const string &text,int &width,int &height); +#endif _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
