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.

drinks.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * drinks
  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 <dirent.h>
  12. #include <unistd.h>
  13. #include <sys/stat.h>
  14. #include "defines.h"
  15. #include "xmem.h"
  16. #include "printlog.h"
  17. #include "lists.h"
  18. #include "drinks.h"
  19. #include "pint.h"
  20. extern char *pinte;
  21. extern char *chemin;
  22. extern Elt *drinks_list;
  23. extern FILE *logfile;
  24. extern FILE *outerr;
  25. char *drinks_get_from_file (const char *nom) {
  26. struct stat fileinfos;
  27. char filename[MAXSTRLEN + 1];
  28. char *datas;
  29. FILE *fichier;
  30. snprintf (filename, MAXSTRLEN, "%s/%s", chemin, nom);
  31. if (stat (filename, &fileinfos) == -1) {
  32. printlog (LOG_ERROR, "Acces impossible au fichier [%s]\n",
  33. filename);
  34. perror ("stat ");
  35. return NULL;
  36. }
  37. if ( (fichier = fopen (filename, "r")) == NULL) return NULL;
  38. datas = xmalloc ((fileinfos.st_size + 3) * sizeof (char));
  39. memset (datas, 0, fileinfos.st_size + 3);
  40. fread (datas, sizeof (char), fileinfos.st_size, fichier);
  41. fclose (fichier);
  42. return datas;
  43. }
  44. char *drinks_get (const char *nom) {
  45. char *breuvage = NULL;
  46. int i;
  47. if (nom == NULL) return NULL;
  48. /* Element 0 (Guinness) disponible par defaut */
  49. if (strcmp (nom, "guinness") == 0) breuvage = xstrdup (pinte);
  50. /* Sinon on cherche dans la liste des fichiers :
  51. * on commence a 1 pour eviter la pinte de Guinness qui est traitee
  52. * juste avant.
  53. */
  54. if (! breuvage) {
  55. for (i = 1 ; i < get_nb_elts (drinks_list) ; i++)
  56. if (strcmp (nom, elt_number (drinks_list, i)) == 0)
  57. breuvage = drinks_get_from_file (nom);
  58. }
  59. /* Dernier cas : on n'a rien trouve => pinte par defaut */
  60. if (! breuvage) breuvage = xstrdup (pinte);
  61. return breuvage;
  62. }
  63. void drinks_list_files (const char *path) {
  64. DIR *rep;
  65. struct dirent *dirinfos;
  66. struct stat fileinfos;
  67. int nbfiles = 0;
  68. char filename[MAXSTRLEN + 1];
  69. if ( (rep = opendir (path)) == NULL) {
  70. printlog (LOG_ERROR, "Impossible d'ouvrir le repertoire [%s]\n", path);
  71. #ifdef DEBUG
  72. perror ("opendir ");
  73. #endif
  74. return;
  75. }
  76. printlog (LOG_NOTIFY, "Liste des fichiers a integrer :\n");
  77. while ((dirinfos = readdir (rep)) != NULL) {
  78. snprintf (filename, MAXSTRLEN, "%s/%s", path, dirinfos->d_name);
  79. if (stat (filename, &fileinfos) == -1) {
  80. printlog (LOG_ERROR, "Acces impossible au fichier [%s]\n",
  81. filename);
  82. #ifdef DEBUG
  83. perror ("stat ");
  84. #endif
  85. }
  86. if ( (dirinfos->d_name[0] != '.') &&
  87. ( (fileinfos.st_mode & S_IFMT) == S_IFREG) ) {
  88. printlog (LOG_NOTIFY, " fichier : [%s]\n",
  89. dirinfos->d_name);
  90. add_elt (&drinks_list, dirinfos->d_name);
  91. nbfiles++;
  92. }
  93. }
  94. printlog (LOG_NOTIFY, "Fin de la liste : %d fichier%s.\n",
  95. nbfiles, nbfiles > 1 ? "s" : "");
  96. closedir (rep);
  97. }
  98. void drinks_display_list () {
  99. int i;
  100. char *breuvage;
  101. printlog (LOG_NOTIFY, "Boissons disponibles :\n");
  102. for (i = 0 ; i < get_nb_elts (drinks_list) ; i++) {
  103. breuvage = elt_number (drinks_list, i);
  104. printlog (LOG_NOTIFY, "%d\t: %s\n", i, breuvage);
  105. }
  106. }