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

edited March 2018 in Beginner C/C++

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

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 LTrim(unsigned char **string);

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

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

if(*string==NULL)return;

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

//Detect the first leftmost non-blank
//character:
///
while(string_offset_pointer<string_length)
{
if(
(string+string_offset_pointer)!=' ' &&
(string+string_offset_pointer)!='\t'
)
{
break;
}

string_offset_pointer++;
}

//Copy the non-blank spaces over the
//originally blank spaces at the beginning
//of the string, from the first non-blank
//character up to the string length:
///
while(string_offset_pointer<string_length)
{
(string+string_offset_rebase)=
(string+string_offset_pointer);

string_offset_rebase++;
string_offset_pointer++;
}

//Terminate the newly-copied substring
//with a null byte for an ASCIIZ string.
//If the string was fully blank we will
//just get an empty string:
///
(string+string_offset_rebase)=0;

//Free the now unused part of the
//string. It assumes that realloc()
//will keep the current contents of our
//memory buffers and will just truncate them,
//like in this case where we are requesting
//to shrink the buffer:
///
realloc((char )string,strlen((char )string)+1);
}

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

unsigned char *_string=NULL;

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

printf("{%s} %p\n",_string,_string);
LTrim(&_string);
printf("{%s} %p\n",_string,_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