Code:
#include<stdlib.h>
#include<stdio.h>
#include<sys/stat.h>
int CLenFrom(char *AString,int From);//W
int CPos(char *AString,char *SubString, int Skips);//W
int CLen(char *AString);//W
void CCopy(char *AString,int FromPos_Main,int ToPos,char *ResultBuffer,int FromPos_Result,int IncludeEOL);//W
/*
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Even though a call like CCopy(A,0,0,A,0,1) Seems to work it should not be used.
Saving to the source buffer causes confusion in these functions.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
*/
void CReplaceFrom(char *AString,char *SubString,int FromPos,char *ResultBuffer);//W
void CInsert(char *AString,char *SubString,int AtPos,char *ResultBuffer);//W
void CDelete(char *AString,int FromPos,int ToPos,char *ResultBuffer);//W
int FileExists(char *AString);//W
/*Stat Returns MetaData Of A File In A stat Structure*/
int FileExists(char *AString)
{
return stat(AString,NULL) == 0; //I have no idea if NULL will cause a memory leak.
}
void CDelete(char *AString,int FromPos,int ToPos,char *ResultBuffer)
{
if(FromPos==0)
FromPos==1;
CCopy(AString,0,FromPos-1,ResultBuffer,0,0);
CCopy(AString,ToPos+1,CLen(AString),ResultBuffer,FromPos,1);
}
void CInsert(char *AString,char *SubString,int AtPos,char *ResultBuffer)
{
int Length_SubString = CLen(SubString);
CCopy(AString,0,AtPos,ResultBuffer,0,0);
CCopy(SubString,0,Length_SubString,ResultBuffer,AtPos,0);
CCopy(AString,AtPos,CLen(AString),ResultBuffer,AtPos+Length_SubString,1);
}
void CReplaceFrom(char *AString,char *SubString,int FromPos,char *ResultBuffer)
{
CCopy(AString,0,FromPos,ResultBuffer,0,0); //Stores all chars up and to the replace point in the ResultBuffer
int i = FromPos;
int Length_SubString = CLen(SubString);
int Length_String = CLen(AString);
int j=0;
for(i;j!=Length_SubString;i++)
{
ResultBuffer[i]=SubString[j++];
}
if(AString[i]=='\0')
{
ResultBuffer[i]='\0';
return;
}
else
{
i--;//A Step Back
for(i++;i!=Length_String;i++)
{
ResultBuffer[i]=AString[i];
}
ResultBuffer[i]='\0';
}
}
void CCopy(char *AString,int FromPos_Main,int ToPos,char *ResultBuffer,int FromPos_Result,int IncludeEOL)
{
//This function does not malloc / realloc / calloc / free;
int i = FromPos_Main;
int j=FromPos_Result;
if (ToPos<FromPos_Main)
return;
for(i;i!=ToPos+1;i++)
{
ResultBuffer[j++]=AString[i];
}
if (IncludeEOL)
ResultBuffer[j++]='\0';
return;
}
int CPos(char *AString,char *SubString, int Skips)
{
int Length_SubString=CLen(SubString);
int Length_String=CLen(AString);
int i = 0;
int Skipped = 0;
int j;
int Matches;
for(i;i!=Length_String;i++) //I will look for the /0 char myself.
{
j=0;
Matches=0;
if (AString[i]==SubString[j])
{
Matches++;
while((j!=Length_SubString) && (Matches!=Length_SubString))
{
j++;
if(AString[j+i]!=SubString[j])
{break;}
else
{Matches++;}
}
if (Matches==Length_SubString)
{
Skipped++;
if (Skipped>Skips)
return Length_SubString-j+i-1;
}
else
i=i+j-1;
}
}
return -1;
}
int CLen(char *AString)
{
int i = CLenFrom(AString,0);
return i;
}
int CLenFrom(char *AString,int From)
{
int i = From;
for(i;;i++)
{
if (AString[i]=='\0')
{break;}
}
return i-From;
}
I started programming in delphi which has a copy , insert and delete function for working with strings. They make string handling pretty easy so I decided to rewrite them in C.