Category : OS/2 Files
Archive   : KSH48.ZIP
Filename : EXPAND.H

 
Output of file : EXPAND.H contained in archive : KSH48.ZIP
/*
* Expanding strings
*/

#if 0 /* Usage */
XString xs;
char *xp;

Xinit(xs, xp, 128); /* allocate initial string */
while ((c = generate()) {
Xcheck(xs, xp); /* expand string if neccessary */
Xput(xs, xp, c); /* add character */
}
return Xclose(xs, xp); /* resize string */
#endif

typedef struct XString {
char *end, *beg; /* end, begin of string */
#if 1
char *oth, *old; /* togo, adjust */
#endif
size_t len; /* length */
} XString;

typedef char * XStringP;

/* initialize expandable string */
#define Xinit(xs, xp, length) { \
(xs).len = length; \
(xs).beg = alloc((xs).len + 8, ATEMP); \
(xs).end = (xs).beg + (xs).len; \
xp = (xs).beg; \
}

/* stuff char into string */
#define Xput(xs, xp, c) *xp++ = (c)

/* check for overflow, expand string */
#define Xcheck(xs, xp) if (xp >= (xs).end) { \
char *old_beg = (xs).beg; \
(xs).len += (xs).len; /* double size */ \
(xs).beg = aresize((xs).beg, (xs).len + 8, ATEMP); \
(xs).end = (xs).beg + (xs).len; \
xp = (xs).beg + (xp - old_beg); /* adjust pointer */ \
}

/* free string */
#define Xfree(xs, xp) afree((void*) (xs).beg, ATEMP)

/* close, return string */
#define Xclose(xs, xp) (char*) aresize((void*)(xs).beg, \
(size_t)(xp - (xs).beg), ATEMP)
/* begin of string */
#define Xstring(xs, xp) ((xs).beg)

#define Xsavepos(xs, xp) (xp - (xs).beg)
#define Xrestpos(xs, xp, n) ((xs).beg + (n))

/*
* expandable vector of generic pointers
*/

typedef struct XPtrV {
void **cur; /* next avail pointer */
void **beg, **end; /* begin, end of vector */
} XPtrV;

#define XPinit(x, n) { \
register void **vp; \
vp = (void**) alloc(sizeofN(void*, n), ATEMP); \
(x).cur = (x).beg = vp; \
(x).end = vp + n; \
}

#define XPput(x, p) { \
if ((x).cur >= (x).end) { \
int n = XPsize(x); \
(x).beg = (void**) aresize((void*) (x).beg, \
sizeofN(void*, n*2), ATEMP); \
(x).cur = (x).beg + n; \
(x).end = (x).cur + n; \
} \
*(x).cur++ = (p); \
}

#define XPptrv(x) ((x).beg)
#define XPsize(x) ((x).cur - (x).beg)

#define XPclose(x) (void**) aresize((void*)(x).beg, \
sizeofN(void*, XPsize(x)), ATEMP)

#define XPfree(x) afree((void*) (x).beg, ATEMP)



  3 Responses to “Category : OS/2 Files
Archive   : KSH48.ZIP
Filename : EXPAND.H

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/