Advertisement
Guest User

UDC_main.c

a guest
Jan 11th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.54 KB | None | 0 0
  1. //gcc main.c -o main `pkg-config --cflags --libs glib-2.0`
  2. #include <glib-2.0/glib.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. char *
  8. common_text_get_line (FILE *fichier)
  9. /**
  10.  * \brief Renvoie la ligne en cours de la variable fichier.
  11.  * \param fichier : la variable fichier.
  12.  * \return
  13.  *   Succès : pointeur vers la ligne de texte.\n
  14.  *   Échec : NULL :
  15.  *     - fichier == NULL,
  16.  *     - Erreur d'allocation mémoire.
  17.  */
  18. {
  19. #define CUR_MAX 256
  20.   char *buffer, *ligne_tmp, *retour = NULL;
  21.  
  22.   buffer = malloc (sizeof (char) * CUR_MAX);
  23.  
  24.   do
  25.   {
  26.     if ((fgets (buffer, CUR_MAX, fichier) == NULL) && (retour == NULL))
  27.     {
  28.       free (buffer);
  29.       return NULL;
  30.     }
  31.     ligne_tmp = retour;
  32.     if (ligne_tmp == NULL)
  33.       retour = g_strconcat (buffer, NULL);
  34.     else
  35.     {
  36.       retour = g_strconcat (ligne_tmp, buffer, NULL);
  37.       free (ligne_tmp);
  38.     }
  39.    
  40.     // On a atteint la fin de la ligne
  41.     if (buffer[strlen(buffer)-1] == '\n')
  42.     {
  43.       free (buffer);
  44.       // Suppression du retour chariot
  45.       retour[strlen(retour)-1] = 0;
  46.       return retour;
  47.     }
  48.   } while (TRUE);
  49. #undef CUR_MAX
  50. }
  51.  
  52.  
  53. typedef struct
  54. {
  55.   int nb;
  56.   char *categorie, *description, *titre;
  57. } Udc;
  58.  
  59.  
  60. char* sous_cat (GList *depart)
  61. {
  62.   Udc *udc = depart->data;
  63.   GList *list;
  64.   char *retour = g_strdup_printf ("");
  65.   int i = udc->nb-1;
  66.  
  67.   if (udc->nb == 0)
  68.     return retour;
  69.  
  70.   list = depart;
  71.   while (i != -1)
  72.   {
  73.     Udc *udc2 = list->data;
  74.     char *tmp;
  75.    
  76.     if (udc2->nb == i)
  77.     {
  78.       tmp = retour;
  79.       retour = g_strdup_printf ("%s/%s", udc2->categorie, retour);
  80.       g_free (tmp);
  81.       i--;
  82.     }
  83.    
  84.     list = g_list_previous (list);
  85.   }
  86.   return retour;
  87. }
  88.  
  89.  
  90. char *sous_cats_wiki (GList *cat)
  91. {
  92.   Udc *udc = cat->data;
  93.   GList *list = g_list_next (cat);
  94.   char *retour = NULL;
  95.    
  96.   while (list != NULL)
  97.   {
  98.     Udc *udc2 =list->data;
  99.    
  100.     if (udc2->nb <= udc->nb)
  101.       return retour;
  102.    
  103.     if (udc2->nb == udc->nb+1)
  104.     {
  105.       if (retour == NULL)
  106.         retour = g_strdup_printf ("* '''[[%s|%s]]''' - %s\n", udc2->titre, udc2->categorie, udc2->description);
  107.       else
  108.       {
  109.         char *tmp = retour;
  110.         retour = g_strdup_printf ("%s* '''[[%s|%s]]''' - %s\n", retour, udc2->titre, udc2->categorie, udc2->description);
  111.         free (tmp);
  112.       }
  113.     }
  114.    
  115.     list = g_list_next (list);
  116.   }
  117.  
  118.   return retour;
  119. }
  120.  
  121.  
  122. int main ()
  123. {
  124.   FILE  *udc_f = fopen ("UDC", "r");
  125.   char  *ligne = common_text_get_line (udc_f);
  126.   GList *tout = NULL, *liste;
  127.  
  128.   while (ligne != NULL)
  129.   {
  130.     Udc  *udc = malloc (sizeof (Udc));
  131.     int   i;
  132.    
  133.     i = 0;
  134.     while (ligne[i] == ' ')
  135.       i++;
  136.     udc->nb = i;
  137.     udc->categorie = &(ligne[i]);
  138.     while (ligne[i] != ' ')
  139.       i++;
  140.     ligne[i] = '\000';
  141.     udc->description = &(ligne[i+1]);
  142.     tout = g_list_append (tout, udc);
  143.     udc->titre = g_strdup_printf ("CDU/%s%s", sous_cat (g_list_last (tout)), udc->categorie);
  144.    
  145.     ligne = common_text_get_line (udc_f);
  146.   }
  147.  
  148.   liste = tout;
  149.   while (liste != NULL)
  150.   {
  151.     Udc  *udc = liste->data;
  152.     char *cats;
  153.    
  154.     printf ("Titre Wikibooks : 'CDU/%s'\n", udc->titre);
  155.     printf ("Code Wikipedia:\n");
  156.     printf ("{{Titre simple|%s - %s}}\n\n", udc->categorie, udc->description);
  157.     cats = sous_cats_wiki (liste);
  158.     if (cats != NULL)
  159.     {
  160.       printf ("Choisissez la division suivante :\n\n%s", cats);
  161.       free (cats);
  162.     }
  163.    
  164.     liste = g_list_next (liste);
  165.   }
  166.  
  167.   return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement