Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 4480 lines (3853 sloc) 129 KB
#!/bin/bash
#
# Generates C99 Program Project Skeletons Simply and Properly
# Justine Tunney <jtunney@lobstertech.com>
# Licensed MIT
#
function ask {
dest=$1
descrip=$2
default=$3
[[ ! $default ]] && default=$4
[[ ! $default ]] && default=$5
echo -n "$descrip "
[[ $default ]] && echo -n "[$default] "
read $dest
[[ ! $(eval echo $dest) ]] && eval $dest="$default"
}
[[ $DEST ]] || DEST="."
[[ $PROJ ]] || PROJ="$1"
[[ $PROJ ]] || ask PROJ "project name?"
[[ $PROJ ]] || exit 1
if [[ -e $PROJ ]]; then echo "$PROJ already exists!" >&2; exit 1; fi
[[ $DESCRIP ]] || ask DESCRIP "description?" "$PROJ"
[[ $LIBS ]] || ask LIBS "libraries? (space delimited)"
for s in $LIBS; do pkg-config $s --cflags >/dev/null || exit $?; done
[[ $OBJS ]] || ask OBJS "objects? (space delimited)"
[[ $EMAIL ]] || ask EMAIL "author email?" "$(git config user.email)" "$USER@$(hostname -f)"
[[ $AUTHOR ]] || ask AUTHOR "author name?" "$(git config user.name)" "$USER"
[[ $LICENSE ]] || ask LICENSE "license?" "GNU AGPL v3 or later"
PROJ_SYMBOL=$(echo $PROJ | tr a-z A-Z | tr - _)
mkdir $PROJ
cd $PROJ
mkdir -p doc
mkdir -p src/test
echo "generating .gitignore..."
cat >.gitignore <<EOF
# stuff for this project
/$PROJ
# typical c project stuff
*.~
*.o
*.a
*.lo
*.la
*.pyc
doc
core
vgcore.*
callgrind.out
TAGS
.libs
.gdb_history
# libtoolize/aclocal should make these for us
/m4/pkg.m4
/m4/libtool.m4
/m4/ltoptions.m4
/m4/ltsugar.m4
/m4/ltversion.m4
/m4/lt~obsolete.m4
# trash autotools leaves laying around
/.libs
/.deps
/config
/libtool
/stamp-h1
/Makefile
/config.h
/configure
/aclocal.m4
/config.log
/Makefile.in
/config.h.in
/config.h.in~
/config.cache
/config.status
/autom4te.cache
EOF
echo "generating README.rst..."
cat >README.rst <<EOF
.. -*-rst-*-
=$(echo $PROJ | sed s/./=/g)=
$PROJ
=$(echo $PROJ | sed s/./=/g)=
:name: $PROJ
:description: $DESCRIP
:copyright: © $(date +%Y) $AUTHOR
:license: $LICENSE
Setup
=====
How to install::
./configure
make
sudo make install
How to run::
$PROJ
Developers
----------
Stuff you should install::
sudo apt-get install build-essential libtool autoconf automake pkg-config
sudo apt-get install gdb valgrind strace ltrace tshark
How to build::
./autogen.sh
./configure -C CFLAGS="-g3 -O0 -Wall -pedantic"
make
How to test::
make check
./leak ./test
EOF
######################################################################
echo "generating Makefile.am..."
cat >Makefile.am <<EOF
NAME = $PROJ
AUTOMAKE_OPTS = foreign
ACLOCAL_AMFLAGS = -I m4
EXTRA_DIST = autogen.sh
bin_PROGRAMS = $PROJ
${PROJ}_SOURCES = src/$PROJ.c src/${PROJ}_private.h src/log.c src/log.h src/bstrlib.c src/bstrlib.h$(for s in $OBJS; do echo -n " src/$s.c src/$s.h"; done)
${PROJ}_CFLAGS = -Isrc$(for s in $LIBS; do echo -n " \$($(echo $s | tr a-z A-Z | tr - _)_CFLAGS)"; done)
${PROJ}_LDADD =$(for s in $LIBS; do echo -n " \$($(echo $s | tr a-z A-Z | tr - _)_LIBS)"; done)
check_PROGRAMS = test
TESTS = \$(check_PROGRAMS)
test_SOURCES = src/test/test.c
test_CFLAGS = -Isrc$(for s in $LIBS; do echo -n " \$($(echo $s | tr a-z A-Z | tr - _)_CFLAGS)"; done)
test_LDADD =$(for s in $LIBS; do echo -n " \$($(echo $s | tr a-z A-Z | tr - _)_LIBS)"; done)
maintainer-clean-local:
rm -rf config
rm -f aclocal.m4 config.h* configure Makefile.in \\
m4/pkg.m4 m4/libtool.m4 m4/ltoptions.m4 m4/ltsugar.m4 \\
m4/ltversion.m4 m4/lt~obsolete.m4
find . -type f -name \*.pyc \\
-or -name \*~ \\
-or -name -name \.*~ \\
-or -name \#\* \\
-or -name \.\#\* \\
-or -name core\* \\
-or -name vgcore\* \\
| xargs rm -f
EOF
######################################################################
echo "generating configure.ac..."
cat >configure.ac <<EOF
# -*- Autoconf -*-
AC_PREREQ(2.61)
AC_INIT([$PROJ], [0.1], [$EMAIL])
AC_CONFIG_AUX_DIR(config)
AC_CONFIG_MACRO_DIR(m4)
AM_INIT_AUTOMAKE([1.11.1 silent-rules foreign -Wall])
AC_CONFIG_SRCDIR([src/$PROJ.c])
AM_CONFIG_HEADER([config.h])
AM_SILENT_RULES
AC_CANONICAL_BUILD
AC_CANONICAL_HOST
AC_USE_SYSTEM_EXTENSIONS
AC_PROG_CC_C99
AM_PROG_CC_C_O
PKG_PROG_PKG_CONFIG([0.22])
AC_CHECK_LIB([m], [cos], [], [AC_MSG_ERROR([no math library?!])])
AC_CHECK_LIB([rt], [clock_gettime])
EOF
for pname in $LIBS; do
vname=$(echo $pname | tr a-z A-Z | tr - _)
echo "PKG_CHECK_MODULES($vname, $pname)" >>configure.ac
done
cat >>configure.ac <<EOF
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
EOF
######################################################################
echo "generating autogen.sh..."
cat >autogen.sh <<EOF
#!/bin/sh
set -x
rm -rf aclocal.m4 config.cache autom4te*.cache config.h.in
libtoolize --copy --force --automake || exit \$?
aclocal -I m4 --install --force || exit \$?
autoheader -f || exit \$?
automake --copy --gnu --add-missing || exit \$?
autoconf || exit \$?
EOF
chmod +x autogen.sh
######################################################################
echo "generating leak..."
cat >leak <<EOF
#!/bin/sh
exec valgrind --tool=memcheck --leak-check=yes --leak-resolution=high --show-reachable=yes --track-origins=yes --num-callers=20 --track-fds=yes \$@
EOF
chmod +x leak
######################################################################
echo "generating src/${PROJ}_private.h..."
cat >src/${PROJ}_private.h <<EOF
/*
* $PROJ -- $DESCRIP
* Copyright (c) $(date +%Y) $AUTHOR
* Licensed $LICENSE
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <argp.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <dirent.h>
#include <fcntl.h>
#include <regex.h>
#include <netdb.h>
#include <sched.h>
EOF
for s in $LIBS; do
if [[ -f /usr/include/$s.h || -f /usr/local/include/$s.h ]]; then
echo "#include <$s.h>" >>src/${PROJ}_private.h
echo >>src/${PROJ}_private.h
fi
done
if echo $LIBS | grep sofia-sip-ua >/dev/null; then
cat >>src/${PROJ}_private.h <<EOF
#include <sofia-sip/su.h>
#include <sofia-sip/url.h>
#include <sofia-sip/bnf.h>
#include <sofia-sip/sdp.h>
#include <sofia-sip/msg.h>
#include <sofia-sip/nua.h>
#include <sofia-sip/su_log.h>
#include <sofia-sip/sdp_tag.h>
#include <sofia-sip/su_wait.h>
#include <sofia-sip/string0.h>
#include <sofia-sip/su_alloc.h>
#include <sofia-sip/msg_addr.h>
#include <sofia-sip/sip_util.h>
#include <sofia-sip/msg_types.h>
#include <sofia-sip/su_tag_io.h>
#include <sofia-sip/su_strlst.h>
#include <sofia-sip/su_vector.h>
#include <sofia-sip/sip_extra.h>
#include <sofia-sip/tport_tag.h>
#include <sofia-sip/sip_protos.h>
#include <sofia-sip/hostdomain.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/http_status.h>
#include <sofia-sip/stun_common.h>
#include <sofia-sip/su_addrinfo.h>
#include <sofia-sip/su_uniqueid.h>
#include <sofia-sip/su_localinfo.h>
#include "bstrlib.h"
#include "log.h"
EOF
fi
for NAME in $OBJS; do
echo "#include \"$NAME.h\"" >>src/${PROJ}_private.h
done
cat >>src/${PROJ}_private.h <<EOF
struct optargs {
const char *logfile; /**< Used internally to configure logger */
int verbosity; /**< Used internally to configure logger */
};
extern struct optargs args[1];
/* For Emacs:
* Local Variables:
* indent-tabs-mode:nil
* tab-width:4
* c-basic-offset:4
* c-file-style:nil
* End:
* For VIM:
* vim:set expandtab softtabstop=4 shiftwidth=4 tabstop=4:
*/
EOF
######################################################################
echo "generating src/$PROJ.c..."
cat >src/$PROJ.c <<EOF
/*
* $PROJ -- $DESCRIP
* Copyright (c) $(date +%Y) $AUTHOR
* Licensed $LICENSE
*/
#include "${PROJ}_private.h"
int main(int argc, char *argv[])
{
int n = 1;
log_devel("hello kitty no. %d", n++);
log_devel("hello kitty no. %d", n++);
log_debug("hello kitty no. %d", n++);
log_info("hello kitty no. %d", n++);
log("hello kitty no. %d", n++);
log_notice("hello kitty no. %d", n++);
log_warning("hello kitty no. %d", n++);
log_error("hello kitty no. %d", n++);
log_crash("hello kitty no. %d", n++);
exit(0);
}
/* For Emacs:
* Local Variables:
* indent-tabs-mode:nil
* tab-width:4
* c-basic-offset:4
* c-file-style:nil
* End:
* For VIM:
* vim:set expandtab softtabstop=4 shiftwidth=4 tabstop=4:
*/
EOF
######################################################################
echo "generating src/test/test.c..."
cat >src/test/test.c <<EOF
/*
* $PROJ -- $DESCRIP
* Copyright (c) $(date +%Y) $AUTHOR
* Licensed $LICENSE
*/
#include "${PROJ}_private.h"
int main(int argc, char *argv[])
{
exit(0);
}
/* For Emacs:
* Local Variables:
* indent-tabs-mode:nil
* tab-width:4
* c-basic-offset:4
* c-file-style:nil
* End:
* For VIM:
* vim:set expandtab softtabstop=4 shiftwidth=4 tabstop=4:
*/
EOF
######################################################################
echo "generating src/log.h..."
cat >src/log.h <<EOF
/*
* $PROJ -- $DESCRIP
* Copyright (c) $(date +%Y) $AUTHOR
* Licensed $LICENSE
*/
#ifndef ${PROJ_SYMBOL}_LOG_H
#define ${PROJ_SYMBOL}_LOG_H
#include <stdio.h>
#include <stdbool.h>
#define LOG_CRASH 0
#define LOG_ERROR 1
#define LOG_WARNING 2
#define LOG_NOTICE 3
#define LOG_INFO 4
#define LOG_DEBUG 5
#define LOG_DEVEL 6
#define BOLD "\x1b[1m"
#define UNBOLD "\x1b[22m"
#define FLIP "\x1b[7m"
#define PLAIN "\x1b[0m"
#define RED "\x1b[31m"
#define BLUE "\x1b[34m"
#define CYAN "\x1b[36m"
#define GREEN "\x1b[32m"
#define YELLOW "\x1b[33m"
#define MAGENTA "\x1b[35m"
#define HOTPINK "\x1b[38;5;200m"
#define FGOFF "\x1b[39m"
#define BGOFF "\x1b[49m"
#define BOLDOFF "\x1b[22m"
#define FLIPOFF "\x1b[27m"
/* useful for reducing binary size or security */
#ifdef NO_LINENOS
# define FUNK NULL, NULL, 0
#else
# define FUNK __func__, __FILE__, __LINE__
#endif
#define log(...) loggy(LOG_INFO, FUNK, __VA_ARGS__)
#define log_devel(...) loggy(LOG_DEVEL, FUNK, __VA_ARGS__)
#define log_debug(...) loggy(LOG_DEBUG, FUNK, __VA_ARGS__)
#define log_info(...) loggy(LOG_INFO, FUNK, __VA_ARGS__)
#define log_notice(...) loggy(LOG_NOTICE, FUNK, __VA_ARGS__)
#define log_warning(...) loggy(LOG_WARNING, FUNK, __VA_ARGS__)
#define log_error(...) loggy(LOG_ERROR, FUNK, __VA_ARGS__)
#define log_crash(...) loggy(LOG_CRASH, FUNK, __VA_ARGS__)
/**
* Configures logger based on environment variables
*
* - LOGFILE: Absolute filename like /var/log/$PROJ.log (default stderr)
* If this is set blank, logging is disabled.
*
* - LOGLEVEL: Maximum verbosity. Can be crash, warning, notice, info,
* debug, or devel. The default is warning.
*
* This function is called for your automatically so only call it again
* if you changed the environment variables.
*/
void log_autoconf();
/**
* Configure logger manually
*
* \code
* setenv("TZ", "UTC");
* static const char *my_log_formats[] = {
* "{DATE}T{TIME}.{MS}Z CRASH {MSG}",
* "{DATE}T{TIME}.{MS}Z WARNING {MSG}",
* "{DATE}T{TIME}.{MS}Z NOTICE {MSG}",
* "{DATE}T{TIME}.{MS}Z INFO {MSG}",
* "{DATE}T{TIME}.{MS}Z DEBUG {MSG}",
* "{DATE}T{TIME}.{US}Z DEVEL {MSG} ({ELAP} {FUNC}@{FILENAME}:{LINE})",
* NULL,
* };
* log_conf(fopen("/var/log/$PROJ.log", "ab"), true, my_log_formats);
* \endcode
*
* The following format variables are available:
*
* - DATE: ISO 8601 date formatted as "%Y-%m-%d"
* - TIME: ISO 8601 time formatted as "%H:%M:%S"
* - MS: Milliseconds
* - US: Microseconds
* - ELAP: Shows seconds since last log emission with μs precision
* - FUNC Calling source function name
* - FILE: Calling source file (relative to project root)
* - FILENAME: Calling source filename (basename of FILE)
* - LINE: Line in source file that invoked logger.
*
* @param level Highest verbosity log level you want
* @param outfp Output standard i/o file.
* @param autoclose Automatically fclose(outfp) for you on exit.
* @param fmts List of NULL terminated format strings. If NULL then
* the defaults will be used. The colorized defaults will
* be enabled if outfp is a terminal.
*/
void log_conf(int level, FILE *outfp, bool autoclose, const char **fmts);
/**
* Backend logging function
*/
void loggy(int level, const char *func, const char *file, int line,
const char *fmt, ...)
__attribute__ ((format (printf, 5, 6)));
#endif /* ${PROJ_SYMBOL}_LOG_H */
/* For Emacs:
* Local Variables:
* indent-tabs-mode:nil
* tab-width:4
* c-basic-offset:4
* c-file-style:nil
* End:
* For VIM:
* vim:set expandtab softtabstop=4 shiftwidth=4 tabstop=4:
*/
EOF
######################################################################
echo "generating src/log.c..."
cat >src/log.c <<EOF
/*
* $PROJ -- $DESCRIP
* Copyright (c) $(date +%Y) $AUTHOR
* Licensed $LICENSE
*/
#include "${PROJ}_private.h"
static const char *formats_plain[] = {
"{DATE}T{TIME}.{US} [CRASH] {MSG} ({FUNC}@{FILENAME}:{LINE})\n",
"{DATE}T{TIME}.{US} [ERROR] {MSG}\n",
"{DATE}T{TIME}.{US} [WARNING] {MSG}\n",
"{DATE}T{TIME}.{US} [NOTICE] {MSG}\n",
"{DATE}T{TIME}.{US} [INFO] {MSG}\n",
"{DATE}T{TIME}.{US} [DEBUG] {MSG} ({FUNC}@{FILENAME}:{LINE})\n",
"{DATE}T{TIME}.{US} {ELAP} [DEVEL] {MSG} ({FUNC}@{FILENAME}:{LINE})\n",
NULL,
};
static const char *formats_color[] = {
BOLD RED FLIP "{DATE}T{TIME}.{US} [CRASH] {MSG} ({FUNC}@{FILENAME}:{LINE})" PLAIN "\n",
RED "{DATE}T{TIME}.{US} [" BOLD "ERROR" UNBOLD "] {MSG}" PLAIN "\n",
"{DATE}T{TIME}.{US} [" BOLD RED "WARNING" PLAIN "] {MSG}\n",
"{DATE}T{TIME}.{US} [" BOLD YELLOW "NOTICE" PLAIN "] {MSG}\n",
"{DATE}T{TIME}.{US} [" BOLD "INFO" PLAIN "] {MSG}\n",
GREEN "{DATE}T{TIME}.{US} [" BOLD "DEBUG" UNBOLD "] {MSG} ({FUNC}@{FILENAME}:{LINE})" PLAIN "\n",
MAGENTA "{DATE}T{TIME}.{US} {ELAP} [" BOLD "DEVEL" UNBOLD "] {MSG} ({FUNC}@{FILENAME}:{LINE})" PLAIN "\n",
NULL,
};
static int loglevel;
static int loglevel_max;
static FILE *logfile = NULL;
static const char **formats;
static bool initialized = false;
static bool logfile_autoclose = false;
static void cleanup()
{
if (logfile && logfile_autoclose) {
fclose(logfile);
logfile = NULL;
}
}
void log_conf(int level, FILE *out, bool autoclose, const char **fmts)
{
if (!initialized) {
initialized = true;
atexit(cleanup);
}
cleanup();
loglevel = level;
if (out) {
logfile = out;
logfile_autoclose = autoclose;
} else {
logfile = stderr;
logfile_autoclose = false;
}
if (!fmts) {
formats = isatty(fileno(logfile)) ? formats_color : formats_plain;
} else {
formats = fmts;
assert(formats[0]);
}
for (loglevel_max = 0; formats[loglevel_max + 1]; loglevel_max++);
}
void log_autoconf()
{
FILE *out = NULL;
const char *ts;
int level;
bool autoclose;
ts = getenv("LOGFILE");
if (ts) {
if (*ts) {
out = fopen(ts, "ab");
autoclose = true;
} else {
out = NULL;
autoclose = false;
}
} else {
out = stderr;
autoclose = false;
}
ts = getenv("LOGLEVEL");
if (ts) {
if (strcasecmp(ts, "devel") == 0) {
level = LOG_DEVEL;
} else if (strcasecmp(ts, "debug") == 0) {
level = LOG_DEBUG;
} else if (strcasecmp(ts, "info") == 0) {
level = LOG_INFO;
} else if (strcasecmp(ts, "notice") == 0) {
level = LOG_NOTICE;
} else if (strcasecmp(ts, "warning") == 0) {
level = LOG_WARNING;
} else if (strcasecmp(ts, "crash") == 0) {
level = LOG_CRASH;
} else {
level = atoi(ts);
}
} else {
level = LOG_WARNING;
}
log_conf(level, out, autoclose, NULL);
}
static int replace(bstring bs, const char *fs, const char *rs, int rl)
{
struct tagbstring bf;
struct tagbstring br;
btfromcstr(bf, fs);
blk2tbstr(br, rs, (rl >= 0) ? rl : strlen(rs));
return bfindreplace(bs, &bf, &br, 0);
}
void loggy(int level, const char *func, const char *file, int line,
const char *fmt, ...)
{
if (!initialized)
log_autoconf();
if (level > loglevel || !logfile)
return;
if (!(0 <= level && level <= loglevel_max))
return;
/* get string representation of date/time */
struct tm tm[1];
struct timeval tv[1];
char datebuf[32], timebuf[32], msbuf[16], usbuf[16];
gettimeofday(tv, NULL);
localtime_r(&tv->tv_sec, tm);
strftime(datebuf, sizeof(datebuf), "%Y-%m-%d", tm);
strftime(timebuf, sizeof(timebuf), "%H:%M:%S", tm);
snprintf(msbuf, sizeof(msbuf), "%03ld", tv->tv_usec / 1000);
snprintf(usbuf, sizeof(usbuf), "%06ld", tv->tv_usec);
/* calculate how much time elapsed from last log emit */
char elapbuf[16] = { 0 };
#if HAVE_LIBRT
struct timespec ts[1];
if (clock_gettime(CLOCK_MONOTONIC_RAW, ts) == 0) {
int64_t stamp = ts->tv_sec * 1e9 + ts->tv_nsec;
static int64_t stamp_last = -1;
if (stamp_last == -1)
stamp_last = stamp;
float elap = (float)(stamp - stamp_last) / 1e9;
stamp_last = stamp;
snprintf(elapbuf, sizeof(elapbuf), "%.6f", elap);
}
#endif
/* format the printf style message */
char *msg;
int lmsg;
va_list ap;
va_start(ap, fmt);
lmsg = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
assert(lmsg >= 0);
msg = malloc(lmsg + 1);
va_start(ap, fmt);
vsnprintf(msg, lmsg + 1, fmt, ap);
va_end(ap);
char linebuf[16] = { 0 };
const char *filename = "";
const char *filefull = "";
if (file) {
snprintf(linebuf, sizeof(linebuf), "%d", line);
filefull = file;
filename = strrchr(filefull, '/');
filename = filename ? filename + 1 : filefull;
}
/* emit the log entry */
bstring data = bfromcstralloc(lmsg + 1024, formats[level]);
replace(data, "{DATE}", datebuf, -1);
replace(data, "{TIME}", timebuf, -1);
replace(data, "{MS}", msbuf, -1);
replace(data, "{US}", usbuf, -1);
replace(data, "{ELAP}", elapbuf, -1);
replace(data, "{FUNC}", func, -1);
replace(data, "{MSG}", msg, lmsg);
replace(data, "{FILE}", filefull, -1);
replace(data, "{FILENAME}", filename, -1);
replace(data, "{LINE}", linebuf, -1);
fwrite(data->data, 1, data->slen, logfile);
fflush(logfile);
bdestroy(data);
free(msg);
}
/* For Emacs:
* Local Variables:
* indent-tabs-mode:nil
* tab-width:4
* c-basic-offset:4
* c-file-style:nil
* End:
* For VIM:
* vim:set expandtab softtabstop=4 shiftwidth=4 tabstop=4:
*/
EOF
######################################################################
cd src
for NAME in $OBJS; do
export NAME PROJ DESCRIP AUTHOR EMAIL LICENSE USE_SOFIA
c-object-bone
done
cd ..
######################################################################
echo "generating src/args.c..."
cat >src/arg.c <<EOF
/*
* ${PROJ} -- ${DESCRIP}
* Copyright (c) $(date +%Y) $AUTHOR
* Licensed $LICENSE
*/
#include "${NAME}_private.h"
struct optargs args[1];
const char *argp_program_version = PACKAGE_STRING;
static const char doc[] = "${PROJ} -- ${DESCRIP}\
\vGo in more depth here.\
\n";
static const char args_doc[] = "[ARG ...]";
static const struct argp_option options[] = {
{ 0, 0, 0, 0, "Miscellaneous:" },
{ "logfile", 'L', 0, 0,
"Emit logger output to this file (Default stderr)" },
{ "verbose", 'v', 0, 0,
"Increases log verbosity. The more v's the more verbose." },
{ 0 },
};
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
FILE *f;
switch (key) {
case 'L': /* log file */
f = fopen(arg, "ab");
if (!f)
argp_error(state, "couldn't open log file: %s", arg);
fclose(f);
args->logfile = arg;
break;
case 'v': /* verbosity */
args->verbosity++;
break;
case ARGP_KEY_ARG:
break;
case ARGP_KEY_NO_ARGS:
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
void parse_args(int argc, char *argv[])
{
struct argp argp[1] = {{ options, parse_opt, args_doc, doc }};
if (argp_parse(argp, argc, argv, 0, 0, args) != 0) {
fprintf(stderr, "argp_parse() failed: %s\n", strerror(errno));
exit(1);
}
FILE *flog = fopen(args->logfile, "ab");
log_conf(LOG_CRASH + args->verbosity, flog, true, NULL);
}
/* For Emacs:
* Local Variables:
* indent-tabs-mode:nil
* tab-width:4
* c-basic-offset:4
* c-file-style:nil
* End:
* For VIM:
* vim:set expandtab softtabstop=4 shiftwidth=4 tabstop=4:
*/
EOF
######################################################################
echo "generating .gdbinit..."
cat >.gdbinit <<EOF
# -*-GDB-Script-*-
#
# Justine's .gdbinit
# Makes GDB fabulous
#
# This wouldn't be possible without all the code I stole from the
# wonderful .gdbinit maintained by the friendly hackers at
# reverse-engineering.net
#
# Some of the extra commands provided here (like 'reg') will only work
# on x86-64 machines.
#
set history save on
set history expansion on
set prompt \033[0;38;5;200m(>'.')> \033[0m
set confirm off
set verbose off
set print union on
set print array on
set print null-stop on
set print pretty on
set print sevenbit-strings on
set height 0
set width 0
# for those of us who aren't old men with beards
set disassembly-flavor intel
define flags
# OF (overflow) flag
if ((\$eflags >> 0xB) & 1)
printf "O "
set \$_of_flag = 1
else
printf "o "
set \$_of_flag = 0
end
if ((\$eflags >> 0xA) & 1)
printf "D "
else
printf "d "
end
if ((\$eflags >> 9) & 1)
printf "I "
else
printf "i "
end
if ((\$eflags >> 8) & 1)
printf "T "
else
printf "t "
end
# SF (sign) flag
if ((\$eflags >> 7) & 1)
printf "S "
set \$_sf_flag = 1
else
printf "s "
set \$_sf_flag = 0
end
# ZF (zero) flag
if ((\$eflags >> 6) & 1)
printf "Z "
set \$_zf_flag = 1
else
printf "z "
set \$_zf_flag = 0
end
if ((\$eflags >> 4) & 1)
printf "A "
else
printf "a "
end
# PF (parity) flag
if ((\$eflags >> 2) & 1)
printf "P "
set \$_pf_flag = 1
else
printf "p "
set \$_pf_flag = 0
end
# CF (carry) flag
if (\$eflags & 1)
printf "C "
set \$_cf_flag = 1
else
printf "c "
set \$_cf_flag = 0
end
printf "\n"
end
document flags
Print flags register.
end
define eflags
printf " OF <%d> DF <%d> IF <%d> TF <%d>",\
((\$eflags >> 0xB) & 1), ((\$eflags >> 0xA) & 1), \
((\$eflags >> 9) & 1), ((\$eflags >> 8) & 1)
printf " SF <%d> ZF <%d> AF <%d> PF <%d> CF <%d>\n",\
((\$eflags >> 7) & 1), ((\$eflags >> 6) & 1),\
((\$eflags >> 4) & 1), ((\$eflags >> 2) & 1), (\$eflags & 1)
printf " ID <%d> VIP <%d> VIF <%d> AC <%d>",\
((\$eflags >> 0x15) & 1), ((\$eflags >> 0x14) & 1), \
((\$eflags >> 0x13) & 1), ((\$eflags >> 0x12) & 1)
printf " VM <%d> RF <%d> NT <%d> IOPL <%d>\n",\
((\$eflags >> 0x11) & 1), ((\$eflags >> 0x10) & 1),\
((\$eflags >> 0xE) & 1), ((\$eflags >> 0xC) & 3)
end
document eflags
Print eflags register.
end
define reg
echo \033[32m
printf "RAX:"
echo \033[0m
printf "%016X ", \$rax
echo \033[32m
printf "RBX:"
echo \033[0m
printf "%016X ", \$rbx
echo \033[32m
printf "RCX:"
echo \033[0m
printf "%016X ", \$rcx
echo \033[32m
printf "RDX:"
echo \033[0m
printf "%016X\n", \$rdx
echo \033[32m
printf " R8:"
echo \033[0m
printf "%016X ", \$r8
echo \033[32m
printf " R9:"
echo \033[0m
printf "%016X ", \$r9
echo \033[32m
printf "R10:"
echo \033[0m
printf "%016X ", \$r10
echo \033[32m
printf "R11:"
echo \033[0m
printf "%016X\n", \$r11
echo \033[32m
printf "R12:"
echo \033[0m
printf "%016X ", \$r12
echo \033[32m
printf "R13:"
echo \033[0m
printf "%016X ", \$r13
echo \033[32m
printf "R14:"
echo \033[0m
printf "%016X ", \$r14
echo \033[32m
printf "R15:"
echo \033[0m
printf "%016X\n", \$r15
echo \033[32m
printf "RSI:"
echo \033[0m
printf "%016X ", \$rsi
echo \033[32m
printf "RDI:"
echo \033[0m
printf "%016X ", \$rdi
echo \033[32m
printf "RBP:"
echo \033[0m
printf "%016X ", \$rbp
echo \033[32m
printf "RSP:"
echo \033[0m
printf "%016X\n", \$rsp
echo \033[32m
printf "RIP:"
echo \033[0m
printf "%016X", \$rip
printf " "
echo \033[1;31m
flags
echo \033[0m
# # call smallregisters
# smallregisters
# # display conditional jump routine
# dumpjump
# printf "\n"
end
document reg
Print CPU registers. Currently x86-64 only.
end
define init
tbreak _init
r
end
document init
Run program and break on _init().
end
define start
tbreak _start
r
end
document start
Run program and break on _start().
end
define sstart
tbreak __libc_start_main
r
end
document sstart
Run program and break on __libc_start_main().
Useful for stripped executables.
end
define main
tbreak main
r
end
document main
Run program and break on main().
end
EOF
######################################################################
echo "generating src/bstrlib.h..."
cat >src/bstrlib.h <<EOF
/*
* This source file is part of the bstring string library. This code was
* written by Paul Hsieh in 2002-2010, and is covered by either the 3-clause
* BSD open source license or GPL v2.0. Refer to the accompanying documentation
* for details on usage and license.
*/
/*
* bstrlib.h
*
* This file is the header file for the core module for implementing the
* bstring functions.
*/
#ifndef BSTRLIB_INCLUDE
#define BSTRLIB_INCLUDE
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#if !defined (BSTRLIB_VSNP_OK) && !defined (BSTRLIB_NOVSNP)
# if defined (__TURBOC__) && !defined (__BORLANDC__)
# define BSTRLIB_NOVSNP
# endif
#endif
#define BSTR_ERR (-1)
#define BSTR_OK (0)
#define BSTR_BS_BUFF_LENGTH_GET (0)
typedef struct tagbstring * bstring;
typedef const struct tagbstring * const_bstring;
/* Copy functions */
#define cstr2bstr bfromcstr
extern bstring bfromcstr (const char * str);
extern bstring bfromcstralloc (int mlen, const char * str);
extern bstring blk2bstr (const void * blk, int len);
extern char * bstr2cstr (const_bstring s, char z);
extern int bcstrfree (char * s);
extern bstring bstrcpy (const_bstring b1);
extern int bassign (bstring a, const_bstring b);
extern int bassignmidstr (bstring a, const_bstring b, int left, int len);
extern int bassigncstr (bstring a, const char * str);
extern int bassignblk (bstring a, const void * s, int len);
/* Destroy function */
extern int bdestroy (bstring b);
/* Space allocation hinting functions */
extern int balloc (bstring s, int len);
extern int ballocmin (bstring b, int len);
/* Substring extraction */
extern bstring bmidstr (const_bstring b, int left, int len);
/* Various standard manipulations */
extern int bconcat (bstring b0, const_bstring b1);
extern int bconchar (bstring b0, char c);
extern int bcatcstr (bstring b, const char * s);
extern int bcatblk (bstring b, const void * s, int len);
extern int binsert (bstring s1, int pos, const_bstring s2, unsigned char fill);
extern int binsertch (bstring s1, int pos, int len, unsigned char fill);
extern int breplace (bstring b1, int pos, int len, const_bstring b2, unsigned char fill);
extern int bdelete (bstring s1, int pos, int len);
extern int bsetstr (bstring b0, int pos, const_bstring b1, unsigned char fill);
extern int btrunc (bstring b, int n);
/* Scan/search functions */
extern int bstricmp (const_bstring b0, const_bstring b1);
extern int bstrnicmp (const_bstring b0, const_bstring b1, int n);
extern int biseqcaseless (const_bstring b0, const_bstring b1);
extern int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len);
extern int biseq (const_bstring b0, const_bstring b1);
extern int bisstemeqblk (const_bstring b0, const void * blk, int len);
extern int biseqcstr (const_bstring b, const char * s);
extern int biseqcstrcaseless (const_bstring b, const char * s);
extern int bstrcmp (const_bstring b0, const_bstring b1);
extern int bstrncmp (const_bstring b0, const_bstring b1, int n);
extern int binstr (const_bstring s1, int pos, const_bstring s2);
extern int binstrr (const_bstring s1, int pos, const_bstring s2);
extern int binstrcaseless (const_bstring s1, int pos, const_bstring s2);
extern int binstrrcaseless (const_bstring s1, int pos, const_bstring s2);
extern int bstrchrp (const_bstring b, int c, int pos);
extern int bstrrchrp (const_bstring b, int c, int pos);
#define bstrchr(b,c) bstrchrp ((b), (c), 0)
#define bstrrchr(b,c) bstrrchrp ((b), (c), blength(b)-1)
extern int binchr (const_bstring b0, int pos, const_bstring b1);
extern int binchrr (const_bstring b0, int pos, const_bstring b1);
extern int bninchr (const_bstring b0, int pos, const_bstring b1);
extern int bninchrr (const_bstring b0, int pos, const_bstring b1);
extern int bfindreplace (bstring b, const_bstring find, const_bstring repl, int pos);
extern int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl, int pos);
/* List of string container functions */
struct bstrList {
int qty, mlen;
bstring * entry;
};
extern struct bstrList * bstrListCreate (void);
extern int bstrListDestroy (struct bstrList * sl);
extern int bstrListAlloc (struct bstrList * sl, int msz);
extern int bstrListAllocMin (struct bstrList * sl, int msz);
/* String split and join functions */
extern struct bstrList * bsplit (const_bstring str, unsigned char splitChar);
extern struct bstrList * bsplits (const_bstring str, const_bstring splitStr);
extern struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr);
extern bstring bjoin (const struct bstrList * bl, const_bstring sep);
extern int bsplitcb (const_bstring str, unsigned char splitChar, int pos,
int (* cb) (void * parm, int ofs, int len), void * parm);
extern int bsplitscb (const_bstring str, const_bstring splitStr, int pos,
int (* cb) (void * parm, int ofs, int len), void * parm);
extern int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos,
int (* cb) (void * parm, int ofs, int len), void * parm);
/* Miscellaneous functions */
extern int bpattern (bstring b, int len);
extern int btoupper (bstring b);
extern int btolower (bstring b);
extern int bltrimws (bstring b);
extern int brtrimws (bstring b);
extern int btrimws (bstring b);
/* <*>printf format functions */
#if !defined (BSTRLIB_NOVSNP)
extern bstring bformat (const char * fmt, ...);
extern int bformata (bstring b, const char * fmt, ...);
extern int bassignformat (bstring b, const char * fmt, ...);
extern int vbassignformat (bstring b, const char * fmt, va_list arglist);
extern int bvcformata (bstring b, int count, const char * fmt, va_list arglist);
#define bvformata(ret, b, fmt, lastarg) { \
bstring bstrtmp_b = (b); \
const char * bstrtmp_fmt = (fmt); \
int bstrtmp_r = BSTR_ERR, bstrtmp_sz = 16; \
for (;;) { \
va_list bstrtmp_arglist; \
va_start (bstrtmp_arglist, lastarg); \
bstrtmp_r = bvcformata (bstrtmp_b, bstrtmp_sz, bstrtmp_fmt, bstrtmp_arglist); \
va_end (bstrtmp_arglist); \
if (bstrtmp_r >= 0) { /* Everything went ok */ \
bstrtmp_r = BSTR_OK; \
break; \
} else if (-bstrtmp_r <= bstrtmp_sz) { /* A real error? */ \
bstrtmp_r = BSTR_ERR; \
break; \
} \
bstrtmp_sz = -bstrtmp_r; /* Doubled or target size */ \
} \
ret = bstrtmp_r; \
}
#endif
typedef int (*bNgetc) (void *parm);
typedef size_t (* bNread) (void *buff, size_t elsize, size_t nelem, void *parm);
/* Input functions */
extern bstring bgets (bNgetc getcPtr, void * parm, char terminator);
extern bstring bread (bNread readPtr, void * parm);
extern int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator);
extern int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator);
extern int breada (bstring b, bNread readPtr, void * parm);
/* Stream functions */
extern struct bStream * bsopen (bNread readPtr, void * parm);
extern void * bsclose (struct bStream * s);
extern int bsbufflength (struct bStream * s, int sz);
extern int bsreadln (bstring b, struct bStream * s, char terminator);
extern int bsreadlns (bstring r, struct bStream * s, const_bstring term);
extern int bsread (bstring b, struct bStream * s, int n);
extern int bsreadlna (bstring b, struct bStream * s, char terminator);
extern int bsreadlnsa (bstring r, struct bStream * s, const_bstring term);
extern int bsreada (bstring b, struct bStream * s, int n);
extern int bsunread (struct bStream * s, const_bstring b);
extern int bspeek (bstring r, const struct bStream * s);
extern int bssplitscb (struct bStream * s, const_bstring splitStr,
int (* cb) (void * parm, int ofs, const_bstring entry), void * parm);
extern int bssplitstrcb (struct bStream * s, const_bstring splitStr,
int (* cb) (void * parm, int ofs, const_bstring entry), void * parm);
extern int bseof (const struct bStream * s);
struct tagbstring {
int mlen;
int slen;
unsigned char * data;
};
/* Accessor macros */
#define blengthe(b, e) (((b) == (void *)0 || (b)->slen < 0) ? (int)(e) : ((b)->slen))
#define blength(b) (blengthe ((b), 0))
#define bdataofse(b, o, e) (((b) == (void *)0 || (b)->data == (void*)0) ? (char *)(e) : ((char *)(b)->data) + (o))
#define bdataofs(b, o) (bdataofse ((b), (o), (void *)0))
#define bdatae(b, e) (bdataofse (b, 0, e))
#define bdata(b) (bdataofs (b, 0))
#define bchare(b, p, e) ((((unsigned)(p)) < (unsigned)blength(b)) ? ((b)->data[(p)]) : (e))
#define bchar(b, p) bchare ((b), (p), '\0')
/* Static constant string initialization macro */
#define bsStaticMlen(q,m) {(m), (int) sizeof(q)-1, (unsigned char *) ("" q "")}
#if defined(_MSC_VER)
/* There are many versions of MSVC which emit __LINE__ as a non-constant. */
# define bsStatic(q) bsStaticMlen(q,-32)
#endif
#ifndef bsStatic
# define bsStatic(q) bsStaticMlen(q,-__LINE__)
#endif
/* Static constant block parameter pair */
#define bsStaticBlkParms(q) ((void *)("" q "")), ((int) sizeof(q)-1)
/* Reference building macros */
#define cstr2tbstr btfromcstr
#define btfromcstr(t,s) { \
(t).data = (unsigned char *) (s); \
(t).slen = ((t).data) ? ((int) (strlen) ((char *)(t).data)) : 0; \
(t).mlen = -1; \
}
#define blk2tbstr(t,s,l) { \
(t).data = (unsigned char *) (s); \
(t).slen = l; \
(t).mlen = -1; \
}
#define btfromblk(t,s,l) blk2tbstr(t,s,l)
#define bmid2tbstr(t,b,p,l) { \
const_bstring bstrtmp_s = (b); \
if (bstrtmp_s && bstrtmp_s->data && bstrtmp_s->slen >= 0) { \
int bstrtmp_left = (p); \
int bstrtmp_len = (l); \
if (bstrtmp_left < 0) { \
bstrtmp_len += bstrtmp_left; \
bstrtmp_left = 0; \
} \
if (bstrtmp_len > bstrtmp_s->slen - bstrtmp_left) \
bstrtmp_len = bstrtmp_s->slen - bstrtmp_left; \
if (bstrtmp_len <= 0) { \
(t).data = (unsigned char *)""; \
(t).slen = 0; \
} else { \
(t).data = bstrtmp_s->data + bstrtmp_left; \
(t).slen = bstrtmp_len; \
} \
} else { \
(t).data = (unsigned char *)""; \
(t).slen = 0; \
} \
(t).mlen = -__LINE__; \
}
#define btfromblkltrimws(t,s,l) { \
int bstrtmp_idx = 0, bstrtmp_len = (l); \
unsigned char * bstrtmp_s = (s); \
if (bstrtmp_s && bstrtmp_len >= 0) { \
for (; bstrtmp_idx < bstrtmp_len; bstrtmp_idx++) { \
if (!isspace (bstrtmp_s[bstrtmp_idx])) break; \
} \
} \
(t).data = bstrtmp_s + bstrtmp_idx; \
(t).slen = bstrtmp_len - bstrtmp_idx; \
(t).mlen = -__LINE__; \
}
#define btfromblkrtrimws(t,s,l) { \
int bstrtmp_len = (l) - 1; \
unsigned char * bstrtmp_s = (s); \
if (bstrtmp_s && bstrtmp_len >= 0) { \
for (; bstrtmp_len >= 0; bstrtmp_len--) { \
if (!isspace (bstrtmp_s[bstrtmp_len])) break; \
} \
} \
(t).data = bstrtmp_s; \
(t).slen = bstrtmp_len + 1; \
(t).mlen = -__LINE__; \
}
#define btfromblktrimws(t,s,l) { \
int bstrtmp_idx = 0, bstrtmp_len = (l) - 1; \
unsigned char * bstrtmp_s = (s); \
if (bstrtmp_s && bstrtmp_len >= 0) { \
for (; bstrtmp_idx <= bstrtmp_len; bstrtmp_idx++) { \
if (!isspace (bstrtmp_s[bstrtmp_idx])) break; \
} \
for (; bstrtmp_len >= bstrtmp_idx; bstrtmp_len--) { \
if (!isspace (bstrtmp_s[bstrtmp_len])) break; \
} \
} \
(t).data = bstrtmp_s + bstrtmp_idx; \
(t).slen = bstrtmp_len + 1 - bstrtmp_idx; \
(t).mlen = -__LINE__; \
}
/* Write protection macros */
#define bwriteprotect(t) { if ((t).mlen >= 0) (t).mlen = -1; }
#define bwriteallow(t) { if ((t).mlen == -1) (t).mlen = (t).slen + ((t).slen == 0); }
#define biswriteprotected(t) ((t).mlen <= 0)
#ifdef __cplusplus
}
#endif
#endif
EOF
######################################################################
echo "generating src/bstrlib.c..."
cat >src/bstrlib.c <<EOF
/*
* This source file is part of the bstring string library. This code was
* written by Paul Hsieh in 2002-2010, and is covered by either the 3-clause
* BSD open source license or GPL v2.0. Refer to the accompanying documentation
* for details on usage and license.
*/
/*
* bstrlib.c
*
* This file is the core module for implementing the bstring functions.
*/
#if defined (_MSC_VER)
/* These warnings from MSVC++ are totally pointless. */
# define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "bstrlib.h"
/* Optionally include a mechanism for debugging memory */
#if defined(MEMORY_DEBUG) || defined(BSTRLIB_MEMORY_DEBUG)
#include "memdbg.h"
#endif
#ifndef bstr__alloc
#define bstr__alloc(x) malloc (x)
#endif
#ifndef bstr__free
#define bstr__free(p) free (p)
#endif
#ifndef bstr__realloc
#define bstr__realloc(p,x) realloc ((p), (x))
#endif
#ifndef bstr__memcpy
#define bstr__memcpy(d,s,l) memcpy ((d), (s), (l))
#endif
#ifndef bstr__memmove
#define bstr__memmove(d,s,l) memmove ((d), (s), (l))
#endif
#ifndef bstr__memset
#define bstr__memset(d,c,l) memset ((d), (c), (l))
#endif
#ifndef bstr__memcmp
#define bstr__memcmp(d,c,l) memcmp ((d), (c), (l))
#endif
#ifndef bstr__memchr
#define bstr__memchr(s,c,l) memchr ((s), (c), (l))
#endif
/* Just a length safe wrapper for memmove. */
#define bBlockCopy(D,S,L) { if ((L) > 0) bstr__memmove ((D),(S),(L)); }
/* Compute the snapped size for a given requested size. By snapping to powers
of 2 like this, repeated reallocations are avoided. */
static int snapUpSize (int i) {
if (i < 8) {
i = 8;
} else {
unsigned int j;
j = (unsigned int) i;
j |= (j >> 1);
j |= (j >> 2);
j |= (j >> 4);
j |= (j >> 8); /* Ok, since int >= 16 bits */
#if (UINT_MAX != 0xffff)
j |= (j >> 16); /* For 32 bit int systems */
#if (UINT_MAX > 0xffffffffUL)
j |= (j >> 32); /* For 64 bit int systems */
#endif
#endif
/* Least power of two greater than i */
j++;
if ((int) j >= i) i = (int) j;
}
return i;
}
/* int balloc (bstring b, int len)
*
* Increase the size of the memory backing the bstring b to at least len.
*/
int balloc (bstring b, int olen) {
int len;
if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen <= 0 ||
b->mlen < b->slen || olen <= 0) {
return BSTR_ERR;
}
if (olen >= b->mlen) {
unsigned char * x;
if ((len = snapUpSize (olen)) <= b->mlen) return BSTR_OK;
/* Assume probability of a non-moving realloc is 0.125 */
if (7 * b->mlen < 8 * b->slen) {
/* If slen is close to mlen in size then use realloc to reduce
the memory defragmentation */
reallocStrategy:;
x = (unsigned char *) bstr__realloc (b->data, (size_t) len);
if (x == NULL) {
/* Since we failed, try allocating the tighest possible
allocation */
if (NULL == (x = (unsigned char *) bstr__realloc (b->data, (size_t) (len = olen)))) {
return BSTR_ERR;
}
}
} else {
/* If slen is not close to mlen then avoid the penalty of copying
the extra bytes that are allocated, but not considered part of
the string */
if (NULL == (x = (unsigned char *) bstr__alloc ((size_t) len))) {
/* Perhaps there is no available memory for the two
allocations to be in memory at once */
goto reallocStrategy;
} else {
if (b->slen) bstr__memcpy ((char *) x, (char *) b->data, (size_t) b->slen);
bstr__free (b->data);
}
}
b->data = x;
b->mlen = len;
b->data[b->slen] = (unsigned char) '\0';
}
return BSTR_OK;
}
/* int ballocmin (bstring b, int len)
*
* Set the size of the memory backing the bstring b to len or b->slen+1,
* whichever is larger. Note that repeated use of this function can degrade
* performance.
*/
int ballocmin (bstring b, int len) {
unsigned char * s;
if (b == NULL || b->data == NULL || (b->slen+1) < 0 || b->mlen <= 0 ||
b->mlen < b->slen || len <= 0) {
return BSTR_ERR;
}
if (len < b->slen + 1) len = b->slen + 1;
if (len != b->mlen) {
s = (unsigned char *) bstr__realloc (b->data, (size_t) len);
if (NULL == s) return BSTR_ERR;
s[b->slen] = (unsigned char) '\0';
b->data = s;
b->mlen = len;
}
return BSTR_OK;
}
/* bstring bfromcstr (const char * str)
*
* Create a bstring which contains the contents of the '\0' terminated char *
* buffer str.
*/
bstring bfromcstr (const char * str) {
bstring b;
int i;
size_t j;
if (str == NULL) return NULL;
j = (strlen) (str);
i = snapUpSize ((int) (j + (2 - (j != 0))));
if (i <= (int) j) return NULL;
b = (bstring) bstr__alloc (sizeof (struct tagbstring));
if (NULL == b) return NULL;
b->slen = (int) j;
if (NULL == (b->data = (unsigned char *) bstr__alloc (b->mlen = i))) {
bstr__free (b);
return NULL;
}
bstr__memcpy (b->data, str, j+1);
return b;
}
/* bstring bfromcstralloc (int mlen, const char * str)
*
* Create a bstring which contains the contents of the '\0' terminated char *
* buffer str. The memory buffer backing the string is at least len
* characters in length.
*/
bstring bfromcstralloc (int mlen, const char * str) {
bstring b;
int i;
size_t j;
if (str == NULL) return NULL;
j = (strlen) (str);
i = snapUpSize ((int) (j + (2 - (j != 0))));
if (i <= (int) j) return NULL;
b = (bstring) bstr__alloc (sizeof (struct tagbstring));
if (b == NULL) return NULL;
b->slen = (int) j;
if (i < mlen) i = mlen;
if (NULL == (b->data = (unsigned char *) bstr__alloc (b->mlen = i))) {
bstr__free (b);
return NULL;
}
bstr__memcpy (b->data, str, j+1);
return b;
}
/* bstring blk2bstr (const void * blk, int len)
*
* Create a bstring which contains the content of the block blk of length
* len.
*/
bstring blk2bstr (const void * blk, int len) {
bstring b;
int i;
if (blk == NULL || len < 0) return NULL;
b = (bstring) bstr__alloc (sizeof (struct tagbstring));
if (b == NULL) return NULL;
b->slen = len;
i = len + (2 - (len != 0));
i = snapUpSize (i);
b->mlen = i;
b->data = (unsigned char *) bstr__alloc ((size_t) b->mlen);
if (b->data == NULL) {
bstr__free (b);
return NULL;
}
if (len > 0) bstr__memcpy (b->data, blk, (size_t) len);
b->data[len] = (unsigned char) '\0';
return b;
}
/* char * bstr2cstr (const_bstring s, char z)
*
* Create a '\0' terminated char * buffer which is equal to the contents of
* the bstring s, except that any contained '\0' characters are converted
* to the character in z. This returned value should be freed with a
* bcstrfree () call, by the calling application.
*/
char * bstr2cstr (const_bstring b, char z) {
int i, l;
char * r;
if (b == NULL || b->slen < 0 || b->data == NULL) return NULL;
l = b->slen;
r = (char *) bstr__alloc ((size_t) (l + 1));
if (r == NULL) return r;
for (i=0; i < l; i ++) {
r[i] = (char) ((b->data[i] == '\0') ? z : (char) (b->data[i]));
}
r[l] = (unsigned char) '\0';
return r;
}
/* int bcstrfree (char * s)
*
* Frees a C-string generated by bstr2cstr (). This is normally unnecessary
* since it just wraps a call to bstr__free (), however, if bstr__alloc ()
* and bstr__free () have been redefined as a macros within the bstrlib
* module (via defining them in memdbg.h after defining
* BSTRLIB_MEMORY_DEBUG) with some difference in behaviour from the std
* library functions, then this allows a correct way of freeing the memory
* that allows higher level code to be independent from these macro
* redefinitions.
*/
int bcstrfree (char * s) {
if (s) {
bstr__free (s);
return BSTR_OK;
}
return BSTR_ERR;
}
/* int bconcat (bstring b0, const_bstring b1)
*
* Concatenate the bstring b1 to the bstring b0.
*/
int bconcat (bstring b0, const_bstring b1) {
int len, d;
bstring aux = (bstring) b1;
if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL) return BSTR_ERR;
d = b0->slen;
len = b1->slen;
if ((d | (b0->mlen - d) | len | (d + len)) < 0) return BSTR_ERR;
if (b0->mlen <= d + len + 1) {
ptrdiff_t pd = b1->data - b0->data;
if (0 <= pd && pd < b0->mlen) {
if (NULL == (aux = bstrcpy (b1))) return BSTR_ERR;
}
if (balloc (b0, d + len + 1) != BSTR_OK) {
if (aux != b1) bdestroy (aux);
return BSTR_ERR;
}
}
bBlockCopy (&b0->data[d], &aux->data[0], (size_t) len);
b0->data[d + len] = (unsigned char) '\0';
b0->slen = d + len;
if (aux != b1) bdestroy (aux);
return BSTR_OK;
}
/* int bconchar (bstring b, char c)
/ *
* Concatenate the single character c to the bstring b.
*/
int bconchar (bstring b, char c) {
int d;
if (b == NULL) return BSTR_ERR;
d = b->slen;
if ((d | (b->mlen - d)) < 0 || balloc (b, d + 2) != BSTR_OK) return BSTR_ERR;
b->data[d] = (unsigned char) c;
b->data[d + 1] = (unsigned char) '\0';
b->slen++;
return BSTR_OK;
}
/* int bcatcstr (bstring b, const char * s)
*
* Concatenate a char * string to a bstring.
*/
int bcatcstr (bstring b, const char * s) {
char * d;
int i, l;
if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen < b->slen
|| b->mlen <= 0 || s == NULL) return BSTR_ERR;
/* Optimistically concatenate directly */
l = b->mlen - b->slen;
d = (char *) &b->data[b->slen];
for (i=0; i < l; i++) {
if ((*d++ = *s++) == '\0') {
b->slen += i;
return BSTR_OK;
}
}
b->slen += i;
/* Need to explicitely resize and concatenate tail */
return bcatblk (b, (const void *) s, (int) strlen (s));
}
/* int bcatblk (bstring b, const void * s, int len)
*
* Concatenate a fixed length buffer to a bstring.
*/
int bcatblk (bstring b, const void * s, int len) {
int nl;
if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen < b->slen
|| b->mlen <= 0 || s == NULL || len < 0) return BSTR_ERR;
if (0 > (nl = b->slen + len)) return BSTR_ERR; /* Overflow? */
if (b->mlen <= nl && 0 > balloc (b, nl + 1)) return BSTR_ERR;
bBlockCopy (&b->data[b->slen], s, (size_t) len);
b->slen = nl;
b->data[nl] = (unsigned char) '\0';
return BSTR_OK;
}
/* bstring bstrcpy (const_bstring b)
*
* Create a copy of the bstring b.
*/
bstring bstrcpy (const_bstring b) {
bstring b0;
int i,j;
/* Attempted to copy an invalid string? */
if (b == NULL || b->slen < 0 || b->data == NULL) return NULL;
b0 = (bstring) bstr__alloc (sizeof (struct tagbstring));
if (b0 == NULL) {
/* Unable to allocate memory for string header */
return NULL;
}
i = b->slen;
j = snapUpSize (i + 1);
b0->data = (unsigned char *) bstr__alloc (j);
if (b0->data == NULL) {
j = i + 1;
b0->data = (unsigned char *) bstr__alloc (j);
if (b0->data == NULL) {
/* Unable to allocate memory for string data */
bstr__free (b0);
return NULL;
}
}
b0->mlen = j;
b0->slen = i;
if (i) bstr__memcpy ((char *) b0->data, (char *) b->data, i);
b0->data[b0->slen] = (unsigned char) '\0';
return b0;
}
/* int bassign (bstring a, const_bstring b)
*
* Overwrite the string a with the contents of string b.
*/
int bassign (bstring a, const_bstring b) {
if (b == NULL || b->data == NULL || b->slen < 0)
return BSTR_ERR;
if (b->slen != 0) {
if (balloc (a, b->slen) != BSTR_OK) return BSTR_ERR;
bstr__memmove (a->data, b->data, b->slen);
} else {
if (a == NULL || a->data == NULL || a->mlen < a->slen ||
a->slen < 0 || a->mlen == 0)
return BSTR_ERR;
}
a->data[b->slen] = (unsigned char) '\0';
a->slen = b->slen;
return BSTR_OK;
}
/* int bassignmidstr (bstring a, const_bstring b, int left, int len)
*
* Overwrite the string a with the middle of contents of string b
* starting from position left and running for a length len. left and
* len are clamped to the ends of b as with the function bmidstr.
*/
int bassignmidstr (bstring a, const_bstring b, int left, int len) {
if (b == NULL || b->data == NULL || b->slen < 0)
return BSTR_ERR;
if (left < 0) {
len += left;
left = 0;
}
if (len > b->slen - left) len = b->slen - left;
if (a == NULL || a->data == NULL || a->mlen < a->slen ||
a->slen < 0 || a->mlen == 0)
return BSTR_ERR;
if (len > 0) {
if (balloc (a, len) != BSTR_OK) return BSTR_ERR;
bstr__memmove (a->data, b->data + left, len);
a->slen = len;
} else {
a->slen = 0;
}
a->data[a->slen] = (unsigned char) '\0';
return BSTR_OK;
}
/* int bassigncstr (bstring a, const char * str)
*
* Overwrite the string a with the contents of char * string str. Note that
* the bstring a must be a well defined and writable bstring. If an error
* occurs BSTR_ERR is returned however a may be partially overwritten.
*/
int bassigncstr (bstring a, const char * str) {
int i;
size_t len;
if (a == NULL || a->data == NULL || a->mlen < a->slen ||
a->slen < 0 || a->mlen == 0 || NULL == str)
return BSTR_ERR;
for (i=0; i < a->mlen; i++) {
if ('\0' == (a->data[i] = str[i])) {
a->slen = i;
return BSTR_OK;
}
}
a->slen = i;
len = strlen (str + i);
if (len > INT_MAX || i + len + 1 > INT_MAX ||
0 > balloc (a, (int) (i + len + 1))) return BSTR_ERR;
bBlockCopy (a->data + i, str + i, (size_t) len + 1);
a->slen += (int) len;
return BSTR_OK;
}
/* int bassignblk (bstring a, const void * s, int len)
*
* Overwrite the string a with the contents of the block (s, len). Note that
* the bstring a must be a well defined and writable bstring. If an error
* occurs BSTR_ERR is returned and a is not overwritten.
*/
int bassignblk (bstring a, const void * s, int len) {
if (a == NULL || a->data == NULL || a->mlen < a->slen ||
a->slen < 0 || a->mlen == 0 || NULL == s || len + 1 < 1)
return BSTR_ERR;
if (len + 1 > a->mlen && 0 > balloc (a, len + 1)) return BSTR_ERR;
bBlockCopy (a->data, s, (size_t) len);
a->data[len] = (unsigned char) '\0';
a->slen = len;
return BSTR_OK;
}
/* int btrunc (bstring b, int n)
*
* Truncate the bstring to at most n characters.
*/
int btrunc (bstring b, int n) {
if (n < 0 || b == NULL || b->data == NULL || b->mlen < b->slen ||
b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
if (b->slen > n) {
b->slen = n;
b->data[n] = (unsigned char) '\0';
}
return BSTR_OK;
}
#define upcase(c) (toupper ((unsigned char) c))
#define downcase(c) (tolower ((unsigned char) c))
#define wspace(c) (isspace ((unsigned char) c))
/* int btoupper (bstring b)
*
* Convert contents of bstring to upper case.
*/
int btoupper (bstring b) {
int i, len;
if (b == NULL || b->data == NULL || b->mlen < b->slen ||
b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
for (i=0, len = b->slen; i < len; i++) {
b->data[i] = (unsigned char) upcase (b->data[i]);
}
return BSTR_OK;
}
/* int btolower (bstring b)
*
* Convert contents of bstring to lower case.
*/
int btolower (bstring b) {
int i, len;
if (b == NULL || b->data == NULL || b->mlen < b->slen ||
b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
for (i=0, len = b->slen; i < len; i++) {
b->data[i] = (unsigned char) downcase (b->data[i]);
}
return BSTR_OK;
}
/* int bstricmp (const_bstring b0, const_bstring b1)
*
* Compare two strings without differentiating between case. The return
* value is the difference of the values of the characters where the two
* strings first differ after lower case transformation, otherwise 0 is
* returned indicating that the strings are equal. If the lengths are
* different, then a difference from 0 is given, but if the first extra
* character is '\0', then it is taken to be the value UCHAR_MAX+1.
*/
int bstricmp (const_bstring b0, const_bstring b1) {
int i, v, n;
if (bdata (b0) == NULL || b0->slen < 0 ||
bdata (b1) == NULL || b1->slen < 0) return SHRT_MIN;
if ((n = b0->slen) > b1->slen) n = b1->slen;
else if (b0->slen == b1->slen && b0->data == b1->data) return BSTR_OK;
for (i = 0; i < n; i ++) {
v = (char) downcase (b0->data[i])
- (char) downcase (b1->data[i]);
if (0 != v) return v;
}
if (b0->slen > n) {
v = (char) downcase (b0->data[n]);
if (v) return v;
return UCHAR_MAX + 1;
}
if (b1->slen > n) {
v = - (char) downcase (b1->data[n]);
if (v) return v;
return - (int) (UCHAR_MAX + 1);
}
return BSTR_OK;
}
/* int bstrnicmp (const_bstring b0, const_bstring b1, int n)
*
* Compare two strings without differentiating between case for at most n
* characters. If the position where the two strings first differ is
* before the nth position, the return value is the difference of the values
* of the characters, otherwise 0 is returned. If the lengths are different
* and less than n characters, then a difference from 0 is given, but if the
* first extra character is '\0', then it is taken to be the value
* UCHAR_MAX+1.
*/
int bstrnicmp (const_bstring b0, const_bstring b1, int n) {
int i, v, m;
if (bdata (b0) == NULL || b0->slen < 0 ||
bdata (b1) == NULL || b1->slen < 0 || n < 0) return SHRT_MIN;
m = n;
if (m > b0->slen) m = b0->slen;
if (m > b1->slen) m = b1->slen;
if (b0->data != b1->data) {
for (i = 0; i < m; i ++) {
v = (char) downcase (b0->data[i]);
v -= (char) downcase (b1->data[i]);
if (v != 0) return b0->data[i] - b1->data[i];
}
}
if (n == m || b0->slen == b1->slen) return BSTR_OK;
if (b0->slen > m) {
v = (char) downcase (b0->data[m]);
if (v) return v;
return UCHAR_MAX + 1;
}
v = - (char) downcase (b1->data[m]);
if (v) return v;
return - (int) (UCHAR_MAX + 1);
}
/* int biseqcaseless (const_bstring b0, const_bstring b1)
*
* Compare two strings for equality without differentiating between case.
* If the strings differ other than in case, 0 is returned, if the strings
* are the same, 1 is returned, if there is an error, -1 is returned. If
* the length of the strings are different, this function is O(1). '\0'
* termination characters are not treated in any special way.
*/
int biseqcaseless (const_bstring b0, const_bstring b1) {
int i, n;
if (bdata (b0) == NULL || b0->slen < 0 ||
bdata (b1) == NULL || b1->slen < 0) return BSTR_ERR;
if (b0->slen != b1->slen) return BSTR_OK;
if (b0->data == b1->data || b0->slen == 0) return 1;
for (i=0, n=b0->slen; i < n; i++) {
if (b0->data[i] != b1->data[i]) {
unsigned char c = (unsigned char) downcase (b0->data[i]);
if (c != (unsigned char) downcase (b1->data[i])) return 0;
}
}
return 1;
}
/* int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len)
*
* Compare beginning of string b0 with a block of memory of length len
* without differentiating between case for equality. If the beginning of b0
* differs from the memory block other than in case (or if b0 is too short),
* 0 is returned, if the strings are the same, 1 is returned, if there is an
* error, -1 is returned. '\0' characters are not treated in any special
* way.
*/
int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len) {
int i;
if (bdata (b0) == NULL || b0->slen < 0 || NULL == blk || len < 0)
return BSTR_ERR;
if (b0->slen < len) return BSTR_OK;
if (b0->data == (const unsigned char *) blk || len == 0) return 1;
for (i = 0; i < len; i ++) {
if (b0->data[i] != ((const unsigned char *) blk)[i]) {
if (downcase (b0->data[i]) !=
downcase (((const unsigned char *) blk)[i])) return 0;
}
}
return 1;
}
/*
* int bltrimws (bstring b)
*
* Delete whitespace contiguous from the left end of the string.
*/
int bltrimws (bstring b) {
int i, len;
if (b == NULL || b->data == NULL || b->mlen < b->slen ||
b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
for (len = b->slen, i = 0; i < len; i++) {
if (!wspace (b->data[i])) {
return bdelete (b, 0, i);
}
}
b->data[0] = (unsigned char) '\0';
b->slen = 0;
return BSTR_OK;
}
/*
* int brtrimws (bstring b)
*
* Delete whitespace contiguous from the right end of the string.
*/
int brtrimws (bstring b) {
int i;
if (b == NULL || b->data == NULL || b->mlen < b->slen ||
b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
for (i = b->slen - 1; i >= 0; i--) {
if (!wspace (b->data[i])) {
if (b->mlen > i) b->data[i+1] = (unsigned char) '\0';
b->slen = i + 1;
return BSTR_OK;
}
}
b->data[0] = (unsigned char) '\0';
b->slen = 0;
return BSTR_OK;
}
/*
* int btrimws (bstring b)
*
* Delete whitespace contiguous from both ends of the string.
*/
int btrimws (bstring b) {
int i, j;
if (b == NULL || b->data == NULL || b->mlen < b->slen ||
b->slen < 0 || b->mlen <= 0) return BSTR_ERR;
for (i = b->slen - 1; i >= 0; i--) {
if (!wspace (b->data[i])) {
if (b->mlen > i) b->data[i+1] = (unsigned char) '\0';
b->slen = i + 1;
for (j = 0; wspace (b->data[j]); j++) {}
return bdelete (b, 0, j);
}
}
b->data[0] = (unsigned char) '\0';
b->slen = 0;
return BSTR_OK;
}
/* int biseq (const_bstring b0, const_bstring b1)
*
* Compare the string b0 and b1. If the strings differ, 0 is returned, if
* the strings are the same, 1 is returned, if there is an error, -1 is
* returned. If the length of the strings are different, this function is
* O(1). '\0' termination characters are not treated in any special way.
*/
int biseq (const_bstring b0, const_bstring b1) {
if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL ||
b0->slen < 0 || b1->slen < 0) return BSTR_ERR;
if (b0->slen != b1->slen) return BSTR_OK;
if (b0->data == b1->data || b0->slen == 0) return 1;
return !bstr__memcmp (b0->data, b1->data, b0->slen);
}
/* int bisstemeqblk (const_bstring b0, const void * blk, int len)
*
* Compare beginning of string b0 with a block of memory of length len for
* equality. If the beginning of b0 differs from the memory block (or if b0
* is too short), 0 is returned, if the strings are the same, 1 is returned,
* if there is an error, -1 is returned. '\0' characters are not treated in
* any special way.
*/
int bisstemeqblk (const_bstring b0, const void * blk, int len) {
int i;
if (bdata (b0) == NULL || b0->slen < 0 || NULL == blk || len < 0)
return BSTR_ERR;
if (b0->slen < len) return BSTR_OK;
if (b0->data == (const unsigned char *) blk || len == 0) return 1;
for (i = 0; i < len; i ++) {
if (b0->data[i] != ((const unsigned char *) blk)[i]) return BSTR_OK;
}
return 1;
}
/* int biseqcstr (const_bstring b, const char *s)
*
* Compare the bstring b and char * string s. The C string s must be '\0'
* terminated at exactly the length of the bstring b, and the contents
* between the two must be identical with the bstring b with no '\0'
* characters for the two contents to be considered equal. This is
* equivalent to the condition that their current contents will be always be
* equal when comparing them in the same format after converting one or the
* other. If the strings are equal 1 is returned, if they are unequal 0 is
* returned and if there is a detectable error BSTR_ERR is returned.
*/
int biseqcstr (const_bstring b, const char * s) {
int i;
if (b == NULL || s == NULL || b->data == NULL || b->slen < 0) return BSTR_ERR;
for (i=0; i < b->slen; i++) {
if (s[i] == '\0' || b->data[i] != (unsigned char) s[i]) return BSTR_OK;
}
return s[i] == '\0';
}
/* int biseqcstrcaseless (const_bstring b, const char *s)
*
* Compare the bstring b and char * string s. The C string s must be '\0'
* terminated at exactly the length of the bstring b, and the contents
* between the two must be identical except for case with the bstring b with
* no '\0' characters for the two contents to be considered equal. This is
* equivalent to the condition that their current contents will be always be
* equal ignoring case when comparing them in the same format after
* converting one or the other. If the strings are equal, except for case,
* 1 is returned, if they are unequal regardless of case 0 is returned and
* if there is a detectable error BSTR_ERR is returned.
*/
int biseqcstrcaseless (const_bstring b, const char * s) {
int i;
if (b == NULL || s == NULL || b->data == NULL || b->slen < 0) return BSTR_ERR;
for (i=0; i < b->slen; i++) {
if (s[i] == '\0' ||
(b->data[i] != (unsigned char) s[i] &&
downcase (b->data[i]) != (unsigned char) downcase (s[i])))
return BSTR_OK;
}
return s[i] == '\0';
}
/* int bstrcmp (const_bstring b0, const_bstring b1)
*
* Compare the string b0 and b1. If there is an error, SHRT_MIN is returned,
* otherwise a value less than or greater than zero, indicating that the
* string pointed to by b0 is lexicographically less than or greater than
* the string pointed to by b1 is returned. If the the string lengths are
* unequal but the characters up until the length of the shorter are equal
* then a value less than, or greater than zero, indicating that the string
* pointed to by b0 is shorter or longer than the string pointed to by b1 is
* returned. 0 is returned if and only if the two strings are the same. If
* the length of the strings are different, this function is O(n). Like its
* standard C library counter part strcmp, the comparison does not proceed
* past any '\0' termination characters encountered.
*/
int bstrcmp (const_bstring b0, const_bstring b1) {
int i, v, n;
if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL ||
b0->slen < 0 || b1->slen < 0) return SHRT_MIN;
n = b0->slen; if (n > b1->slen) n = b1->slen;
if (b0->slen == b1->slen && (b0->data == b1->data || b0->slen == 0))
return BSTR_OK;
for (i = 0; i < n; i ++) {
v = ((char) b0->data[i]) - ((char) b1->data[i]);
if (v != 0) return v;
if (b0->data[i] == (unsigned char) '\0') return BSTR_OK;
}
if (b0->slen > n) return 1;
if (b1->slen > n) return -1;
return BSTR_OK;
}
/* int bstrncmp (const_bstring b0, const_bstring b1, int n)
*
* Compare the string b0 and b1 for at most n characters. If there is an
* error, SHRT_MIN is returned, otherwise a value is returned as if b0 and
* b1 were first truncated to at most n characters then bstrcmp was called
* with these new strings are paremeters. If the length of the strings are
* different, this function is O(n). Like its standard C library counter
* part strcmp, the comparison does not proceed past any '\0' termination
* characters encountered.
*/
int bstrncmp (const_bstring b0, const_bstring b1, int n) {
int i, v, m;
if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL ||
b0->slen < 0 || b1->slen < 0) return SHRT_MIN;
m = n;
if (m > b0->slen) m = b0->slen;
if (m > b1->slen) m = b1->slen;
if (b0->data != b1->data) {
for (i = 0; i < m; i ++) {
v = ((char) b0->data[i]) - ((char) b1->data[i]);
if (v != 0) return v;
if (b0->data[i] == (unsigned char) '\0') return BSTR_OK;
}
}
if (n == m || b0->slen == b1->slen) return BSTR_OK;
if (b0->slen > m) return 1;
return -1;
}
/* bstring bmidstr (const_bstring b, int left, int len)
*
* Create a bstring which is the substring of b starting from position left
* and running for a length len (clamped by the end of the bstring b.) If
* b is detectably invalid, then NULL is returned. The section described
* by (left, len) is clamped to the boundaries of b.
*/
bstring bmidstr (const_bstring b, int left, int len) {
if (b == NULL || b->slen < 0 || b->data == NULL) return NULL;
if (left < 0) {
len += left;
left = 0;
}
if (len > b->slen - left) len = b->slen - left;
if (len <= 0) return bfromcstr ("");
return blk2bstr (b->data + left, len);
}
/* int bdelete (bstring b, int pos, int len)
*
* Removes characters from pos to pos+len-1 inclusive and shifts the tail of
* the bstring starting from pos+len to pos. len must be positive for this
* call to have any effect. The section of the string described by (pos,
* len) is clamped to boundaries of the bstring b.
*/
int bdelete (bstring b, int pos, int len) {
/* Clamp to left side of bstring */
if (pos < 0) {
len += pos;
pos = 0;
}
if (len < 0 || b == NULL || b->data == NULL || b->slen < 0 ||
b->mlen < b->slen || b->mlen <= 0)
return BSTR_ERR;
if (len > 0 && pos < b->slen) {
if (pos + len >= b->slen) {
b->slen = pos;
} else {
bBlockCopy ((char *) (b->data + pos),
(char *) (b->data + pos + len),
b->slen - (pos+len));
b->slen -= len;
}
b->data[b->slen] = (unsigned char) '\0';
}
return BSTR_OK;
}
/* int bdestroy (bstring b)
*
* Free up the bstring. Note that if b is detectably invalid or not writable
* then no action is performed and BSTR_ERR is returned. Like a freed memory
* allocation, dereferences, writes or any other action on b after it has
* been bdestroyed is undefined.
*/
int bdestroy (bstring b) {
if (b == NULL || b->slen < 0 || b->mlen <= 0 || b->mlen < b->slen ||
b->data == NULL)
return BSTR_ERR;
bstr__free (b->data);
/* In case there is any stale usage, there is one more chance to
notice this error. */
b->slen = -1;
b->mlen = -__LINE__;
b->data = NULL;
bstr__free (b);
return BSTR_OK;
}
/* int binstr (const_bstring b1, int pos, const_bstring b2)
*
* Search for the bstring b2 in b1 starting from position pos, and searching
* forward. If it is found then return with the first position where it is
* found, otherwise return BSTR_ERR. Note that this is just a brute force
* string searcher that does not attempt clever things like the Boyer-Moore
* search algorithm. Because of this there are many degenerate cases where
* this can take much longer than it needs to.
*/
int binstr (const_bstring b1, int pos, const_bstring b2) {
int j, ii, ll, lf;
unsigned char * d0;
unsigned char c0;
register unsigned char * d1;
register unsigned char c1;
register int i;
if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||
b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;
if (b1->slen == pos) return (b2->slen == 0)?pos:BSTR_ERR;
if (b1->slen < pos || pos < 0) return BSTR_ERR;
if (b2->slen == 0) return pos;
/* No space to find such a string? */
if ((lf = b1->slen - b2->slen + 1) <= pos) return BSTR_ERR;
/* An obvious alias case */
if (b1->data == b2->data && pos == 0) return 0;
i = pos;
d0 = b2->data;
d1 = b1->data;
ll = b2->slen;
/* Peel off the b2->slen == 1 case */
c0 = d0[0];
if (1 == ll) {
for (;i < lf; i++) if (c0 == d1[i]) return i;
return BSTR_ERR;
}
c1 = c0;
j = 0;
lf = b1->slen - 1;
ii = -1;
if (i < lf) do {
/* Unrolled current character test */
if (c1 != d1[i]) {
if (c1 != d1[1+i]) {
i += 2;
continue;
}
i++;
}
/* Take note if this is the start of a potential match */
if (0 == j) ii = i;
/* Shift the test character down by one */
j++;
i++;
/* If this isn't past the last character continue */
if (j < ll) {
c1 = d0[j];
continue;
}
N0:;
/* If no characters mismatched, then we matched */
if (i == ii+j) return ii;
/* Shift back to the beginning */
i -= j;
j = 0;
c1 = c0;
} while (i < lf);
/* Deal with last case if unrolling caused a misalignment */
if (i == lf && ll == j+1 && c1 == d1[i]) goto N0;
return BSTR_ERR;
}
/* int binstrr (const_bstring b1, int pos, const_bstring b2)
*
* Search for the bstring b2 in b1 starting from position pos, and searching
* backward. If it is found then return with the first position where it is
* found, otherwise return BSTR_ERR. Note that this is just a brute force
* string searcher that does not attempt clever things like the Boyer-Moore
* search algorithm. Because of this there are many degenerate cases where
* this can take much longer than it needs to.
*/
int binstrr (const_bstring b1, int pos, const_bstring b2) {
int j, i, l;
unsigned char * d0, * d1;
if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||
b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;
if (b1->slen == pos && b2->slen == 0) return pos;
if (b1->slen < pos || pos < 0) return BSTR_ERR;
if (b2->slen == 0) return pos;
/* Obvious alias case */
if (b1->data == b2->data && pos == 0 && b2->slen <= b1->slen) return 0;
i = pos;
if ((l = b1->slen - b2->slen) < 0) return BSTR_ERR;
/* If no space to find such a string then snap back */
if (l + 1 <= i) i = l;
j = 0;
d0 = b2->data;
d1 = b1->data;
l = b2->slen;
for (;;) {
if (d0[j] == d1[i + j]) {
j ++;
if (j >= l) return i;
} else {
i --;
if (i < 0) break;
j=0;
}
}
return BSTR_ERR;
}
/* int binstrcaseless (const_bstring b1, int pos, const_bstring b2)
*
* Search for the bstring b2 in b1 starting from position pos, and searching
* forward but without regard to case. If it is found then return with the
* first position where it is found, otherwise return BSTR_ERR. Note that
* this is just a brute force string searcher that does not attempt clever
* things like the Boyer-Moore search algorithm. Because of this there are
* many degenerate cases where this can take much longer than it needs to.
*/
int binstrcaseless (const_bstring b1, int pos, const_bstring b2) {
int j, i, l, ll;
unsigned char * d0, * d1;
if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||
b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;
if (b1->slen == pos) return (b2->slen == 0)?pos:BSTR_ERR;
if (b1->slen < pos || pos < 0) return BSTR_ERR;
if (b2->slen == 0) return pos;
l = b1->slen - b2->slen + 1;
/* No space to find such a string? */
if (l <= pos) return BSTR_ERR;
/* An obvious alias case */
if (b1->data == b2->data && pos == 0) return BSTR_OK;
i = pos;
j = 0;
d0 = b2->data;
d1 = b1->data;
ll = b2->slen;
for (;;) {
if (d0[j] == d1[i + j] || downcase (d0[j]) == downcase (d1[i + j])) {
j ++;
if (j >= ll) return i;
} else {
i ++;
if (i >= l) break;
j=0;
}
}
return BSTR_ERR;
}
/* int binstrrcaseless (const_bstring b1, int pos, const_bstring b2)
*
* Search for the bstring b2 in b1 starting from position pos, and searching
* backward but without regard to case. If it is found then return with the
* first position where it is found, otherwise return BSTR_ERR. Note that
* this is just a brute force string searcher that does not attempt clever
* things like the Boyer-Moore search algorithm. Because of this there are
* many degenerate cases where this can take much longer than it needs to.
*/
int binstrrcaseless (const_bstring b1, int pos, const_bstring b2) {
int j, i, l;
unsigned char * d0, * d1;
if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||
b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;
if (b1->slen == pos && b2->slen == 0) return pos;
if (b1->slen < pos || pos < 0) return BSTR_ERR;
if (b2->slen == 0) return pos;
/* Obvious alias case */
if (b1->data == b2->data && pos == 0 && b2->slen <= b1->slen) return BSTR_OK;
i = pos;
if ((l = b1->slen - b2->slen) < 0) return BSTR_ERR;
/* If no space to find such a string then snap back */
if (l + 1 <= i) i = l;
j = 0;
d0 = b2->data;
d1 = b1->data;
l = b2->slen;
for (;;) {
if (d0[j] == d1[i + j] || downcase (d0[j]) == downcase (d1[i + j])) {
j ++;
if (j >= l) return i;
} else {
i --;
if (i < 0) break;
j=0;
}
}
return BSTR_ERR;
}
/* int bstrchrp (const_bstring b, int c, int pos)
*
* Search for the character c in b forwards from the position pos
* (inclusive).
*/
int bstrchrp (const_bstring b, int c, int pos) {
unsigned char * p;
if (b == NULL || b->data == NULL || b->slen <= pos || pos < 0) return BSTR_ERR;
p = (unsigned char *) bstr__memchr ((b->data + pos), (unsigned char) c, (b->slen - pos));
if (p) return (int) (p - b->data);
return BSTR_ERR;
}
/* int bstrrchrp (const_bstring b, int c, int pos)
*
* Search for the character c in b backwards from the position pos in string
* (inclusive).
*/
int bstrrchrp (const_bstring b, int c, int pos) {
int i;
if (b == NULL || b->data == NULL || b->slen <= pos || pos < 0) return BSTR_ERR;
for (i=pos; i >= 0; i--) {
if (b->data[i] == (unsigned char) c) return i;
}
return BSTR_ERR;
}
#if !defined (BSTRLIB_AGGRESSIVE_MEMORY_FOR_SPEED_TRADEOFF)
#define LONG_LOG_BITS_QTY (3)
#define LONG_BITS_QTY (1 << LONG_LOG_BITS_QTY)
#define LONG_TYPE unsigned char
#define CFCLEN ((1 << CHAR_BIT) / LONG_BITS_QTY)
struct charField { LONG_TYPE content[CFCLEN]; };
#define testInCharField(cf,c) ((cf)->content[(c) >> LONG_LOG_BITS_QTY] & (((long)1) << ((c) & (LONG_BITS_QTY-1))))
#define setInCharField(cf,idx) { \
unsigned int c = (unsigned int) (idx); \
(cf)->content[c >> LONG_LOG_BITS_QTY] |= (LONG_TYPE) (1ul << (c & (LONG_BITS_QTY-1))); \
}
#else
#define CFCLEN (1 << CHAR_BIT)
struct charField { unsigned char content[CFCLEN]; };
#define testInCharField(cf,c) ((cf)->content[(unsigned char) (c)])
#define setInCharField(cf,idx) (cf)->content[(unsigned int) (idx)] = ~0
#endif
/* Convert a bstring to charField */
static int buildCharField (struct charField * cf, const_bstring b) {
int i;
if (b == NULL || b->data == NULL || b->slen <= 0) return BSTR_ERR;
memset ((void *) cf->content, 0, sizeof (struct charField));
for (i=0; i < b->slen; i++) {
setInCharField (cf, b->data[i]);
}
return BSTR_OK;
}
static void invertCharField (struct charField * cf) {
int i;
for (i=0; i < CFCLEN; i++) cf->content[i] = ~cf->content[i];
}
/* Inner engine for binchr */
static int binchrCF (const unsigned char * data, int len, int pos, const struct charField * cf) {
int i;
for (i=pos; i < len; i++) {
unsigned char c = (unsigned char) data[i];
if (testInCharField (cf, c)) return i;
}
return BSTR_ERR;
}
/* int binchr (const_bstring b0, int pos, const_bstring b1);
*
* Search for the first position in b0 starting from pos or after, in which
* one of the characters in b1 is found and return it. If such a position
* does not exist in b0, then BSTR_ERR is returned.
*/
int binchr (const_bstring b0, int pos, const_bstring b1) {
struct charField chrs;
if (pos < 0 || b0 == NULL || b0->data == NULL ||
b0->slen <= pos) return BSTR_ERR;
if (1 == b1->slen) return bstrchrp (b0, b1->data[0], pos);
if (0 > buildCharField (&chrs, b1)) return BSTR_ERR;
return binchrCF (b0->data, b0->slen, pos, &chrs);
}
/* Inner engine for binchrr */
static int binchrrCF (const unsigned char * data, int pos, const struct charField * cf) {
int i;
for (i=pos; i >= 0; i--) {
unsigned int c = (unsigned int) data[i];
if (testInCharField (cf, c)) return i;
}
return BSTR_ERR;
}
/* int binchrr (const_bstring b0, int pos, const_bstring b1);
*
* Search for the last position in b0 no greater than pos, in which one of
* the characters in b1 is found and return it. If such a position does not
* exist in b0, then BSTR_ERR is returned.
*/
int binchrr (const_bstring b0, int pos, const_bstring b1) {
struct charField chrs;
if (pos < 0 || b0 == NULL || b0->data == NULL || b1 == NULL ||
b0->slen < pos) return BSTR_ERR;
if (pos == b0->slen) pos--;
if (1 == b1->slen) return bstrrchrp (b0, b1->data[0], pos);
if (0 > buildCharField (&chrs, b1)) return BSTR_ERR;
return binchrrCF (b0->data, pos, &chrs);
}
/* int bninchr (const_bstring b0, int pos, const_bstring b1);
*
* Search for the first position in b0 starting from pos or after, in which
* none of the characters in b1 is found and return it. If such a position
* does not exist in b0, then BSTR_ERR is returned.
*/
int bninchr (const_bstring b0, int pos, const_bstring b1) {
struct charField chrs;
if (pos < 0 || b0 == NULL || b0->data == NULL ||
b0->slen <= pos) return BSTR_ERR;
if (buildCharField (&chrs, b1) < 0) return BSTR_ERR;
invertCharField (&chrs);
return binchrCF (b0->data, b0->slen, pos, &chrs);
}
/* int bninchrr (const_bstring b0, int pos, const_bstring b1);
*
* Search for the last position in b0 no greater than pos, in which none of
* the characters in b1 is found and return it. If such a position does not
* exist in b0, then BSTR_ERR is returned.
*/
int bninchrr (const_bstring b0, int pos, const_bstring b1) {
struct charField chrs;
if (pos < 0 || b0 == NULL || b0->data == NULL ||
b0->slen < pos) return BSTR_ERR;
if (pos == b0->slen) pos--;
if (buildCharField (&chrs, b1) < 0) return BSTR_ERR;
invertCharField (&chrs);
return binchrrCF (b0->data, pos, &chrs);
}
/* int bsetstr (bstring b0, int pos, bstring b1, unsigned char fill)
*
* Overwrite the string b0 starting at position pos with the string b1. If
* the position pos is past the end of b0, then the character "fill" is
* appended as necessary to make up the gap between the end of b0 and pos.
* If b1 is NULL, it behaves as if it were a 0-length string.
*/
int bsetstr (bstring b0, int pos, const_bstring b1, unsigned char fill) {
int d, newlen;
ptrdiff_t pd;
bstring aux = (bstring) b1;
if (pos < 0 || b0 == NULL || b0->slen < 0 || NULL == b0->data ||
b0->mlen < b0->slen || b0->mlen <= 0) return BSTR_ERR;
if (b1 != NULL && (b1->slen < 0 || b1->data == NULL)) return BSTR_ERR;
d = pos;
/* Aliasing case */
if (NULL != aux) {
if ((pd = (ptrdiff_t) (b1->data - b0->data)) >= 0 && pd < (ptrdiff_t) b0->mlen) {
if (NULL == (aux = bstrcpy (b1))) return BSTR_ERR;
}
d += aux->slen;
}
/* Increase memory size if necessary */
if (balloc (b0, d + 1) != BSTR_OK) {
if (aux != b1) bdestroy (aux);
return BSTR_ERR;
}
newlen = b0->slen;
/* Fill in "fill" character as necessary */
if (pos > newlen) {
bstr__memset (b0->data + b0->slen, (int) fill, (size_t) (pos - b0->slen));
newlen = pos;
}
/* Copy b1 to position pos in b0. */
if (aux != NULL) {
bBlockCopy ((char *) (b0->data + pos), (char *) aux->data, aux->slen);
if (aux != b1) bdestroy (aux);
}
/* Indicate the potentially increased size of b0 */
if (d > newlen) newlen = d;
b0->slen = newlen;
b0->data[newlen] = (unsigned char) '\0';
return BSTR_OK;
}
/* int binsert (bstring b1, int pos, bstring b2, unsigned char fill)
*
* Inserts the string b2 into b1 at position pos. If the position pos is
* past the end of b1, then the character "fill" is appended as necessary to
* make up the gap between the end of b1 and pos. Unlike bsetstr, binsert
* does not allow b2 to be NULL.
*/
int binsert (bstring b1, int pos, const_bstring b2, unsigned char fill) {
int d, l;
ptrdiff_t pd;
bstring aux = (bstring) b2;
if (pos < 0 || b1 == NULL || b2 == NULL || b1->slen < 0 ||
b2->slen < 0 || b1->mlen < b1->slen || b1->mlen <= 0) return BSTR_ERR;
/* Aliasing case */
if ((pd = (ptrdiff_t) (b2->data - b1->data)) >= 0 && pd < (ptrdiff_t) b1->mlen) {
if (NULL == (aux = bstrcpy (b2))) return BSTR_ERR;
}
/* Compute the two possible end pointers */
d = b1->slen + aux->slen;
l = pos + aux->slen;
if ((d|l) < 0) return BSTR_ERR;
if (l > d) {
/* Inserting past the end of the string */
if (balloc (b1, l + 1) != BSTR_OK) {
if (aux != b2) bdestroy (aux);
return BSTR_ERR;
}
bstr__memset (b1->data + b1->slen, (int) fill, (size_t) (pos - b1->slen));
b1->slen = l;
} else {
/* Inserting in the middle of the string */
if (balloc (b1, d + 1) != BSTR_OK) {
if (aux != b2) bdestroy (aux);
return BSTR_ERR;
}
bBlockCopy (b1->data + l, b1->data + pos, d - l);
b1->slen = d;
}
bBlockCopy (b1->data + pos, aux->data, aux->slen);
b1->data[b1->slen] = (unsigned char) '\0';
if (aux != b2) bdestroy (aux);
return BSTR_OK;
}
/* int breplace (bstring b1, int pos, int len, bstring b2,
* unsigned char fill)
*
* Replace a section of a string from pos for a length len with the string b2.
* fill is used is pos > b1->slen.
*/
int breplace (bstring b1, int pos, int len, const_bstring b2,
unsigned char fill) {
int pl, ret;
ptrdiff_t pd;
bstring aux = (bstring) b2;
if (pos < 0 || len < 0 || (pl = pos + len) < 0 || b1 == NULL ||
b2 == NULL || b1->data == NULL || b2->data == NULL ||
b1->slen < 0 || b2->slen < 0 || b1->mlen < b1->slen ||
b1->mlen <= 0) return BSTR_ERR;
/* Straddles the end? */
if (pl >= b1->slen) {
if ((ret = bsetstr (b1, pos, b2, fill)) < 0) return ret;
if (pos + b2->slen < b1->slen) {
b1->slen = pos + b2->slen;
b1->data[b1->slen] = (unsigned char) '\0';
}
return ret;
}
/* Aliasing case */
if ((pd = (ptrdiff_t) (b2->data - b1->data)) >= 0 && pd < (ptrdiff_t) b1->slen) {
if (NULL == (aux = bstrcpy (b2))) return BSTR_ERR;
}
if (aux->slen > len) {
if (balloc (b1, b1->slen + aux->slen - len) != BSTR_OK) {
if (aux != b2) bdestroy (aux);
return BSTR_ERR;
}
}
if (aux->slen != len) bstr__memmove (b1->data + pos + aux->slen, b1->data + pos + len, b1->slen - (pos + len));
bstr__memcpy (b1->data + pos, aux->data, aux->slen);
b1->slen += aux->slen - len;
b1->data[b1->slen] = (unsigned char) '\0';
if (aux != b2) bdestroy (aux);
return BSTR_OK;
}
/*
* findreplaceengine is used to implement bfindreplace and
* bfindreplacecaseless. It works by breaking the three cases of
* expansion, reduction and replacement, and solving each of these
* in the most efficient way possible.
*/
typedef int (*instr_fnptr) (const_bstring s1, int pos, const_bstring s2);
#define INITIAL_STATIC_FIND_INDEX_COUNT 32
static int findreplaceengine (bstring b, const_bstring find, const_bstring repl, int pos, instr_fnptr instr) {
int i, ret, slen, mlen, delta, acc;
int * d;
int static_d[INITIAL_STATIC_FIND_INDEX_COUNT+1]; /* This +1 is unnecessary, but it shuts up LINT. */
ptrdiff_t pd;
bstring auxf = (bstring) find;
bstring auxr = (bstring) repl;
if (b == NULL || b->data == NULL || find == NULL ||
find->data == NULL || repl == NULL || repl->data == NULL ||
pos < 0 || find->slen <= 0 || b->mlen < 0 || b->slen > b->mlen ||
b->mlen <= 0 || b->slen < 0 || repl->slen < 0) return BSTR_ERR;
if (pos > b->slen - find->slen) return BSTR_OK;
/* Alias with find string */
pd = (ptrdiff_t) (find->data - b->data);
if ((ptrdiff_t) (pos - find->slen) < pd && pd < (ptrdiff_t) b->slen) {
if (NULL == (auxf = bstrcpy (find))) return BSTR_ERR;
}
/* Alias with repl string */
pd = (ptrdiff_t) (repl->data - b->data);
if ((ptrdiff_t) (pos - repl->slen) < pd && pd < (ptrdiff_t) b->slen) {
if (NULL == (auxr = bstrcpy (repl))) {
if (auxf != find) bdestroy (auxf);
return BSTR_ERR;
}
}
delta = auxf->slen - auxr->slen;
/* in-place replacement since find and replace strings are of equal
length */
if (delta == 0) {
while ((pos = instr (b, pos, auxf)) >= 0) {
bstr__memcpy (b->data + pos, auxr->data, auxr->slen);
pos += auxf->slen;
}
if (auxf != find) bdestroy (auxf);
if (auxr != repl) bdestroy (auxr);
return BSTR_OK;
}
/* shrinking replacement since auxf->slen > auxr->slen */
if (delta > 0) {
acc = 0;
while ((i = instr (b, pos, auxf)) >= 0) {
if (acc && i > pos)
bstr__memmove (b->data + pos - acc, b->data + pos, i - pos);
if (auxr->slen)
bstr__memcpy (b->data + i - acc, auxr->data, auxr->slen);
acc += delta;
pos = i + auxf->slen;
}
if (acc) {
i = b->slen;
if (i > pos)
bstr__memmove (b->data + pos - acc, b->data + pos, i - pos);
b->slen -= acc;
b->data[b->slen] = (unsigned char) '\0';
}
if (auxf != find) bdestroy (auxf);
if (auxr != repl) bdestroy (auxr);
return BSTR_OK;
}
/* expanding replacement since find->slen < repl->slen. Its a lot
more complicated. This works by first finding all the matches and
storing them to a growable array, then doing at most one resize of
the destination bstring and then performing the direct memory transfers
of the string segment pieces to form the final result. The growable
array of matches uses a deferred doubling reallocing strategy. What
this means is that it starts as a reasonably fixed sized auto array in
the hopes that many if not most cases will never need to grow this
array. But it switches as soon as the bounds of the array will be
exceeded. An extra find result is always appended to this array that
corresponds to the end of the destination string, so slen is checked
against mlen - 1 rather than mlen before resizing.
*/
mlen = INITIAL_STATIC_FIND_INDEX_COUNT;
d = (int *) static_d; /* Avoid malloc for trivial/initial cases */
acc = slen = 0;
while ((pos = instr (b, pos, auxf)) >= 0) {
if (slen >= mlen - 1) {
int sl, *t;
mlen += mlen;
sl = sizeof (int *) * mlen;
if (static_d == d) d = NULL; /* static_d cannot be realloced */
if (mlen <= 0 || sl < mlen || NULL == (t = (int *) bstr__realloc (d, sl))) {
ret = BSTR_ERR;
goto done;
}
if (NULL == d) bstr__memcpy (t, static_d, sizeof (static_d));
d = t;
}
d[slen] = pos;
slen++;
acc -= delta;
pos += auxf->slen;
if (pos < 0 || acc < 0) {
ret = BSTR_ERR;
goto done;
}
}
/* slen <= INITIAL_STATIC_INDEX_COUNT-1 or mlen-1 here. */
d[slen] = b->slen;
if (BSTR_OK == (ret = balloc (b, b->slen + acc + 1))) {
b->slen += acc;
for (i = slen-1; i >= 0; i--) {
int s, l;
s = d[i] + auxf->slen;
l = d[i+1] - s; /* d[slen] may be accessed here. */
if (l) {
bstr__memmove (b->data + s + acc, b->data + s, l);
}
if (auxr->slen) {
bstr__memmove (b->data + s + acc - auxr->slen,
auxr->data, auxr->slen);
}
acc += delta;
}
b->data[b->slen] = (unsigned char) '\0';
}
done:;
if (static_d == d) d = NULL;
bstr__free (d);
if (auxf != find) bdestroy (auxf);
if (auxr != repl) bdestroy (auxr);
return ret;
}
/* int bfindreplace (bstring b, const_bstring find, const_bstring repl,
* int pos)
*
* Replace all occurrences of a find string with a replace string after a
* given point in a bstring.
*/
int bfindreplace (bstring b, const_bstring find, const_bstring repl, int pos) {
return findreplaceengine (b, find, repl, pos, binstr);
}
/* int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl,
* int pos)
*
* Replace all occurrences of a find string, ignoring case, with a replace
* string after a given point in a bstring.
*/
int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl, int pos) {
return findreplaceengine (b, find, repl, pos, binstrcaseless);
}
/* int binsertch (bstring b, int pos, int len, unsigned char fill)
*
* Inserts the character fill repeatedly into b at position pos for a
* length len. If the position pos is past the end of b, then the
* character "fill" is appended as necessary to make up the gap between the
* end of b and the position pos + len.
*/
int binsertch (bstring b, int pos, int len, unsigned char fill) {
int d, l, i;
if (pos < 0 || b == NULL || b->slen < 0 || b->mlen < b->slen ||
b->mlen <= 0 || len < 0) return BSTR_ERR;
/* Compute the two possible end pointers */
d = b->slen + len;
l = pos + len;
if ((d|l) < 0) return BSTR_ERR;
if (l > d) {
/* Inserting past the end of the string */
if (balloc (b, l + 1) != BSTR_OK) return BSTR_ERR;
pos = b->slen;
b->slen = l;
} else {
/* Inserting in the middle of the string */
if (balloc (b, d + 1) != BSTR_OK) return BSTR_ERR;
for (i = d - 1; i >= l; i--) {
b->data[i] = b->data[i - len];
}
b->slen = d;
}
for (i=pos; i < l; i++) b->data[i] = fill;
b->data[b->slen] = (unsigned char) '\0';
return BSTR_OK;
}
/* int bpattern (bstring b, int len)
*
* Replicate the bstring, b in place, end to end repeatedly until it
* surpasses len characters, then chop the result to exactly len characters.
* This function operates in-place. The function will return with BSTR_ERR
* if b is NULL or of length 0, otherwise BSTR_OK is returned.
*/
int bpattern (bstring b, int len) {
int i, d;
d = blength (b);
if (d <= 0 || len < 0 || balloc (b, len + 1) != BSTR_OK) return BSTR_ERR;
if (len > 0) {
if (d == 1) return bsetstr (b, len, NULL, b->data[0]);
for (i = d; i < len; i++) b->data[i] = b->data[i - d];
}
b->data[len] = (unsigned char) '\0';
b->slen = len;
return BSTR_OK;
}
#define BS_BUFF_SZ (1024)
/* int breada (bstring b, bNread readPtr, void * parm)
*
* Use a finite buffer fread-like function readPtr to concatenate to the
* bstring b the entire contents of file-like source data in a roughly
* efficient way.
*/
int breada (bstring b, bNread readPtr, void * parm) {
int i, l, n;
if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen ||
b->mlen <= 0 || readPtr == NULL) return BSTR_ERR;
i = b->slen;
for (n=i+16; ; n += ((n < BS_BUFF_SZ) ? n : BS_BUFF_SZ)) {
if (BSTR_OK != balloc (b, n + 1)) return BSTR_ERR;
l = (int) readPtr ((void *) (b->data + i), 1, n - i, parm);
i += l;
b->slen = i;
if (i < n) break;
}
b->data[i] = (unsigned char) '\0';
return BSTR_OK;
}
/* bstring bread (bNread readPtr, void * parm)
*
* Use a finite buffer fread-like function readPtr to create a bstring
* filled with the entire contents of file-like source data in a roughly
* efficient way.
*/
bstring bread (bNread readPtr, void * parm) {
bstring buff;
if (0 > breada (buff = bfromcstr (""), readPtr, parm)) {
bdestroy (buff);
return NULL;
}
return buff;
}
/* int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator)
*
* Use an fgetc-like single character stream reading function (getcPtr) to
* obtain a sequence of characters which are concatenated to the end of the
* bstring b. The stream read is terminated by the passed in terminator
* parameter.
*
* If getcPtr returns with a negative number, or the terminator character
* (which is appended) is read, then the stream reading is halted and the
* function returns with a partial result in b. If there is an empty partial
* result, 1 is returned. If no characters are read, or there is some other
* detectable error, BSTR_ERR is returned.
*/
int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator) {
int c, d, e;
if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen ||
b->mlen <= 0 || getcPtr == NULL) return BSTR_ERR;
d = 0;
e = b->mlen - 2;
while ((c = getcPtr (parm)) >= 0) {
if (d > e) {
b->slen = d;
if (balloc (b, d + 2) != BSTR_OK) return BSTR_ERR;
e = b->mlen - 2;
}
b->data[d] = (unsigned char) c;
d++;
if (c == terminator) break;
}
b->data[d] = (unsigned char) '\0';
b->slen = d;
return d == 0 && c < 0;
}
/* int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator)
*
* Use an fgetc-like single character stream reading function (getcPtr) to
* obtain a sequence of characters which are concatenated to the end of the
* bstring b. The stream read is terminated by the passed in terminator
* parameter.
*
* If getcPtr returns with a negative number, or the terminator character
* (which is appended) is read, then the stream reading is halted and the
* function returns with a partial result concatentated to b. If there is
* an empty partial result, 1 is returned. If no characters are read, or
* there is some other detectable error, BSTR_ERR is returned.
*/
int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator) {
int c, d, e;
if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen ||
b->mlen <= 0 || getcPtr == NULL) return BSTR_ERR;
d = b->slen;
e = b->mlen - 2;
while ((c = getcPtr (parm)) >= 0) {
if (d > e) {
b->slen = d;
if (balloc (b, d + 2) != BSTR_OK) return BSTR_ERR;
e = b->mlen - 2;
}
b->data[d] = (unsigned char) c;
d++;
if (c == terminator) break;
}
b->data[d] = (unsigned char) '\0';
b->slen = d;
return d == 0 && c < 0;
}
/* bstring bgets (bNgetc getcPtr, void * parm, char terminator)
*
* Use an fgetc-like single character stream reading function (getcPtr) to
* obtain a sequence of characters which are concatenated into a bstring.
* The stream read is terminated by the passed in terminator function.
*
* If getcPtr returns with a negative number, or the terminator character
* (which is appended) is read, then the stream reading is halted and the
* result obtained thus far is returned. If no characters are read, or
* there is some other detectable error, NULL is returned.
*/
bstring bgets (bNgetc getcPtr, void * parm, char terminator) {
bstring buff;
if (0 > bgetsa (buff = bfromcstr (""), getcPtr, parm, terminator) || 0 >= buff->slen) {
bdestroy (buff);
buff = NULL;
}
return buff;
}
struct bStream {
bstring buff; /* Buffer for over-reads */
void * parm; /* The stream handle for core stream */
bNread readFnPtr; /* fread compatible fnptr for core stream */
int isEOF; /* track file's EOF state */
int maxBuffSz;
};
/* struct bStream * bsopen (bNread readPtr, void * parm)
*
* Wrap a given open stream (described by a fread compatible function
* pointer and stream handle) into an open bStream suitable for the bstring
* library streaming functions.
*/
struct bStream * bsopen (bNread readPtr, void * parm) {
struct bStream * s;
if (readPtr == NULL) return NULL;
s = (struct bStream *) bstr__alloc (sizeof (struct bStream));
if (s == NULL) return NULL;
s->parm = parm;
s->buff = bfromcstr ("");
s->readFnPtr = readPtr;
s->maxBuffSz = BS_BUFF_SZ;
s->isEOF = 0;
return s;
}
/* int bsbufflength (struct bStream * s, int sz)
*
* Set the length of the buffer used by the bStream. If sz is zero, the
* length is not set. This function returns with the previous length.
*/
int bsbufflength (struct bStream * s, int sz) {
int oldSz;
if (s == NULL || sz < 0) return BSTR_ERR;
oldSz = s->maxBuffSz;
if (sz > 0) s->maxBuffSz = sz;
return oldSz;
}
int bseof (const struct bStream * s) {
if (s == NULL || s->readFnPtr == NULL) return BSTR_ERR;
return s->isEOF && (s->buff->slen == 0);
}
/* void * bsclose (struct bStream * s)
*
* Close the bStream, and return the handle to the stream that was originally
* used to open the given stream.
*/
void * bsclose (struct bStream * s) {
void * parm;
if (s == NULL) return NULL;
s->readFnPtr = NULL;
if (s->buff) bdestroy (s->buff);
s->buff = NULL;
parm = s->parm;
s->parm = NULL;
s->isEOF = 1;
bstr__free (s);
return parm;
}
/* int bsreadlna (bstring r, struct bStream * s, char terminator)
*
* Read a bstring terminated by the terminator character or the end of the
* stream from the bStream (s) and return it into the parameter r. This
* function may read additional characters from the core stream that are not
* returned, but will be retained for subsequent read operations.
*/
int bsreadlna (bstring r, struct bStream * s, char terminator) {
int i, l, ret, rlo;
char * b;
struct tagbstring x;
if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0 ||
r->slen < 0 || r->mlen < r->slen) return BSTR_ERR;
l = s->buff->slen;
if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
b = (char *) s->buff->data;
x.data = (unsigned char *) b;
/* First check if the current buffer holds the terminator */
b[l] = terminator; /* Set sentinel */
for (i=0; b[i] != terminator; i++) ;
if (i < l) {
x.slen = i + 1;
ret = bconcat (r, &x);
s->buff->slen = l;
if (BSTR_OK == ret) bdelete (s->buff, 0, i + 1);
return BSTR_OK;
}
rlo = r->slen;
/* If not then just concatenate the entire buffer to the output */
x.slen = l;
if (BSTR_OK != bconcat (r, &x)) return BSTR_ERR;
/* Perform direct in-place reads into the destination to allow for
the minimum of data-copies */
for (;;) {
if (BSTR_OK != balloc (r, r->slen + s->maxBuffSz + 1)) return BSTR_ERR;
b = (char *) (r->data + r->slen);
l = (int) s->readFnPtr (b, 1, s->maxBuffSz, s->parm);
if (l <= 0) {
r->data[r->slen] = (unsigned char) '\0';
s->buff->slen = 0;
s->isEOF = 1;
/* If nothing was read return with an error message */
return BSTR_ERR & -(r->slen == rlo);
}
b[l] = terminator; /* Set sentinel */
for (i=0; b[i] != terminator; i++) ;
if (i < l) break;
r->slen += l;
}
/* Terminator found, push over-read back to buffer */
i++;
r->slen += i;
s->buff->slen = l - i;
bstr__memcpy (s->buff->data, b + i, l - i);
r->data[r->slen] = (unsigned char) '\0';
return BSTR_OK;
}
/* int bsreadlnsa (bstring r, struct bStream * s, bstring term)
*
* Read a bstring terminated by any character in the term string or the end
* of the stream from the bStream (s) and return it into the parameter r.
* This function may read additional characters from the core stream that
* are not returned, but will be retained for subsequent read operations.
*/
int bsreadlnsa (bstring r, struct bStream * s, const_bstring term) {
int i, l, ret, rlo;
unsigned char * b;
struct tagbstring x;
struct charField cf;
if (s == NULL || s->buff == NULL || r == NULL || term == NULL ||
term->data == NULL || r->mlen <= 0 || r->slen < 0 ||
r->mlen < r->slen) return BSTR_ERR;
if (term->slen == 1) return bsreadlna (r, s, term->data[0]);
if (term->slen < 1 || buildCharField (&cf, term)) return BSTR_ERR;
l = s->buff->slen;
if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
b = (unsigned char *) s->buff->data;
x.data = b;
/* First check if the current buffer holds the terminator */
b[l] = term->data[0]; /* Set sentinel */
for (i=0; !testInCharField (&cf, b[i]); i++) ;
if (i < l) {
x.slen = i + 1;
ret = bconcat (r, &x);
s->buff->slen = l;
if (BSTR_OK == ret) bdelete (s->buff, 0, i + 1);
return BSTR_OK;
}
rlo = r->slen;
/* If not then just concatenate the entire buffer to the output */
x.slen = l;
if (BSTR_OK != bconcat (r, &x)) return BSTR_ERR;
/* Perform direct in-place reads into the destination to allow for
the minimum of data-copies */
for (;;) {
if (BSTR_OK != balloc (r, r->slen + s->maxBuffSz + 1)) return BSTR_ERR;
b = (unsigned char *) (r->data + r->slen);
l = (int) s->readFnPtr (b, 1, s->maxBuffSz, s->parm);
if (l <= 0) {
r->data[r->slen] = (unsigned char) '\0';
s->buff->slen = 0;
s->isEOF = 1;
/* If nothing was read return with an error message */
return BSTR_ERR & -(r->slen == rlo);
}
b[l] = term->data[0]; /* Set sentinel */
for (i=0; !testInCharField (&cf, b[i]); i++) ;
if (i < l) break;
r->slen += l;
}
/* Terminator found, push over-read back to buffer */
i++;
r->slen += i;
s->buff->slen = l - i;
bstr__memcpy (s->buff->data, b + i, l - i);
r->data[r->slen] = (unsigned char) '\0';
return BSTR_OK;
}
/* int bsreada (bstring r, struct bStream * s, int n)
*
* Read a bstring of length n (or, if it is fewer, as many bytes as is
* remaining) from the bStream. This function may read additional
* characters from the core stream that are not returned, but will be
* retained for subsequent read operations. This function will not read
* additional characters from the core stream beyond virtual stream pointer.
*/
int bsreada (bstring r, struct bStream * s, int n) {
int l, ret, orslen;
char * b;
struct tagbstring x;
if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0
|| r->slen < 0 || r->mlen < r->slen || n <= 0) return BSTR_ERR;
n += r->slen;
if (n <= 0) return BSTR_ERR;
l = s->buff->slen;
orslen = r->slen;
if (0 == l) {
if (s->isEOF) return BSTR_ERR;
if (r->mlen > n) {
l = (int) s->readFnPtr (r->data + r->slen, 1, n - r->slen, s->parm);
if (0 >= l || l > n - r->slen) {
s->isEOF = 1;
return BSTR_ERR;
}
r->slen += l;
r->data[r->slen] = (unsigned char) '\0';
return 0;
}
}
if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
b = (char *) s->buff->data;
x.data = (unsigned char *) b;
do {
if (l + r->slen >= n) {
x.slen = n - r->slen;
ret = bconcat (r, &x);
s->buff->slen = l;
if (BSTR_OK == ret) bdelete (s->buff, 0, x.slen);
return BSTR_ERR & -(r->slen == orslen);
}
x.slen = l;
if (BSTR_OK != bconcat (r, &x)) break;
l = n - r->slen;
if (l > s->maxBuffSz) l = s->maxBuffSz;
l = (int) s->readFnPtr (b, 1, l, s->parm);
} while (l > 0);
if (l < 0) l = 0;
if (l == 0) s->isEOF = 1;
s->buff->slen = l;
return BSTR_ERR & -(r->slen == orslen);
}
/* int bsreadln (bstring r, struct bStream * s, char terminator)
*
* Read a bstring terminated by the terminator character or the end of the
* stream from the bStream (s) and return it into the parameter r. This
* function may read additional characters from the core stream that are not
* returned, but will be retained for subsequent read operations.
*/
int bsreadln (bstring r, struct bStream * s, char terminator) {
if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0)
return BSTR_ERR;
if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
r->slen = 0;
return bsreadlna (r, s, terminator);
}
/* int bsreadlns (bstring r, struct bStream * s, bstring term)
*
* Read a bstring terminated by any character in the term string or the end
* of the stream from the bStream (s) and return it into the parameter r.
* This function may read additional characters from the core stream that
* are not returned, but will be retained for subsequent read operations.
*/
int bsreadlns (bstring r, struct bStream * s, const_bstring term) {
if (s == NULL || s->buff == NULL || r == NULL || term == NULL
|| term->data == NULL || r->mlen <= 0) return BSTR_ERR;
if (term->slen == 1) return bsreadln (r, s, term->data[0]);
if (term->slen < 1) return BSTR_ERR;
if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
r->slen = 0;
return bsreadlnsa (r, s, term);
}
/* int bsread (bstring r, struct bStream * s, int n)
*
* Read a bstring of length n (or, if it is fewer, as many bytes as is
* remaining) from the bStream. This function may read additional
* characters from the core stream that are not returned, but will be
* retained for subsequent read operations. This function will not read
* additional characters from the core stream beyond virtual stream pointer.
*/
int bsread (bstring r, struct bStream * s, int n) {
if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0
|| n <= 0) return BSTR_ERR;
if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;
r->slen = 0;
return bsreada (r, s, n);
}
/* int bsunread (struct bStream * s, const_bstring b)
*
* Insert a bstring into the bStream at the current position. These
* characters will be read prior to those that actually come from the core
* stream.
*/
int bsunread (struct bStream * s, const_bstring b) {
if (s == NULL || s->buff == NULL) return BSTR_ERR;
return binsert (s->buff, 0, b, (unsigned char) '?');
}
/* int bspeek (bstring r, const struct bStream * s)
*
* Return the currently buffered characters from the bStream that will be
* read prior to reads from the core stream.
*/
int bspeek (bstring r, const struct bStream * s) {
if (s == NULL || s->buff == NULL) return BSTR_ERR;
return bassign (r, s->buff);
}
/* bstring bjoin (const struct bstrList * bl, const_bstring sep);
*
* Join the entries of a bstrList into one bstring by sequentially
* concatenating them with the sep string in between. If there is an error
* NULL is returned, otherwise a bstring with the correct result is returned.
*/
bstring bjoin (const struct bstrList * bl, const_bstring sep) {
bstring b;
int i, c, v;
if (bl == NULL || bl->qty < 0) return NULL;
if (sep != NULL && (sep->slen < 0 || sep->data == NULL)) return NULL;
for (i = 0, c = 1; i < bl->qty; i++) {
v = bl->entry[i]->slen;
if (v < 0) return NULL; /* Invalid input */
c += v;
if (c < 0) return NULL; /* Wrap around ?? */
}
if (sep != NULL) c += (bl->qty - 1) * sep->slen;
b = (bstring) bstr__alloc (sizeof (struct tagbstring));
if (NULL == b) return NULL; /* Out of memory */
b->data = (unsigned char *) bstr__alloc (c);
if (b->data == NULL) {
bstr__free (b);
return NULL;
}
b->mlen = c;
b->slen = c-1;
for (i = 0, c = 0; i < bl->qty; i++) {
if (i > 0 && sep != NULL) {
bstr__memcpy (b->data + c, sep->data, sep->slen);
c += sep->slen;
}
v = bl->entry[i]->slen;
bstr__memcpy (b->data + c, bl->entry[i]->data, v);
c += v;
}
b->data[c] = (unsigned char) '\0';
return b;
}
#define BSSSC_BUFF_LEN (256)
/* int bssplitscb (struct bStream * s, const_bstring splitStr,
* int (* cb) (void * parm, int ofs, const_bstring entry), void * parm)
*
* Iterate the set of disjoint sequential substrings read from a stream
* divided by any of the characters in splitStr. An empty splitStr causes
* the whole stream to be iterated once.
*
* Note: At the point of calling the cb function, the bStream pointer is
* pointed exactly at the position right after having read the split
* character. The cb function can act on the stream by causing the bStream
* pointer to move, and bssplitscb will continue by starting the next split
* at the position of the pointer after the return from cb.
*
* However, if the cb causes the bStream s to be destroyed then the cb must
* return with a negative value, otherwise bssplitscb will continue in an
* undefined manner.
*/
int bssplitscb (struct bStream * s, const_bstring splitStr,
int (* cb) (void * parm, int ofs, const_bstring entry), void * parm) {
struct charField chrs;
bstring buff;
int i, p, ret;
if (cb == NULL || s == NULL || s->readFnPtr == NULL
|| splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;
if (NULL == (buff = bfromcstr (""))) return BSTR_ERR;
if (splitStr->slen == 0) {
while (bsreada (buff, s, BSSSC_BUFF_LEN) >= 0) ;
if ((ret = cb (parm, 0, buff)) > 0)
ret = 0;
} else {
buildCharField (&chrs, splitStr);
ret = p = i = 0;
for (;;) {
if (i >= buff->slen) {
bsreada (buff, s, BSSSC_BUFF_LEN);
if (i >= buff->slen) {
if (0 < (ret = cb (parm, p, buff))) ret = 0;
break;
}
}
if (testInCharField (&chrs, buff->data[i])) {
struct tagbstring t;
unsigned char c;
blk2tbstr (t, buff->data + i + 1, buff->slen - (i + 1));
if ((ret = bsunread (s, &t)) < 0) break;
buff->slen = i;
c = buff->data[i];
buff->data[i] = (unsigned char) '\0';
if ((ret = cb (parm, p, buff)) < 0) break;
buff->data[i] = c;
buff->slen = 0;
p += i + 1;
i = -1;
}
i++;
}
}
bdestroy (buff);
return ret;
}
/* int bssplitstrcb (struct bStream * s, const_bstring splitStr,
* int (* cb) (void * parm, int ofs, const_bstring entry), void * parm)
*
* Iterate the set of disjoint sequential substrings read from a stream
* divided by the entire substring splitStr. An empty splitStr causes
* each character of the stream to be iterated.
*
* Note: At the point of calling the cb function, the bStream pointer is
* pointed exactly at the position right after having read the split
* character. The cb function can act on the stream by causing the bStream
* pointer to move, and bssplitscb will continue by starting the next split
* at the position of the pointer after the return from cb.
*
* However, if the cb causes the bStream s to be destroyed then the cb must
* return with a negative value, otherwise bssplitscb will continue in an
* undefined manner.
*/
int bssplitstrcb (struct bStream * s, const_bstring splitStr,
int (* cb) (void * parm, int ofs, const_bstring entry), void * parm) {
bstring buff;
int i, p, ret;
if (cb == NULL || s == NULL || s->readFnPtr == NULL
|| splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;
if (splitStr->slen == 1) return bssplitscb (s, splitStr, cb, parm);
if (NULL == (buff = bfromcstr (""))) return BSTR_ERR;
if (splitStr->slen == 0) {
for (i=0; bsreada (buff, s, BSSSC_BUFF_LEN) >= 0; i++) {
if ((ret = cb (parm, 0, buff)) < 0) {
bdestroy (buff);
return ret;
}
buff->slen = 0;
}
return BSTR_OK;
} else {
ret = p = i = 0;
for (i=p=0;;) {
if ((ret = binstr (buff, 0, splitStr)) >= 0) {
struct tagbstring t;
blk2tbstr (t, buff->data, ret);
i = ret + splitStr->slen;
if ((ret = cb (parm, p, &t)) < 0) break;
p += i;
bdelete (buff, 0, i);
} else {
bsreada (buff, s, BSSSC_BUFF_LEN);
if (bseof (s)) {
if ((ret = cb (parm, p, buff)) > 0) ret = 0;
break;
}
}
}
}
bdestroy (buff);
return ret;
}
/* int bstrListCreate (void)
*
* Create a bstrList.
*/
struct bstrList * bstrListCreate (void) {
struct bstrList * sl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));
if (sl) {
sl->entry = (bstring *) bstr__alloc (1*sizeof (bstring));
if (!sl->entry) {
bstr__free (sl);
sl = NULL;
} else {
sl->qty = 0;
sl->mlen = 1;
}
}
return sl;
}
/* int bstrListDestroy (struct bstrList * sl)
*
* Destroy a bstrList that has been created by bsplit, bsplits or bstrListCreate.
*/
int bstrListDestroy (struct bstrList * sl) {
int i;
if (sl == NULL || sl->qty < 0) return BSTR_ERR;
for (i=0; i < sl->qty; i++) {
if (sl->entry[i]) {
bdestroy (sl->entry[i]);
sl->entry[i] = NULL;
}
}
sl->qty = -1;
sl->mlen = -1;
bstr__free (sl->entry);
sl->entry = NULL;
bstr__free (sl);
return BSTR_OK;
}
/* int bstrListAlloc (struct bstrList * sl, int msz)
*
* Ensure that there is memory for at least msz number of entries for the
* list.
*/
int bstrListAlloc (struct bstrList * sl, int msz) {
bstring * l;
int smsz;
size_t nsz;
if (!sl || msz <= 0 || !sl->entry || sl->qty < 0 || sl->mlen <= 0 || sl->qty > sl->mlen) return BSTR_ERR;
if (sl->mlen >= msz) return BSTR_OK;
smsz = snapUpSize (msz);
nsz = ((size_t) smsz) * sizeof (bstring);
if (nsz < (size_t) smsz) return BSTR_ERR;
l = (bstring *) bstr__realloc (sl->entry, nsz);
if (!l) {
smsz = msz;
nsz = ((size_t) smsz) * sizeof (bstring);
l = (bstring *) bstr__realloc (sl->entry, nsz);
if (!l) return BSTR_ERR;
}
sl->mlen = smsz;
sl->entry = l;
return BSTR_OK;
}
/* int bstrListAllocMin (struct bstrList * sl, int msz)
*
* Try to allocate the minimum amount of memory for the list to include at
* least msz entries or sl->qty whichever is greater.
*/
int bstrListAllocMin (struct bstrList * sl, int msz) {
bstring * l;
size_t nsz;
if (!sl || msz <= 0 || !sl->entry || sl->qty < 0 || sl->mlen <= 0 || sl->qty > sl->mlen) return BSTR_ERR;
if (msz < sl->qty) msz = sl->qty;
if (sl->mlen == msz) return BSTR_OK;
nsz = ((size_t) msz) * sizeof (bstring);
if (nsz < (size_t) msz) return BSTR_ERR;
l = (bstring *) bstr__realloc (sl->entry, nsz);
if (!l) return BSTR_ERR;
sl->mlen = msz;
sl->entry = l;
return BSTR_OK;
}
/* int bsplitcb (const_bstring str, unsigned char splitChar, int pos,
* int (* cb) (void * parm, int ofs, int len), void * parm)
*
* Iterate the set of disjoint sequential substrings over str divided by the
* character in splitChar.
*
* Note: Non-destructive modification of str from within the cb function
* while performing this split is not undefined. bsplitcb behaves in
* sequential lock step with calls to cb. I.e., after returning from a cb
* that return a non-negative integer, bsplitcb continues from the position
* 1 character after the last detected split character and it will halt
* immediately if the length of str falls below this point. However, if the
* cb function destroys str, then it *must* return with a negative value,
* otherwise bsplitcb will continue in an undefined manner.
*/
int bsplitcb (const_bstring str, unsigned char splitChar, int pos,
int (* cb) (void * parm, int ofs, int len), void * parm) {
int i, p, ret;
if (cb == NULL || str == NULL || pos < 0 || pos > str->slen)
return BSTR_ERR;
p = pos;
do {
for (i=p; i < str->slen; i++) {
if (str->data[i] == splitChar) break;
}
if ((ret = cb (parm, p, i - p)) < 0) return ret;
p = i + 1;
} while (p <= str->slen);
return BSTR_OK;
}
/* int bsplitscb (const_bstring str, const_bstring splitStr, int pos,
* int (* cb) (void * parm, int ofs, int len), void * parm)
*
* Iterate the set of disjoint sequential substrings over str divided by any
* of the characters in splitStr. An empty splitStr causes the whole str to
* be iterated once.
*
* Note: Non-destructive modification of str from within the cb function
* while performing this split is not undefined. bsplitscb behaves in
* sequential lock step with calls to cb. I.e., after returning from a cb
* that return a non-negative integer, bsplitscb continues from the position
* 1 character after the last detected split character and it will halt
* immediately if the length of str falls below this point. However, if the
* cb function destroys str, then it *must* return with a negative value,
* otherwise bsplitscb will continue in an undefined manner.
*/
int bsplitscb (const_bstring str, const_bstring splitStr, int pos,
int (* cb) (void * parm, int ofs, int len), void * parm) {
struct charField chrs;
int i, p, ret;
if (cb == NULL || str == NULL || pos < 0 || pos > str->slen
|| splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;
if (splitStr->slen == 0) {
if ((ret = cb (parm, 0, str->slen)) > 0) ret = 0;
return ret;
}
if (splitStr->slen == 1)
return bsplitcb (str, splitStr->data[0], pos, cb, parm);
buildCharField (&chrs, splitStr);
p = pos;
do {
for (i=p; i < str->slen; i++) {
if (testInCharField (&chrs, str->data[i])) break;
}
if ((ret = cb (parm, p, i - p)) < 0) return ret;
p = i + 1;
} while (p <= str->slen);
return BSTR_OK;
}
/* int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos,
* int (* cb) (void * parm, int ofs, int len), void * parm)
*
* Iterate the set of disjoint sequential substrings over str divided by the
* substring splitStr. An empty splitStr causes the whole str to be
* iterated once.
*
* Note: Non-destructive modification of str from within the cb function
* while performing this split is not undefined. bsplitstrcb behaves in
* sequential lock step with calls to cb. I.e., after returning from a cb
* that return a non-negative integer, bsplitscb continues from the position
* 1 character after the last detected split character and it will halt
* immediately if the length of str falls below this point. However, if the
* cb function destroys str, then it *must* return with a negative value,
* otherwise bsplitscb will continue in an undefined manner.
*/
int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos,
int (* cb) (void * parm, int ofs, int len), void * parm) {
int i, p, ret;
if (cb == NULL || str == NULL || pos < 0 || pos > str->slen
|| splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;
if (0 == splitStr->slen) {
for (i=pos; i < str->slen; i++) {
if ((ret = cb (parm, i, 1)) < 0) return ret;
}
return BSTR_OK;
}
if (splitStr->slen == 1)
return bsplitcb (str, splitStr->data[0], pos, cb, parm);
for (i=p=pos; i <= str->slen - splitStr->slen; i++) {
if (0 == bstr__memcmp (splitStr->data, str->data + i, splitStr->slen)) {
if ((ret = cb (parm, p, i - p)) < 0) return ret;
i += splitStr->slen;
p = i;
}
}
if ((ret = cb (parm, p, str->slen - p)) < 0) return ret;
return BSTR_OK;
}
struct genBstrList {
bstring b;
struct bstrList * bl;
};
static int bscb (void * parm, int ofs, int len) {
struct genBstrList * g = (struct genBstrList *) parm;
if (g->bl->qty >= g->bl->mlen) {
int mlen = g->bl->mlen * 2;
bstring * tbl;
while (g->bl->qty >= mlen) {
if (mlen < g->bl->mlen) return BSTR_ERR;
mlen += mlen;
}
tbl = (bstring *) bstr__realloc (g->bl->entry, sizeof (bstring) * mlen);
if (tbl == NULL) return BSTR_ERR;
g->bl->entry = tbl;
g->bl->mlen = mlen;
}
g->bl->entry[g->bl->qty] = bmidstr (g->b, ofs, len);
g->bl->qty++;
return BSTR_OK;
}
/* struct bstrList * bsplit (const_bstring str, unsigned char splitChar)
*
* Create an array of sequential substrings from str divided by the character
* splitChar.
*/
struct bstrList * bsplit (const_bstring str, unsigned char splitChar) {
struct genBstrList g;
if (str == NULL || str->data == NULL || str->slen < 0) return NULL;
g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));
if (g.bl == NULL) return NULL;
g.bl->mlen = 4;
g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring));
if (NULL == g.bl->entry) {
bstr__free (g.bl);
return NULL;
}
g.b = (bstring) str;
g.bl->qty = 0;
if (bsplitcb (str, splitChar, 0, bscb, &g) < 0) {
bstrListDestroy (g.bl);
return NULL;
}
return g.bl;
}
/* struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr)
*
* Create an array of sequential substrings from str divided by the entire
* substring splitStr.
*/
struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr) {
struct genBstrList g;
if (str == NULL || str->data == NULL || str->slen < 0) return NULL;
g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));
if (g.bl == NULL) return NULL;
g.bl->mlen = 4;
g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring));
if (NULL == g.bl->entry) {
bstr__free (g.bl);
return NULL;
}
g.b = (bstring) str;
g.bl->qty = 0;
if (bsplitstrcb (str, splitStr, 0, bscb, &g) < 0) {
bstrListDestroy (g.bl);
return NULL;
}
return g.bl;
}
/* struct bstrList * bsplits (const_bstring str, bstring splitStr)
*
* Create an array of sequential substrings from str divided by any of the
* characters in splitStr. An empty splitStr causes a single entry bstrList
* containing a copy of str to be returned.
*/
struct bstrList * bsplits (const_bstring str, const_bstring splitStr) {
struct genBstrList g;
if ( str == NULL || str->slen < 0 || str->data == NULL ||
splitStr == NULL || splitStr->slen < 0 || splitStr->data == NULL)
return NULL;
g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));
if (g.bl == NULL) return NULL;
g.bl->mlen = 4;
g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring));
if (NULL == g.bl->entry) {
bstr__free (g.bl);
return NULL;
}
g.b = (bstring) str;
g.bl->qty = 0;
if (bsplitscb (str, splitStr, 0, bscb, &g) < 0) {
bstrListDestroy (g.bl);
return NULL;
}
return g.bl;
}
#if defined (__TURBOC__) && !defined (__BORLANDC__)
# ifndef BSTRLIB_NOVSNP
# define BSTRLIB_NOVSNP
# endif
#endif
/* Give WATCOM C/C++, MSVC some latitude for their non-support of vsnprintf */
#if defined(__WATCOMC__) || defined(_MSC_VER)
#define exvsnprintf(r,b,n,f,a) {r = _vsnprintf (b,n,f,a);}
#else
#ifdef BSTRLIB_NOVSNP
/* This is just a hack. If you are using a system without a vsnprintf, it is
not recommended that bformat be used at all. */
#define exvsnprintf(r,b,n,f,a) {vsprintf (b,f,a); r = -1;}
#define START_VSNBUFF (256)
#else
#ifdef __GNUC__
/* Something is making gcc complain about this prototype not being here, so
I've just gone ahead and put it in. */
extern int vsnprintf (char *buf, size_t count, const char *format, va_list arg);
#endif
#define exvsnprintf(r,b,n,f,a) {r = vsnprintf (b,n,f,a);}
#endif
#endif
#if !defined (BSTRLIB_NOVSNP)
#ifndef START_VSNBUFF
#define START_VSNBUFF (16)
#endif
/* On IRIX vsnprintf returns n-1 when the operation would overflow the target
buffer, WATCOM and MSVC both return -1, while C99 requires that the
returned value be exactly what the length would be if the buffer would be
large enough. This leads to the idea that if the return value is larger
than n, then changing n to the return value will reduce the number of
iterations required. */
/* int bformata (bstring b, const char * fmt, ...)
*
* After the first parameter, it takes the same parameters as printf (), but
* rather than outputting results to stdio, it appends the results to
* a bstring which contains what would have been output. Note that if there
* is an early generation of a '\0' character, the bstring will be truncated
* to this end point.
*/
int bformata (bstring b, const char * fmt, ...) {
va_list arglist;
bstring buff;
int n, r;
if (b == NULL || fmt == NULL || b->data == NULL || b->mlen <= 0
|| b->slen < 0 || b->slen > b->mlen) return BSTR_ERR;