Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // database.c
- #include "database.h"
- // ============================================================================
- struct Database* createDatabase()
- {
- Database* database = (Database*)malloc(sizeof(Database));
- if(database == NULL) {
- fprintf(stderr, "allocation failed");
- return NULL;
- } else {
- database->firstEntry = NULL;
- database->lastEntry = NULL;
- return database;
- }
- }
- // ============================================================================
- void freeDatabase(struct Database* database)
- {
- Entry *entry = database->firstEntry;
- while(entry != NULL) {
- Entry *tmpEntry = entry;
- entry = entry->next;
- freeEntry(tmpEntry);
- }
- free(database);
- }
- // ============================================================================
- void freeEntry(struct Entry* entry) {
- free(entry->username);
- free(entry->passwd);
- free(entry->comment);
- free(entry->home);
- free(entry->shell);
- free(entry);
- }
- // ============================================================================
- int insertEntry(struct Database *database, struct Entry* newEntry, char* username)
- {
- if(database == NULL || newEntry == NULL || username == NULL) {
- return ERROR_WRONG_ARGUMENTS;
- }
- newEntry->next = NULL;
- if(database->firstEntry == NULL) {
- database->firstEntry = newEntry;
- } else {
- Entry* link = database->firstEntry;
- while(link->next != NULL) {
- if(link->uid == newEntry->uid) {
- return ERROR_DUPLICATE_ENTRY;
- }
- link = link->next;
- }
- // if uid of first argument == uid of second argument
- if(link->uid == newEntry->uid) {
- return ERROR_DUPLICATE_ENTRY;
- }
- link->next = newEntry;
- }
- strcpy(username, newEntry->username);
- return NO_ERROR;
- }
- // ============================================================================
- int removeEntry(struct Database* database, int id, char* username) {
- if(id < 1 || id > countEntries(database)) {
- return ERROR_ID_NOT_IN_INTERVAL;
- }
- if(database == NULL || username == NULL) {
- return ERROR_WRONG_ARGUMENTS;
- }
- Entry *entry = database->firstEntry;
- int i = 1;
- if(id == 1) {
- Entry* tmpEntry = entry->next;
- strcpy(username, entry->username);
- freeEntry(entry);
- database->firstEntry = tmpEntry;
- return NO_ERROR;
- } else {
- while(entry != NULL) {
- if(i+1 == id) {
- Entry* tmpEntry = entry->next->next;
- strcpy(username, entry->next->username);
- freeEntry(entry->next);
- entry->next = tmpEntry;
- return NO_ERROR;
- }
- i++;
- entry = entry->next;
- }
- }
- return ERROR_ID_NOT_IN_INTERVAL;
- }
- // ============================================================================
- struct Entry* loadEntry(char* line) {
- if(line == NULL) {
- return NULL;
- }
- Entry* entry = (Entry*)malloc(sizeof(Entry));
- if(entry == NULL) {
- fprintf(stderr, "allocation failed");
- return NULL;
- }
- char* tmp = (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- if(tmp == NULL) {
- fprintf(stderr, "allocation failed");
- free(entry);
- return NULL;
- }
- entry->username = (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- memset(entry->username,0,sizeof(char)*(MAX_FIELD_LENGTH+1));
- entry->passwd= (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- memset(entry->passwd,0,sizeof(char)*(MAX_FIELD_LENGTH+1));
- entry->uid = 0;
- entry->gid = 0;
- entry->comment= (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- memset(entry->comment,0,sizeof(char)*(MAX_FIELD_LENGTH+1));
- entry->home = (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- memset(entry->home,0,sizeof(char)*(MAX_FIELD_LENGTH+1));
- entry->shell = (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- memset(entry->shell,0,sizeof(char)*(MAX_FIELD_LENGTH+1));
- strncpy(entry->username, line, strlen(line) - strlen(strchr(line, ':')));
- entry->username[strlen(line) - strlen(strchr(line, ':'))] = '\0';
- if(strlen(entry->username) == 0){
- freeEntry(entry);
- free(tmp);
- return NULL;
- }
- line = strchr(line, ':')+1;
- strncpy(entry->passwd, line, strlen(line) - strlen(strchr(line, ':')));
- entry->passwd[strlen(line) - strlen(strchr(line, ':'))] = '\0';
- line = strchr(line, ':')+1;
- strncpy(tmp, line, strlen(line) - strlen(strchr(line, ':')));
- tmp[strlen(line) - strlen(strchr(line, ':'))] = '\0';
- line = strchr(line, ':')+1;
- if(atoi(tmp) == 0) {
- freeEntry(entry);
- free(tmp);
- return NULL;
- }
- entry->uid = atoi(tmp);
- strncpy(tmp, line, strlen(line) - strlen(strchr(line, ':')));
- tmp[strlen(line) - strlen(strchr(line, ':'))] = '\0';
- line = strchr(line, ':')+1;
- entry->gid = atoi(tmp);
- strncpy(entry->comment, line, strlen(line) - strlen(strchr(line, ':')));
- entry->comment[strlen(line) - strlen(strchr(line, ':'))] = '\0';
- line = strchr(line, ':')+1;
- strncpy(entry->home, line, strlen(line) - strlen(strchr(line, ':')));
- entry->home[strlen(line) - strlen(strchr(line, ':'))] = '\0';
- line = strchr(line, ':')+1;
- strcpy(entry->shell, line);
- // trim whitespaces from end of the entry->shell
- char *end;
- end = entry->shell + strlen(entry->shell) - 1;
- while(end > entry->shell && isspace(*end)) end--;
- *(end+1) = '\0';
- free(tmp);
- return entry;
- }
- // ============================================================================
- struct Entry* fillEntry() {
- char *tmpString = (char*)malloc(sizeof(char)*MAX_FIELD_LENGTH);
- if(tmpString == NULL) {
- fprintf(stderr, "allocation failed");
- return NULL;
- }
- while (getchar() != '\n') {;}
- Entry *entry = (Entry*)malloc(sizeof(Entry));
- entry->username = (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- memset(entry->username,0,sizeof(char)*MAX_FIELD_LENGTH);
- entry->passwd = (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- memset(entry->passwd,0,sizeof(char)*MAX_FIELD_LENGTH);
- entry->comment = (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- memset(entry->comment,0,sizeof(char)*MAX_FIELD_LENGTH);
- entry->home = (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- memset(entry->home,0,sizeof(char)*MAX_FIELD_LENGTH);
- entry->shell = (char*)malloc(sizeof(char)*(MAX_FIELD_LENGTH+1));
- memset(entry->shell,0,sizeof(char)*MAX_FIELD_LENGTH);
- memset(tmpString,0,sizeof(char)*MAX_FIELD_LENGTH);
- printf("username: ");
- fgets(tmpString,MAX_FIELD_LENGTH+1,stdin);
- if(strlen(tmpString) == 0){
- free(tmpString);
- freeEntry(entry);
- return NULL;
- }
- strncpy(entry->username, tmpString, strlen(tmpString)-1);
- memset(tmpString,0,sizeof(char)*MAX_FIELD_LENGTH);
- printf("password: ");
- fgets(tmpString,MAX_FIELD_LENGTH+1,stdin);
- strncpy(entry->passwd, tmpString, strlen(tmpString)-1);
- memset(tmpString,0,sizeof(char)*MAX_FIELD_LENGTH);
- printf("UID: ");
- fgets(tmpString,MAX_FIELD_LENGTH+1,stdin);
- if(atoi(tmpString) == 0) {
- free(tmpString);
- freeEntry(entry);
- return NULL;
- }
- entry->uid = atoi(tmpString);
- memset(tmpString,0,sizeof(char)*MAX_FIELD_LENGTH);
- printf("GID: ");
- fgets(tmpString,MAX_FIELD_LENGTH+1,stdin);
- entry->gid = atoi(tmpString);
- memset(tmpString,0,sizeof(char)*MAX_FIELD_LENGTH);
- printf("comment: ");
- fgets(tmpString,MAX_FIELD_LENGTH+1,stdin);
- strncpy(entry->comment, tmpString, strlen(tmpString)-1);
- memset(tmpString,0,sizeof(char)*MAX_FIELD_LENGTH);
- printf("home: ");
- fgets(tmpString,MAX_FIELD_LENGTH+1,stdin);
- strncpy(entry->home, tmpString, strlen(tmpString)-1);
- memset(tmpString,0,sizeof(char)*MAX_FIELD_LENGTH);
- printf("shell: ");
- fgets(tmpString,MAX_FIELD_LENGTH+1,stdin);
- strncpy(entry->shell, tmpString, strlen(tmpString)-1);
- // trim whitespaces from end of the entry->shell
- char *end;
- end = entry->shell + strlen(entry->shell) - 1;
- while(end > entry->shell && isspace(*end)) end--;
- *(end+1) = '\0';
- free(tmpString);
- return entry;
- }
- // ============================================================================
- int changePassword(struct Database* database, int entryID, char* newPassword) {
- if(entryID < 1 || entryID > countEntries(database)) {
- return ERROR_ID_NOT_IN_INTERVAL;
- }
- if(database == NULL || newPassword == NULL) {
- return ERROR_WRONG_ARGUMENTS;
- }
- Entry *entry = database->firstEntry;
- int i = 1;
- while(entry != NULL) {
- if(i == entryID) {
- memset(entry->passwd,0,sizeof(char)*(MAX_FIELD_LENGTH+1));
- strcpy(entry->passwd, newPassword);
- }
- i++;
- entry = entry->next;
- }
- return NO_ERROR;
- }
- // ============================================================================
- void printDatabase(struct Database* database) {
- if(database == NULL) {
- fprintf(stderr, "no database connection");
- }else{
- if(database->firstEntry == NULL) {
- puts("No entries found");
- } else {
- Entry* entry = database->firstEntry;
- unsigned int i = 1;
- while(entry != NULL) {
- printf("%d\t%s\t%s\t%u\t%u\t%s\t%s\t%s\r\n", i++, entry->username, entry->passwd,
- entry->uid, entry->gid, entry->comment, entry->home, entry->shell);
- entry = entry->next;
- }
- }
- }
- }
- // ============================================================================
- int sortDatabase(struct Database* database, int desc)
- {
- // TODO: implement
- return NO_ERROR;
- }
- // ============================================================================
- void swapEntries(struct Entry *a, struct Entry *b)
- {
- // TODO: implement
- }
- // ===========================================================================
- int saveDatabase(const char* filename, const struct Database* database) {
- // check input arguments
- if(database == NULL || filename == NULL)
- return ERROR_WRONG_ARGUMENTS;
- FILE* file;
- struct Entry* entry;
- // try to open file
- if(!(file = fopen(filename, "w")))
- return ERROR_OPENING_FILE;
- entry = database->firstEntry;
- // go through all the entries and write to the standard output
- while(entry != NULL) {
- fprintf(file, "%s:%s:%u:%u:%s:%s:%s\n", entry->username, entry->passwd,
- entry->uid, entry->gid, entry->comment, entry->home, entry->shell);
- entry = entry->next;
- }
- // close access to file stream
- fflush(file);
- fclose(file);
- return NO_ERROR;
- }
- // ============================================================================
- int countEntries(struct Database* database) {
- Entry* entry = database->firstEntry;
- int counter = 0;
- while(entry != NULL) {
- counter++;
- entry = entry->next;
- }
- return counter;
- }
- // ============================================================================
- int loadDatabase(struct Database** db, char* filename, int silence) {
- // check name
- if(!filename) {
- fprintf(stderr, "wrong filename!\n");
- return ERROR_WRONG_ARGUMENTS;
- }
- // try to open file
- FILE* inputF = fopen(filename, "rb");
- // check opening correctness
- if(inputF == NULL) {
- fprintf(stderr, "error during opening filename '%s'!\n", filename);
- return ERROR_OPENING_FILE;
- }
- char line[MAX_LINE_LENGTH]; // line buffer
- memset(line, 0, MAX_LINE_LENGTH); // clear buffer
- // create empty database
- if(*db == NULL)
- *db = createDatabase();
- if(*db == NULL)
- return ERROR_ALLOCATION;
- // temporary space for username
- char* username = (char*)malloc(sizeof(char)*MAX_FIELD_LENGTH);
- if(username == NULL)
- return ERROR_ALLOCATION;
- // read all lines till the end of the file
- while(fgets(line, MAX_LINE_LENGTH, inputF) != NULL) {
- memset(username, 0, MAX_FIELD_LENGTH);
- // load entry
- struct Entry* entry = loadEntry(line);
- // insert it into database
- int insertResult = ERROR;
- if(entry != NULL)
- insertResult = insertEntry(*db, entry, username);
- // print information about loading
- if(!silence) {
- switch(insertResult) {
- case NO_ERROR:
- printf("user '%s' inserted\n", username);
- break;
- case ERROR_DUPLICATE_ENTRY:
- printf("user with the same UID already exists!\n");
- freeEntry(entry);
- break;
- case ERROR_WRONG_ARGUMENTS:
- printf("entry wasn't inserted!\n");
- freeEntry(entry);
- break;
- case ERROR:
- printf("loading entry failed\n");
- break;
- }
- }
- memset(line, 0, MAX_LINE_LENGTH); // clear line buffer
- }
- // release temporary memory
- free(username);
- username = NULL;
- // close access to file stream
- fclose(inputF);
- return NO_ERROR;
- }
- // ============================================================================
- // main.c
- #define MAINIMPL
- #ifdef MAINIMPL
- #define CLEAR_SCREEN "clear" // windows = "cls", unix = "clear"
- #include "database.h"
- #include "entry.h"
- int guiPrintMenu();
- void guiCreateDatabase();
- void guiLoadDatabase();
- void guiSaveDatabase();
- void guiPrintDatabase();
- void guiInsertEntry();
- void guiMainLoop();
- void guiRemoveEntry();
- void guiSortDatabase();
- void guiChangePassword();
- #define NO_ACTIVE_DATABASE "No active database!\n"
- int returnCode = 0;
- struct Database* database = NULL;
- // ============================================================================
- /**
- * Entry point of the application
- *
- * @return 0 = no error, !0 otherwise
- */
- int main(int argc, char* argv[]) {
- printf("***************************************************************\n");
- printf(" HW06 - database \n");
- printf("***************************************************************\n");
- // test number of input parameters
- if(argc > 2)
- printf("Wrong input parameters! \"database [file]\"");
- // load input database file if the file was provided
- if(argc == 2) {
- loadDatabase(&database, argv[1], 0);
- printf("***************************************************************\n\n");
- }
- // main menu loop
- guiMainLoop();
- // have to free allocated memory
- if(database != NULL)
- freeDatabase(database);
- printf(" Good luck! \n");
- printf("***************************************************************\n");
- return returnCode;
- }
- // ============================================================================
- /**
- * Print main user's menu.
- */
- int guiPrintMenu() {
- char ch = '0';
- // print all actions
- printf("1) create new database\n");
- printf("2) list entries\n");
- printf("3) insert entry\n");
- printf("4) remove entry\n");
- printf("5) change password\n");
- printf("6) save\n");
- printf("7) load\n");
- printf("8) BONUS: sort\n");
- printf("9) exit\n");
- // get next choice
- while(ch < '1' || ch > '9') {
- if((int)ch != 10) // skip EOL flag
- printf("choice: ");
- scanf("%c", &ch);
- }
- return ch - '0';
- }
- // ============================================================================
- /**
- * Main GUI loop handler. Calls while user wants to work with the application.
- */
- void guiMainLoop() {
- int result = guiPrintMenu();
- // clear screen
- system(CLEAR_SCREEN);
- printf("***************************************************************\n");
- switch(result) {
- case 1:
- guiCreateDatabase();
- break;
- case 2:
- guiPrintDatabase();
- break;
- case 3:
- guiInsertEntry();
- break;
- case 4:
- guiRemoveEntry();
- break;
- case 5:
- guiChangePassword();
- break;
- case 6:
- guiSaveDatabase();
- break;
- case 7:
- guiLoadDatabase();
- break;
- case 8:
- guiSortDatabase();
- break;
- case 9:
- return;
- default:
- printf("wrong choice!\n choice: ");
- break;
- }
- printf("***************************************************************\n\n");
- // redisplay the menu
- guiMainLoop();
- }
- // ============================================================================
- /**
- * This method creates a new database connection and set it as active.
- * If some old connection already exists, kill it.
- */
- void guiCreateDatabase() {
- // if any database connection is available, kill them
- if(database != NULL) {
- freeDatabase(database);
- database = NULL;
- }
- // create a new database
- if((database = createDatabase()) == NULL)
- fprintf(stderr, "error while creating database\n");
- else
- printf("database created\n");
- }
- // ============================================================================
- /**
- * User interface for inserting a new entry into database.
- * User is asked for filling up the fields, then new entry is created and stored
- * into the database.
- */
- void guiInsertEntry() {
- // check if some connection is available
- if(database == NULL)
- guiCreateDatabase();
- struct Entry* entry = NULL;
- // get the data fields from the user
- if((entry = fillEntry()) == NULL)
- printf("error while filling the entry!\n");
- else {
- printf("***************************************************************\n");
- char* username = (char*)malloc(sizeof(char)*MAX_FIELD_LENGTH);
- if(username == NULL) {
- fprintf(stderr, "allocation failed\n");
- return;
- }
- int result = insertEntry(database, entry, username);
- // add entry into database
- switch(result) {
- case ERROR_DUPLICATE_ENTRY:
- printf("user with this UID already exists!\n");
- freeEntry(entry);
- break;
- case ERROR_WRONG_ARGUMENTS:
- printf("entry wasn't inserted!\n");
- freeEntry(entry);
- break;
- default:
- printf("user '%s' inserted\n", username);
- break;
- }
- free(username);
- username = NULL;
- }
- }
- // ============================================================================
- /**
- * User interface for removing an entry.
- */
- void guiRemoveEntry() {
- int id = -1, result = 1;
- if(database != NULL) {
- // get removing entry ID - NOT a UID or GID
- printf("write ID of entry to remove: ");
- scanf("%d", &id);
- printf("\n");
- // allocate temporary memory for user name from removed entry
- char* username = (char*)malloc(sizeof(char)*MAX_FIELD_LENGTH);
- if(username == NULL) {
- fprintf(stderr, "allocation failed\n");
- return;
- }
- memset(username, 0, MAX_FIELD_LENGTH);
- // try to remove entry
- result = removeEntry(database, id, username);
- if(result)
- fprintf(stderr, "error while removing entry with ID=%u\n", id);
- else
- printf("user '%s' removed\n", username);
- free(username);
- username = NULL;
- } else
- fprintf(stderr, NO_ACTIVE_DATABASE);
- }
- // ============================================================================
- /**
- * User interface for changing password.
- */
- void guiChangePassword() {
- unsigned id = 0;
- char passwd[MAX_FIELD_LENGTH];
- if(database != NULL) {
- // get removing entry ID - NOT a UID or GID
- printf("write ID of changing entry: ");
- scanf("%u", &id);
- printf("\n");
- // get new password
- printf("new password: ");
- scanf("%s", passwd);
- printf("\n");
- if(changePassword(database, id, passwd))
- fprintf(stderr, "error during changing password in entry of ID=%u\n", id);
- else
- printf("password changed\n");
- } else
- fprintf(stderr, NO_ACTIVE_DATABASE);
- }
- // ============================================================================
- /**
- * This method prints the database to the standard output in a given format.
- */
- void guiPrintDatabase() {
- if(database == NULL) // check is connection exists
- printf(NO_ACTIVE_DATABASE);
- else {
- // print the header
- printf("list of %u entries :\n\n", countEntries(database));
- printf("%2s. %-10s %-15s %-5s %-5s %-20s\n", "ID", "USERNAME",
- "PASSWORD", "UID", "GID", "COMMENT");
- printf(" %-20s %-20s\n", "HOME", "SHELL");
- printf("_______________________________________________________________\n");
- // prints all entries
- printDatabase(database);
- }
- }
- // ============================================================================
- /**
- * User interface for loading database.
- */
- void guiLoadDatabase() {
- char filename[MAX_FILENAME_SIZE];
- printf("database to load: ");
- scanf("%s", filename);
- printf("\n");
- // read database
- loadDatabase(&database, filename, 0);
- }
- // ============================================================================
- /**
- * User interface for saving database.
- */
- void guiSaveDatabase() {
- char filename[MAX_FILENAME_SIZE];
- // gets target filename from user
- printf("target filename: ");
- scanf("%s", filename);
- printf("\n");
- if(saveDatabase(filename, database) != 0)
- fprintf(stderr, "error while storing to file '%s'!\n", filename);
- else
- printf("storing to file '%s' was successful\n", filename);
- }
- // ============================================================================
- /*
- * User interface for sorting database.
- */
- void guiSortDatabase() {
- int desc = 0;
- if(database == NULL)
- printf(NO_ACTIVE_DATABASE);
- else {
- // get sorting type
- printf("descendant(0) or ascendant(1)?: ");
- scanf("%d", &desc);
- printf("\n");
- if(sortDatabase(database, desc))
- printf("database sorting failed\n");
- else
- printf("database sorted (%s)\n", (desc == 0) ? "descendant" : "ascendant");
- }
- }
- // ============================================================================
- #endif
- // entry.h
- #ifndef ENTRY_H
- #define ENTRY_H
- /**
- *
- */
- typedef struct Entry
- {
- char *username; // user's name
- char *passwd; // user's password
- unsigned short uid; // user's ID
- unsigned short gid; // user's group ID
- char *comment; // user's comment
- char *home; // user's home directory
- char *shell; // user's shell
- struct Entry *next; // pointer to the next user
- struct Entry *previous; // pointer to the previous user - use it only when implementing BONUS
- } Entry;
- #endif
- // database.h
- #ifndef DATABASE_H
- #define DATABASE_H
- #include "entry.h"
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_FILENAME_SIZE 100 // maximal length of input/output filename
- #define MAX_LINE_LENGTH 500 // maximal length of input line
- #define MAX_FIELD_LENGTH 30 // maximal length of data attribute when inserting by the user
- #define NO_ERROR 0
- #define ERROR_OPENING_FILE 1
- #define ERROR_CREATING_DATABASE 2
- #define ERROR_INSERTING_ENTRY 3
- #define ERROR_WRONG_ARGUMENTS 4
- #define ERROR_DUPLICATE_ENTRY 5
- #define ERROR_ID_NOT_IN_INTERVAL 6
- #define ERROR 7
- #define ERROR_ALLOCATION 8
- #define ZERO_CHAR '\0'
- /**
- *
- */
- typedef struct Database
- {
- // first database entry pointer
- struct Entry* firstEntry;
- // only for BONUS: pointer to the last entry of the database
- struct Entry* lastEntry;
- } Database;
- /**
- * Save the database to given external file.
- *
- * @param file Target file.
- * @param database Which database.
- * @return NO_ERROR = no error, ERROR_WRONG_ARGUMENTS = wrong input arguments, ERROR_OPENING_FILE = failed when opening file
- */
- int saveDatabase(const char* file, const struct Database* database);
- /**
- * Creates new empty database without any entry and set all pointers to NULL.
- *
- * @return Pointer to the memory, where the structure was created.
- */
- struct Database* createDatabase();
- /**
- * Prints all entries to standard output in "user readable" format.
- * If database is NULL, print "no database connection" to stderr.
- *
- * @param database Database to print.
- */
- void printDatabase(struct Database* database);
- /**
- * Release all memory of given database which was allocated during working.
- */
- void freeDatabase(struct Database* database);
- /**
- * Release all memory of entry fields.
- */
- void freeEntry(struct Entry* entry);
- /**
- * Provide interface for creating new entry to the user. User fills each data field.
- * You can rely on that the maximum data attribute length won't be more than MAX_FIELD_LENGTH
- * and that the data on the input will be correct (ie. int where expected, ...).
- * Correct UID is from interval <1, unsigned short>.
- *
- * @return NULL if strlen(username) == 0 or UID == 0 , Entry otherwise
- */
- struct Entry* fillEntry();
- /**
- * Insert entry into database. New entry is placed to the end of database.
- *
- * @param database Target database.
- * @param newEntry Inserted entry.
- * @param username If entry is inserted, copy of username from inserted entry to inform the user.
- * @return NO_ERROR = no error, ERROR_WRONG_ARGUMENTS = wrong input arguments, ERROR_DUPLICATE_ENTRY = entry with the same UID already exists
- */
- int insertEntry(struct Database *database, struct Entry* newEntry, char* username);
- /**
- * Remove entry from the database.
- *
- * @param database Database from which the entry should be removed.
- * @param id ID of removing entry. ID is not the same as UID or GID and is inside interval <1, num_of_entries>
- * @param username If entry is removed, copy of username from removed entry to inform the user.
- * @return NO_ERROR = entry removed, ERROR_ID_NOT_IN_INTERVAL = id is not in interval , ERROR_WRONG_ARGUMENTS = wrong input arguments
- */
- int removeEntry(struct Database* database, int id, char* username);
- /**
- * Count all entries in database.
- *
- * @param database Database to be printed.
- * @return Number of entries in the database.
- */
- int countEntries(struct Database* database);
- /**
- * Parse the input line, create a new empty Entry and fills it with data.
- *
- * @param line One line from input file.
- * @return NULL = error during loading (wrong argument), Entry otherwise
- */
- struct Entry* loadEntry(char* line);
- /**
- * Load the database from file.
- *
- * @param db pointer to database, if NULL create a new database first
- * @param filename input filename
- * @param silence 0 no output, OTHERWISE print information about entry insertion to the stdout
- * @return NO_ERROR = load successful, ERROR_WRONG_ARGUMENTS = wrong input arguments,
- * ERROR_OPENING_FILE = error during opening input file, ERROR_ALLOCATION - error during memory allocation
- */
- int loadDatabase(struct Database** db, char* filename, int silence);
- // BONUS
- /**
- * Swap two entries with all of their bindings.
- *
- * @param a Entry
- * @param b Entry
- */
- void swapEntries(struct Entry* a, struct Entry* b);
- /**
- * Sort the given database according to UID entry attribute and desc argument.
- *
- * @param database Database to be sorted.
- * @param desc 0 = descendant, 1 = ascendant
- * @return NO_ERROR = no error, !0 = otherwise
- */
- int sortDatabase(struct Database* database, int desc);
- /**
- * Change the password to the given user - entry ID.
- *
- * @param database In which database change the entry.
- * @param entryID ID of the changing entry.
- * @param newPassword New user password.
- * @return NO_ERROR = no error, ERROR_ID_NOT_IN_INTERVAL = entry with ID doesn't exist,
- * ERROR_WRONG_ARGUMENTS = wrong arguments, ERROR_ALLOCATION = error during memory allocation
- */
- int changePassword(struct Database* database, int entryID, char* newPassword);
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement