| 414 | |
| 415 | == Le service ''dejavu'' == |
| 416 | |
| 417 | '''Fichier dejavu.c''' |
| 418 | {{{ |
| 419 | 1 #include "hte.h" |
| 420 | 2 #include <stdio.h> |
| 421 | 3 #include <string.h> |
| 422 | 4 #include <stdlib.h> |
| 423 | 5 |
| 424 | 6 /* |
| 425 | 7 * structure définissant un item du dictionnaire |
| 426 | 8 * Un item est formé d'un couple (clé,valeur) plus un pointeur |
| 427 | 9 * permettant de chainer les items ayant le meme index de hachage |
| 428 | 10 */ |
| 429 | 11 struct hte_item_s |
| 430 | 12 { |
| 431 | 13 struct hte_item_s *NEXT; /* pointeur sur l'item suivant */ |
| 432 | 14 char KEY[]; /* tableau flexible contenant la clé */ |
| 433 | 15 }; |
| 434 | 16 |
| 435 | 17 |
| 436 | 18 unsigned dejavu (char *key) |
| 437 | 19 { |
| 438 | 20 unsigned index; |
| 439 | 21 hte_item_t *curr; |
| 440 | 22 static hte_root_t *root_namealloc; |
| 441 | 23 |
| 442 | 24 if (key) |
| 443 | 25 { |
| 444 | 26 /* création du dictionnaire la première fois */ |
| 445 | 27 if (root_namealloc == NULL) |
| 446 | 28 root_namealloc = hte_create (MAX_NAMEALLOC); |
| 447 | 29 |
| 448 | 30 /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
| 449 | 31 index = hte_hash (key) % root_namealloc->NB_LIST; |
| 450 | 32 |
| 451 | 33 /* recherche et l'item dans sa liste et sortie si trouvé */ |
| 452 | 34 for (curr = root_namealloc->LIST[index]; curr; curr = curr->NEXT) |
| 453 | 35 { |
| 454 | 36 if ((int) curr == 0x4d52) |
| 455 | 37 fprintf (stderr, |
| 456 | 38 "------------------------------------------------------------ ARG1 \n"); |
| 457 | 39 if (strcmp (curr->KEY, key) == 0) |
| 458 | 40 return 1; |
| 459 | 41 } |
| 460 | 42 |
| 461 | 43 /* création d'une nouvelle entrée */ |
| 462 | 44 curr = malloc (sizeof (struct hte_item_t *) + strlen (key) + 1); |
| 463 | 45 if (curr) |
| 464 | 46 { |
| 465 | 47 if ((int) curr == 0x4d52) |
| 466 | 48 fprintf (stderr, |
| 467 | 49 "------------------------------------------------------------ ARG2 \n"); |
| 468 | 50 strcpy (curr->KEY, key); |
| 469 | 51 curr->NEXT = root_namealloc->LIST[index]; |
| 470 | 52 if ((int) curr == 0x4d52) |
| 471 | 53 fprintf (stderr, |
| 472 | 54 "------------------------------------------------------------ ARG3 \n"); |
| 473 | 55 root_namealloc->LIST[index] = curr; |
| 474 | 56 return 0; |
| 475 | 57 } |
| 476 | 58 } |
| 477 | 59 perror ("dejavu"); |
| 478 | 60 exit (1); |
| 479 | 61 } |
| 480 | }}} |
| 481 | |
| 482 | == Le service ''dictionnaire'' == |
| 483 | |
| 484 | '''Fichier dico.c''' |
| 485 | |
| 486 | {{{ |
| 487 | 1 #include <stdio.h> |
| 488 | 2 #include <string.h> |
| 489 | 3 #include <stdlib.h> |
| 490 | 4 #include "hte.h" |
| 491 | 5 |
| 492 | 6 |
| 493 | 7 /* |
| 494 | 8 * structure définissant un item du dictionnaire |
| 495 | 9 * Un item est formé d'un couple (clé,valeur) plus un pointeur |
| 496 | 10 * permettant de chainer les items ayant le meme index de hachage |
| 497 | 11 */ |
| 498 | 12 struct hte_item_s |
| 499 | 13 { |
| 500 | 14 struct hte_item_s *NEXT; /* pointeur sur l'item suivant */ |
| 501 | 15 hte_data_t *DATA; /* valeur associée à l'item */ |
| 502 | 16 char KEY[]; /* tableau flexible contenant la clé */ |
| 503 | 17 }; |
| 504 | 18 |
| 505 | 19 |
| 506 | 20 /* |
| 507 | 21 * Recherche d'un item de clé key dans le dictionaire root |
| 508 | 22 */ |
| 509 | 23 hte_data_t *hte_get (hte_root_t * root, char *key) |
| 510 | 24 { |
| 511 | 25 int index; |
| 512 | 26 hte_item_t *curr; |
| 513 | 27 |
| 514 | 28 if (key) |
| 515 | 29 { |
| 516 | 30 /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
| 517 | 31 index = hte_hash (key) % root->NB_LIST; |
| 518 | 32 |
| 519 | 33 /* recherche et l'item dans sa liste et sortie si trouvé */ |
| 520 | 34 for (curr = root->LIST[index]; curr; curr = curr->NEXT) |
| 521 | 35 if (strcmp (key, curr->KEY) == 0) |
| 522 | 36 return curr->DATA; |
| 523 | 37 |
| 524 | 38 return NULL; |
| 525 | 39 } |
| 526 | 40 perror ("hte_get"); |
| 527 | 41 exit (1); |
| 528 | 42 } |
| 529 | 43 |
| 530 | 44 /* |
| 531 | 45 * Recherche d'un item de clé key dans le dictionnaire root |
| 532 | 46 * avec création en cas d'absence |
| 533 | 47 * et affectation d'une nouvelle donnée data |
| 534 | 48 */ |
| 535 | 49 void hte_add (hte_root_t * root, char *key, hte_data_t * data) |
| 536 | 50 { |
| 537 | 51 int index; |
| 538 | 52 hte_item_t *curr; |
| 539 | 53 |
| 540 | 54 if (key) |
| 541 | 55 { |
| 542 | 56 /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
| 543 | 57 index = hte_hash (key) % root->NB_LIST; |
| 544 | 58 |
| 545 | 59 /* recherche et l'item dans sa liste et sortie si trouvé */ |
| 546 | 60 for (curr = root->LIST[index]; curr; curr = curr->NEXT) |
| 547 | 61 { |
| 548 | 62 if (strcmp (curr->KEY, key) == 0) |
| 549 | 63 { |
| 550 | 64 curr->DATA = data; |
| 551 | 65 return; |
| 552 | 66 } |
| 553 | 67 } |
| 554 | 68 |
| 555 | 69 /* création d'une nouvelle entrée */ |
| 556 | 70 curr = malloc (sizeof (struct hte_item_t *) + sizeof (void *) + strlen (key) + 1); |
| 557 | 71 if (curr) |
| 558 | 72 { |
| 559 | 73 strcpy (curr->KEY, key); |
| 560 | 74 curr->NEXT = root->LIST[index]; |
| 561 | 75 root->LIST[index] = curr; |
| 562 | 76 curr->DATA = data; |
| 563 | 77 return; |
| 564 | 78 } |
| 565 | 79 } |
| 566 | 80 perror ("hte_add"); |
| 567 | 81 exit (1); |
| 568 | 82 } |
| 569 | }}} |
| 570 | |
| 571 | === Le service allocateur de nom === |
| 572 | |
| 573 | '''Fichier namealloc.c''' |
| 574 | {{{ |
| 575 | 1 #include "hte.h" |
| 576 | 2 #include <stdio.h> |
| 577 | 3 #include <string.h> |
| 578 | 4 #include <stdlib.h> |
| 579 | 5 |
| 580 | 6 /* |
| 581 | 7 * structure définissant un item du dictionnaire |
| 582 | 8 * Un item est formé d'un couple (clé,valeur) plus un pointeur |
| 583 | 9 * permettant de chainer les items ayant le meme index de hachage |
| 584 | 10 */ |
| 585 | 11 struct hte_item_s |
| 586 | 12 { |
| 587 | 13 struct hte_item_s *NEXT; /* pointeur sur l'item suivant */ |
| 588 | 14 char KEY[]; /* tableau flexible contenant la clé */ |
| 589 | 15 }; |
| 590 | 16 |
| 591 | 17 |
| 592 | 18 char *namealloc (char *key) |
| 593 | 19 { |
| 594 | 20 int index; |
| 595 | 21 hte_item_t *curr; |
| 596 | 22 static hte_root_t *root_namealloc; |
| 597 | 23 |
| 598 | 24 if (key) |
| 599 | 25 { |
| 600 | 26 /* création du dictionnaire la première fois */ |
| 601 | 27 if (root_namealloc == NULL) |
| 602 | 28 root_namealloc = hte_create (MAX_NAMEALLOC); |
| 603 | 29 |
| 604 | 30 /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
| 605 | 31 index = hte_hash (key) % root_namealloc->NB_LIST; |
| 606 | 32 |
| 607 | 33 /* recherche et l'item dans sa liste et sortie si trouvé */ |
| 608 | 34 for (curr = root_namealloc->LIST[index]; curr; curr = curr->NEXT) |
| 609 | 35 { |
| 610 | 36 if (strcmp (curr->KEY, key) == 0) |
| 611 | 37 return curr->KEY; |
| 612 | 38 } |
| 613 | 39 |
| 614 | 40 /* création d'une nouvelle entrée */ |
| 615 | 41 curr = malloc (sizeof (struct hte_item_t *) + strlen (key) + 1); |
| 616 | 42 if (curr) |
| 617 | 43 { |
| 618 | 44 strcpy (curr->KEY, key); |
| 619 | 45 curr->NEXT = root_namealloc->LIST[index]; |
| 620 | 46 root_namealloc->LIST[index] = curr; |
| 621 | 47 return curr->KEY; |
| 622 | 48 } |
| 623 | 49 } |
| 624 | 50 perror ("namealloc"); |
| 625 | 51 exit (1); |
| 626 | 52 } |
| 627 | }}} |