Merge remote branch 'origin/master' into YaSp
[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 #include "os_strdup.h"
20 /*--------------------------------------------------------------------------*/
21 static int cmp( const void *a ,const void *b)
22 {
23         return strcmp(*(const char **)a, *(const char **)b );
24 }
25 /*--------------------------------------------------------------------------*/
26 static int cmpPos(char *str1,char *str2)
27 {
28         if (str1 && str2)
29         {
30                 int i = 0;
31                 char *s1 = NULL;
32                 char *s2 = NULL;
33                 int lenstr1 = (int) strlen(str1);
34                 int lenstr2 = (int) strlen(str2);
35                 int slen;
36
37                 if (lenstr1 > lenstr2)
38                 {
39                         s1 = str2;
40                         s2 = str1;
41                         slen = lenstr1;
42                         lenstr1 = lenstr2;
43                         lenstr2 = slen;
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 os_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 = os_strdup(currentstr);
88                 commonpart[r] = '\0';
89         }
90         return commonpart;
91 }
92 /*--------------------------------------------------------------------------*/