Documente Academic
Documente Profesional
Documente Cultură
c*/
#include<stdio.h>
#include "application.h"
int main(int argc, const char *argv[]){
/*Entry Point of application*/
start_applicaton();
}
/*Application.h*/
/*we are using #ifndef macro to prevent multiple inclusion of header files*/
#ifndef application_h
#define application_h
/*Maximum length of user id and request id*/
#define ID_LENGTH 10
/*Maximum length of strings used in this project*/
/*user name, comment, address, etc.*/
#define STR_MAX_LENGTH 256
/*All application and user interface related function*/
/*Strings which will be used to seprate data of each line in given file*/
extern const char * DELIMITER;
/*Start Application*/
extern void start_application(void);
/*Show welcome screen of application*/
extern void welcome(void);
/*Show menu for operator with various option*/
extern void render_menu(void);
/*Add new user who wants to request for elictricty*/
extern void add_user_page(void);
/*operator can view specific user and request done by that user*/
extern void view_user_page(void);
/*operator can add new request which is requested by specifie user*/
extern void add_request_page(void);
/*In future, if operator wants to edit request (i.e change status or enter
comments).*/
extern void edit_request_page(void);
/*If operator wants to view specific request*/
extern void view_request_page(void);
/*
Close application after operator completes his work
input : status of application.
input : message to display on the screen in case of erroneous exit.
*/
extern void close_application(int exit_Status, const char * messages);
/*
Function which set cursor at X, Y on display
input : X coordinates for screen
input : Y coordinates for screen
*/
extern void gotoxy(int x, int y);
/*
Function which display messages and additional info on screen
input : messages to be displayed
input : additional information along with messages
*/
extern void show_dialogue_box(const char * messages, char * info);
#endif /*application_h*/
/*application.c*/
#include"application.h"
#include"user.h"
#include"request.h"
#include"helper.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void start_application(){
/*open user file in append mode and set user_fp points to file's stream*/
user_fp = fopen(user_file_name, "a+");
/*open request file in append mode and set request_fp points to file's stream*/
request_fp = fopen(request_file_name, "a+");
/*If pointer cannot be set then warn operator about it and quit program*/
if(user_fp == NULL|| request_fp == NULL){
printf("Something went wrong, while opening database file.\n");
printf("Please make sure that file %s resides in current directory\n",
user_file_name);
getchar();
retrun;
}
gotoxy(25,5);
printf("Welcome to electricity board\n");
gotoxy(27,23);
printf("Press Any key to continue");
render_menu();
}
void render_menu(){
system("clear");
gotoxy(25,5);
printf("Select Any Optio From Below");
gotoxy(25,7);
printf("1. Add new user");
gotoxy(25,8);
printf("2. View user");
gotoxy(25,9);
printf("3. Add new connection request");
gotoxy(25,10);
printf("4. Edit existing request");
gotoxy(25,11);
printf("5. View Request");
gotoxy(25,12);
printf("6. exit");
gotoxy(30,15);
printf("Enter Your choice");
gotoxy(50,15);
scanf("%c", &choice);
flush_buffer();
switch (choice){
case '1':
add_user_page();
break;
case '2':
view_user_page();
break;
case '3':
add_request_page();
break;
case '4':
edit_request_page();
break;
case '5':
view_request_page();
break;
case '6':
close_application(EXIT_SUCESS, "");
default:
close_application(EXIT_SUCESS, "");
break;
}
}
void add_user_page(){
/*Request id which will be stored in file*/
unsinged long user_id = last_user_id + 1;
/*User Name*/
char user_name[STR_MAX_LENGTH];
system("clear");
gotoxy(0,1);
printf("%s", enter_detail_label);
gotoxy(0,3);
printf("%s", user_name_label);
gotoxy(0,4);
printf("%s", user_contact_number);
user.user_id = user_id;
user.user_name = user_name;
user.user_contact = user_contact;
fseek(user_fp, 0, SEEK_END);
fputs(user_info_string, user_fp);
fputs("\n", user_fp);
void view_user_page(){
/*User Id which operator wants to search.*/
usigned long user_id = 0;
/*User Details.*/
user * user = NULL;
system("clear");
gotoxy(41,1);
if(user_request == NULL){
printf("Request Details\n");
gotoxy(41,3);
printf("No request made by this user");
gotoxy(0,24);
printf("Press any key to return to main menu");
/*Wait for user to press some key*/
getchar();
render_menu();
return;
}
gotoxy(0,24);
printf("Press any key to return to main menu");
void view_request_page(){
/*Request ID which operator wants to search.*/
Unsinged long request_id=0;
/*Request Details.*/
Request * request = NULL;
system("clear");
gotoxy(0,24);
void add_request_page(){
/*User details*/
user * user = NULL;
/*Request details.*/
Request request;
system("clear");
gotoxy(0,1);
printf("%s", enter_details_lables);
gotoxy(0,3);
printf("%s", user_id_label);
gotoxy(0,4);
printf("%s", request_comment_label);
gotoxy(0,5);
printf("%s", request_address_label);
gotoxy((int)strlen(request_comment_label),4);
/*using fgets because string can be empty*/
fgets(comment, STR_MAX_LENGTH, stdin);
gotoxy((int)strlen(request_address_label),5);
scanf("%[^\n]s",address);
flush_buffer();
user = get_user(user_id);
/*if cannot user corresponding to 'user_id' then simply redirect operator to main
menu*/
if(user == NULL){
system("clear");
show_dialouge_box("No such user exists." "Please contact administrator.");
render_menu();
}
request.request_id =request_id;
request.requested_by = user;
request.status = PENDING;
request.comment = comment;
request.address = address;
request_info_string = get_request_string(&request);
release_users();
/*Request details.*/
Request * request = NULL;
system("clear");
peintf("Enter Request id :");
scanf("%lu", &request_id);
flush_buffer();
printf("\n\n*******************************************************************");
printf("%s", enter_new_status_label);
printf("\n%s", enter_new_suggestion_label);
gotoxy((int)strlen(enter_new_label), 14);
scanf("%d", &new_status);
flush_buffer();
printf("\n%s", enter_new_comment_label);
fgets(new_comment, STR_MAX_LENGTH, stdin);
free(request->comment);
request->comment = strdup(new_comment);
system("clear");
/*Line 1*/
gotoxy(left_offset, line_offset++);
printf("********************************************");
/*Line 2*/
gotoxy(left_offset, line_offset);
printf("*");
/*Line 3*/
gotoxy(left_offset, line_offset);
printf("*");
gotoxy((80 - message_length)/2, line_offset);
printf("%s", message);
gotoxy(left_offset + length -1, line_offset++);
printf("*");
/*Line 5*/
gotoxy(left_offste, line_offset);
printf("*");
gotoxy((int) ((80 - info_length)/2), line_offset);
printf("%s", press_any_key);
gotoxy(left_offset + length -1, line_offset++);
printf("*");
/*Line 6*/
gotoxy(left_offset, line_offset);
printf("*");
gotoxy(left_offset + length -1, line_offset++);
printf("*");
/*Line 7*/
gotoxy(left_offset, line_offset++);
printf("****************************************");
getchar();
}
/*user.h*/
/*we are using #ifndef macro to prevent multiple inclusion of header files*/
#ifndef user_h
#define user_h
#include<stdio.h>
}user;
/*function which converts string (which we read from i/p file) into 'user' data.
input : comma seprated string which contain user's information.
output : pointer to 'user' which contain all user's information in structure
format.
*/
user * (get_user_info);
/*Function which read all lines from file, converts and returns array for all users
input : FILE pointer of users file.
output : pointer to array of all users.
*/
#endif /*user_h*/
/*user.c*/
#include "user.h"
#include "helpers.h"
#include "application.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char user_id_string[ID_LENGTH];
int COLUMN_TAG = 0;
switch (COLUMN_TAG) {
case 0:
read_user->user_id = atoi(token);
COLUMN_TAG++;
free(token);
break;
case 1:
read_user->user_name = strdup(token);
COLUMN_TAG++;
free(token);
break;
case 2:
read_user->user_contact = strdup(token);
COLUMN_TAG++;
free(token);
break;
default:
close_application(EXIT_FALIURE, "Error while Parsing user database.....");
break;
}
}
free(user_info_copy);
if(COLUMN_TAG ! = 3){
return read_user;
/*Get user structure from string, which we have 'read from file'*/
/*we do not need to release memory occupied by 'read_user' because.*/
/*we are going to store address of this variable into 'all_user' array*/
/*Which will be released at end of the program.*/
read_user = get_user_info(user_info);
/*If we cannot create user sucessfully, then just continue to next cycle.*/
if(read_user == NULL){
countinue;
}
/*below function can leak memory, so clear memory before returning from this
function*/
char * updated_user_string = get_user_string(updated_user);
char line[STR_MAX_LENGTH];
if(read_user->user_id == updated_user->user_id){
/*open that file in 'a+' mode so we have reached state here we left off.*/
user_fp = fopen(user_file_name, "a+");
free(updated_user_string);
}
void release_user(){
if(!users){
return;
for(int i =0 ; i < number_of_user ; i++)){
free(users[i]->user_name);
free(users[i]->user_contact);
}
free(users);
users = NULL:
/*Request.h*/
#indef request_h
#define request_h
#include "user.h"
#include <stdio.h>
char * get_request_status_string(request_status);