|  | @@ -0,0 +1,180 @@
 | 
	
		
			
				|  |  | +/* 
 | 
	
		
			
				|  |  | + * File:   str.c
 | 
	
		
			
				|  |  | + * Author: Arthur Brandao
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * Created on 28 octobre 2018
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +#define _POSIX_C_SOURCE 200112L
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +#include <stdio.h>
 | 
	
		
			
				|  |  | +#include <stdlib.h>
 | 
	
		
			
				|  |  | +#include <math.h>
 | 
	
		
			
				|  |  | +#include "str.h"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +char** str_split(char* str, const char delim, int* length) {
 | 
	
		
			
				|  |  | +    char** res;
 | 
	
		
			
				|  |  | +    char* signet, * tmp = str;
 | 
	
		
			
				|  |  | +    char last = 0;
 | 
	
		
			
				|  |  | +    int compteur = 0, nb = 1, taille, count = 0;
 | 
	
		
			
				|  |  | +    //Compte le nombre d'element
 | 
	
		
			
				|  |  | +    while (*tmp) {
 | 
	
		
			
				|  |  | +        if (*tmp == delim && last != delim) {
 | 
	
		
			
				|  |  | +            nb++;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        last = *tmp;
 | 
	
		
			
				|  |  | +        tmp++;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Creation du tableau
 | 
	
		
			
				|  |  | +    res = malloc(sizeof (char*));
 | 
	
		
			
				|  |  | +    //Decoupage
 | 
	
		
			
				|  |  | +    tmp = str;
 | 
	
		
			
				|  |  | +    while (*tmp) {
 | 
	
		
			
				|  |  | +        //Si c'est le dernier mot
 | 
	
		
			
				|  |  | +        if (compteur == nb - 1) {
 | 
	
		
			
				|  |  | +            //Ajoute tous ce qui reste
 | 
	
		
			
				|  |  | +            res[compteur] = malloc(sizeof (char) * (strlen(str) - count));
 | 
	
		
			
				|  |  | +            int i = 0;
 | 
	
		
			
				|  |  | +            while(*tmp){
 | 
	
		
			
				|  |  | +                res[compteur][i++] = *tmp;
 | 
	
		
			
				|  |  | +                tmp++;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            //Recup la taille du mot
 | 
	
		
			
				|  |  | +            signet = tmp;
 | 
	
		
			
				|  |  | +            taille = 0;
 | 
	
		
			
				|  |  | +            while (*tmp != delim) {
 | 
	
		
			
				|  |  | +                taille++;
 | 
	
		
			
				|  |  | +                tmp++;
 | 
	
		
			
				|  |  | +                count++;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            //Creation du mot
 | 
	
		
			
				|  |  | +            res[compteur] = malloc(sizeof (char) * taille);
 | 
	
		
			
				|  |  | +            //Ajout du mot
 | 
	
		
			
				|  |  | +            for (int i = 0; i < taille; i++) {
 | 
	
		
			
				|  |  | +                res[compteur][i] = *signet;
 | 
	
		
			
				|  |  | +                signet++;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            compteur++;
 | 
	
		
			
				|  |  | +            //Passe les delimiteurs consecutif
 | 
	
		
			
				|  |  | +            while (*tmp == delim) {
 | 
	
		
			
				|  |  | +                tmp++;
 | 
	
		
			
				|  |  | +                count++;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Retour nombre de mot et tableau
 | 
	
		
			
				|  |  | +    *length = nb;
 | 
	
		
			
				|  |  | +    return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +char* trim(char* str){
 | 
	
		
			
				|  |  | +    return ltrim(rtrim(str, ' '), ' ');
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +char* mtrim(char* str, char mask){
 | 
	
		
			
				|  |  | +    return ltrim(rtrim(str, mask), mask);
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +char* ltrim(char* str, char mask){
 | 
	
		
			
				|  |  | +    //Variable
 | 
	
		
			
				|  |  | +    int cmpt = 0;
 | 
	
		
			
				|  |  | +    char* res;
 | 
	
		
			
				|  |  | +    //Compte le nombre d'espace
 | 
	
		
			
				|  |  | +    while(str[cmpt] == mask){
 | 
	
		
			
				|  |  | +        cmpt++;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Si aucun espace au debut
 | 
	
		
			
				|  |  | +    if(cmpt == 0){
 | 
	
		
			
				|  |  | +        return str;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Sinon creation nouvelle chaine
 | 
	
		
			
				|  |  | +    res = malloc(sizeof(char) * (strlen(str) - cmpt));
 | 
	
		
			
				|  |  | +    for(int i = cmpt; i < strlen(str); i++){
 | 
	
		
			
				|  |  | +        res[i] = str[cmpt++];
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Retour nouvelle chaine
 | 
	
		
			
				|  |  | +    return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +char* rtrim(char* str, char mask){
 | 
	
		
			
				|  |  | +    //Variable
 | 
	
		
			
				|  |  | +    int cmpt = strlen(str) - 1;
 | 
	
		
			
				|  |  | +    char* res;
 | 
	
		
			
				|  |  | +    //Compte le nombre d'espace
 | 
	
		
			
				|  |  | +    while(str[cmpt] == mask){
 | 
	
		
			
				|  |  | +        cmpt--;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Si aucun espace au debut
 | 
	
		
			
				|  |  | +    if(cmpt == strlen(str) - 1){
 | 
	
		
			
				|  |  | +        return str;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    cmpt++;
 | 
	
		
			
				|  |  | +    //Sinon creation nouvelle chaine
 | 
	
		
			
				|  |  | +    res = malloc(sizeof(char) * (cmpt + 1));
 | 
	
		
			
				|  |  | +    for(int i = 0; i < cmpt; i++){
 | 
	
		
			
				|  |  | +        res[i] = str[i];
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    //Retour nouvelle chaine
 | 
	
		
			
				|  |  | +    return res;
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// reverses a string 'str' of length 'len' 
 | 
	
		
			
				|  |  | +void reverse(char *str, int len) 
 | 
	
		
			
				|  |  | +{ 
 | 
	
		
			
				|  |  | +    int i=0, j=len-1, temp; 
 | 
	
		
			
				|  |  | +    while (i<j) 
 | 
	
		
			
				|  |  | +    { 
 | 
	
		
			
				|  |  | +        temp = str[i]; 
 | 
	
		
			
				|  |  | +        str[i] = str[j]; 
 | 
	
		
			
				|  |  | +        str[j] = temp; 
 | 
	
		
			
				|  |  | +        i++; j--; 
 | 
	
		
			
				|  |  | +    } 
 | 
	
		
			
				|  |  | +} 
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | + // Converts a given integer x to string str[].  d is the number 
 | 
	
		
			
				|  |  | + // of digits required in output. If d is more than the number 
 | 
	
		
			
				|  |  | + // of digits in x, then 0s are added at the beginning. 
 | 
	
		
			
				|  |  | +int intToStr(int x, char str[], int d) 
 | 
	
		
			
				|  |  | +{ 
 | 
	
		
			
				|  |  | +    int i = 0; 
 | 
	
		
			
				|  |  | +    while (x) 
 | 
	
		
			
				|  |  | +    { 
 | 
	
		
			
				|  |  | +        str[i++] = (x%10) + '0'; 
 | 
	
		
			
				|  |  | +        x = x/10; 
 | 
	
		
			
				|  |  | +    } 
 | 
	
		
			
				|  |  | +  
 | 
	
		
			
				|  |  | +    // If number of digits required is more, then 
 | 
	
		
			
				|  |  | +    // add 0s at the beginning 
 | 
	
		
			
				|  |  | +    while (i < d) 
 | 
	
		
			
				|  |  | +        str[i++] = '0'; 
 | 
	
		
			
				|  |  | +  
 | 
	
		
			
				|  |  | +    reverse(str, i); 
 | 
	
		
			
				|  |  | +    str[i] = '\0'; 
 | 
	
		
			
				|  |  | +    return i; 
 | 
	
		
			
				|  |  | +} 
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// Converts a floating point number to string. 
 | 
	
		
			
				|  |  | +void ftoa(float n, char *res, int afterpoint) 
 | 
	
		
			
				|  |  | +{ 
 | 
	
		
			
				|  |  | +    // Extract integer part 
 | 
	
		
			
				|  |  | +    int ipart = (int)n; 
 | 
	
		
			
				|  |  | +  
 | 
	
		
			
				|  |  | +    // Extract floating part 
 | 
	
		
			
				|  |  | +    float fpart = n - (float)ipart; 
 | 
	
		
			
				|  |  | +  
 | 
	
		
			
				|  |  | +    // convert integer part to string 
 | 
	
		
			
				|  |  | +    int i = intToStr(ipart, res, 0); 
 | 
	
		
			
				|  |  | +  
 | 
	
		
			
				|  |  | +    // check for display option after point 
 | 
	
		
			
				|  |  | +    if (afterpoint != 0) 
 | 
	
		
			
				|  |  | +    { 
 | 
	
		
			
				|  |  | +        res[i] = '.';  // add dot 
 | 
	
		
			
				|  |  | +  
 | 
	
		
			
				|  |  | +        // Get the value of fraction part upto given no. 
 | 
	
		
			
				|  |  | +        // of points after dot. The third parameter is needed 
 | 
	
		
			
				|  |  | +        // to handle cases like 233.007 
 | 
	
		
			
				|  |  | +        fpart = fpart * pow(10, afterpoint); 
 | 
	
		
			
				|  |  | +  
 | 
	
		
			
				|  |  | +        intToStr((int)fpart, res + i + 1, afterpoint); 
 | 
	
		
			
				|  |  | +    } 
 | 
	
		
			
				|  |  | +} 
 |