comparison libnnchat.c @ 102:b096ae97fc7d

Renamed functions.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 11 Oct 2010 15:48:05 +0300
parents 218efd2f0641
children eaa524e153f9
comparison
equal deleted inserted replaced
101:7eaf065f7a65 102:b096ae97fc7d
1 /* 1 /*
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms 2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
3 * Written by Matti 'ccr' Hämäläinen 3 * Written by Matti 'ccr' Hämäläinen
4 * (C) Copyright 2008 Tecnic Software productions (TNSP) 4 * (C) Copyright 2008-2010 Tecnic Software productions (TNSP)
5 */ 5 */
6 #include "libnnchat.h" 6 #include "libnnchat.h"
7 7
8 8
9 typedef struct { 9 typedef struct {
560 else 560 else
561 buf->pos = pos; 561 buf->pos = pos;
562 } 562 }
563 563
564 564
565 static uint8_t hashUserName(char *name) 565 static uint8_t nn_hash_user(const char *name)
566 { 566 {
567 int n = 0; 567 int n = 0;
568 uint8_t *c = (uint8_t *)name; 568 const uint8_t *c = (uint8_t *)name;
569 uint8_t hash = 0xff; 569 uint8_t hash = 0xff;
570 570
571 while (*c && n < 4) { 571 while (*c && n < 4) {
572 hash = (hash << 1) ^ tolower(*c); 572 hash = (hash << 1) ^ tolower(*c);
573 c++; n++; 573 c++; n++;
575 575
576 return (hash & 0xff); 576 return (hash & 0xff);
577 } 577 }
578 578
579 579
580 static void insertUser(nn_user_t **list, nn_user_t *node) 580 static void nn_user_insert(nn_user_t **list, nn_user_t *node)
581 { 581 {
582 node->next = *list; 582 node->next = *list;
583 *list = node; 583 *list = node;
584 } 584 }
585 585
586 586
587 nn_user_t *findUserEnc(nn_userhash_t *list, char *encname) 587 nn_user_t *nn_user_find_enc(const nn_userhash_t *list, const char *encname)
588 { 588 {
589 uint8_t hash; 589 uint8_t hash;
590 590
591 if (list == NULL) return NULL; 591 if (list == NULL) return NULL;
592 592
593 hash = hashUserName(encname); 593 hash = nn_hash_user(encname);
594 if (list->buckets[hash] != NULL) { 594 if (list->buckets[hash] != NULL) {
595 nn_user_t *curr = list->buckets[hash]; 595 nn_user_t *curr = list->buckets[hash];
596 while (curr != NULL) { 596 while (curr != NULL) {
597 if (strcasecmp(curr->encname, encname) == 0) 597 if (strcasecmp(curr->encname, encname) == 0)
598 return curr; 598 return curr;
602 602
603 return NULL; 603 return NULL;
604 } 604 }
605 605
606 606
607 int addUserToHash(nn_userhash_t **list, char *encname) 607 int nn_user_add_to_hash(nn_userhash_t **list, const char *encname)
608 { 608 {
609 uint8_t hash; 609 uint8_t hash;
610 nn_user_t *user; 610 nn_user_t *user;
611 611
612 /* Check arguments */ 612 /* Check arguments */
619 if (*list == NULL) 619 if (*list == NULL)
620 return -2; 620 return -2;
621 } 621 }
622 622
623 /* Check if username is already there */ 623 /* Check if username is already there */
624 if (findUserEnc(*list, encname) != NULL) 624 if (nn_user_find_enc(*list, encname) != NULL)
625 return 1; 625 return 1;
626 626
627 /* No, we'll add it */ 627 /* No, we'll add it */
628 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL) 628 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL)
629 return -3; 629 return -3;
631 user->encname = th_strdup(encname); 631 user->encname = th_strdup(encname);
632 user->name = nn_dbldecode_str(encname); 632 user->name = nn_dbldecode_str(encname);
633 if (user->encname == NULL || user->name == NULL) 633 if (user->encname == NULL || user->name == NULL)
634 return -4; 634 return -4;
635 635
636 hash = hashUserName(encname); 636 hash = nn_hash_user(encname);
637 insertUser(&((*list)->buckets[hash]), user); 637 nn_user_insert(&((*list)->buckets[hash]), user);
638 return 0; 638 return 0;
639 } 639 }
640 640
641 641
642 nn_user_t *copyUser(nn_user_t *src) 642 nn_user_t *nn_user_copy(const nn_user_t *src)
643 { 643 {
644 nn_user_t *user; 644 nn_user_t *user;
645 645
646 if (src == NULL) return NULL; 646 if (src == NULL) return NULL;
647 647
656 656
657 return user; 657 return user;
658 } 658 }
659 659
660 660
661 static void freeUser(nn_user_t *user) 661 static void nn_user_free(nn_user_t *user)
662 { 662 {
663 th_free(user->encname); 663 th_free(user->encname);
664 th_free(user->name); 664 th_free(user->name);
665 th_free(user); 665 th_free(user);
666 } 666 }
667 667
668 668
669 void freeUserList(nn_user_t *list) 669 void nn_user_free_list(nn_user_t *list)
670 { 670 {
671 nn_user_t *next, *curr = list; 671 nn_user_t *next, *curr = list;
672 while (curr != NULL) { 672 while (curr != NULL) {
673 next = curr->next; 673 next = curr->next;
674 freeUser(curr); 674 nn_user_free(curr);
675 curr = next; 675 curr = next;
676 } 676 }
677 } 677 }
678 678
679 void freeUserHash(nn_userhash_t *hash) 679 void nn_user_free_hash(nn_userhash_t *hash)
680 { 680 {
681 int i; 681 int i;
682 if (hash == NULL) 682 if (hash == NULL)
683 return; 683 return;
684 684
685 for (i = 0; i < NN_NUM_BUCKETS; i++) { 685 for (i = 0; i < NN_NUM_BUCKETS; i++) {
686 freeUserList(hash->buckets[i]); 686 nn_user_free_list(hash->buckets[i]);
687 hash->buckets[i] = NULL; 687 hash->buckets[i] = NULL;
688 } 688 }
689 689
690 th_free(hash); 690 th_free(hash);
691 } 691 }
692