855855c7dcddebabe87fa120681272062941c9ce
[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 "MALLOC.h"
18 #ifdef _MSC_VER
19 #include "strdup_windows.h"
20 #endif
21 /*--------------------------------------------------------------------------*/
22 static int cmp( const void *a ,const void *b)
23 {
24         return strcmp(*(const char **)a, *(const char **)b );
25 }
26 /*--------------------------------------------------------------------------*/
27 static int cmpPos(char *str1,char *str2)
28 {
29         if (str1 && str2)
30         {
31                 int i = 0;
32                 char *s1 = NULL;
33                 char *s2 = NULL;
34                 int lenstr1 = (int) strlen(str1);
35                 int lenstr2 = (int) strlen(str2);
36
37                 if (lenstr1 > lenstr2) 
38                 {
39                         s1 = str2;
40                         s2 = str1;
41                         lenstr1 = (int) strlen(str1);
42                         lenstr2 = (int) strlen(str2);
43                 }
44                 else
45                 {
46                         s1 = str1;
47                         s2 = str2;
48                 }
49                 for (i = 0; i < lenstr1; i++)
50                 {
51                         if ( s1[i] != s2[i] ) return i;
52                 }
53         }
54         return -1;
55 }
56 /*--------------------------------------------------------------------------*/
57 char *getCommonPart(char **dictionary, int sizeDictionary)
58 {
59         char *commonpart = NULL;
60
61         if (sizeDictionary == 1)
62         {
63                 return strdup(dictionary[0]);
64         }
65
66         if (sizeDictionary >= 2)
67         {
68                 int i = 0;
69                 int r = 0;
70                 char *currentstr = dictionary[0];
71                 qsort(dictionnary, sizeof dictionary / sizeof dictionary[0], sizeof dictionary[0], cmp);
72
73                 r = cmpPos(currentstr, dictionnary[1]);
74                 for (i = 1; i < sizeDictionnary - 1; i++)
75                 {
76                         int current_r = cmpPos(currentstr, dictionary[i+1]);
77                         if (r > current_r  )
78                         {
79                                 r = current_r;
80                                 currentstr = dictionary[i+1];
81                         }
82                 }
83                 
84                 commonpart = strdup(currentstr);
85                 commonpart[r] = '\0';
86         }
87         return commonpart;
88 }
89 /*--------------------------------------------------------------------------*/