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

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

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

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

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

*/

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

include <stdio.h> //printf

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

include <string.h> //strlen, strcpy

void RTrim(unsigned char **string);

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

void RTrim(unsigned char **string)
{
unsigned long string_length;
unsigned long string_offset_pointer;

//We need to get the ASCIIZ
//string length. We have a double pointer,
//the first pointer level points to the
//address of our passed external
//char * variable argument,
//and the second points to the actual string
//chars held, but here we need the address of the
//external char * as we need to evaluate
//from the actual base address of the
//string's buffer pointer:
///
string_length=strlen((const char)string);

if(/* we were passed a / string_length / of /==0 ||
/
or if the pointer to a / *string
/
points instead to /==NULL)
{
/
we need to */ return;
//immediately since we have no valid data
//to work on.
}

/* Now for the loop we need to
set the start of the / string_offset_pointer=
/
to / string_length-1;/. That will make us
point to the very last character in the string
before the terminating null byte of the ASCIIZ string. */

while(string_offset_pointer)
{
if(
//the current string position contains a
//blank space or a tab...
(unsigned char)(string+string_offset_pointer)!=' ' &&
(unsigned char)(string+string_offset_pointer)!='\t'
)
{
/* ...then */break;
}

//If we get here it was because the
//current character was indeed a blank space
//or a tab and now we need to decrease in 1
/* the / string_offset_pointer--; / with that
we will see if the character before the current one
is also a blank space or tab, in the next iteration.
We are evaluating from the end to the start of the string. */
}

//Overwrite the first blank character
//at the rightmost position of the string
//with a byte with 0 value to make it
//end before any rightmost spaces are found.
//Thiw is what actually causes the RTrim effect
//(rightmost trim of spaces/tabs).
//
//With (string) we get the value of our
//original external string pointer.
//
//With (
string+string_offset_pointer+1) we get
//the byte position of our last non-blank
//character.
//
//With (string+string_offset_pointer+1) we access
//the byte position after our last non-blank character.
//At that position we currently have the first blank space
//or tab, and that's precisely where we want to terminate
//our string given by (unsigned char)0.
///
(string+string_offset_pointer+1)=(unsigned char)0;

/Now we want to */ realloc/ate the memory for the
string so that it doesn't keep memory we are no longer
using for the rightmost blank spaces. It will effectively
truncate the string.

Here we assume that realloc() will always keep our current
memory contents intact. */
(
/** Parameters for Resizing the String Buffer **/

/**/ (void )string, /*-- We will point to the
location of our actual buffer.
Here we deal wit the actual value
that points to the base address
of our string buffer. */

/**/ strlen((const char)string)+1 /-- We will reserve as many bytes
as what we marked with our new ASCIIZ
terminator plus the terminating zero byte,
that we marked with the line:
*(
string+string_offset_pointer+1)=(unsigned char)0;
*/
);
}

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

unsigned char *string1=NULL;

int main(void)
{
//I have to dynamically allocate the string
//and copy it to the allocated memory so that
//the program doesn't crash when compiled with
//some versions of GCC:
///
string1=(unsigned char )malloc(strlen("abc \t ")+1);
strcpy((char
)string1,(const char*)"abc \t ");

printf("Original: {%s}\n",string1);

RTrim(&string1);

printf("RTrimmed: {%s}\n",string1);

free(string1);
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