[Code Library] C Implementation of Visual Basic 6 Trim Function

/*
http://devel.archefire.org/forum/viewtopic.php?t=2409

Compile with GCC:
gcc LTrim.c -o LTrim.exe

Compile with Open Watcom (Windows):
wcl386 -bt=nt LTrim.c

Compile with Open Watcom (DOS):
wcl LTrim.c

*/

////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////

include <stdio.h> //printf

include <stdlib.h> //malloc, realloc, free

include <string.h> //strlen, strcpy

void Trim(unsigned char **string);

////INIT: void Trim(unsigned char **string);
////INIT: void Trim(unsigned char **string);
////INIT: void Trim(unsigned char **string);
////INIT: void Trim(unsigned char **string);
////INIT: void Trim(unsigned char **string);
////INIT: void Trim(unsigned char **string);
////INIT: void Trim(unsigned char **string);
////INIT: void Trim(unsigned char **string);

void Trim(unsigned char **string)
{
unsigned long string_length;
unsigned long string_offset_pointer;
unsigned long string_offset_rebase=0;

if(string==NULL)return;
string_length=strlen((char *)
string);
if(string_length==0)return;

string_offset_pointer=0;

//Find the position of the first non-blank
//at the leftmost of the string, from the
//start of the string at offset 0:
///
while(string_offset_pointer<string_length)
{
if(
(string+string_offset_pointer)!=' ' &&
(string+string_offset_pointer)!='\t'
)
{
break;
}

string_offset_pointer++;
}

//Copy the non-blank characters on the
//previously starting blank ones:
///
while(string_offset_pointer<string_length)
{
(string+string_offset_rebase)=
(string+string_offset_pointer);

string_offset_rebase++;
string_offset_pointer++;
}

//Place a new terminating null byte:
///
(string+string_offset_rebase)=0;

//Now trim the characters at the rightmost
//of the string, from the very end offset
//of it:
///
string_length=strlen((char )string);
if(string_length==0)return;

string_offset_pointer=string_length-1;

while(string_offset_pointer!=0)
{
if(
(string+string_offset_pointer)!=' ' &&
(string+string_offset_pointer)!='\t'
)
{
break;
}

string_offset_pointer--;
}

//Place a new terminating null byte
//to get rid of the rightmost spaces.
//With this we have fully trimed the string:
///
(string+string_offset_pointer+1)=0;

//Release the now-unused string area that only
//contained spaces:
///
realloc(string,strlen((char *)string+1));
}

////END: void Trim(unsigned char **string);
////END: void Trim(unsigned char **string);
////END: void Trim(unsigned char **string);
////END: void Trim(unsigned char **string);
////END: void Trim(unsigned char **string);
////END: void Trim(unsigned char **string);
////END: void Trim(unsigned char **string);
////END: void Trim(unsigned char **string);

unsigned char *_string=NULL;

int main(void)
{
// _string=malloc(strlen(" LTrimRtrim ")+1);
// strcpy((char *)_string, (const char *)" LTrimRtrim ");

_string=malloc(strlen(" ")+1);
strcpy((char *)_string, (const char *)" ");

printf("{%s}\n",_string);
Trim(&_string);
printf("{%s}\n",_string);

return 0;
}

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion