#include "etc.h" /* Compare deux chaines sans tenir compte des \r. Retourne les mêmes résultat que strcmp(). On utilise cette fonction pour comparer des chaines provenant de fichier DOS et UNIX. */ export int strcmp_nocr (const char *str1, const char *str2) { int ret = 0; while (1){ if (*str1 == '\0'){ while (*str2 == '\r') str2++; if (*str2 != '\0'){ ret = -1; } break; }else if (*str2 == '\0'){ /* *str1 != '\0' donc str1 > str2 sauf si fini par \r*/ while (*str1 == '\r') str1++; if (*str1 != '\0'){ ret = 1; } break; }else if (*str1 == *str2){ str1++; str2++; }else if (*str1 == '\r'){ str1++; }else if (*str2 == '\r'){ str2++; }else{ ret = *str1 - *str2; break; } } return ret; } #ifdef TEST static struct { char *str1; char *str2; int ret; } tb[]={ "allo", "allo", 0, "allo1", "allo", 1, "allo", "allo1", -1, "allo\r\r\r\r", "allo", 0, "allo", "allo\r\r\r", 0, "allo\r\nallo", "allo\nallo", 0, NULL }; int main (int, char *[]) { int i=0; while (tb[i].str1 != NULL){ int ret = strcmp_nocr (tb[i].str1,tb[i].str2); if (ret < 0) ret = -1; if (ret > 0) ret = 1; printf (":%s: <> :%s: -> %d ",tb[i].str1,tb[i].str2,ret); if (ret == tb[i].ret){ printf ("OK\n"); }else{ printf ("***** %d %d\n",ret,tb[i].ret); } i++; } } #endif