bug 3051 + cleaning
[scilab.git] / scilab / modules / completion / src / c / getCommonPart.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008 - DIGITEO - Allan CORNET
4 *
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution.  The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 *
11 */
12
13 /*--------------------------------------------------------------------------*/
14 #include <string.h>
15 #include <stdlib.h>
16 #include "getCommonPart.h"
17 #include "core_math.h"
18 #include "MALLOC.h"
19 #ifdef _MSC_VER
20 #include "strdup_windows.h"
21 #endif
22 /*--------------------------------------------------------------------------*/
23 static int cmp( const void *a ,const void *b)
24 {
25         return strcmp(*(const char **)a, *(const char **)b );
26 }
27 /*--------------------------------------------------------------------------*/
28 static int cmpPos(char *str1,char *str2)
29 {
30         if (str1 && str2)
31         {
32                 int i = 0;
33                 char *s1 = NULL;
34                 char *s2 = NULL;
35                 int lenstr1 = (int) strlen(str1);
36                 int lenstr2 = (int) strlen(str2);
37
38                 if (lenstr1 > lenstr2) 
39                 {
40                         s1 = str2;
41                         s2 = str1;
42                         lenstr1 = (int) strlen(str1);
43                         lenstr2 = (int) strlen(str2);
44                 }
45                 else
46                 {
47                         s1 = str1;
48                         s2 = str2;
49                 }
50                 for (i = 0; i < lenstr1; i++)
51                 {
52                         if ( s1[i] != s2[i] ) return i;
53                 }
54
55                 return Min(lenstr1,lenstr2);
56         }
57         return -1;
58 }
59 /*--------------------------------------------------------------------------*/
60 char *getCommonPart(char **dictionary, int sizeDictionary)
61 {
62         char *commonpart = NULL;
63
64         if (sizeDictionary == 1)
65         {
66                 return strdup(dictionary[0]);
67         }
68
69         if (sizeDictionary >= 2)
70         {
71                 int i = 0;
72                 int r = 0;
73                 char *currentstr = dictionary[0];
74                 qsort(dictionary, sizeof dictionary / sizeof dictionary[0], sizeof dictionary[0], cmp);
75
76                 r = cmpPos(currentstr, dictionary[1]);
77                 for (i = 1; i < sizeDictionary - 1; i++)
78                 {
79                         int current_r = cmpPos(currentstr, dictionary[i+1]);
80                         if (r > current_r  )
81                         {
82                                 r = current_r;
83                                 currentstr = dictionary[i+1];
84                         }
85                 }
86                 
87                 commonpart = strdup(currentstr);
88                 if (r>0) commonpart[r] = '\0';
89         }
90         return commonpart;
91 }
92 /*--------------------------------------------------------------------------*/