X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fcokebank_basic%2Fmain.c;h=4a5afb3d3cbe3505f40c3bed0d4b06f5c2ec7da7;hb=fa5a63b28f0b56be4258b4b08d1f30fc4f08d602;hp=21f4e063e63980fe92f35f4052b27643eaa321c0;hpb=6b194d420f1554ace61a251f2c416712d68ab633;p=tpg%2Fopendispense2.git diff --git a/src/cokebank_basic/main.c b/src/cokebank_basic/main.c index 21f4e06..4a5afb3 100644 --- a/src/cokebank_basic/main.c +++ b/src/cokebank_basic/main.c @@ -35,10 +35,11 @@ // === PROTOTYPES === void Init_Cokebank(const char *Argument); +static int Bank_int_ReadDatabase(void); +static int Bank_int_WriteEntry(int ID); int Bank_Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason); int Bank_CreateUser(const char *Username); int Bank_GetMaxID(void); -static int Bank_int_WriteEntry(int ID); int Bank_GetUserID(const char *Username); int Bank_GetBalance(int User); int Bank_GetFlags(int User); @@ -76,24 +77,15 @@ void Init_Cokebank(const char *Argument) // Open Cokebank gBank_File = fopen(Argument, "rb+"); - if( !gBank_File ) { - gBank_File = fopen(Argument, "wb+"); - } - if( !gBank_File ) { - perror("Opening coke bank"); - } + if( !gBank_File ) gBank_File = fopen(Argument, "wb+"); + if( !gBank_File ) perror("Opening coke bank"); + Bank_int_ReadDatabase(); // Open log file // TODO: Do I need this? gBank_LogFile = fopen("cokebank.log", "a"); if( !gBank_LogFile ) gBank_LogFile = stdout; - - // Read in cokebank - fseek(gBank_File, 0, SEEK_END); - giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]); - fseek(gBank_File, 0, SEEK_SET); - gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) ); - fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File); + #if USE_LDAP // Connect to LDAP @@ -120,7 +112,7 @@ void Init_Cokebank(const char *Argument) struct berval *servcred; cred.bv_val = "secret"; cred.bv_len = 6; - rv = ldap_sasl_bind_s(gpLDAP, "cn=root,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au", + rv = ldap_sasl_bind_s(gpLDAP, "cn=admin,dc=ucc,dc=gu,dc=uwa,dc=edu,dc=au", "", &cred, NULL, NULL, &servcred); if(rv) { fprintf(stderr, "ldap_start_tls_s: %s\n", ldap_err2string(rv)); @@ -130,6 +122,51 @@ void Init_Cokebank(const char *Argument) #endif } +#if 1 +static int Bank_int_ReadDatabase(void) +{ + if( gaBank_Users ) return 1; + + // Get size + fseek(gBank_File, 0, SEEK_END); + giBank_NumUsers = ftell(gBank_File) / sizeof(gaBank_Users[0]); + fseek(gBank_File, 0, SEEK_SET); + // Read data + gaBank_Users = malloc( giBank_NumUsers * sizeof(gaBank_Users[0]) ); + fread(gaBank_Users, sizeof(gaBank_Users[0]), giBank_NumUsers, gBank_File); + + return 0; +} +#else +static int Bank_int_ReadDatabase(void) +{ + char buf[BUFSIZ]; + // Alternate data format + // > Plain Text + // ,,,,,, + fseek(gBank_File, 0, SEEK_SET); + + while(1) + { + fgets(buf, BUFSIZ-1, gBank_File); + } + #endif +} +#endif + +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; +} + /** * \brief Transfers money from one user to another * \param SourceUser Source user @@ -170,19 +207,6 @@ int Bank_GetMaxID(void) return giBank_NumUsers; } -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; -} - /** * \brief Get the User ID of the named user */