/*Uppgift 1 Josef Cotaidis*/ #include #include #include #include #include "list.h" void help(void){ printf("%-10s%s", "Command :","Help\n"); printf("%-10s%s", "Command :","Insert line\n"); printf("%-10s%s", "Command :","Delete line\n"); printf("%-10s%s", "Command

:","Print line\n"); printf("%-10s%s", "Command :","Print all\n"); printf("%-10s%s", "Command :","Renumber\n"); printf("%-10s%s", "Command :","Quite program\n"); } int main(void){ char filename[FILENAME_MAX]; char str[LINE_MAX]; char* ch; FILE* in; FILE* out; int flag = 1; int row; printf("Open file: "); if((ch=strchr(fgets(filename , FILENAME_MAX, stdin), '\n'))) *ch = '\0'; else{ fprintf(stderr, "För långt filnamn!"); exit(EXIT_FAILURE); } if ((in = fopen(filename, "r"))){ load(in); fclose(in); } while(flag){ printf(">"); *ch = toupper(*fgets(str, LINE_MAX ,stdin)); if(*ch == 'H'){ help(); } else if(*ch == 'D'){ printf("Line: "); scanf("%d", &row); while (getchar() != '\n') ; if (!delete(row)) printf("There is no line to delete!\n"); } else if(*ch == 'I'){ printf("Line: "); scanf("%d", &row); while (getchar() != '\n') ; printf("Insert line: "); fgets(ch, LINE_MAX, stdin); if (!add_row(ch, row)) printf("You have to delete the existing line first!\n"); } else if(*ch == 'A'){ print_all(); } else if(*ch == 'P'){ printf("Line: "); scanf("%d", &row); while (getchar() != '\n') ; if (!print(row)) printf("Line does not exist!\n"); } else if(*ch == 'R'){ renumber(); } else if(*ch == 'Q'){ printf("Save file? [y/n]: "); char inchar; while ((inchar = toupper(fgetc(stdin)))!= 'N' && inchar != 'Y'){ while (getchar() != '\n') ; printf("Save file? [y/n]: "); } if (inchar == 'Y'){ printf("Filename: "); if((ch=strchr(fgets(filename , FILENAME_MAX, stdin), '\n'))) *ch = '\0'; else{ fprintf(stderr, "Error!"); exit(EXIT_FAILURE); } if (out = fopen(filename, "w+")) save(out); fclose(out); } flag = 0; } } return 0; }