/*
 * File:   string_ops.c
 * Author: Christian
 *
 * Created on 15 de abril de 2019, 18:46
 */


#include "string_ops.h"


char* p_llint(char* buffer, long long int val, char endWithNull, char* max)
{
	if(max-buffer > 0){
        int i=0;
        char aux[20];
        long long int v = (val<0)?(-val):val;
        while(v>0 && i<20){
            aux[i++] =(char) (v%10)+48; 
            v/=10;
        } 
        if(i==0){*buffer = '0'; i=1; return 0;}  
        if(val<0){*buffer = '-'; buffer++;} 
        if(endWithNull){*(buffer+i)='\0';}

        
        for(i-=1; i>=0; i--){
            *buffer++ = aux[i];
            if(buffer==max)break;
        }
	    return buffer;
	}
    return buffer;
}
char* p_lint(char* buffer, long int val, char endWithNull, char* max)
{
	if(max-buffer > 0){
        int i=0;
        char aux[10];
        long int v = (val<0)?(-val):val;
        while(v>0 && i<10){
            aux[i++] =(char) (v%10)+48; 
            v/=10;
        } 
        if(i==0){
            *buffer = '0'; 
            if(endWithNull) *(buffer+1) = 0;
            return 0;
        }  
        if(val<0){*buffer = '-'; buffer++;} 
        if(endWithNull){*(buffer+i)='\0';}
        
        for(i-=1; i>=0; i--){
            *buffer++ = aux[i];
            if(buffer==max)break;
        }
	    return buffer;
	}
    return buffer;
}
char* p_int(char* buffer, int val, char endWithNull, char* max)
{
	if(max-buffer > 0){
	    int i=0;
	    char aux[6];
	    int v = (val<0)?(-val):val;
	    while(v>0 && i<6){
	        aux[i++] =(char) (v%10)+48; 
	        v/=10;
	    } 
	    if(i==0){
	        *buffer = '0'; 
	        if(endWithNull) *(buffer+1) = 0;
	        return 0;
	    }  
	    if(val<0){*buffer = '-'; buffer++;} 
	    if(endWithNull){*(buffer+i)='\0';}
	    
	    for(i-=1; i>=0; i--){
	        *buffer++ = aux[i];
	        if(buffer==max)break;
	    }
	    return buffer;
	}
    return buffer;
}
char* p_int_fl(char* buffer, int val, char len, char endWithNull, char* max)
{
	if(max-buffer){
	    int i;
		int j= len-1<(max-buffer)?len-1:(max-buffer);
	    int v= (val<0)?(-val):val;
	    
	    for(i = j; i>0; i--){
	        if(v){j--;}
	        buffer[i] = v ? (char) (v%10)+48 : ' '; 
	        v/=10;
	    }
	    buffer[i] = v ? (char) (v%10)+48 : ' ';
	    if(val<0){buffer[j] = '-';}
	    else if(val==0){buffer[j] = 48;}
	    
	    if(endWithNull){buffer[(int)len]='\0';}
	    return len-1<(max-buffer)?buffer+(len-1):max;
	}
	return buffer;
}
char* p_lint_fl(char* buffer, long int val, char len, char endWithNull, char* max)
{
	if(max-buffer){
        int i;
        int j= len-1<(max-buffer)?len-1:(max-buffer);
        int v= (val<0)?(-val):val;
        
        for(i = j; i>0; i--){
            if(v){j--;}
            buffer[i] = v ? (char) (v%10)+48 : ' '; 
            v/=10;
        }
        buffer[i] = v ? (char) (v%10)+48 : ' ';
        if(val<0){buffer[j] = '-';}
	    else if(val==0){buffer[j] = 48;}
        
        if(endWithNull){buffer[(int)len]='\0';}
        return len-1<(max-buffer)?buffer+(len-1):max;
	}
	return buffer;
}
char* p_uint_fl(char* buffer, unsigned int val, char len, char endWithNull, char* max)
{
	
	printf("%p\n%p\n\n",buffer,max);
	
	if(max-buffer){
	
	    int i;
	    int j= len-1<(max-buffer)?len-1:(max-buffer);
	    int v=(unsigned int)val;
	    
	    for(i = j; i>0; i--){
	        if(v){j--;}
	        buffer[i] = v ? (char) (v%10)+48 : ' '; 
	        v/=10;
	    }
	    buffer[i] = v ? (char) (v%10)+48 : ' ';
	    
	    if(j==(len-1)){buffer[j] = '0';}
	    
	    if(endWithNull){buffer[(int)len]='\0';}
    
    	return len-1<(max-buffer)?buffer+(len-1):max;
	}
	return buffer;
}

char* p_int_ec(char* buffer, int val, char endWithNull, char ec, char* max)
{
	if(max-buffer > 0){
	    int i=0;
	    char aux[6];
	    int v = (val<0)?(-val):val;
	    while(v>0 && i<6){
	        aux[i++] =(char) (v%10)+48; 
	        v/=10;
	    } 
	    if(i==0){
	        *buffer = '0'; 
            if(ec && (buffer+1)<max){*(buffer+1)=ec;}
            else if(endWithNull){*(buffer+1) = 0;}
	        if(endWithNull && (buffer+2)<max) *(buffer+2) = 0;
	        return 0;
	    }  
	    if(val<0){*buffer = '-'; buffer++;} 
        if(ec && (buffer+i)<max){*(buffer+i)=ec;}
	    if(endWithNull &&(buffer+i+1)<max){*(buffer+i+1)='\0';}
	    
	    for(i-=1; i>=0; i--){
	        *buffer++ = aux[i];
	        if(buffer==max)break;
	    }
	    return endWithNull&&ec ? buffer+2 : (endWithNull!=ec ? buffer+1 : buffer);
	}
    return buffer;
}
char* p_uint_ec(char* buffer, unsigned int val, char endWithNull, char ec, char* max){
    if(max-buffer > 0){
	    int i=0;
	    char aux[6];
	    int v = val;
	    while(v>0 && i<6){
	        aux[i++] =(char) (v%10)+48; 
	        v/=10;
	    } 
	    if(i==0){
	        *buffer = '0'; 
            if(ec && (buffer+1)<max){*(buffer+1)=ec;}
            else if(endWithNull){*(buffer+1) = 0;}
	        if(endWithNull && (buffer+2)<max) *(buffer+2) = 0;
	        return 0;
	    }  
        if(ec && (buffer+i)<max){*(buffer+i)=ec;}
	    if(endWithNull &&(buffer+i+1)<max){*(buffer+i+1)='\0';}
	    
	    for(i-=1; i>=0; i--){
	        *buffer++ = aux[i];
	        if(buffer==max)break;
	    }
	    return endWithNull&&ec ? buffer+2 : (endWithNull!=ec ? buffer+1 : buffer);
	}
    return buffer;
}

char* assign(char* buffer, char* strB, char* max){
    
    while(buffer<max && *strB!='\0'){
        *buffer++ = *strB++;
    }
    *buffer='\0';
    return buffer;
}
char* append(char* strA, char* strB, char* max){
    
    while(*strA!='\0' && strA<max){strA++;}
    
    while(strA<max && *strB!='\0'){
        *strA++ = *strB++;
    }
    
    *strA='\0';
    
    return strA;
}
char* concat(char* buffer, char* strA, char* strB, char* max){
    
    while(buffer<max && *strA!='\0'){
        *buffer++ = *strA++;
    }
    while(buffer<max && *strB!='\0'){
        *buffer++ = *strB++;
    }
    *buffer='\0';
    return buffer;
}

int length(char* string){
    int length=0;
    while(string[length]!='\0'){length++;}
    return length;
}