Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1756 connectés 

  FORUM HardWare.fr
  Programmation
  C

  [C] retourner un tableau de string

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C] retourner un tableau de string

n°2142191
invent
Posté le 13-05-2012 à 17:08:07  profilanswer
 

Bonjour,
 
Voici une fonction qui fonctionne et m'affiche dans la console le résultat demandé. Je voudrais maintenant le stocker dans un tableau pour le traiter plus tard.
 
Merci d'avance.
 
le main.c

Code :
  1. static const char *deb = "<a href=";
  2. static const char *fin = ">";
  3. parse_html(res.memory, deb, fin);


 
fonction.c

Code :
  1. void parse_html(char *p, const char *deb, const char *fin)
  2. {
  3.     const size_t deblen = strlen (deb);
  4.     const size_t finlen = strlen (fin);
  5.     char *occurence = NULL;
  6.     while (p != NULL && *p != 0)
  7.     {
  8.         /* To seek the balise of beginning */
  9.         p = strstr (p, deb);
  10.         if (p != NULL)
  11.         {
  12.             /* To jump the balise */
  13.             p += deblen;
  14.             /* To store the beginning */
  15.             occurence = p;
  16.             /* To seek the balise of end */
  17.             p = strstr (p, fin);
  18.             if (p != NULL)
  19.             {
  20.                 /* To mark the end */
  21.                 *p = 0;
  22.                 if (!strstr (occurence, "main" ))
  23.                 {
  24.                     strip(occurence);
  25.                     printf ("%s\n", occurence);
  26.                 }
  27.                 /* To jump the balise */
  28.                 p += finlen;
  29.             }
  30.         }
  31.     }
  32. }

mood
Publicité
Posté le 13-05-2012 à 17:08:07  profilanswer
 

n°2142199
gilou
Modérateur
Modzilla
Posté le 13-05-2012 à 19:20:00  profilanswer
 

Vu que tu as la chaîne a l'appel, et que de plus tu mets un 0 a chaque fin de sous chaîne qui t'intéresse, pourquoi ne pas stocker chacune des valeurs du pointeur occurence (après l'appel à strip) dans un tableau (alloué dynamiquement) qui sera retourné par adresse?
A+,

Message cité 1 fois
Message édité par gilou le 13-05-2012 à 19:23:35

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2142204
invent
Posté le 13-05-2012 à 20:34:00  profilanswer
 

gilou a écrit :

Vu que tu as la chaîne a l'appel, et que de plus tu mets un 0 a chaque fin de sous chaîne qui t'intéresse, pourquoi ne pas stocker chacune des valeurs du pointeur occurence (après l'appel à strip) dans un tableau (alloué dynamiquement) qui sera retourné par adresse?
A+,


 
Merci de la réponse. J'y suis presque, voici mon nouveau code.
 
main.c

Code :
  1. char *tab = calloc(16, sizeof(char));
  2. static const char *deb = "<a href=";
  3. static const char *fin = ">";
  4. parse_html(res.memory, deb, fin, tab);
  5. ...
  6. for (i = 0; i < sizeof(tab); i++)
  7.    printf("%s\n", &tab[i]);


 

Code :
  1. char parse_html(char *p, const char *deb, const char *fin, char *tab)
  2. {
  3.     const size_t deblen = strlen (deb);
  4.     const size_t finlen = strlen (fin);
  5.     char *occurence = NULL;
  6.     int i = 0;
  7.     while (p != NULL && *p != 0)
  8.     {
  9.         /* To seek the balise of beginning */
  10.         p = strstr (p, deb);
  11.         if (p != NULL)
  12.         {
  13.             /* To jump the balise */
  14.             p += deblen;
  15.             /* To store the beginning */
  16.             occurence = p;
  17.             /* To seek the balise of end */
  18.             p = strstr (p, fin);
  19.             if (p != NULL)
  20.             {
  21.                 /* To mark the end */
  22.                 *p = 0;
  23.                 if (!strstr (occurence, "main" ))
  24.                 {
  25.                     strip(occurence);
  26.                     tab[i] = *occurence;
  27.                     i++;
  28.                     //printf ("%s\n", occurence);
  29.                 }
  30.                 /* To jump the balise */
  31.                 p += finlen;
  32.             }
  33.         }
  34.     }
  35.     return tab;
  36. }


 
Le résultat:
ddgghkkmppuwPlOs
dgghkkmppuwPlOs
gghkkmppuwPlOs
ghkkmppuwPlOs
 
Il me donne la premiere lettre de chaque occurrence au lieu de l'occurrence complete.  

n°2142220
gilou
Modérateur
Modzilla
Posté le 13-05-2012 à 21:51:35  profilanswer
 

Un exemple vite fait à partir de ton code:

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void parse_html(char *p, const char *deb, const char *fin, char **tab) {
  5.     const size_t deblen = strlen (deb);
  6.     const size_t finlen = strlen (fin);
  7.     char *occurence = NULL;
  8.     int i = 0;
  9.     while (p != NULL && *p != 0)
  10.     {
  11.         /* To seek the balise of beginning */
  12.         p = strstr (p, deb);
  13.         if (p != NULL)
  14.         {
  15.             /* To jump the balise */
  16.             p += deblen;
  17.             /* To store the beginning */
  18.             occurence = p;
  19.             /* To seek the balise of end */
  20.             p = strstr (p, fin);
  21.             if (p != NULL)
  22.             {
  23.                 /* To mark the end */
  24.                 *p = 0;
  25.                 if (!strstr (occurence, "main" ))
  26.                 {
  27.                     // strip(occurence);
  28.                     tab[i] = occurence;
  29.                     i++;
  30.                     //printf ("%s\n", occurence);
  31.                 }
  32.                 /* To jump the balise */
  33.                 p += finlen;
  34.             }
  35.         }
  36.     }
  37.     tab[i] = NULL;
  38.     return;
  39. }
  40. int main() {
  41.     char **tab = calloc(16, sizeof(char*));
  42.     static const char *deb = "<a href=";
  43.     static const char *fin = ">";
  44.     char buffer[256];
  45.     int i;
  46.     strcpy(buffer, "toto <a href=\"abc\"> tutu <a href=\"dkiugiugef\"> titi" );
  47.     parse_html(buffer, deb, fin, tab);
  48.     for (i = 0; (i < sizeof(tab)) && tab[i]; i++)
  49.         printf("%s\n", tab[i]);
  50.     free(tab);
  51.     return 0;
  52. }


C:\clang>gcc -o invent.exe invent.c
 
C:\clang>invent
"abc"
"dkiugiugef"


A+,


Message édité par gilou le 13-05-2012 à 22:11:30

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2142225
gilou
Modérateur
Modzilla
Posté le 13-05-2012 à 22:34:41  profilanswer
 

Et un exemple plus propre, qui gere lui même dynamiquement son allocation:
 

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. char **parse_html(char *p, const char *deb, const char *fin) {
  5.     const size_t deblen = strlen (deb);
  6.     const size_t finlen = strlen (fin);
  7.     int tabsize = 16;
  8.     char **tab = malloc(tabsize * sizeof(char*));
  9.     char *occurence = NULL;
  10.     int i = 0;
  11.     while (p != NULL && *p != 0)
  12.     {
  13.         /* To seek the balise of beginning */
  14.         p = strstr (p, deb);
  15.         if (p != NULL)
  16.         {
  17.             /* To jump the balise */
  18.             p += deblen;
  19.             /* To store the beginning */
  20.             occurence = p;
  21.             /* To seek the balise of end */
  22.             p = strstr (p, fin);
  23.             if (p != NULL)
  24.             {
  25.                 /* To mark the end */
  26.                 *p = 0;
  27.                 if (!strstr (occurence, "main" ))
  28.                 {
  29.                     // strip(occurence);
  30.                     tab[i] = occurence;
  31.                     i++;
  32.                     if (i+1 == tabsize) {
  33.                         tabsize *= 2;
  34.                         tab = realloc(tab, tabsize * sizeof(char*));
  35.                     }
  36.                     //printf ("%s\n", occurence);
  37.                 }
  38.                 /* To jump the balise */
  39.                 p += finlen;
  40.             }
  41.         }
  42.     }
  43.     tab[i] = NULL;
  44.     return tab;
  45. }
  46. int main() {
  47.     static const char *deb = "<a href=";
  48.     static const char *fin = ">";
  49.     char buffer[256];
  50.     char **tab;
  51.     strcpy(buffer, "toto <a href=\"abc\"> tutu <a href=\"dkiugiugef\"> titi" );
  52.     tab = parse_html(buffer, deb, fin);
  53.     while (*tab) {
  54.         printf("%s\n", *tab++);
  55.     }
  56.     free(tab);
  57.     return 0;
  58. }


A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2142229
invent
Posté le 14-05-2012 à 00:01:52  profilanswer
 

gilou a écrit :

Et un exemple plus propre, qui gere lui même dynamiquement son allocation:
 
 
A+,


 
Merci de ton aide !


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  [C] retourner un tableau de string

 

Sujets relatifs
[C] Remplir un tableau 2 dimensions avec un fichierRenvoyer l'adresse d'un tableau en C
[C] random sur un enumDifférence d'affichage d'un tableau dans une div avec IE et Safari
[C] fonctions imbriquées, je m'y perds [résolu]Macro tableau croisé dynamique
[C] SDL avec combo BlitSurface() + Flip()[C# .net] Navigation vers un TAB précis d'un FORM.
Plus de sujets relatifs à : [C] retourner un tableau de string


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR