comparison libnnchat.c @ 94:6e47426efb6a

Add preliminary userlist data handling functions.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 07 May 2009 06:14:21 +0300
parents acfc4b4bc180
children 69f9c46e4a94
comparison
equal deleted inserted replaced
93:7f9f6af26a65 94:6e47426efb6a
559 buf->pos = buf->len; 559 buf->pos = buf->len;
560 else 560 else
561 buf->pos = pos; 561 buf->pos = pos;
562 } 562 }
563 563
564
565 static uint8_t hashUserName(char *name)
566 {
567 int n = 0;
568 uint8_t *c = (uint8_t *)name;
569 uint8_t hash = 0xff;
570
571 while (*c && n < 4) {
572 hash = (hash << 1) ^ tolower(*c);
573 c++; n++;
574 }
575
576 return (hash & 0xff);
577 }
578
579
580 static void insertUser(nn_user_t **list, nn_user_t *node)
581 {
582 node->next = *list;
583 *list = node;
584 }
585
586
587 nn_user_t *findUserEnc(nn_userhash_t *list, char *encname)
588 {
589 uint8_t hash;
590
591 if (list == NULL) return NULL;
592
593 hash = hashUserName(encname);
594 if (list->buckets[hash] != NULL) {
595 nn_user_t *curr = list->buckets[hash];
596 while (curr != NULL) {
597 if (strcasecmp(curr->encname, encname) == 0)
598 return curr;
599 curr = curr->next;
600 }
601 }
602
603 return NULL;
604 }
605
606
607 int addUserToHash(nn_userhash_t **list, char *encname)
608 {
609 uint8_t hash;
610 nn_user_t *user;
611
612 /* Check arguments */
613 if (list == NULL || encname == NULL)
614 return -1;
615
616 /* Check if list data exists */
617 if (*list == NULL) {
618 *list = th_calloc(1, sizeof(nn_userhash_t));
619 if (*list == NULL)
620 return -2;
621 }
622
623 /* Check if username is already there */
624 if (findUserEnc(*list, encname) != NULL)
625 return 1;
626
627 /* No, we'll add it */
628 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL)
629 return -3;
630
631 user->encname = th_strdup(encname);
632 user->name = doubleDecodeStr(encname);
633 if (user->encname == NULL || user->name == NULL)
634 return -4;
635
636 hash = hashUserName(encname);
637 insertUser(&((*list)->buckets[hash]), user);
638 return 0;
639 }
640
641
642 nn_user_t *copyUser(nn_user_t *src)
643 {
644 nn_user_t *user;
645
646 if (src == NULL) return NULL;
647
648 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL)
649 return NULL;
650
651 /* Copy relevant data */
652 user->encname = th_strdup(src->encname);
653 user->name = th_strdup(src->name);
654 user->lastspoke = src->lastspoke;
655 user->joined = src->joined;
656
657 return user;
658 }
659
660
661 static void freeUser(nn_user_t *user)
662 {
663 th_free(user->encname);
664 th_free(user->name);
665 th_free(user);
666 }
667
668
669 void freeUserList(nn_user_t *list)
670 {
671 nn_user_t *next, *curr = list;
672 while (curr != NULL) {
673 next = curr->next;
674 freeUser(curr);
675 curr = next;
676 }
677 }
678
679 void freeUserHash(nn_userhash_t *hash)
680 {
681 int i;
682 if (hash == NULL)
683 return;
684
685 for (i = 0; i < NN_NUM_BUCKETS; i++) {
686 freeUserList(hash->buckets[i]);
687 hash->buckets[i] = NULL;
688 }
689
690 th_free(hash);
691 }
692