From: John Hodge Date: Sat, 8 Jan 2011 08:29:02 +0000 (+0800) Subject: Abstraced updates to the cokebank file to a function X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=f02c7f90fe0e654d5587fa88c8dd8297ef150b54;p=tpg%2Fopendispense2.git Abstraced updates to the cokebank file to a function --- diff --git a/src/cokebank_basic/bank.c b/src/cokebank_basic/bank.c index ce963c6..56db422 100644 --- a/src/cokebank_basic/bank.c +++ b/src/cokebank_basic/bank.c @@ -27,6 +27,19 @@ tUser *gaBank_Users; FILE *gBank_File; // === CODE === +static int Bank_int_WriteEntry(int ID) +{ + if( ID < 0 || ID >= giBank_NumUsers ) { + return -1; + } + + // Commit to file + fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET); + fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File); + + return 0; +} + int Bank_GetUserByName(const char *Username) { int i, uid; @@ -113,6 +126,8 @@ int Bank_SetUserFlags(int ID, int Mask, int Value) gaBank_Users[ID].Flags &= ~Mask; gaBank_Users[ID].Flags |= Value; + + Bank_int_WriteEntry(ID); return 0; } @@ -126,25 +141,7 @@ int Bank_AlterUserBalance(int ID, int Delta) // Update gaBank_Users[ID].Balance += Delta; - // Commit - fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET); - fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File); - - return 0; -} - -int Bank_SetUserBalance(int ID, int Value) -{ - // Sanity - if( ID < 0 || ID >= giBank_NumUsers ) - return -1; - - // Update - gaBank_Users[ID].Balance = Value; - - // Commit - fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET); - fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File); + Bank_int_WriteEntry(ID); return 0; } @@ -200,13 +197,11 @@ int Bank_AddUser(const char *Username) else if( strcmp(Username, "root") == 0 ) { gaBank_Users[giBank_NumUsers].Flags = USER_FLAG_WHEEL|USER_FLAG_COKE; } - - // Commit to file - fseek(gBank_File, giBank_NumUsers*sizeof(gaBank_Users[0]), SEEK_SET); - fwrite(&gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File); // Increment count giBank_NumUsers ++; + + Bank_int_WriteEntry(giBank_NumUsers - 1); return 0; } diff --git a/src/cokebank_basic/main.c b/src/cokebank_basic/main.c index 9cc1308..f7d32f0 100644 --- a/src/cokebank_basic/main.c +++ b/src/cokebank_basic/main.c @@ -155,7 +155,7 @@ int SetFlags(int User, int Mask, int Flags) */ int GetBalance(int User) { - return Bank_GetUserBalance(User);; + return Bank_GetUserBalance(User); } /**