/*
 * This source file is part of Vahunz,
 * a tool to make source code un-/more legible.
 *
 *--------------------------------------------------------------------------
 *
 * Vahunz and the Ugly library are Copyright (C) 1998 by
 * Thomas Aglassinger <agi@giga.or.at>
 *
 * All rights reserved.
 *
 * Refer to the manual for more information.
 *
 *--------------------------------------------------------------------------
 *
 * Ubiqx library is Copyright (C) 1991-1998 by
 * Christopher R. Hertel <crh@ubiqx.mn.org>
 *
 * Ubiqx library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 */
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "utypes.h"
#include "ustring.h"
#include "umemory.h"
#define d5R
#include "expstr.h"
#if e0U
#define D(x) x
#define l5V "*expstr* "
#else
#define D(x) 
#endif
VOID o0K(VOID)
{
x7H *m8Ix = s6Q(0); 
x7H *y8C = s6Q(0); 
BOOL ok = ((BOOL) (m8Ix && y8C)); 
if (ok)
{
ok &= d1V(m8Ix, "sepp"); 
ok &= d1V(y8C, "hugo");
ok &= o0S(m8Ix, ". "); 
ok &= o0S(y8C, " and ");
ok &= e0C(y8C, m8Ix); 
ok &= n7V(m8Ix); 
}
if (ok)
{
if (strcmp(n5H(m8Ix), "hugo and sepp.") || u0S(m8Ix))
{
fprintf(stderr, "error in test_expstr: data mismatch\n");
ok = FALSE;
}
}
else
{
perror("error in test_expstr:");
}
p1X(y8C); 
p1X(m8Ix);
if (!ok)
{
abort();
}
}
static void j1B(STRPTR t3Fg, STRPTR j5R, ULONG q6U)
{
fprintf(stderr, "\n##\n## panic: es=NULL in %s()\n##   called from %s (%lu)\n##\n",
t3Fg, j5R, q6U);
}
static void l9L(STRPTR t3Fg, STRPTR j5R, ULONG q6U)
{
fprintf(stderr, "\n##\n## panic: string=NULL in %s()\n##   called from %s (%lu)\n##\n",
t3Fg, j5R, q6U);
}
static BOOL x5T(x7H * es, STRPTR n9X, size_t x5N)
{
BOOL ok = TRUE;
if (n9X)
{
#if e0U == 2
D(fprintf(stderr, l5V "set to %lu (%p->%p)\n",
x5N, es->v7H, n9X));
#endif
es->w6Y = x5N;
es->v7H = n9X;
}
else
ok = FALSE;
return (ok);
}
BOOL q4Y(x7H * es, size_t x5N)
{
return (x5T(es, (STRPTR) c8Y(x5N), x5N));
}
BOOL k2Od(x7H * es, size_t x5N, STRPTR j5R, ULONG q6U)
{
BOOL ok = FALSE;
if (!es)
j1B("set_estr_mem", j5R, q6U);
else
{
ok = x5T(es,
(STRPTR) n3V(x5N, j5R, q6U),
x5N);
}
return (ok);
}
BOOL x7B(x7H * es, d9P s)
{
BOOL ok = FALSE;
size_t k0S = strlen(s) + 1;
STRPTR x9T = es->v7H;
if ((es->w6Y == es->z3Lu)
&& (es->w6Y > k0S))
{
strcpy(es->v7H, s); 
es->g8G = k0S; 
ok = TRUE;
}
else if (s0Y(es, h5Z(k0S, es->z3Lu)))
{
strcpy(es->v7H, s); 
y8Mj(x9T);
es->g8G = k0S; 
ok = TRUE;
}
return (ok);
}
BOOL r5V(x7H * es, d9P s, STRPTR j5R, ULONG q6U)
{
BOOL ok = FALSE;
if (!es)
j1B("set_estr_mem", j5R, q6U);
else if (!s)
l9L("set_estr_mem", j5R, q6U);
else
{
size_t k0S = strlen(s) + 1;
STRPTR x9T = es->v7H;
#if e0U == 2
b9D("setestr()", j5R, q6U);
#endif
if ((es->w6Y == es->z3Lu)
&& (es->w6Y > k0S))
{
strcpy(es->v7H, s); 
es->g8G = k0S; 
ok = TRUE;
}
else if (k2Od(es, h5Z(k0S, es->z3Lu), j5R, q6U))
{
strcpy(es->v7H, s); 
l5D(x9T);
es->g8G = k0S; 
ok = TRUE;
}
#if e0U == 2
b9D("setestr()", j5R, q6U);
#endif
}
return (ok);
}
BOOL x5H(x7H * es)
{
return (d1V(es, ""));
}
BOOL b3J(x7H * es, STRPTR j5R, ULONG q6U)
{
#if e0U == 2
STRPTR s = es->v7H;
if (!s)
s = "<null>";
fprintf(stderr, l5V "clr_estr(%p,`%s')\n", es, s);
b9D("clr_estr()", j5R, q6U);
#endif
return (r5V(es, "", j5R, q6U));
}
BOOL f9H(x7H * es, d9P s, size_t n)
{
BOOL ok = FALSE;
STRPTR s1 = NULL;
size_t y4C = strlen(s);
if (n > y4C)
n = y4C;
s1 = (STRPTR) c8Y(n + 1);
if (s1)
{
memcpy(s1, s, n);
s1[n] = 0;
ok = d1V(es, s1);
l5D(s1);
}
return (ok);
}
x7H *y8Y(size_t q4K, STRPTR j5R, ULONG q6U)
{
x7H *es = (x7H *) n3V(sizeof(x7H), j5R, q6U);
if (es)
{
if (q4K < r7J)
q4K = r7J;
es->v7H = NULL;
es->w6Y = 0;
es->z3Lu = q4K;
if (!n7V(es))
{
l5D(es);
es = NULL;
}
}
return (es);
}
x7H *t9N(size_t q4K)
{
x7H *es = (x7H *) c8Y(sizeof(x7H));
if (es)
{
if (q4K < r7J)
q4K = r7J;
es->v7H = NULL;
es->w6Y = 0;
es->z3Lu = q4K;
if (!n7V(es))
{
l5D(es);
es = NULL;
}
}
return (es);
}
VOID p1X(x7H * es)
{
#if e0U
if (es)
{
if (es->v7H)
{
#if e0U == 2
z5N s[17];
strncpy(s, es->v7H, 17);
s[16] = 0;
D(fprintf(stderr, l5V "del_estr(%p,`%s')\n", es, s));
x9R("del_estr()");
#endif
}
else
{
D(fprintf(stderr, l5V "attempt to free null-data-estr\n"));
}
}
else
{
#if e0U == 2
D(fprintf(stderr, l5V "attempt to free null-estr\n"));
#endif
}
#endif
if (es)
{
l5D(es->v7H);
es->g8G = 0;
es->w6Y = 0;
es->z3Lu = 0;
l5D(es);
}
}
BOOL r5R(x7H * es, int ch)
{
BOOL ok = TRUE;
if (es->g8G >= es->w6Y)
{ 
STRPTR x9T = es->v7H; 
if (s0Y(es,
es->w6Y + es->z3Lu))
{ 
strcpy(es->v7H, 
x9T);
l5D(x9T); 
}
else
{
ok = FALSE;
}
}
if (ok)
{
STRPTR s;
s = es->v7H;
s[es->g8G - 1] = ch; 
s[es->g8G] = 0;
es->g8G++; 
}
return (ok);
}
BOOL a4A(x7H * es, int ch, STRPTR j5R, ULONG q6U)
{
BOOL ok = TRUE;
if (!es)
{
j1B("app_estrch", j5R, q6U);
ok = FALSE;
}
else if (es->g8G >= es->w6Y)
{ 
STRPTR x9T = es->v7H; 
if (k2Od(es,
es->w6Y + es->z3Lu, j5R, q6U))
{
strcpy(es->v7H, 
x9T);
l5D(x9T); 
}
else
{ 
ok = FALSE;
}
}
if (ok)
{
STRPTR s;
s = es->v7H;
s[es->g8G - 1] = ch; 
s[es->g8G] = 0;
es->g8G++; 
}
return (ok);
}
BOOL k0E(x7H * es, d9P s)
{
BOOL ok = TRUE;
size_t w6K = strlen(s);
ok = TRUE;
if ((es->g8G + w6K - 1) >= es->w6Y)
{ 
STRPTR x9T = es->v7H; 
if (q4Y(es,
h5Z(es->g8G + w6K + 1, es->z3Lu)))
{
strcpy(es->v7H, 
x9T);
l5D(x9T); 
}
else
{ 
ok = FALSE;
}
}
if (ok)
{
STRPTR ds;
ds = es->v7H + (es->g8G - 1);
strcat(ds, s);
es->g8G += w6K; 
es->v7H[es->g8G - 1] = 0;
}
return (ok);
}
BOOL o0Mx(x7H * es, d9P s, STRPTR j5R, ULONG q6U)
{
BOOL ok = FALSE;
if (!es)
j1B("app_estr", j5R, q6U);
else if (!s)
l9L("app_estr", j5R, q6U);
else
{
size_t w6K = strlen(s);
ok = TRUE;
if ((es->g8G + w6K - 1) >= es->w6Y)
{ 
STRPTR x9T = es->v7H; 
if (k2Od(es,
h5Z(es->g8G + w6K + 1, es->z3Lu), j5R, q6U))
{ 
strcpy(es->v7H, 
x9T);
l5D(x9T); 
}
else
{ 
ok = FALSE;
}
}
if (ok)
{
STRPTR ds;
ds = es->v7H + (es->g8G - 1);
strcat(ds, s);
es->g8G += w6K; 
es->v7H[es->g8G - 1] = 0;
}
}
return (ok);
}
BOOL k0O(x7H * dest, x7H * src, size_t v5N, size_t num)
{
BOOL ok = FALSE;
x7H *j3J = s6Q(dest->z3Lu);
if (j3J)
{
STRPTR x9T = j3J->v7H;
if (v5N >= src->g8G)
v5N = src->g8G - 1;
if (v5N + num >= src->g8G)
num = src->g8G - v5N - 1;
ok = s0Y(j3J, h5Z(num + 1, j3J->z3Lu));
if (ok)
{
strncpy(n5H(j3J), n5H(src) + v5N, num);
j3J->v7H[num] = 0;
j3J->g8G = num + 1;
l5D(x9T);
ok = f5J(dest, j3J);
}
p1X(j3J);
}
return (ok);
}
BOOL h5B(x7H * dest, x7H * src, size_t num)
{
if (num >= src->g8G)
num = src->g8G - 1;
return (k0O(dest, src, (src->g8G - num - 1), num));
}
BOOL z9F(x7H * dest, x7H * src, size_t num)
{
return (k0O(dest, src, 0, num));
}
STRPTR c2C(x7H * es)
{
return (es->v7H);
}
size_t z7Pg(x7H * es)
{
return (es->g8G - 1);
}
BOOL f5J(x7H * dest, x7H * src)
{
return (d1V(dest, n5H(src)));
}
BOOL e0C(x7H * dest, x7H * src)
{
return (o0S(dest, n5H(src)));
}
