//=================
//Bstring Functions
//=================
#include <stdio.h>
#include <stdlib.h>
typedef char * bstring;
//------------------------
bstring NewString(int n)
//========================
{
int *p;
*p = n;
p++;
return (char*) p;
}
//------------------
int Len(bstring s)
//==================
{
int * p=(int*) s;
p--;
return *p;
}
//----------------------------------------
void FillString(bstring s, int n, int c)
//========================================
{
int i;
for (i=0 ; i<n ; i++)
{
*s = (char) c;
s++;
}
}
//-------------------------
bstring NullString(int n)
//=========================
{
int *p;
*p = n;
p++;
FillString((bstring) p,n+2,0);
return (bstring) p;
}
//-------------------------
int FreeString(bstring s)
//=========================
{
return 0;
}
//------------------------------------
bstring Mid(bstring s, int i, int l)
//====================================
{
bstring t;
bstring u;
int le;
int les=Len(s);
int j;
if (i<0)
{
i=les+1+i; //offset from the right
}
le=l+i-1;
if (le>les)
{
l=les-i+1;
}
if (l<0) l=0;
t=NullString(l);
if (i>les) return t;
u=t;
s+=i-1;
for (j=0 ; j<=l ; j++) // includes boundary null
{
*u++ = *s++;
}
return t;
}
//------------------------------
bstring Left(bstring s, int i)
//==============================
{
return Mid(s,1,i);
}
//-------------------------------
bstring Right(bstring s, int i)
//===============================
{
return Mid(s,-i,Len(s));
}
//------------------
bstring Chr(int i)
//==================
{
bstring s=NewString(1);
*s=(char) i;
s[1]=(char) 0;
return s;
}
//--------------------------
short Asc(bstring s,int i)
//==========================
{
long le=Len(s);
if (i<0)
{
i=le+i+1; //offset from the right
}
if (i<1) return 0;
if (i>le) return 0;
i--;
return (short) s[i] & 0xff;
}
//-----------
int main ()
//===========
{
int i,n;
bstring s,t;
printf ("How long do you want the string? ");
s=NullString(i);
t=NullString(i);
for (n=0; n<i; n++)
s[i]='\0';
t=Mid(s,-2,64);
printf ("Random string: %s, %s, %i, %s \n",s
, t
, Asc
(t
,2), Chr
(65) ); FreeString (s);
FreeString (t);
return 0;
};