Switched to boost:regex

This commit is contained in:
= 2015-07-03 00:56:32 -07:00
parent 344b9ed855
commit 68aaadeb45
8 changed files with 102 additions and 267 deletions

7
.gitignore vendored
View File

@ -2,9 +2,6 @@
sites/*/*settings*.php sites/*/*settings*.php
# Ignore paths that contain generated content. # Ignore paths that contain generated content.
files/
sites/*/files
sites/*/private
# Ignore default text files # Ignore default text files
.deps .deps
@ -32,6 +29,8 @@ libpipecolors.so
.libs/ .libs/
libpipecolors.la libpipecolors.la
libpipecolors.lo libpipecolors.lo
libpipecolors.pc
test/
timestamp
# Ignore backup files # Ignore backup files
*~ *~

View File

@ -1,8 +1,3 @@
man-hook:
gzip $(mandir)/man7/libpipecolors.7
gzip $(mandir)/man3/pcprintf.3
pkgconfig_DATA = libpipecolors.pc pkgconfig_DATA = libpipecolors.pc
AM_CXXFLAGS = -std=c++11 AM_CXXFLAGS = -std=c++11
ACLOCAL_AMFLAGS = -I m4 -I config ACLOCAL_AMFLAGS = -I m4 -I config
@ -13,7 +8,7 @@ libtool: $(LIBTOOL_DEPS)
lib_LTLIBRARIES = libpipecolors.la lib_LTLIBRARIES = libpipecolors.la
libpipecolors_la_SOURCES = libpipecolors.cpp libpipecolors_la_SOURCES = libpipecolors.cpp
#libpipecolors_la_LIBADD = -ltermcap libpipecolors_la_LIBADD = -lboost_regex
libpipecolors_la_CFLAGS = -fPIC -DPIC -pthread libpipecolors_la_CFLAGS = -fPIC -DPIC -pthread
libpipecolors_la_LDFLAGS = -module \ libpipecolors_la_LDFLAGS = -module \
-release ${PIPECOLORS_VERSION} -release ${PIPECOLORS_VERSION}
@ -35,3 +30,4 @@ MAINTAINERCLEANFILES = \
-rf config \ -rf config \
-rf m4 \ -rf m4 \
configure \ configure \
Makefile.in

View File

@ -1,130 +0,0 @@
/*
* libpipecolors: linux color code library
*
* Authors:
* Eric Wheeler <eric@ericwheeler.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <iostream>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
#include <map>
#include <regex>
#include "pipecolors.h"
namespace pipecolors {
#define C_DEFAULT 0
#define C_BOLD 1
#define C_ITALIC 3
#define C_UNDERLINE 4
#define C_INVERT 7
#define C_B0 "\x1b[1m"
#define C_B1 "\x1b[0m"
#define C_FG_BLACK "\x1b[0;30m"
#define C_FG_RED "\x1b[0;31m"
#define C_FG_GREEN "\x1b[0;32m"
#define C_FG_YELLOW "\x1b[0;33m"
#define C_FG_BLUE "\x1b[0;34m"
#define C_FG_MAGENTA "\x1b[0;35m"
#define C_FG_CYAN "\x1b[0;36m"
#define C_FG_GRAY "\x1b[0;37m"
#define C_FG_DEFAULT "\x1b[0;39m"
#define C_FG_GRAY_D "\x1b[0;90m"
#define C_FG_RED_L "\x1b[0;91m"
#define C_FG_GREEN_L "\x1b[0;92m"
#define C_FG_YELLOW_L "\x1b[0;93m"
#define C_FG_BLUE_L "\x1b[0;94m"
#define C_FG_MAGENTA_L "\x1b[0;95m"
#define C_FG_CYAN_L "\x1b[0;96m"
#define C_FG_WHITE "\x1b[0;97m"
#define C_BG_NONE ""
#define C_BG_RED "\x1b[1;41m"
#define C_BG_GREEN "\x1b[1;42m"
#define C_BG_BLUE "\x1b[1;44m"
#define C_BG_DEFAULT "\x1b[1;49m"
std::map<std::string, std::string> getColors() {
std::map<std::string, std::string> colors;
colors["|00"] = C_FG_BLACK;
colors["|01"] = C_FG_BLUE;
colors["|02"] = C_FG_GREEN;
colors["|03"] = C_FG_CYAN;
colors["|04"] = C_FG_RED;
colors["|05"] = C_FG_MAGENTA;
colors["|06"] = C_FG_YELLOW;
colors["|07"] = C_FG_GRAY;
colors["|08"] = C_FG_GRAY_D;
colors["|09"] = C_FG_BLUE_L;
colors["|10"] = C_FG_GREEN_L;
colors["|11"] = C_FG_CYAN_L;
colors["|12"] = C_FG_RED_L;
colors["|13"] = C_FG_MAGENTA_L;
colors["|14"] = C_FG_YELLOW_L;
colors["|15"] = C_FG_WHITE;
colors["|16"] = C_BG_DEFAULT; // C_BG_BLACK
colors["|17"] = C_BG_BLUE;
colors["|18"] = C_BG_GREEN;
colors["|19"] = C_BG_BLUE; // C_BG_CYAN
colors["|20"] = C_BG_RED;
colors["|21"] = C_BG_RED; // C_BG_MAGENTA
colors["|22"] = C_FG_WHITE; //C_BG_BROWN
colors["|23"] = C_BG_DEFAULT; // C_BG_WHITE
colors["|30"] = C_B0; //Bold OFF
colors["|31"] = C_B1; // Bold ON
return colors;
}
bool has_colors(void) {
return isatty(fileno(stdout));
}
void pcprintf( const char * fmt, ... ) {
char buffer[256];
std::map<std::string, std::string> colors;
va_list args;
va_start(args, fmt);
vasprintf(buffer, fmt, args);
std::string text(buffer), s(buffer);
va_end(args);
std::size_t index;
std::smatch matches;
std::regex reg ("(\\|\\d\\d)", std::regex_constants::ECMAScript | std::regex_constants::nosubs);
colors = getColors();
while (std::regex_search (text,matches,reg)) {
for (auto x:matches) {
while ((index = s.find(x)) != std::string::npos)
s.replace(index, x.length(), colors[x]);
}
text = matches.suffix().str();
}
std::cout << s;
}
}

View File

@ -41,7 +41,6 @@ AC_PROG_LIBTOOL
AC_PROG_LN_S AC_PROG_LN_S
LT_INIT LT_INIT
AC_LANG_CPLUSPLUS AC_LANG_CPLUSPLUS
AC_PREFIX_DEFAULT([/usr]) AC_PREFIX_DEFAULT([/usr])
#AC_CHECK_LIB([pipecolors], [cprintf]) #AC_CHECK_LIB([pipecolors], [cprintf])
@ -66,11 +65,12 @@ if test "${gcc_minor_vers}" -lt 9; then
else else
AC_MSG_RESULT([${gcc_full_vers} ... acceptable]) AC_MSG_RESULT([${gcc_full_vers} ... acceptable])
fi fi
AC_HEADER_STDC #AC_HEADER_STDC
AC_CHECK_FUNCS([vprintf vsprintf vasprintf printf])
# Checks for libraries. # Checks for libraries.
AC_CHECK_HEADERS([stdarg.h regex.h iostream map string unistd.h stdlib.h],[],[AC_MSG_ERROR[no]]) AC_CHECK_HEADERS([stdarg.h stdlib.h unistd.h string iostream map boost/regex.hpp],[],[AC_MSG_ERROR([We couldn't find some of the headers.])])
AC_CHECK_FUNCS([vprintf vsprintf vasprintf printf])
AC_CHECK_TYPE([size_t]) AC_CHECK_TYPE([size_t])
AC_CHECK_TYPES([std::string], [], [], [[ AC_CHECK_TYPES([std::string], [], [], [[
@ -84,5 +84,5 @@ AC_ARG_ENABLE([the-force],
AM_CONDITIONAL([ENABLE_THE_FORCE], [test "x$enable_the_force" = "xyes"]) AM_CONDITIONAL([ENABLE_THE_FORCE], [test "x$enable_the_force" = "xyes"])
AC_CONFIG_FILES([Makefile libpipecolors.pc:libpipecolors.pc.in],[],[APIVERSION=PIPECOLORS_VERSION]) AC_CONFIG_FILES([Makefile libpipecolors.pc:libpipecolors.pc.in],[],[APIVERSION=PIPECOLORS_VERSION])
AC_CONFIG_COMMANDS([default],[[]],[[]]) AC_CONFIG_COMMANDS([timestamp], [date >timestamp])
AC_OUTPUT AC_OUTPUT

View File

@ -1,16 +0,0 @@
#include <stdio.h>
#include <unistd.h>
#include <pipecolors.h>
using namespace pipecolors;
int main(void) {
const char* str = "|0101|0202|0303|0404|0505|0606|0707|0808|0909|1010|1111|1212|1313|1414|1515|07";
const char* name = "|01P|02i|03p|04e|05c|06ol|07o|08r|09s |10l|11i|12b|13r|14a|15r|14y |130|12.|111|07";
pcprintf("\n%s\n\n", name);
pcprintf("%s\n", str);
}

View File

@ -18,119 +18,95 @@
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include <cstdio>
#include <iostream> #include <iostream>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <string> #include <string>
#include <stdarg.h>
#include <map> #include <map>
#include <regex> #include <boost/regex.hpp>
//#include "libpipecolors.h" #include "pipecolors.h"
using namespace boost;
namespace pipecolors { namespace pipecolors {
#define C_DEFAULT 0
#define C_BOLD 1
#define C_ITALIC 3
#define C_UNDERLINE 4
#define C_INVERT 7
#define C_B0 "\x1b[1m"
#define C_B1 "\x1b[0m"
#define C_FG_BLACK "\x1b[0;30m"
#define C_FG_RED "\x1b[0;31m"
#define C_FG_GREEN "\x1b[0;32m"
#define C_FG_YELLOW "\x1b[0;33m"
#define C_FG_BLUE "\x1b[0;34m"
#define C_FG_MAGENTA "\x1b[0;35m"
#define C_FG_CYAN "\x1b[0;36m"
#define C_FG_GRAY "\x1b[0;37m"
#define C_FG_DEFAULT "\x1b[0;39m"
#define C_FG_GRAY_D "\x1b[0;90m"
#define C_FG_RED_L "\x1b[0;91m"
#define C_FG_GREEN_L "\x1b[0;92m"
#define C_FG_YELLOW_L "\x1b[0;93m"
#define C_FG_BLUE_L "\x1b[0;94m"
#define C_FG_MAGENTA_L "\x1b[0;95m"
#define C_FG_CYAN_L "\x1b[0;96m"
#define C_FG_WHITE "\x1b[0;97m"
#define C_BG_NONE ""
#define C_BG_RED "\x1b[1;41m"
#define C_BG_GREEN "\x1b[1;42m"
#define C_BG_BLUE "\x1b[1;44m"
#define C_BG_DEFAULT "\x1b[1;49m"
std::map<std::string, std::string> getColors() {
std::map<std::string, std::string> colors; std::map<std::string, std::string> colors;
colors["|00"] = C_FG_BLACK;
colors["|01"] = C_FG_BLUE;
colors["|02"] = C_FG_GREEN;
colors["|03"] = C_FG_CYAN;
colors["|04"] = C_FG_RED;
colors["|05"] = C_FG_MAGENTA;
colors["|06"] = C_FG_YELLOW;
colors["|07"] = C_FG_GRAY;
colors["|08"] = C_FG_GRAY_D;
colors["|09"] = C_FG_BLUE_L;
colors["|10"] = C_FG_GREEN_L;
colors["|11"] = C_FG_CYAN_L;
colors["|12"] = C_FG_RED_L;
colors["|13"] = C_FG_MAGENTA_L;
colors["|14"] = C_FG_YELLOW_L;
colors["|15"] = C_FG_WHITE;
colors["|16"] = C_BG_DEFAULT; // C_BG_BLACK
colors["|17"] = C_BG_BLUE;
colors["|18"] = C_BG_GREEN;
colors["|19"] = C_BG_BLUE; // C_BG_CYAN
colors["|20"] = C_BG_RED;
colors["|21"] = C_BG_RED; // C_BG_MAGENTA
colors["|22"] = C_FG_WHITE; //C_BG_BROWN
colors["|23"] = C_BG_DEFAULT; // C_BG_WHITE
colors["|30"] = C_B0; //Bold OFF
colors["|31"] = C_B1; // Bold ON
return colors;
}
bool has_colors(void) { std::map<std::string, std::string> getColors() {
return isatty(fileno(stdout)); std::map<std::string, std::string> colors;
} colors["|00"] = "\x1b[0;30m"; // FG_BLACK;
colors["|01"] = "\x1b[0;34m"; // FG_BLUE;
void pcprintf( const char * fmt, ... ) { colors["|02"] = "\x1b[0;32m"; // FG_GREEN;
colors["|03"] = "\x1b[0;36m"; // FG_CYAN;
char *buffer; colors["|04"] = "\x1b[0;31m"; // FG_RED;
std::map<std::string, std::string> colors; colors["|05"] = "\x1b[0;35m"; // FG_MAGENTA;
std::size_t index; colors["|06"] = "\x1b[0;33m"; // FG_YELLOW;
std::smatch matches; colors["|07"] = "\x1b[0;37m"; // FG_GRAY;
colors["|08"] = "\x1b[0;90m"; // FG_GRAY_D;
va_list args; colors["|09"] = "\x1b[0;94m"; // FG_BLUE_L;
va_start(args, fmt); colors["|10"] = "\x1b[0;92m"; // FG_GREEN_L;
colors["|11"] = "\x1b[0;96m"; // FG_CYAN_L;
if(int size = vasprintf(&buffer, fmt, args) == -1) { colors["|12"] = "\x1b[0;91m"; // FG_RED_L;
free(buffer); colors["|13"] = "\x1b[0;95m"; // FG_MAGENTA_L;
exit(EXIT_FAILURE); colors["|14"] = "\x1b[0;93m"; // FG_YELLOW_L;
colors["|15"] = "\x1b[0;97m"; // FG_WHITE;
colors["|16"] = "\x1b[1;40m"; // BG_DEFAULT;
colors["|17"] = "\x1b[1;44m"; // BG_BLUE;
colors["|18"] = "\x1b[1;42m"; // BG_GREEN;
colors["|19"] = "\x1b[1;46m"; // BG_CYAN;
colors["|20"] = "\x1b[1;41m"; // BG_RED;
colors["|21"] = "\x1b[1;45m"; // BG_MAGENTA;
colors["|22"] = "\x1b[1;43m"; // BG_YELLOW;
colors["|23"] = "\x1b[1;47m"; // BG_WHITE;
colors["|30"] = "\x1b[1m"; // Bold ON
colors["|31"] = "\x1b[0m"; // Bold OFF
colors["|39"] = "\x1b[0;39m"; // FG_DEFAULT
return colors;
}
bool has_colors(void) {
return isatty(fileno(stdout));
} }
std::string text(buffer), s(buffer); int pcprintf( const char * fmt, ...)
va_end(args); {
free(buffer); char * buffer;
va_list args;
std::regex reg ("(\\|\\d\\d)", std::regex_constants::ECMAScript | std::regex_constants::nosubs); int ret;
colors = getColors();
colors = getColors(); va_start(args, fmt);
while (std::regex_search (text,matches,reg)) { ret = vasprintf(&buffer, fmt, args);
for (auto x:matches) { va_end(args);
while ((index = s.find(x)) != std::string::npos) if(ret == -1) {
s.replace(index, x.length(), colors[x]); free(buffer);
exit(EXIT_FAILURE);
} }
text = matches.suffix().str();
std::string s(buffer), result(buffer);
free(buffer);
regex re( "(\\|\\d\\d)" );
std::size_t index;
std::string::const_iterator start, end;
start = s.begin();
end = s.end();
match_results<std::string::const_iterator> match;
match_flag_type flags = boost::match_default;
while(regex_search(start, end, match, re, flags))
{
while ((index = s.find(match[0])) != std::string::npos)
{
s.replace(index, match[0].length(), colors[match[0]]);
}
start = match[0].second;
// update flags:
flags |= boost::match_prev_avail;
flags |= boost::match_not_bob;
}
std::cout << s;
return(ret);
} }
std::cout << s; } // namespace
}
}

10
libpipecolors.pc.in Normal file
View File

@ -0,0 +1,10 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
Name: libpipecolors
Description: Uses old renegade pipe color codes to print colors to the terminal
Version: @PACKAGE_VERSION@
Libs: -L${libdir} -lpipecolors
Cflags: -I${includedir}/pipecolors

View File

@ -10,8 +10,8 @@ namespace pipecolors {
extern "C" { extern "C" {
#endif #endif
void pcprintf(const char* fmt, ...); int pcprintf( const char * format, ... );
void pcsprintf(char **strp, const char *fmt, va_list ap); //int pcsprintf(char **strp, const char *fmt, va_list ap);
#ifdef __cplusplus #ifdef __cplusplus
} }