1 /***************************************************************************
2  *   copyright           : (C) 2002 by Hendrik Sattler                     *
3  *   mail                : post@hendrik-sattler.de                         *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  ***************************************************************************/
11 
12 #include "helper.h"
13 #include <string.h>
14 #include <stdlib.h>
15 #include <sys/types.h>
16 
str_split(char * str,char * split,unsigned long count)17 str_array_t* str_split (char* str, char* split, unsigned long count) {
18   unsigned int i = 0;
19   char* temp = str;
20   char* temp2 = str;
21   char** retval = NULL;
22   size_t len = str_len(split);
23 
24   while (temp2 != NULL) {
25     ++i;
26     temp2 = strstr(temp,split);
27     if (temp2 != NULL) temp = temp2 + len;
28   }
29   if (count == 0 || i < count) count = i;
30   retval = mem_alloc((count+1)*sizeof(*retval),0);
31   retval[count] = NULL;
32 
33   for (i = 0; i < count; ++i) {
34     retval[i] = str;
35     str = strstr(retval[i],split);
36     if (str != NULL) {
37       memset(str,0,len);
38       str += len;
39     }
40   }
41   return retval;
42 }
43 
str_array_uniq(str_array_t * array)44 void str_array_uniq (str_array_t* array) {
45   size_t i = 0, k = 0, l = 0;
46 
47   while (array[i] != NULL) ++i;
48   for (; k < i; ++k)
49     if (array[k] != NULL)
50       for (l = k+1; l < i; ++l)
51 	if (array[l] != NULL && strcmp(array[l],array[k]) == 0)
52 	  array[l] = NULL;
53   l = 0;
54   for (k = 0; k < i; ++k) {
55     if (array[k] == NULL) {
56       if (l <= k) l = k+1;
57       while (array[l] == NULL && l < i) ++l;
58       if (l == i) break;
59       array[k] = array[l];
60       array[l++] = NULL;
61     }
62   }
63 }
64 
str_array_append(str_array_t * array1,size_t size1,str_array_t * array2,size_t size2)65 str_array_t* str_array_append (str_array_t* array1, size_t size1,
66 			       str_array_t* array2, size_t size2)
67 {
68   if (size2 > 0) {
69     array1 = mem_realloc(array1,(size1+size2+1)*sizeof(array1));
70     array1[size1+size2] = NULL;
71     do {
72       --size2;
73       array1[size1+size2] = array2[size2];
74     } while (size2 > 0);
75   }
76   return array1;
77 }
78 
str_array_qsort_cb(const void * a,const void * b)79 static int str_array_qsort_cb (const void* a, const void* b) {
80   return strcmp((const char*)a,(const char*)b);
81 }
82 
str_array_sort(str_array_t * array)83 void str_array_sort (str_array_t* array) {
84   size_t i = 0;
85 
86   while(array[i] != NULL) ++i;
87   qsort((void*)array[0],i,sizeof(*array),str_array_qsort_cb);
88 }
89