From bb482071835d6155974f3d5f91cc288e690e20d1 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 21 Nov 2010 01:04:18 +0800 Subject: [PATCH] Started work on an alternate cokebank that actually does something --- src/{cokebank => cokebank_basic}/Makefile | 0 src/cokebank_basic/bank.c | 114 ++++++++++++++++++++++ src/cokebank_basic/main.c | 108 ++++++++++++++++++++ src/cokebank_stub/Makefile | 20 ++++ src/{cokebank => cokebank_stub}/main.c | 0 5 files changed, 242 insertions(+) rename src/{cokebank => cokebank_basic}/Makefile (100%) create mode 100644 src/cokebank_basic/bank.c create mode 100644 src/cokebank_basic/main.c create mode 100644 src/cokebank_stub/Makefile rename src/{cokebank => cokebank_stub}/main.c (100%) diff --git a/src/cokebank/Makefile b/src/cokebank_basic/Makefile similarity index 100% rename from src/cokebank/Makefile rename to src/cokebank_basic/Makefile diff --git a/src/cokebank_basic/bank.c b/src/cokebank_basic/bank.c new file mode 100644 index 0000000..c9aa6b3 --- /dev/null +++ b/src/cokebank_basic/bank.c @@ -0,0 +1,114 @@ +/* + * OpenDispense 2 + * UCC (University [of WA] Computer Club) Electronic Accounting System + * - Cokebank (Basic Version) + * + * bank.c - Actual bank database + * + * This file is licenced under the 3-clause BSD Licence. See the file COPYING + * for full details. + */ +#include + +enum { + FLAG_TYPEMASK = 0x03, + USER_FLAG_NORMAL = 0x00, + USER_FLAG_COKE = 0x01, + USER_FLAG_WHEEL = 0x02, + USER_FLAG_GOD = 0x03 +}; + +// === CODE === +int Bank_GetUserByUnixID(int UnixUID) +{ + // Expensive search :( + for( i = 0; i < giBank_NumUsers; i ++ ) + { + if( gaBank_Users[i].UnixID == UnixID ) + return i; + } + + return -1; +} + +int Bank_GetUserBalance(int ID) +{ + if( ID < 0 || ID >= giBank_NumUsers ) + return INT_MIN; + + return gaBank_Users[ID].Balance; +} + +int Bank_AlterUserBalance(int ID, int Delta) +{ + // Sanity + if( ID < 0 || ID >= giBank_NumUsers ) + return -1; + + // 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); + + return 0; +} + +int Bank_GetMinAllowedBalance(int ID) +{ + if( ID < 0 || ID >= giBank_NumUsers ) + return -1; + + switch( gaBank_Users[ID].Flags & FLAG_TYPEMASK ) + { + case USER_TYPE_NORMAL: return 0; + case USER_TYPE_COKE: return -2000; + case USER_TYPE_WHEEL: return -10000; + case USER_TYPE_GOD: return INT_MIN; + default: return 0; + } +} + +/** + * \brief Create a new user in our database + */ +int Bank_AddUser(int UnixID) +{ + void *tmp; + + // Can has moar space plz? + tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0])); + if( !tmp ) return -1; + gaBank_Users = tmp; + + // Crete new user + gaBank_Users[giBank_NumUsers].UnixID = UnixID; + gaBank_Users[giBank_NumUsers].Balance = 0; + gaBank_Users[giBank_NumUsers].Flags = 0; + + // 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 ++; + + return 0; +} diff --git a/src/cokebank_basic/main.c b/src/cokebank_basic/main.c new file mode 100644 index 0000000..2388be8 --- /dev/null +++ b/src/cokebank_basic/main.c @@ -0,0 +1,108 @@ +/* + * OpenDispense 2 + * UCC (University [of WA] Computer Club) Electronic Accounting System + * + * cokebank.c - Coke-Bank management + * + * This file is licenced under the 3-clause BSD Licence. See the file COPYING + * for full details. + */ +#include +#include +#include +#include + +// === IMPORTS === + int Bank_GetMinAllowedBalance(int ID); + int Bank_GetUserBalance(int ID); + int Bank_AlterUserBalance(int ID, int Delta); + int Bank_GetUserByUnixID(int UnixID); + int Bank_GetUserByName(const char *Name); + int Bank_AddUser(int UnixID); + +// === PROTOTYPES === +void Init_Cokebank(void); + int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason); + int GetBalance(int User); +char *GetUserName(int User); + int GetUserID(const char *Username); + int GetUserAuth(const char *Username, const char *Password); + +// === CODE === +/** + * \brief Load the cokebank database + */ +void Init_Cokebank(void) +{ + +} + +/** + * \brief Transfers money from one user to another + * \param SourceUser Source user + * \param DestUser Destination user + * \param Ammount Ammount of cents to move from \a SourceUser to \a DestUser + * \param Reason Reason for the transfer (essentially a comment) + * \return Boolean failure + */ +int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason) +{ + if( Bank_GetUserBalance(SourceUser) - Ammount < Bank_GetMinAllowedBalance(SourceUser) ) + return 1; + if( Bank_GetUserBalance(DestUser) + Ammount < Bank_GetMinAllowedBalance(DestUser) ) + return 1; + Bank_AlterUserBalance(DestUser, Ammount); + Bank_AlterUserBalance(SourceUser, -Ammount); + return 0; +} + +/** + * \brief Get the balance of the passed user + */ +int GetBalance(int User) +{ + return 0; +} + +/** + * \brief Return the name the passed user + */ +char *GetUserName(int User) +{ + return NULL; +} + +/** + * \brief Get the User ID of the named user + */ +int GetUserID(const char *Username) +{ + struct passwd *pwd; + int ret; + + // Get user ID + pwd = getpwnam(Username); + if( !pwd ) { + return -1; + } + + // Get internal ID (or create new user) + ret = Bank_GetUserByUnixID(pwd->pw_uid); + if( ret == -1 ) { + ret = Bank_AddUser(pwd->pw_uid); + } + + return ret; +} + +/** + * \brief Authenticate a user + * \return User ID, or -1 if authentication failed + */ +int GetUserAuth(const char *Username, const char *Password) +{ + if( strcmp(Username, "test") == 0 ) + return Bank_GetUserByName("test"); + return -1; +} + diff --git a/src/cokebank_stub/Makefile b/src/cokebank_stub/Makefile new file mode 100644 index 0000000..8e6ea60 --- /dev/null +++ b/src/cokebank_stub/Makefile @@ -0,0 +1,20 @@ + +BIN := ../../cokebank.so +OBJ := main.o + +CPPFLAGS := +CFLAGS := -Wall -Werror -g -fPIC +LDFLAGS := -shared -Wl,-soname,cokebank.so + +.PHONY: all clean + +all: $(BIN) + +clean: + $(RM) $(BIN) $(OBJ) + +$(BIN): $(OBJ) + $(CC) $(LDFLAGS) -o $(BIN) $(OBJ) + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS) diff --git a/src/cokebank/main.c b/src/cokebank_stub/main.c similarity index 100% rename from src/cokebank/main.c rename to src/cokebank_stub/main.c -- 2.20.1