Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "WalletManager.h"
- #include <assert.h>
- #include <string.h>
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- // Is it dangerous to cache Wallets instead of writing immediately?
- // You can lose a ton of information on a failed write.
- const char* WALLETS_FILE_NAME = "wallets.dat";
- const IN_MEMORY_WALLETS = 256;
- unsigned LastUsedWalletId = 0;
- Wallet* Wallets = NULL;
- unsigned NextWallet = 0;
- FILE* WalletsFile = NULL;
- char ReadWalletsCount()
- {
- WalletsFile = fopen(WALLETS_FILE_NAME, "rb");
- if (WalletsFile)
- {
- if (fread(&LastUsedWalletId, sizeof(unsigned), 1, WalletsFile) != 1)
- {
- printf(
- "Initialization of Wallet Generator failed."
- " Internal error.\n");
- fclose(WalletsFile);
- return 0;
- };
- fclose(WalletsFile);
- WalletsFile = NULL;
- }
- else if (!WalletsFile && errno != ENOENT)
- {
- // if the file exists but can't be opened
- printf(
- "Initialization of Wallet Generator failed."
- " Unable to open database file.\n");
- return 0;
- }
- return 1;
- }
- char CheckGenerateDataBase()
- {
- if (!WalletsFile && errno == ENOENT)
- {
- WalletsFile = fopen(WALLETS_FILE_NAME, "wb");
- if (fwrite(&LastUsedWalletId, sizeof(unsigned), 1, WalletsFile) != 1)
- {
- printf(
- "Initialization of Wallet Generator failed."
- " Unable to generate database file.\n");
- fclose(WalletsFile);
- return 0;
- }
- }
- return 1;
- }
- char AllocateWalletsMemory()
- {
- Wallets = (Wallet*)(malloc(IN_MEMORY_WALLETS * sizeof(Wallet)));
- if (!Wallets)
- {
- printf(
- "Initialization of Wallet Generator failed."
- " Unable to allocate internal data.\n");
- return 0;
- }
- return 1;
- }
- char OpenDataBase()
- {
- if (!WalletsFile)
- {
- WalletsFile = fopen(WALLETS_FILE_NAME, "rb+");
- if (!WalletsFile)
- {
- printf(
- "Initialization of Wallet Generator failed."
- " Unable to open database file.\n");
- free(Wallets);
- return 0;
- }
- fseek(
- WalletsFile,
- LastUsedWalletId * sizeof(Wallet) + sizeof(unsigned),
- SEEK_SET);
- }
- return 1;
- }
- char InitializeWalletGenerator()
- {
- if (!ReadWalletsCount())
- {
- return 0;
- }
- if (!CheckGenerateDataBase())
- {
- return 0;
- }
- if (!AllocateWalletsMemory())
- {
- return 0;
- }
- if (!OpenDataBase())
- {
- return 0;
- }
- return 1;
- }
- void WriteWalletsToDisk()
- {
- size_t WalletsWritten = fwrite(
- Wallets,
- sizeof(Wallet),
- NextWallet,
- WalletsFile);
- if (WalletsWritten != NextWallet)
- {
- printf(
- "Internal error while writing wallets to disk."
- "%u wallets successfully written.\n",
- (unsigned)WalletsWritten);
- }
- }
- void TerminateWalletGenerator()
- {
- if (NextWallet)
- {
- WriteWalletsToDisk();
- fseek(WalletsFile, 0, SEEK_SET);
- if (fwrite(&LastUsedWalletId, sizeof(unsigned), 1, WalletsFile) != 1)
- {
- printf(
- "Corrupted %s file head."
- " Please validate file before new run.\n",
- WALLETS_FILE_NAME);
- }
- fclose(WalletsFile);
- free(Wallets);
- }
- }
- char GenerateWallet(char* owner, double fiatMoney)
- {
- if (Wallets)
- {
- if (LastUsedWalletId == UINT_MAX - 1)
- {
- printf("Wallets limit exceeded!\n");
- return 0;
- }
- assert(fiatMoney >= 0);
- assert(owner[0] != 0);
- assert(strlen(owner) < WALLET_MANAGER_WALLET_OWNER_MAX_SIZE);
- Wallet* wallet = &Wallets[NextWallet];
- wallet->Id = ++LastUsedWalletId;
- wallet->FiatMoney = fiatMoney;
- strcpy(wallet->Owner, owner);
- ++NextWallet;
- if (NextWallet == IN_MEMORY_WALLETS)
- {
- WriteWalletsToDisk();
- NextWallet = 0;
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement