Skip to content

Instantly share code, notes, and snippets.

@spacelatte
Created September 5, 2016 23:40
Show Gist options
  • Save spacelatte/be8b154993fa6cfce47b17230f8cca36 to your computer and use it in GitHub Desktop.
Save spacelatte/be8b154993fa6cfce47b17230f8cca36 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define flush fpurge
#define SEP "+-------------+------------------+---------------+---+"
#define LINE "| % 11d | %16s | %13lu | %1c |"
typedef struct {
void *data;
void *next;
} node_t;
typedef struct {
unsigned id;
char name[24];
unsigned long phone;
char gender;
} person_t;
unsigned ai = 1;
const char magic[] = { 'm'-'a', 'e'-'a', 'r'-'a', 't'-'a', 0x0 };
node_t *create(void *data)
{
node_t *n = (node_t*)malloc(sizeof(node_t));
n->data = data;
n->next = NULL;
return n;
}
void add(node_t *list, node_t *node)
{
while(list->next != NULL)
list = list->next;
list->next = node;
return;
}
void delete(node_t *list, node_t *node)
{
if(node == NULL)
return;
while(list->next != NULL && list->next != node)
list = list->next;
node_t *rem = list->next;
if(rem != NULL)
list->next = rem->next;
else
list->next = NULL;
free(rem->data);
free(rem);
return;
}
node_t *search(node_t *list, node_t *node)
{
while(list->next != NULL && list->next != node)
list = list->next;
return list->next;
}
node_t *search_data(node_t *list, node_t *data)
{
while(list->next != NULL && ((node_t*)list->next)->data != data)
list = list->next;
return list->next;
}
person_t *person_new(unsigned id, char *name, unsigned long phone, char gender)
{
person_t *p = (person_t*)malloc(sizeof(person_t));
p->id = id;
p->phone = phone;
p->gender = gender;
strcpy(p->name,name);
return p;
}
void person_free(person_t *person)
{
//free(person->name);
free(person);
return;
}
node_t *uid_to_node(node_t *list, unsigned id)
{
while(list->next != NULL)
{
person_t *p = ((node_t*)list->next)->data;
if(p != NULL && p->id == id)
return list->next;
list = list->next;
}
printf("error: user couldnt be found!");
return NULL;
}
short menu(void)
{
short sel = -1;
printf(
"0. exit\n"
"1. add new user to list\n"
"2. remove user from list\n"
"3. list users in list\n"
"4. edit user in list\n"
"5. change auto increment start point\n"
"\n"
"9. open from file\n"
"8. save to file\n"
"7. close current file\n"
">> your selection: "
);
scanf(" %hd",&sel);
flush(stdin);
return sel;
}
void menu_add_handler(node_t *list)
{
const char *q[] = {
"now you are adding user please enter\n"
"appropiate information about person\n",
"name of person: ",
"gender of person [m/f]: ",
"phone number: ",
"assign id? (",
", put dot for auto): ",
NULL
};
person_t *person = (person_t*)malloc(sizeof(person_t));
//person->name = (char*)malloc(32*sizeof(char));
printf("%s",q[0]);
printf("%s",q[1]);
scanf("%[^\n]",(person->name));
printf("%s",q[2]);
scanf(" %c",&(person->gender));
printf("%s",q[3]);
scanf(" %lu",&(person->phone));
char buf[11];
printf("%s%d%s",q[4],ai,q[5]);
flush(stdin);
fgets(buf,11,stdin);
if(buf[0] == '\n' || buf[0] == '.')
person->id = (ai++);
else
person->id = (unsigned)atoi(buf);
add(list,create(person));
return;
}
void menu_del_handler(node_t *list)
{
unsigned id = -1;
printf("enter user id to remove: ");
scanf(" %u",&id);
delete(list,uid_to_node(list,id));
return;
}
void menu_list_handler(node_t *list)
{
if(!list->next)
{
printf("error: empty list\n");
return;
}
printf(" " SEP " \n" " | %11s | %16s | %13s | %1s | \n",
"id","name","phone","G");
while(list->next != NULL)
{
person_t *p = (person_t*)((node_t*)list->next)->data;
printf( " " SEP " \n" " " LINE " \n"
// "info of %d:\n"
// "\tname: %s\n"
// "\tphone: %lu\n"
// "\tgender: %c\n"
// "\n"
,p->id,p->name,p->phone,p->gender
);
list = list->next;
}
printf(" " SEP " \n");
return;
}
void menu_edit_handler(node_t *list)
{
printf("enter user id to edit: ");
return;
}
void menu_open_handler(node_t *list)
{
char file[256];
printf("enter path/filename to open: ");
scanf(" %[^\n]",file);
FILE *fp = fopen(file,"rb");
fread(file,sizeof(magic),1,fp);
for(int i=0;i<sizeof(magic);i++)
if(file[i] != magic[i])
{
printf("error! invalid file\n");
return;
}
while(!feof(fp))
{
person_t *p = (person_t*)malloc(sizeof(person_t));
fread(p,sizeof(person_t),1,fp);
if(p->id == 0)
break;
add(list,create(p));
}
fclose(fp);
return;
}
void menu_save_handler(node_t *list)
{
char file[256];
printf("enter path/filename to save: ");
scanf(" %[^\n]",file);
FILE *fp = fopen(file,"wb");
fwrite(magic,sizeof(magic),1,fp);
while(list->next != NULL)
{
fwrite((person_t*)((node_t*)list->next)->data,sizeof(person_t),1,fp);
list = list->next;
}
fclose(fp);
return;
}
node_t *root;
void recursive_delete(node_t *list)
{
if(list->next != NULL)
recursive_delete(list->next);
//person_free((person_t*)list->data);
delete(list,list->next);
return;
}
int main(int argc, char **argv)
{
root = create(NULL); // empty node
while(1)
{
switch(menu())
{
case 1:
menu_add_handler(root);
break;
case 2:
menu_del_handler(root);
break;
case 3:
menu_list_handler(root);
break;
case 4:
menu_edit_handler(root);
break;
case 5:
printf("new offset: ");
scanf(" %d",&ai);
break;
case 9:
menu_open_handler(root);
break;
case 8:
menu_save_handler(root);
break;
case 7:
recursive_delete(root);
break;
case 0:
exit(0);
default:
printf("error! invalid command!\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment