/*****************************************************************************
File: tools.c
Author: Salas Lopez, Jorge		
Date:   29/03/2004
Description: Utils for applications
Invocation:  ---
*****************************************************************************/

/*****************************************************************************	  
   Table of Contents:
   Reusable Functions:      
	  int Append(char **str_1, char *str_2);
	  int Append_withSpace(char **str_1, char *str_2);	  
   Intern Functions:
      ---
   Includes:
    stdio.h; printf
	  stdlib.h; malloc, realloc
	  string.h; strlen, strcpy, strncpy
  	tools.h; utils	  	  	  	  	 
   Source code in this file:
      Headers
*********************************************************************************/

/********************************************************************************
Import Libraries .
*********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tools.h"

/********************************************************************************
Reusable Function.
   int Append(char **str_1, char *str_2);
   Purpose: Concat two string.
   Limitations: Nothing
   Returns: if an error occurs return FALSE
            else return TRUE
*********************************************************************************/
int Append(char **str_1, char *str_2)
{																			
	int t_str_1 = 0;
	int t_str_2 = 0;	

	if (str_2 != NULL){
		// Destination string is not null
		if (*str_1 != NULL){
			// Realloc destination string and concat source string
			t_str_1 = strlen(*str_1);
			t_str_2 = strlen(str_2) + EOF_SIZE;			
			*str_1 = realloc(*str_1, t_str_1 + t_str_2);
			if (*str_1 != NULL){
				strncpy(*str_1+t_str_1, str_2, t_str_2);				
			}
									
		}else{
			// Allocate destination string and copy source string
			MallocSize(*str_1, char, (strlen(str_2)+EOF_SIZE));
			if (*str_1 == NULL){return FALSE;}
			strcpy(*str_1, str_2);			
		}				
		
	}
	return TRUE;
}
 
/********************************************************************************
Reusable Function.
   int Append_withSpace(char **str_1, char *str_2);
   Purpose: Concat two string adding one space between both.
   Limitations: Nothing
   Returns: if an error occurs return FALSE
            else return TRUE
*********************************************************************************/
int Append_withSpace(char **str_1, char *str_2)
{	
	int t_str_1 = 0;
	int t_str_2 = 0;

	if (str_2 != NULL){
		// Destination string is not null
		if (str_1 != NULL){
			// Realloc destination string and concat one space and source string
			t_str_1 = strlen(*str_1) + EOF_SIZE;
			t_str_2 = strlen(str_2) + EOF_SIZE;
			*str_1 = realloc(*str_1, (sizeof(char)*(t_str_1 + t_str_2 + 1)));
			if (*str_1 != NULL){
				strncpy(*str_1+t_str_1-EOF_SIZE, " ", 1);
				strncpy(*str_1+t_str_1, str_2, t_str_2);
			}									
		}else{
			// Allocate destination string and copy source string
			MallocSize(*str_1, char, (strlen(str_2)+EOF_SIZE));
			if (*str_1 == NULL){return FALSE;}
			strcpy(*str_1, str_2);			
		}				
		
	}
	return TRUE;
}
