From 8e00b06cfa1702de2167a3f0017e1e2aac57b03e Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 14 Jul 2015 04:12:59 -0700 Subject: [PATCH] pcsprintf --- examples/example2 | Bin 8904 -> 8397 bytes examples/example2.cpp | 6 ++++-- libpipecolors.cpp | 27 ++++++++++++++++++++++----- pipecolors.h | 2 +- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/examples/example2 b/examples/example2 index 594842ff80cf42665d0b44055c940179ae97f654..155367f5221aaa407b55d0ebbd32cf7d4b273358 100755 GIT binary patch literal 8397 zcmeHMeQaCR6~DHV=A*5X7BWhK_KA`$1!9~wEh&ugoYd_LB&BN;$Qa?pv7N+IVjF*y zv?G+m)f$od!JxfN1WSc|9@ zy0}iP7E6GXIroq_wU;Md6)pbq zQcGkmgeC#q(Rf{1 zSTtDWPxH^?a?5a}e{=IK!=c9ENHjg#INGwgadWdL8S`wA<3;_VHn#8Rmeun3Yf=(| z<_$e!ex3)Wy$(^XR}n^!s0P;ye|vJQ&42p*nTL9=eg5zT_dBn@?_Tde-2v62WI&^p z(5_kpzW{t?3Hx6xg1gY+QuY@YvA-F(hO3zF1E3XykoV&%RsX|_;0@?ctym@EwSBP2 zQ=P=G5<^A!waUI2e+aHC5e+I;yx^L~CB9sIP^B{@`S&F*#AK1Zz`Te@;^AOyIF?8@ zdOb}ZFJ^@>@7iPT3J*q-sc@ogIFL++lfrE8>NG>)go3*|+lFJ&a95yzI4n$aa3mHr zlc_)=Wtx_0X~Re~k`nP?JQ0be281~P8Ddh&=uR>f3f^=RP0h{1ynn~;R7)h5H2VX| zaC19+w>J6rl)~Exhx>@-iPB&&XeLQ&B0Ol}@pKAzVg^S8W*`+FMPE#lw5sbPfk;%C z;ZPtIfTBN{v~WPtkl5bQ-rCmFV{Y^`TV->DXM<-G5401j#EBo11FOaQxg?*2g4UUa zE4BVfEKQea#PcO0ySKqPKBQMiss!rVqV$()`p8ViLD zVWjigaC@9@vElYOr`vGuGu30*aNNK=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47=^AgqgNQ7Ew@kfV;kuThBcDETuA}zoA#$ByJgVKIxA}TEtk!0c!BqTZ` zsTgvZxR>OjQVt{%!r0Qiqdn9OlU$$CWmp+RWAm=iwy+W2-brauIWwx&nl+@$a6ql^*Y~XX)sud!Lg5BP9YqH8z4zHEc+{R z{`LW*n2hcD-ny?^s^fp17AdjV4(opn)fD5g%=g!<_IwYX zQ1(1dZlCQLe^A-CD~Ehvo>T@L*OEwgG^hPzz-Uf5uI9LYPyx;xpqf@#Sd<5|+w=W< zT-nPUBP)Y*9)S514*TLgofajiktfCyi}p$s-$%jTKIb8psz99Man^VC|7m5<&G5b7 zrwqT!hKlq2G5oBd!c9isHdd&h2noE(Fm dRX^P4QKmlfc=-IHWnQWMJxbw%!@0Ble*;<=V}k$y diff --git a/examples/example2.cpp b/examples/example2.cpp index 8faa824..661a6d7 100644 --- a/examples/example2.cpp +++ b/examples/example2.cpp @@ -5,10 +5,12 @@ using namespace pipecolors; int main(void) { - + char * buffer; int num = 5; const char* str = "My number is"; int len = pcprintf("|01%s |41|09%d|39\n", str, num); - pcprintf("|10Length of String is : |15|30%d\n", len); + pcprintf("|10Length of Len is : |15|30%d\n|07", len); + int len2 = pcsprintf(buffer, "%s %d\n", str, num); + printf("%s\n", buffer); return 0; } \ No newline at end of file diff --git a/libpipecolors.cpp b/libpipecolors.cpp index 89f00e1..34ad857 100644 --- a/libpipecolors.cpp +++ b/libpipecolors.cpp @@ -82,7 +82,7 @@ namespace pipecolors { start = s.begin(); end = s.end(); std::string len(s); - int reallen; + colors = getColors(); while(regex_search(start, end, match, re, flags)) @@ -103,8 +103,8 @@ namespace pipecolors { flags |= boost::match_prev_avail | boost::match_not_bob; } - return std::make_pair(s, len.find("\n") ? len.length() - 1 : len.length()); - + return std::make_pair(s, len.length()); + } int pcprintf( const char * fmt, ...) @@ -130,7 +130,24 @@ namespace pipecolors { return(result.second); } - int pcsprintf(char **strp, const char *fmt, va_list ap) { - return true; + int pcsprintf( char * str, const char * fmt, ... ) { + int ret; + + va_list args; + va_start(args, fmt); + ret = vasprintf(&str, fmt, args); + + if(ret == -1) { + free(str); + exit(EXIT_FAILURE); + } + + std::string s(str); + //free(buffer); + + //std::pair result = replace_colors(s); + //char* buffer = result.first; + + return s.length(); } } // namespace diff --git a/pipecolors.h b/pipecolors.h index 6b7a662..c571788 100644 --- a/pipecolors.h +++ b/pipecolors.h @@ -11,7 +11,7 @@ extern "C" { #endif int pcprintf( const char * format, ... ); -int pcsprintf(char **strp, const char *fmt, va_list ap); +int pcsprintf( char * str, const char * fmt, ... ); #ifdef __cplusplus }