'libbase' provides foundational functionality and is intended as a substitute and replacement for glibc.

Specific functionality includes:

Array

Example usage: Array

Class Definition

#ifndef LIBBASE_ARRAY_H
#define LIBBASE_ARRAY_H

typedef struct _Array
{
    void** objects;
    int    length;
    int    size;

} Array;

Array* Array_new();
Array* Array_free   ( Array** self );
Array* Array_push   ( Array*  self, void** object );
void*  Array_shift  ( Array*  self );
Array* Array_unshift( Array*  self, void** object );
int    Array_length ( Array*  self );

#endif

Supporting private functions

#include <stdlib.h>
#include "libtokenizer/Array.h"
#include "libtokenizer/Runtime.h"

void Array_expand( Array* self )
{
    if ( 0 == self->size )
    {
        self->objects = (void**) Runtime_calloc( 1, sizeof( void* ) );
        self->size    = 1;
    }
    else
    {
        int new_size = self->size * 2;

        void** tmp = (void**) Runtime_calloc( new_size, sizeof( void* ) );

        for ( int i=0; i < self->length; i++ )
        {
            tmp[i] = self->objects[i];
        }

        Runtime_free( self->objects );

        self->objects = tmp;
        self->size    = new_size;
    }
}

Constructors

Array* Array_new()
{
    return Runtime_calloc( 1, sizeof( Array ) );
}

Deconstructors

Array* Array_free( Array** _self )
{
    Array* self = *_self;

    if ( self )
    {
        if ( self->objects ) Runtime_free( self->objects );

        self->objects = 0;
        self->length  = 0;
        self->size    = 0;

        self = Runtime_free( self );
    }
    return 0;
}

Array.push

Array* Array_push( Array* self, void** object )
{
    if ( self->length == self->size )
    {
        Array_expand( self );
    }

    self->objects[self->length++] = *object;

    return self;
}

Array.shift

void* Array_shift( Array* self )
{
    if ( self->length )
    {
        void* head = self->objects[0];

        for ( int i=1; i < self->length; i++ )
        {    
            self->objects[i-1] = self->objects[i];
            self->objects[i]   = 0;
        }
        self->length--;
        return head;
    }
    else
    {
        return NULL;
    }
}

Array.unshift

Array* Array_unshift( Array* self, void** _object )
{
    void* object = *_object;

    if ( self->length == self->size )
    {
        Array_expand( self );
    }

    for ( int i=self->length; 0 < i; i-- )
    {    
        self->objects[i]   = self->objects[i-1];
        self->objects[i-1] = 0;
    }
    self->objects[0] = object;

    self->length++;

    return self;
}

Array.length

int Array_length( Array* self )
{
    return self->length;
}

Base

#ifndef LIBTOKENIZER_BASE_H
#define LIBTOKENIZER_BASE_H

#ifndef bool
#define bool int
#endif

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#endif
#ifndef LIBTOKENIZER_STRING_H
#define LIBTOKENIZER_STRING_H

#include "libtokenizer/Base.h"

typedef struct _String
{
    char* content;
    int   length;

} String;

String* new_String( const char* content );
String* free_String( String* self );

const char* String_content( const String* self );
int         String_length ( const String* self );
String*     String_copy   ( const String* self );
String*     String_cat    ( const String* self, const String* other );
bool        String_equals ( const String* self, const String* other );

int   StringLength( const char* s                  );
char* StringCopy  ( const char* s                  );
char* StringCat   ( const char* s1, const char* s2 );
bool  StringEquals( const char* s1, const char* s2 );

#endif
#include <stdlib.h>
#include <string.h>

#include "libtokenizer/Runtime.h"
#include "libtokenizer/String.h"

String* new_String( const char* content )
{
    String* self = Runtime_calloc( 1, sizeof( String ) );

    if ( self )
    {
        self->content = StringCopy  ( content );
        self->length  = StringLength( content );
    }
    return self;
}

String* free_String( String* self )
{
    if ( self )
    {
        free( self->content ); self->content = 0;
        self->length = 0;

        self = Runtime_free( self );
    }
    return self;
}

const char* String_content( const String* self )
{
    return self->content;
}

int String_length( const String* self )
{
    return self->length;
}

String* String_copy( const String* self )
{
    return new_String( self->content );
}

String* String_cat( const String* self, const String* other )
{
    char* tmp = StringCat( self->content, other->content );
    String* ret = new_String( tmp );
    free( tmp );

    return ret;
}

bool String_equals( const String* self, const String* other )
{
    return StringEquals( self->content, other->content );
}

int StringLength( const char* s )
{
    return strlen( s );
}

char* StringCopy( const char* s )
{
    int   len  = StringLength( s ) + 2;
    char* copy = calloc( len, sizeof( char ) );

    return strcpy( copy, s );
}

char* StringCat( const char* s1, const char* s2 )
{
    int len1 = StringLength( s1 );
    int len2 = StringLength( s2 );
    int len  = len1 + len2 + 1;

    char* concatenated = calloc( len, sizeof( char ) );

    int t=0;

    for ( int i=0; i < len1; i++ )
    {
        concatenated[t++] = s1[i];
    }

    for ( int i=0; i < len2; i++ )
    {
        concatenated[t++] = s2[i];
    }

    concatenated[t] = '\0';

    return concatenated;
}

bool StringEquals( const char* s1, const char* s2 )
{
    return (0 == strcmp( s1, s2 ));    
}
public class
{




}
#ifndef LIBTOKENIZER_INPUTSTREAM_H
#define LIBTOKENIZER_INPUTSTREAM_H

typedef struct _InputStream
{
    const char* filepath;
    void* f;

} InputStream;

InputStream* new_InputStream ( const char*  filepath );
InputStream* free_InputStream( InputStream* self     );
int          InputStream_read( InputStream* self     );

#endif
#include <stdio.h>

#include "Libtokenizer/InputStream.h"
#include "Libtokenizer/String.h"

InputStream* new_InputStream( const char* filepath )
{
    InputStream* self = calloc( 1, sizeof( InputStream ) );

    if ( self )
    {
        self->filepath = StringCopy( filepath );
    }
    return self;
}
function Enum( array )
{
    for ( var i in array )
    {
        this[array[i]] = array[i];
    }
}

File

C

#ifndef LIBTOKENIZER_FILE_H
#define LIBTOKENIZER_FILE_H

#include "libtokenizer/Base.h"

bool  File_Exists      ( const char* filepath );
char* File_Get_Contents( const char* filepath );

#endif
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>

#include "libtokenizer/File.h"

char* readline( FILE* stream );

bool File_Exists( const char* filepath )
{
    struct stat buf;

    return (0 == lstat( filepath, &buf ));
}

char* File_Get_Contents( const char* filepath )
{
    char* content = NULL;
    FILE* fp      = fopen( filepath, "r" );

    if ( fp )
    {
        struct stat buf;

        if( 0 == lstat( filepath, &buf ) )
        {
            int size = buf.st_size;

            content = calloc( size + 1, sizeof( char ) );

            int red = fread( content, size, 1, fp );
        }
    }
    return content;
}
#include <stdlib.h>
#include <stdio.h>

#include "libtokenizer/File.h"

int main( int argc, char** argv )
{
    const char* filepath = "./source/mt/1-Overview.txt";

    if ( ! File_Exists( filepath ) )
    {
        fprintf( stderr, "Could not file: %s\n", filepath );
        fflush( stderr );
    }
    else
    {
        char* content = File_Get_Contents( filepath );

        fprintf( stdout, "%s\n", content );

        free( content );
    }

    return 0;
}
#ifndef LIBTOKENIZER_RUNTIME_H
#define LIBTOKENIZER_RUNTIME_H

#include <stdlib.h>

void* Runtime_calloc( size_t count, size_t size );
void* Runtime_free  ( void* ptr  );
int   Runtime_allocated();

#endif
#include <stdlib.h>
#include "libtokenizer/Base.h"
#include "libtokenizer/Runtime.h"

static int allocated = 0;

void* Runtime_calloc( size_t count, size_t size )
{
    allocated++;

    return calloc( count, size );
}

void* Runtime_free( void* ptr  )
{
    allocated--;

    free( ptr );

    return NULL;
}

int Runtime_allocated()
{
    return allocated;
}

Term

Class Definitions

#ifndef LIBTOKENIZER_TERM_H
#define LIBTOKENIZER_TERM_H

#define COLOR_NORMAL   "\033[00m"
#define COLOR_BOLD     "\033[01m"
#define COLOR_LIGHT    "\033[02m"
#define COLOR_STRING   "\033[33m"
#define COLOR_TYPE     "\033[36m"
#define COLOR_MODIFIER "\033[94m"
#define COLOR_VALUE    "\033[33m"
#define COLOR_CHAR     "\033[33m"
#define COLOR_COMMENT  "\033[32m"
#define COLOR_UNKNOWN  "\033[41m"

void Term_Colour( void* stream, const char* color );

#endif

Term.Colour

#include <stdio.h>

void Term_Colour( void* stream, const char* color )
{
    fprintf( stream, "%s", color );
}