2018-04-13 18:04:30 +02:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2018-03-12 11:22:06 +01:00
|
|
|
#include "ktv.h"
|
2018-03-30 11:12:50 +02:00
|
|
|
/**
|
|
|
|
* @brief Default function to compare two values of type #cw_KTV_t.
|
2018-04-17 07:46:09 +02:00
|
|
|
*
|
2018-03-30 11:12:50 +02:00
|
|
|
* @param v1
|
|
|
|
* @param v2
|
2018-04-17 07:46:09 +02:00
|
|
|
* @return
|
2018-03-30 11:12:50 +02:00
|
|
|
*/
|
2018-04-17 07:46:09 +02:00
|
|
|
int cw_ktv_mavlcmp (const void *v1, const void *v2)
|
2018-03-12 11:22:06 +01:00
|
|
|
{
|
2018-04-17 07:46:09 +02:00
|
|
|
char *d1, *d2, *k1, *k2;
|
|
|
|
int l1, l2, rc, i1, i2;
|
2018-03-24 07:56:05 +01:00
|
|
|
|
2018-04-17 07:46:09 +02:00
|
|
|
k1 = ( (cw_KTV_t *) v1)->key;
|
|
|
|
k2 = ( (cw_KTV_t *) v2)->key;
|
2018-03-24 07:56:05 +01:00
|
|
|
|
2018-04-17 07:46:09 +02:00
|
|
|
while (1) {
|
|
|
|
/* Find dots in both keys */
|
|
|
|
d1 = strchr (k1, '.');
|
|
|
|
d2 = strchr (k2, '.');
|
|
|
|
|
|
|
|
/* if there are no dots, compare keys as normal */
|
|
|
|
if (d1 == NULL || d2 == NULL)
|
|
|
|
return strcmp (k1, k2);
|
|
|
|
|
|
|
|
|
|
|
|
/* calculate the length of the key till dots */
|
|
|
|
l1 = d1 - k1; /*((cw_KTV_t *) v1)->key;*/
|
|
|
|
l2 = d2 - k2; /*((cw_KTV_t *) v2)->key;*/
|
|
|
|
|
|
|
|
/* if length differs do a normal compare */
|
|
|
|
if (l1 != l2) {
|
|
|
|
return strcmp (k1, k2); /*((cw_KTV_t *) v1)->key, ((cw_KTV_t *) v2)->key);*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rc = strncmp (k1, k2, l1); /*((cw_KTV_t *) v1)->key,((cw_KTV_t *) v2)->key,l1);*/
|
|
|
|
|
|
|
|
if (rc != 0) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:04:30 +02:00
|
|
|
d1++;
|
|
|
|
d2++;
|
2018-04-17 07:46:09 +02:00
|
|
|
i1 = atoi (d1);
|
|
|
|
i2 = atoi (d2);
|
|
|
|
|
|
|
|
rc = i1 - i2;
|
|
|
|
|
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
while (isdigit (*d1))
|
|
|
|
d1++;
|
|
|
|
|
|
|
|
while (isdigit (*d2))
|
|
|
|
d2++;
|
|
|
|
|
|
|
|
k1=d1;
|
|
|
|
k2=d2;
|
|
|
|
/*return strcmp(d1,d2);*/
|
|
|
|
/*return cw_ktv_mavlcmp(d1,d2);*/
|
|
|
|
}
|
2018-03-12 11:22:06 +01:00
|
|
|
}
|