
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "utypes.h"
#include "ustring.h"
#include "umemory.h"
#define l7N
#include "expstr.h"
#if y2O
#define D(x) x
#define h3V "*expstr* "
#else
#define D(x) 
#endif

VOID v5N(VOID)
{
j7Z *c8K = u6M(0); 
j7Z *e2U = u6M(0); 
BOOL ok = ((BOOL) (c8K && e2U)); 
if (ok)
{
ok &= a0U(c8K, "sepp"); 
ok &= a0U(e2U, "hugo");
ok &= w0O(c8K, ". "); 
ok &= w0O(e2U, " and ");
ok &= h1T(e2U, c8K); 
ok &= k8A(c8K); 
}

if (ok)
{
if (strcmp(w4Y(c8K), "hugo and sepp.") || j9Z(c8K))
{
fprintf(stderr, "error in test_expstr: data mismatch\n");
ok = FALSE;
}
}
else
{
perror("error in test_expstr:");
}
d1H(e2U); 
d1H(c8K);

if (!ok)
{
abort();
}
}


static void h1R(STRPTR r1P, STRPTR file, ULONG line)
{
fprintf(stderr, "\n##\n## panic: es=NULL in %s()\n##   called from %s (%lu)\n##\n",
r1P, file, line);
}
static void j3J(STRPTR r1P, STRPTR file, ULONG line)
{
fprintf(stderr, "\n##\n## panic: string=NULL in %s()\n##   called from %s (%lu)\n##\n",
r1P, file, line);
}


static BOOL k8E(j7Z * es, STRPTR f1V, size_t l7B)
{
BOOL ok = TRUE;
if (f1V)
{
#if y2O == 2
D(fprintf(stderr, h3V "set to %lu (%p->%p)\n",
l7B, es->t9L, f1V));
#endif
es->q6O = l7B;
es->t9L = f1V;
}
else
ok = FALSE;
return (ok);
}
BOOL z5F(j7Z * es, size_t l7B)
{
return (k8E(es, (STRPTR) e0C(l7B), l7B));
}
BOOL o4Kr(j7Z * es, size_t l7B, STRPTR file, ULONG line)
{
BOOL ok = FALSE;
if (!es)
h1R("set_estr_mem", file, line);
else
{
ok = k8E(es,
(STRPTR) z3V(l7B, file, line),
l7B);
}
return (ok);
}

BOOL q0U(j7Z * es, j9B s)
{
BOOL ok = FALSE;
size_t w4O = strlen(s) + 1;
STRPTR a4W = es->t9L;
if ((es->q6O == es->x3J)
&& (es->q6O > w4O))
{
strcpy(es->t9L, s); 
es->u6C = w4O; 
ok = TRUE;
}
else if (j5V(es, q6Od(w4O, es->x3J)))
{
strcpy(es->t9L, s); 
f1LS(a4W);
es->u6C = w4O; 
ok = TRUE;
}
return (ok);
}
BOOL d7Z(j7Z * es, j9B s, STRPTR file, ULONG line)
{
BOOL ok = FALSE;
if (!es)
h1R("set_estr_mem", file, line);
else if (!s)
j3J("set_estr_mem", file, line);
else
{
size_t w4O = strlen(s) + 1;
STRPTR a4W = es->t9L;
#if y2O == 2
z3F("setestr()", file, line);
#endif
if ((es->q6O == es->x3J)
&& (es->q6O > w4O))
{
strcpy(es->t9L, s); 
es->u6C = w4O; 
ok = TRUE;
}
else if (o4Kr(es, q6Od(w4O, es->x3J), file, line))
{
strcpy(es->t9L, s); 
g2I(a4W);
es->u6C = w4O; 
ok = TRUE;
}
#if y2O == 2
z3F("setestr()", file, line);
#endif
}
return (ok);
}

BOOL q8G(j7Z * es)
{
return (a0U(es, ""));
}
BOOL l9F(j7Z * es, STRPTR file, ULONG line)
{
#if y2O == 2
STRPTR s = es->t9L;
if (!s)
s = "<null>";
fprintf(stderr, h3V "clr_estr(%p,`%s')\n", es, s);
z3F("clr_estr()", file, line);
#endif
return (d7Z(es, "", file, line));
}

BOOL y8K(j7Z * es, j9B s, size_t n)
{
BOOL ok = FALSE;
STRPTR s1 = NULL;
size_t len = strlen(s);
if (n > len)
n = len;
s1 = (STRPTR) e0C(n + 1);
if (s1)
{
memcpy(s1, s, n);
s1[n] = 0;
ok = a0U(es, s1);
g2I(s1);
}
return (ok);
}


j7Z *u8A(size_t f1L, STRPTR file, ULONG line)
{
j7Z *es = (j7Z *) z3V(sizeof(j7Z), file, line);
if (es)
{
if (f1L < n5P)
f1L = n5P;
es->t9L = NULL;
es->q6O = 0;
es->x3J = f1L;
if (!k8A(es))
{
g2I(es);
es = NULL;
}
}
return (es);
}
j7Z *a4C(size_t f1L)
{
j7Z *es = (j7Z *) e0C(sizeof(j7Z));
if (es)
{
if (f1L < n5P)
f1L = n5P;
es->t9L = NULL;
es->q6O = 0;
es->x3J = f1L;
if (!k8A(es))
{
g2I(es);
es = NULL;
}
}
return (es);
}

VOID d1H(j7Z * es)
{
#if y2O
if (es)
{
if (es->t9L)
{
#if y2O == 2
x9T s[17];
strncpy(s, es->t9L, 17);
s[16] = 0;
D(fprintf(stderr, h3V "del_estr(%p,`%s')\n", es, s));
e8YR("del_estr()");
#endif
}
else
{
D(fprintf(stderr, h3V "attempt to free null-data-estr\n"));
}
}
else
{
#if y2O == 2
D(fprintf(stderr, h3V "attempt to free null-estr\n"));
#endif
}
#endif
if (es)
{
g2I(es->t9L);
es->u6C = 0;
es->q6O = 0;
es->x3J = 0;
g2I(es);
}
}

BOOL k6S(j7Z * es, int ch)
{
BOOL ok = TRUE;
if (es->u6C >= es->q6O)
{ 
STRPTR a4W = es->t9L; 
if (j5V(es,
es->q6O + es->x3J))
{ 
strcpy(es->t9L, 
a4W);
g2I(a4W); 
}
else
{

ok = FALSE;
}
}
if (ok)
{
STRPTR s;
s = es->t9L;
s[es->u6C - 1] = ch; 
s[es->u6C] = 0;
es->u6C++; 
}
return (ok);
}
BOOL u8W(j7Z * es, int ch, STRPTR file, ULONG line)
{
BOOL ok = TRUE;
if (!es)
{
h1R("app_estrch", file, line);
ok = FALSE;
}
else if (es->u6C >= es->q6O)
{ 
STRPTR a4W = es->t9L; 
if (o4Kr(es,
es->q6O + es->x3J, file, line))
{

strcpy(es->t9L, 
a4W);
g2I(a4W); 
}
else
{ 
ok = FALSE;
}
}
if (ok)
{
STRPTR s;
s = es->t9L;
s[es->u6C - 1] = ch; 
s[es->u6C] = 0;
es->u6C++; 
}
return (ok);
}

BOOL l9J(j7Z * es, j9B s)
{
BOOL ok = TRUE;
size_t t9R = strlen(s);
ok = TRUE;
if ((es->u6C + t9R - 1) >= es->q6O)
{ 
STRPTR a4W = es->t9L; 
if (z5F(es,
q6Od(es->u6C + t9R + 1, es->x3J)))
{

strcpy(es->t9L, 
a4W);
g2I(a4W); 
}
else
{ 
ok = FALSE;
}
}
if (ok)
{
STRPTR ds;
ds = es->t9L + (es->u6C - 1);
strcat(ds, s);

es->u6C += t9R; 
es->t9L[es->u6C - 1] = 0;
}
return (ok);
}
BOOL n3N(j7Z * es, j9B s, STRPTR file, ULONG line)
{
BOOL ok = FALSE;
if (!es)
h1R("app_estr", file, line);
else if (!s)
j3J("app_estr", file, line);
else
{

size_t t9R = strlen(s);
ok = TRUE;
if ((es->u6C + t9R - 1) >= es->q6O)
{ 
STRPTR a4W = es->t9L; 
if (o4Kr(es,
q6Od(es->u6C + t9R + 1, es->x3J), file, line))
{ 
strcpy(es->t9L, 
a4W);
g2I(a4W); 
}
else
{ 
ok = FALSE;
}
}
if (ok)
{
STRPTR ds;
ds = es->t9L + (es->u6C - 1);
strcat(ds, s);

es->u6C += t9R; 
es->t9L[es->u6C - 1] = 0;
}
}
return (ok);
}



BOOL c8Ct(j7Z * dest, j7Z * src, size_t k2Ib, size_t num)
{
BOOL ok = FALSE;
j7Z *m6Q = u6M(dest->x3J);
if (m6Q)
{
STRPTR a4W = m6Q->t9L;

if (k2Ib >= src->u6C)
k2Ib = src->u6C - 1;
if (k2Ib + num >= src->u6C)
num = src->u6C - k2Ib - 1;

ok = j5V(m6Q, q6Od(num + 1, m6Q->x3J));
if (ok)
{

strncpy(w4Y(m6Q), w4Y(src) + k2Ib, num);
m6Q->t9L[num] = 0;
m6Q->u6C = num + 1;
g2I(a4W);
ok = a2U(dest, m6Q);
}
d1H(m6Q);
}
return (ok);
}

BOOL y6EV(j7Z * dest, j7Z * src, size_t num)
{
if (num >= src->u6C)
num = src->u6C - 1;
return (c8Ct(dest, src, (src->u6C - num - 1), num));
}

BOOL l3D(j7Z * dest, j7Z * src, size_t num)
{
return (c8Ct(dest, src, 0, num));
}


STRPTR c8OD(j7Z * es)
{
return (es->t9L);
}

size_t u0G(j7Z * es)
{
return (es->u6C - 1);
}

BOOL a2U(j7Z * dest, j7Z * src)
{
return (a0U(dest, w4Y(src)));
}

BOOL h1T(j7Z * dest, j7Z * src)
{
return (w0O(dest, w4Y(src)));
}
