Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include "WalletManager.h"
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <limits.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <errno.h>
  8.  
  9. // Is it dangerous to cache Wallets instead of writing immediately?
  10. // You can lose a ton of information on a failed write.
  11.  
  12. const char* WALLETS_FILE_NAME = "wallets.dat";
  13. const IN_MEMORY_WALLETS = 256;
  14.  
  15. unsigned LastUsedWalletId = 0;
  16. Wallet* Wallets = NULL;
  17. unsigned NextWallet = 0;
  18. FILE* WalletsFile = NULL;
  19.  
  20. char ReadWalletsCount()
  21. {
  22. WalletsFile = fopen(WALLETS_FILE_NAME, "rb");
  23. if (WalletsFile)
  24. {
  25. if (fread(&LastUsedWalletId, sizeof(unsigned), 1, WalletsFile) != 1)
  26. {
  27. printf(
  28. "Initialization of Wallet Generator failed."
  29. " Internal error.\n");
  30. fclose(WalletsFile);
  31. return 0;
  32. };
  33. fclose(WalletsFile);
  34. WalletsFile = NULL;
  35. }
  36. else if (!WalletsFile && errno != ENOENT)
  37. {
  38. // if the file exists but can't be opened
  39. printf(
  40. "Initialization of Wallet Generator failed."
  41. " Unable to open database file.\n");
  42. return 0;
  43. }
  44. return 1;
  45. }
  46.  
  47. char CheckGenerateDataBase()
  48. {
  49. if (!WalletsFile && errno == ENOENT)
  50. {
  51. WalletsFile = fopen(WALLETS_FILE_NAME, "wb");
  52.  
  53. if (fwrite(&LastUsedWalletId, sizeof(unsigned), 1, WalletsFile) != 1)
  54. {
  55. printf(
  56. "Initialization of Wallet Generator failed."
  57. " Unable to generate database file.\n");
  58. fclose(WalletsFile);
  59. return 0;
  60. }
  61. }
  62.  
  63. return 1;
  64. }
  65.  
  66. char AllocateWalletsMemory()
  67. {
  68. Wallets = (Wallet*)(malloc(IN_MEMORY_WALLETS * sizeof(Wallet)));
  69. if (!Wallets)
  70. {
  71. printf(
  72. "Initialization of Wallet Generator failed."
  73. " Unable to allocate internal data.\n");
  74. return 0;
  75. }
  76. return 1;
  77. }
  78.  
  79. char OpenDataBase()
  80. {
  81. if (!WalletsFile)
  82. {
  83. WalletsFile = fopen(WALLETS_FILE_NAME, "rb+");
  84. if (!WalletsFile)
  85. {
  86. printf(
  87. "Initialization of Wallet Generator failed."
  88. " Unable to open database file.\n");
  89. free(Wallets);
  90. return 0;
  91. }
  92.  
  93. fseek(
  94. WalletsFile,
  95. LastUsedWalletId * sizeof(Wallet) + sizeof(unsigned),
  96. SEEK_SET);
  97. }
  98. return 1;
  99. }
  100.  
  101. char InitializeWalletGenerator()
  102. {
  103. if (!ReadWalletsCount())
  104. {
  105. return 0;
  106. }
  107.  
  108. if (!CheckGenerateDataBase())
  109. {
  110. return 0;
  111. }
  112.  
  113. if (!AllocateWalletsMemory())
  114. {
  115. return 0;
  116. }
  117.  
  118. if (!OpenDataBase())
  119. {
  120. return 0;
  121. }
  122.  
  123. return 1;
  124. }
  125.  
  126. void WriteWalletsToDisk()
  127. {
  128. size_t WalletsWritten = fwrite(
  129. Wallets,
  130. sizeof(Wallet),
  131. NextWallet,
  132. WalletsFile);
  133. if (WalletsWritten != NextWallet)
  134. {
  135. printf(
  136. "Internal error while writing wallets to disk."
  137. "%u wallets successfully written.\n",
  138. (unsigned)WalletsWritten);
  139. }
  140. }
  141.  
  142. void TerminateWalletGenerator()
  143. {
  144. if (NextWallet)
  145. {
  146. WriteWalletsToDisk();
  147.  
  148. fseek(WalletsFile, 0, SEEK_SET);
  149. if (fwrite(&LastUsedWalletId, sizeof(unsigned), 1, WalletsFile) != 1)
  150. {
  151. printf(
  152. "Corrupted %s file head."
  153. " Please validate file before new run.\n",
  154. WALLETS_FILE_NAME);
  155. }
  156.  
  157. fclose(WalletsFile);
  158. free(Wallets);
  159. }
  160. }
  161.  
  162. char GenerateWallet(char* owner, double fiatMoney)
  163. {
  164. if (Wallets)
  165. {
  166. if (LastUsedWalletId == UINT_MAX - 1)
  167. {
  168. printf("Wallets limit exceeded!\n");
  169. return 0;
  170. }
  171.  
  172. assert(fiatMoney >= 0);
  173. assert(owner[0] != 0);
  174. assert(strlen(owner) < WALLET_MANAGER_WALLET_OWNER_MAX_SIZE);
  175.  
  176. Wallet* wallet = &Wallets[NextWallet];
  177.  
  178. wallet->Id = ++LastUsedWalletId;
  179. wallet->FiatMoney = fiatMoney;
  180. strcpy(wallet->Owner, owner);
  181.  
  182. ++NextWallet;
  183. if (NextWallet == IN_MEMORY_WALLETS)
  184. {
  185. WriteWalletsToDisk();
  186. NextWallet = 0;
  187. }
  188. }
  189. return 1;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement