Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: C | Size: 1.56 KB | Hits: 66 | Expires: Never
Copy text to clipboard
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #include "string2.h"
  6.  
  7. void strip(char* str, char car)
  8. {
  9.         int offset = 0, i = 0;
  10.  
  11.         //On enlève les espaces au début
  12.         while(str[offset++] == car);
  13.         while((str[i++] = str[offset + i - 1]) != '\0');
  14.         i--;
  15.         //Et à la fin
  16.         while(str[--i] == car);
  17.         str[i+1] = '\0';
  18. }
  19.  
  20. char** split(const char* str, char delim, int* cnt)
  21. {
  22.         char** stock = NULL;
  23.         int i = 0, j = 0, length = -1, debut = 0, count = 0;
  24.  
  25.         // Compte le nombre de champs
  26.         while(str[i] != '\0') {
  27.                 length++;
  28.                 if(str[i] == delim) {
  29.                         if(length)count++;
  30.                         length = -1;
  31.                 }
  32.                 i++;
  33.         }
  34.  
  35.         if(length > -1) count++;
  36.  
  37.         //Réserve l'espace nécessaire
  38.         if((stock = (char**)calloc(count, sizeof(char*))) == NULL) {
  39.                 *cnt = 0;
  40.                 return NULL;
  41.         }
  42.  
  43.         i = 0; length = -1;
  44.  
  45.         //Stocke les sous-chaines
  46.         while(str[i] != '\0') {
  47.                 length++;
  48.  
  49.                 if(str[i] == delim) {
  50.                         if(length) {
  51.                                 if((stock[j] = (char*)calloc(length+1, sizeof(char))) == NULL) {
  52.                                         free_split(stock, j-1);
  53.                                         *cnt = 0;
  54.                                         return NULL;
  55.                                 }
  56.  
  57.                                 strncpy(stock[j], str+debut, length);
  58.                                 stock[j][length] = '\0';
  59.                                 j++;
  60.                         }
  61.  
  62.                         length = -1; debut = i+1;
  63.                 }
  64.                 i++;
  65.         }
  66.  
  67.         length++;
  68.  
  69.         if(length) {
  70.                 if((stock[j] = (char*)calloc(length+1, sizeof(char))) == NULL) {
  71.                         free_split(stock, j-1);
  72.                         *cnt = 0;
  73.                         return NULL;
  74.                 }
  75.                 strncpy(stock[j], str+debut, length);
  76.                 stock[j][length] = '\0';
  77.         }
  78.  
  79.         *cnt = count;
  80.  
  81.         return stock;
  82. }
  83.  
  84. void free_split(char** str, int cnt)
  85. {
  86.         int i;
  87.         for(i = 0; i < cnt; i++)
  88.                 free(str[i]);
  89.         free(str);
  90. }