char (string) array functions
I'm an old fart basic programmer trying to upgrade my programming knowledge in c and c++.
I have recently forked a long standing Basic -> C translator (BCX).
I decided to start investigating the runtime library starting with the BASIC char string functions.
I first want to establish if they are viable entities as perceived by the talented programmers here.
If not do you see a better way to implement.
I then would like to extend this to the c++ Programing Board for the same functions using std::string.
Thank you for yout time.
James
First up is LEFT$
================================================== ============================
Purpose: LEFT$ returns the leftmost substring of a string
Syntax:
SubStr$ = LEFT$(MainStr$, Length%)
Parameters:
MainStr$ String from which left substring is to be copied.
Length% Leftmost substring length.
Return Value:
SubStr$, the return value, is a substring from Main$.
================================================== ============================
Code:
#ifndef BCXTmpStrSize
#define BCXTmpStrSize 2048
#endif
char *BCX_TmpStr (size_t Bites,size_t iPad,int iAlloc)
{
static int StrCnt;
static char *StrFunc[BCXTmpStrSize];
StrCnt=(StrCnt + 1) & (BCXTmpStrSize-1);
if(StrFunc[StrCnt]) {free (StrFunc[StrCnt]); StrFunc[StrCnt] = NULL;}
#if defined BCX_MAX_VAR_SIZE
if(Bites*sizeof(char)>BCX_MAX_VAR_SIZE)
{
printf("Buffer Overflow caught in BCX_TmpStr - requested space of %d EXCEEDS %d\n",(int)(Bites*sizeof(char)),BCX_MAX_VAR_SIZE);
abort();
}
#endif
if(iAlloc) StrFunc[StrCnt]=(char*)calloc(Bites+128,sizeof(char));
return StrFunc[StrCnt];
}
char *left (const char *S, int length)
{
register int tmplen = strlen(S);
if(length<1) return BCX_TmpStr(1,128,1);
if(length<tmplen) tmplen=length;
char *strtmp = BCX_TmpStr(tmplen,128,1);
return (char*)memcpy(strtmp,S,tmplen);
}