auxiliar.c 5.25 KB
#include "auxiliar.h"

char* substring(char *string, uint8_t start, uint8_t end)
{
        uint8_t size = end - start;
        char *substr;
        substr = (char*) malloc(sizeof(char)*(size + 1));
        memset(substr,0x00,size+1);
        strncpy(substr,string+start,size);
        return substr;
}

int next_occurrence(char *string, char character)
{
        int i;
        int pos = -1;
        for(i = 0; i < strlen(string); i++)
        {
                if(string[i] == character)
                {
                        pos = i;
                        break;
                }
        }
        return pos;
}

int num_occurrences(char *string, char character)
{
        int i;
        int count = 0;
        for(i = 0; i < strlen(string); i++)
        {
                if(string[i] == character)
                {
                        count++;
                }
        }
        if(!count)
        {
                return -1;
        }
        return count;
}

void error()
{
        printf("error");
        exit(-1);
}

char** comma_separate(char *input)
{
        int i;
        int next_pos = 0;
        int pos = 0;
        int input_num = num_occurrences(input,',');
        if(input_num <= 0)
        {
                return NULL;
        }
        char **inputs;
        inputs = (char**) malloc(sizeof(char*) * (input_num+1));
        for(i = 0; i < input_num; i++)
        {
                next_pos += next_occurrence(input+pos,',');
                if(next_pos == -1)
                {
                        return NULL;
                }
                inputs[i] = substring(input,pos,next_pos);
                next_pos++;
                pos = next_pos;
        }
        return inputs;
}

void free_mem(char **inputs, int num_inputs)
{
        int i;
        for(i = 0; i < num_inputs; i++)
        {
                free(inputs[i]);
        }
        free(inputs);
}	

int last_occurrence(char *string, char character)
{
	int i;
	int pos = -1;
	for(i = 0; i < strlen(string); i++)
	{
		if(string[i] == character)
		{
			pos = i;
		}
	}
	return pos;
}

void normalize(char **inputs, int num_inputs)
{
        int i;
        int j;
        int length;

        for(i = 0; i < num_inputs; i++)
        {
                length = strlen(inputs[i]);             
                for(j = 0; j < length; j++)
                {
                        if(inputs[i][j] == '0')
                        {
                                inputs[i][j] = 0;
                        }
                        else
                        {
                                inputs[i][j] = 1;
                        }
                }
        }
}

int bin2dec(char *bin)
{
        int i;
        int num = 0;
        int factor = 1;
        int length = strlen(bin);
        int pos = length - 1;
        char *aux = (char*) malloc(sizeof(char) * (length+1));
        memset(aux,0x00,length+1);
        strcpy(aux,bin);
        if(aux[0] == '1')
        {       
                reverse_two_complement(aux);
                factor = -1;
        }
        normalize(&aux,1);
        for(i = 0; i < length; i++)
        {
                if(aux[pos--])
                {
                        num += 0x1 << i;
                }
        }
        num *= factor;
        free(aux);
        return num;
}

int bin2udec(char *bin)
{
        int i;
        int num = 0;
        int length = strlen(bin);
        int pos = length - 1;
        char *aux = (char*) malloc(sizeof(char) * (length+1));
        memset(aux,0x00,length+1);
        strcpy(aux,bin);
        normalize(&aux,1);
        for(i = 0; i < length; i++)
        {
                if(aux[pos--])
                {
                        num += 0x1 << i;
                }
        }
        free(aux);
        return num;
}

char* dec2bin(int dec, int length)
{
        char *bin = (char*) malloc(sizeof(char) * (length + 1));
        memset(bin,0x00,length+1);
        int pos = length-1;
        int i;
        for(i = 0; i < length; i++)
        {
                if(dec & 0x1)
                {
                        bin[pos--] = '1';
                }
                else
                {
                        bin[pos--] = '0';
                }
                dec >>= 1;
        }
        return bin;
}


void expand_input(char **input, int required_length)
{
        char *new_input = (char*) malloc(sizeof(char)*(required_length + 1));
        memset(new_input,0x00,required_length + 1);
        memset(new_input,'0',required_length+1);
        int offset = required_length - strlen(*input);
        strcpy(new_input + offset,*input);
        free(*input);
        *input = new_input;
}

int str2int(char *string)
{
        int i;
        int num = 0;
        int mul = 1;
        for(i = 0; i < strlen(string); i++)
        {
                num += mul*(string[i] - 48);
                mul *= 10;
        }
        return num;
}

void reverse_two_complement(char *bin)
{
        int i;
        for(i = 0; i < strlen(bin); i++)
        {
                if(bin[i] == '0')
                {
                        bin[i] = '1';
                }
                else
                {
                        bin[i] = '0';
                }
        }
        char *bin_added = dec2bin(bin2dec(bin)+1,strlen(bin));
        strcpy(bin,bin_added);
        free(bin_added);
}