Files
2025-11-17 10:28:09 +01:00

171 lines
2.0 KiB
C
Executable File

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <array.h>
#include <dirent.h>
#include <ctype.h>
#include <string.h>
class char{
int operator==( char * b ) {
return this->compare( b );
}
void operator+=( char * b ) {
// not proper
strcat( this, b );
}
char * operator+( char * b ) {
return this->concatenate( b );
}
int compare( char * b ) {
return strcmp( this, b ) == 0;
}
char * concatenate( char * b ) {
int lengthA = strlen( this );
int lengthB = strlen( b );
char * pointer = this;
char * copy = ( char * ) malloc( ( lengthA + lengthB + 1 ) * sizeof( char ) );
int idx = 0;
while ( * pointer != '\0' ){
copy[idx++] = *pointer++;
}
pointer = &b[0];
while ( * pointer != '\0' ){
copy[idx++] = *pointer++;
}
copy[ idx++ ] = '\0';
return &copy[0];
}
int includes( char * compare ) {
if ( strstr( this, compare ) != NULL ) {
return 1;
} else {
return 0;
}
}
char * clone( ) {
char * newCopy = malloc( sizeof( char ) * strlen( this ) );
strcpy( newCopy, this );
return newCopy;
}
char * copy( ) {
char * newCopy = malloc( sizeof( char ) * strlen( this ) );
strcpy( newCopy, this );
return newCopy;
}
struct array * split( char * needle )
{
char * haystack = this->clone();
struct array * keys = new array();
int count = 0;
char * tmp = haystack;
char * token = strtok( haystack, needle );
int i = 0;
while ( token ) {
array_add( keys, token );
//printf("strtok: %s \n", token );
token = (char *) strtok( NULL, needle );
}
return keys;
}
char * removeWhitespaceLeft()
{
char * s = this;
while(isspace(*s)) s++;
return s;
}
char * removeWhitespaceRight()
{
char * s = this;
char* back = s + strlen(s);
while( isspace( *--back ) );
*( back + 1 ) = '\0';
return s;
}
char * removeWhitespace( )
{
return this->removeWhitespaceLeft()->removeWhitespaceRight();
}
}