Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //gcc main.c -o main `pkg-config --cflags --libs glib-2.0`
- #include <glib-2.0/glib.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char *
- common_text_get_line (FILE *fichier)
- /**
- * \brief Renvoie la ligne en cours de la variable fichier.
- * \param fichier : la variable fichier.
- * \return
- * Succès : pointeur vers la ligne de texte.\n
- * Échec : NULL :
- * - fichier == NULL,
- * - Erreur d'allocation mémoire.
- */
- {
- #define CUR_MAX 256
- char *buffer, *ligne_tmp, *retour = NULL;
- buffer = malloc (sizeof (char) * CUR_MAX);
- do
- {
- if ((fgets (buffer, CUR_MAX, fichier) == NULL) && (retour == NULL))
- {
- free (buffer);
- return NULL;
- }
- ligne_tmp = retour;
- if (ligne_tmp == NULL)
- retour = g_strconcat (buffer, NULL);
- else
- {
- retour = g_strconcat (ligne_tmp, buffer, NULL);
- free (ligne_tmp);
- }
- // On a atteint la fin de la ligne
- if (buffer[strlen(buffer)-1] == '\n')
- {
- free (buffer);
- // Suppression du retour chariot
- retour[strlen(retour)-1] = 0;
- return retour;
- }
- } while (TRUE);
- #undef CUR_MAX
- }
- typedef struct
- {
- int nb;
- char *categorie, *description, *titre;
- } Udc;
- char* sous_cat (GList *depart)
- {
- Udc *udc = depart->data;
- GList *list;
- char *retour = g_strdup_printf ("");
- int i = udc->nb-1;
- if (udc->nb == 0)
- return retour;
- list = depart;
- while (i != -1)
- {
- Udc *udc2 = list->data;
- char *tmp;
- if (udc2->nb == i)
- {
- tmp = retour;
- retour = g_strdup_printf ("%s/%s", udc2->categorie, retour);
- g_free (tmp);
- i--;
- }
- list = g_list_previous (list);
- }
- return retour;
- }
- char *sous_cats_wiki (GList *cat)
- {
- Udc *udc = cat->data;
- GList *list = g_list_next (cat);
- char *retour = NULL;
- while (list != NULL)
- {
- Udc *udc2 =list->data;
- if (udc2->nb <= udc->nb)
- return retour;
- if (udc2->nb == udc->nb+1)
- {
- if (retour == NULL)
- retour = g_strdup_printf ("* '''[[%s|%s]]''' - %s\n", udc2->titre, udc2->categorie, udc2->description);
- else
- {
- char *tmp = retour;
- retour = g_strdup_printf ("%s* '''[[%s|%s]]''' - %s\n", retour, udc2->titre, udc2->categorie, udc2->description);
- free (tmp);
- }
- }
- list = g_list_next (list);
- }
- return retour;
- }
- int main ()
- {
- FILE *udc_f = fopen ("UDC", "r");
- char *ligne = common_text_get_line (udc_f);
- GList *tout = NULL, *liste;
- while (ligne != NULL)
- {
- Udc *udc = malloc (sizeof (Udc));
- int i;
- i = 0;
- while (ligne[i] == ' ')
- i++;
- udc->nb = i;
- udc->categorie = &(ligne[i]);
- while (ligne[i] != ' ')
- i++;
- ligne[i] = '\000';
- udc->description = &(ligne[i+1]);
- tout = g_list_append (tout, udc);
- udc->titre = g_strdup_printf ("CDU/%s%s", sous_cat (g_list_last (tout)), udc->categorie);
- ligne = common_text_get_line (udc_f);
- }
- liste = tout;
- while (liste != NULL)
- {
- Udc *udc = liste->data;
- char *cats;
- printf ("Titre Wikibooks : 'CDU/%s'\n", udc->titre);
- printf ("Code Wikipedia:\n");
- printf ("{{Titre simple|%s - %s}}\n\n", udc->categorie, udc->description);
- cats = sous_cats_wiki (liste);
- if (cats != NULL)
- {
- printf ("Choisissez la division suivante :\n\n%s", cats);
- free (cats);
- }
- liste = g_list_next (liste);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement