esotericbot-0.0.1: c_plugins/plugin++.cpp
#include "plugin.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void mem_error( ){
perror( "Out of memory\n" );
exit( 1 );
}
static struct char_lock strtail( struct char_lock * strl ){
struct char_lock new_strl;
new_strl.alive = 0;
new_strl.str = NULL;
if( strl->alive ){
char * str = strl->str;
int str_len = strlen( str );
if( str_len > 1 ){
new_strl.str = ( char * ) calloc( str_len - 1 , sizeof( char ) );
if( new_strl.str == NULL ){
mem_error( );
}
strcat( new_strl.str , str + 1 );
new_strl.alive = 1;
}
}
return new_strl;
}
struct char_lock * get_input( int argc , char ** argv ){
char * argv1;
struct char_lock * input = ( struct char_lock * ) malloc( sizeof( char_lock ) );
if( input == NULL ){
mem_error( );
}
input->alive = 0;
input->str = NULL;
argv1 = argv[ 1 ];
if( argc > 1 ){
input->str = ( char * ) calloc( strlen( argv1 ) , sizeof( char ) );
if( input->str == NULL ){
mem_error( );
}
strcat( input->str , argv1 );
input->alive = 1;
}
return input;
}
char uncons( struct char_lock * input ){
struct char_lock new_strl;
char the_char;
if( !( input->alive ) ){
fputs( "...ran out of input" , stdout );
exit( 0 );
}
the_char = input->str[ 0 ];
new_strl = strtail( input );
if ( new_strl.alive ){
char * new_str;
input->alive = 1;
new_str = new_strl.str;
free( input->str );
input->str = ( char * ) calloc( strlen( new_str ) , sizeof( char ) );
if( input->str == NULL ){
mem_error( );
}
strcat( input->str , new_str );
free( new_strl.str );
} else {
input->alive = 0;
}
return the_char;
}