-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.c
48 lines (44 loc) · 1.13 KB
/
wallet.c
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
#include <string.h>
#include <malloc.h>
#include "list.h"
#include "wallet.h"
/* @Callback
* Initialize & return a new wallet*/
Wallet createWallet(char *userId) {
Wallet wallet = malloc(sizeof(struct Wallet));
if (wallet != NULL) {
wallet->userId = malloc(sizeof(char *) * strlen(userId) + 1);
if (wallet->userId != NULL) {
strcpy(wallet->userId, userId);
wallet->balance = 0;
wallet->bitcoins = NULL;
listCreate(&wallet->bitcoins, userId);
} else {
return NULL;
}
}
return wallet;
}
/* @Callback
* Compare wallet with userId field*/
int cmpWallet(Wallet w1, char *userId) {
return strcmp(w1->userId, userId);
}
/* @Callback
* Hash function for wallets hashtable*/
unsigned long int walletHash(char *key, unsigned long int capacity) {
int i, sum = 0;
size_t keyLength = strlen(key);
for (i = 0; i < keyLength; i++) {
sum += key[i];
}
return sum % capacity;
}
/* @Callback
* Destroy wallet*/
void destroyWallet(Wallet w) {
free(w->userId);
listDestroy(&w->bitcoins);
free(w);
printf(" ");
}