1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| #ifdef ED
#include "ed/inc/tok.h"
#include <string.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
typedef struct noeud
{
int caract;
int fbase;
struct noeud *a;
struct noeud *b;
struct noeud *c;
struct noeud *d;
struct noeud *e;
struct noeud *f;
struct noeud *g;
struct noeud *h;
struct noeud *i;
struct noeud *j;
struct noeud *k;
struct noeud *l;
struct noeud *m;
struct noeud *n;
struct noeud *o;
struct noeud *p;
struct noeud *q;
struct noeud *r;
struct noeud *s;
struct noeud *t;
struct noeud *u;
struct noeud *v;
struct noeud *w;
struct noeud *x;
struct noeud *y;
struct noeud *z;
}
noeud;
noeud *creernoeud (int carac)
{
noeud *n = malloc (sizeof *n);
if (n != NULL)
{
static const noeud z = { 0 };
/* tous les champs à 0 */
*n = z;
n->caract = carac;
}
return n;
}
#ifdef ED
char **str_split (char const *s, char const *sep, int *p_taille)
{
char **tab = NULL;
if (s != NULL && sep != NULL && p_taille != NULL)
{
sTOK *ptok = TOK_create (s, sep, NULL);
if (ptok != NULL)
{
sTOK_INFO info;
TOK_get (ptok, &info);
/* faire une copie dynamique du tableau de pointeurs */
{
size_t size = info.argc * sizeof *tab;
tab = malloc (size);
if (tab != NULL)
{
memcpy (tab, info.argv, size);
*p_taille = info.argc;
}
}
TOK_delete (ptok), ptok = NULL;
}
}
return tab;
}
#else
char **str_split (char const *s, char const *sep, int *p_taille)
{
char **tab = NULL;
if (s != NULL && sep != NULL && p_taille != NULL)
{
/* ton code ... */
}
return tab;
}
#endif
/* fonction qui permet d'extraire les données contenu dans
le fichier stations.data pour les rentrer dans le graphe */
void extraireDonnees (noeud * arbre, char const nomFichier[])
{
FILE *f = fopen (nomFichier, "r");
if (f != NULL)
{
noeud *courant = arbre;
char ligne[80];
long n = 1;
while (fgets (ligne, sizeof ligne, f) != NULL)
{
int tailleTab = 0;
char **tabChar = str_split (ligne, " ", &tailleTab);
if (tabChar != NULL)
{
size_t i = 0;
while (tabChar[0][i] != 0)
{
switch (tabChar[0][i])
{
/* -ed- laisser la machine ecrire le code... */
#define CASE(lettre, id)\
case lettre:\
if (courant->id == NULL)\
{\
courant->id = creernoeud (tabChar[0][i]);\
}\
courant = courant->id;\
break
CASE ('a', a);
CASE ('b', b);
CASE ('c', c);
CASE ('d', d);
CASE ('e', e);
CASE ('f', f);
CASE ('g', g);
CASE ('h', h);
CASE ('i', i);
CASE ('j', j);
CASE ('k', k);
CASE ('l', l);
CASE ('m', m);
CASE ('n', n);
CASE ('o', o);
CASE ('p', p);
CASE ('q', q);
CASE ('r', r);
CASE ('s', s);
CASE ('t', t);
CASE ('u', u);
CASE ('v', v);
CASE ('w', w);
CASE ('x', x);
CASE ('y', y);
CASE ('z', z);
#undef CASE
}
if (tabChar[0][i + 1] == 0)
courant->fbase = 1;
i++;
}
printf ("%ld\n", n);
n++;
free (tabChar), tabChar = NULL;
}
else
{
printf ("split error\n");
break;
}
assert (tabChar == NULL);
}
fclose (f);
}
else
{
perror (nomFichier);
}
return;
}
int main (void)
{
noeud arbre;
extraireDonnees (&arbre, "dico_fr_min.txt");
printf ("ok\n");
return 0;
} |
Partager