Comment servir des pintes de Guinness "over Internet" ;) Reprise/Fork d'un très ancien code d'un pilier de f.m.b.l : http://tnemeth.free.fr/projets/guinness-server.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lists.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * lists
  3. * architecture clients/serveur guinness
  4. * Thomas Nemeth -- le 15 juin 2001
  5. *
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <strings.h>
  11. #include "defines.h"
  12. #include "xmem.h"
  13. #include "lists.h"
  14. void add_elt (Elt **elt, const char *nom) {
  15. Elt *e;
  16. if (*elt == NULL) {
  17. *elt = xmalloc ((1) * sizeof (Elt));
  18. (*elt)->nom = xstrdup (nom);
  19. (*elt)->next = NULL;
  20. } else {
  21. e = *elt;
  22. while (e->next != NULL) e = e->next;
  23. e->next = xmalloc ((1) * sizeof (Elt));
  24. e = e->next;
  25. e->nom = xstrdup (nom);
  26. e->next = NULL;
  27. }
  28. }
  29. void remove_elt (Elt **elt, const char *nom) {
  30. Elt *e = *elt, *p = NULL;
  31. int res = FALSE;
  32. char *rt, *text ;
  33. do {
  34. if (e != NULL) {
  35. if ((rt = strstr (e->nom, SEP)) != NULL) {
  36. text = xmalloc (rt - e->nom + 2);
  37. memset (text, 0, rt - e->nom + 2);
  38. strncpy (text, e->nom, rt - e->nom);
  39. #ifdef DEBUG
  40. printf ("Comparaison (l) de [%s] avec [%s]\n", text, nom);
  41. #endif
  42. res = (strcmp (text, nom) == 0);
  43. free (text);
  44. } else {
  45. #ifdef DEBUG
  46. printf ("Comparaison (c) de [%s] avec [%s]\n", e->nom, nom);
  47. #endif
  48. res = (strcmp (e->nom, nom) == 0);
  49. }
  50. if (res == FALSE) {
  51. p = e;
  52. e = e->next;
  53. }
  54. }
  55. } while ( (e != NULL) && (res != TRUE) );
  56. if (e == NULL) return;
  57. if (e == *elt) *elt = e->next;
  58. if (p) p->next = e->next;
  59. free (e->nom);
  60. free (e);
  61. }
  62. void remove_elt_n (Elt **elt, int i) {
  63. Elt *e = *elt, *p = NULL;
  64. int n = 0;
  65. while ( (e != NULL) && (n != i) ) {
  66. n++;
  67. e = e->next;
  68. }
  69. if (e == NULL) return;
  70. if (e == *elt) *elt = e->next;
  71. if (p) p->next = e->next;
  72. free (e->nom);
  73. free (e);
  74. }
  75. int get_nb_elts (Elt *elt) {
  76. Elt *e = elt;
  77. int n = 0;
  78. while (e != NULL) {
  79. n++;
  80. e = e->next;
  81. }
  82. return n;
  83. }
  84. char *elt_number (Elt *elt, int i) {
  85. Elt *e = elt;
  86. int n = 0;
  87. while ( (e != NULL) && (n != i) ) {
  88. n++;
  89. e = e->next;
  90. }
  91. if (e == NULL) return NULL;
  92. return e->nom;
  93. }
  94. int exist_elt (Elt *elt, const char *nom) {
  95. Elt *e = elt;
  96. int res = FALSE;
  97. char *rt, *text ;
  98. do {
  99. if (e != NULL) {
  100. if ((rt = strstr (e->nom, SEP)) != NULL) {
  101. text = xmalloc (rt - e->nom + 2);
  102. memset (text, 0, rt - e->nom + 2);
  103. strncpy (text, e->nom, rt - e->nom);
  104. #ifdef DEBUG
  105. printf ("Comparaison (l) de [%s] avec [%s]\n", text, nom);
  106. #endif
  107. res = (strcmp (text, nom) == 0);
  108. free (text);
  109. } else {
  110. #ifdef DEBUG
  111. printf ("Comparaison (c) de [%s] avec [%s]\n", e->nom, nom);
  112. #endif
  113. res = (strcmp (e->nom, nom) == 0);
  114. }
  115. e = e->next;
  116. }
  117. } while ( (e != NULL) && (res != TRUE) );
  118. return res;
  119. }
  120. char *get_elt (Elt *elt, const char *nom) {
  121. Elt *e = elt;
  122. int res = FALSE;
  123. char *rt, *text ;
  124. do {
  125. if (e != NULL) {
  126. if ((rt = strstr (e->nom, SEP)) != NULL) {
  127. text = xmalloc (rt - e->nom + 2);
  128. memset (text, 0, rt - e->nom + 2);
  129. strncpy (text, e->nom, rt - e->nom);
  130. #ifdef DEBUG
  131. printf ("Comparaison (l) de [%s] avec [%s]\n", text, nom);
  132. #endif
  133. res = (strcmp (text, nom) == 0);
  134. free (text);
  135. } else {
  136. #ifdef DEBUG
  137. printf ("Comparaison (c) de [%s] avec [%s]\n", e->nom, nom);
  138. #endif
  139. res = (strcmp (e->nom, nom) == 0);
  140. }
  141. if (res == FALSE) e = e->next;
  142. }
  143. } while ( (e != NULL) && (res != TRUE) );
  144. if (e == NULL) return NULL;
  145. return e->nom;
  146. }
  147. void free_list (Elt **elt) {
  148. Elt *e = *elt, *p;
  149. while (e != NULL) {
  150. p = e;
  151. e = e->next;
  152. free (p->nom);
  153. free (p);
  154. }
  155. *elt = NULL;
  156. }