2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008 - DIGITEO - Allan CORNET
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.1-en.txt
13 /*--------------------------------------------------------------------------*/
16 #include "getCommonPart.h"
17 #include "core_math.h"
19 #include "os_strdup.h"
20 /*--------------------------------------------------------------------------*/
21 static int cmp( const void *a , const void *b)
23 return strcmp(*(const char **)a, *(const char **)b );
25 /*--------------------------------------------------------------------------*/
26 static int cmpPos(char *str1, char *str2)
33 int lenstr1 = (int) strlen(str1);
34 int lenstr2 = (int) strlen(str2);
37 if (lenstr1 > lenstr2)
50 for (i = 0; i < lenstr1; i++)
58 return Min(lenstr1, lenstr2);
62 /*--------------------------------------------------------------------------*/
63 char *getCommonPart(char **dictionary, int sizeDictionary)
65 char *commonpart = NULL;
67 if (sizeDictionary == 1)
69 return os_strdup(dictionary[0]);
72 if (sizeDictionary >= 2)
76 char *currentstr = dictionary[0];
77 qsort(dictionary, sizeof dictionary / sizeof dictionary[0], sizeof dictionary[0], cmp);
79 r = cmpPos(currentstr, dictionary[1]);
80 for (i = 1; i < sizeDictionary - 1; i++)
82 int current_r = cmpPos(currentstr, dictionary[i + 1]);
86 currentstr = dictionary[i + 1];
90 commonpart = os_strdup(currentstr);
95 /*--------------------------------------------------------------------------*/