Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <string.h>
- #include <time.h>
- // constants
- #define CYN "\x1B[36m"
- #define WHT "\x1B[0m"
- #define width 85
- #define height 31
- #define menuTop 5
- #define menuLeft 20
- #define menuRight 66
- #define menuBottom 26
- #define partitions 4
- const CONSOLE_CURSOR_INFO hide = {1, FALSE};
- const CONSOLE_CURSOR_INFO show = {1, TRUE};
- const char *menuItems[6] = {"LOG IN / SIGN UP", "BROWSE QUESTIONS", "ASK A QUESTION", "SEARCH", "SIGN OUT", "QUIT"};
- // global variables
- FILE *f;
- HANDLE out;
- int menuCurrent, page, answerPage, UICurrent, postCount, answerCount, postsAdded = 1, *lineValues, postCurrent = 0;
- // definitions
- typedef struct User {
- char *password;
- char *username;
- } User;
- User *current;
- typedef struct Answer {
- int post;
- char *author;
- char *content;
- } Answer;
- typedef struct Post {
- Answer *answers[8];
- char *title;
- char *author;
- char *date;
- char *content;
- int ansNum;
- int id;
- int points;
- int voted;
- } Post;
- Post **posts;
- typedef int menuFunction(void);
- menuFunction login, UI, addPost, search, signout, quit;
- menuFunction *menuFunctions[6] = {login, UI, addPost, search, signout, quit};
- // utility functions
- void goTo(int x, int y) { COORD c = {x, y}; SetConsoleCursorPosition(out, c); }
- void setCursor(const CONSOLE_CURSOR_INFO *info) { SetConsoleCursorInfo(out, info); }
- char *crypt(char *string, int size) { while (size--) *string++ ^= 420; return string; }
- char *currentTime() {
- time_t mytime = time(0);
- char *time_str = ctime(&mytime);
- time_str[strlen(time_str) - 1] = '\0';
- return time_str;
- }
- int signedIn() {
- if (strcmp(current -> username, "weed")) return 1;
- return 0;
- }
- void checkEmpty() {
- fseek(f, 0, 2);
- if (!ftell(f)) fprintf(f, "0\n");
- rewind(f);
- }
- void trim(char *string) {
- int index = 0, i = 0;
- while (string[index] == 32) index++;
- if (index) {
- while (string[i + index] != '\0') string[i] = string[i++ + index];
- string[i] = '\0';
- } string[strcspn(string, "\n")] = 0;
- }
- // signatures
- // UI instantiators
- void initialize();
- void menu();
- int login();
- int UI();
- int addPost();
- int addAnswer();
- int search();
- int info();
- int quit();
- int postUI();
- int signout();
- void answerUI(Answer **, int);
- // menu drawing
- void drawMenu();
- void eraseMenu();
- void menuSelect(int);
- void menuDeselect(int);
- // UI drawing
- void drawUI();
- void printPosts(int);
- void eraseUI();
- void UISelect(int);
- // post UI drawing
- void drawPostUI(Post *, char *);
- void postUISelect(int, int, char *);
- // answer UI drawing
- void drawAnswerUI();
- int printAnswers(Answer **, int, int);
- // filing
- Post *loadPost(char *, char *, char *, char *, int, int, int);
- Post **loadPosts();
- void updatePoints(int, int);
- void appendPost(Post *);
- Answer *loadAnswer(int, char *, char *);
- void loadAnswers(Post **);
- void appendAnswer(Post **, Answer*);
- // login
- void appendUser(char *, char *);
- void updateUser(char *, char *);
- int checkUser(char *, char *);
- // search
- int searchPosts(char *);
- void main() {
- initialize();
- menu();
- }
- void initialize() {
- out = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTitle("ASKode - Resolve any C query!");
- setCursor(&hide);
- SMALL_RECT window = {0, 0, width, height};
- SetConsoleWindowInfo(out, 1, &window);
- posts = loadPosts();
- current = (User *) malloc(sizeof(User));
- current -> username = strdup("weed");
- }
- int signout() {
- current -> username = "weed";
- system("cls");
- return 1;
- }
- int quit() {
- eraseMenu();
- exit(0);
- }
- int searchPosts(char *query) {
- int i, j, k;
- for (int i = 0; i < postCount; i++) {
- char *title = strdup(posts[i] -> title);
- int len = strlen(title);
- if (!stricmp(title ,query)) return i;
- for (j = 0; j < len; j++) {
- for (k = 0; k < strlen(query); k++) {
- if (title[j + k] != query[k]) break;
- }
- if (k == strlen(query)) return i;
- }
- } return -1;
- }
- int addAnswer() {
- system("cls");
- int i, j, k, x = width / 2;
- for (i = 0; i < 20; i++) {
- goTo(x - i, height / 2 - 6);
- for (j = 0; j < 2 * i + 1; j++) printf(CYN "%c", 196);
- goTo(x - i, height / 2 + 6);
- for (j = 0; j < 2 * i + 1; j++) printf("%c", 196);
- Sleep(20);
- }
- goTo(x - strlen("Add answer") / 2, height / 2 - 5);
- printf(WHT "Add answer");
- for (i = 0; i < 4; i++) {
- goTo(x - 19, height / 2 + i * 2 - 2);
- for (j = 0; j < 39; j++) printf("%c", 196);
- }
- char *content;
- int debounce = 1;
- Answer *a;
- while (debounce) {
- content = calloc(300, sizeof(char));
- debounce = 0;
- goTo(x - 19, height / 2 - 3);
- for (i = 0; i < 195; i++) {
- if (!(i % 39)) goTo(x - 19, height / 2 + 2 * (i / 39 - 1) - 1);
- char c = getche();
- if (c == 9 || c == 13) break;
- if (c == 8) {
- for (j = 0; j < 5; j++) {
- goTo(x - 19, height / 2 + 2 * (j - 1) - 1);
- for (k = 0; k < 39; k++) printf(" ");
- }
- debounce = 1;
- break;
- }
- if (c == 27) return 1;
- content[i] = c;
- } content[i] = '\0';
- }
- goTo(x - strlen("POST") / 2, height / 2 + 8);
- printf(CYN "POST" WHT);
- char in;
- while (420) {
- in = getch();
- switch (in) {
- case 27: return 1;
- case 13: a = loadAnswer(UICurrent, current -> username, content);
- appendAnswer(posts, a);
- return 1;
- default: break;
- }
- }
- }
- int search() {
- system("cls");
- int i, j, x = width / 2;
- for (i = 0; i < 20; i++) {
- goTo(x - i, height / 2 - 2);
- for (j = 0; j < 2 * i + 1; j++) printf(CYN "%c", 196);
- goTo(x - i, height / 2 + 2);
- for (j = 0; j < 2 * i + 1; j++) printf("%c", 196);
- Sleep(20);
- }
- goTo(x - strlen("Search by title") / 2, height / 2 - 1);
- printf(WHT "Search by title");
- goTo(x - 19, height / 2 + 1);
- for (i = 0 ; i < 39; i++) printf("%c", 196);
- char *query, in;
- int debounce = 1, index = 0;
- while (debounce) {
- query = calloc(33, sizeof(char));
- debounce = 0;
- goTo(x - 19, height / 2);
- for (i = 0; i < 32; i++) {
- char c = getche();
- if (c == 9 || c == 13) break;
- if (c == 8) {
- goTo(x - 19, height / 2);
- for (j = 0; j < 27; j++) printf(" ");
- debounce = 1;
- break;
- }
- if (c == 27) return 1;
- query[i] = c;
- } query[i] = '\0';
- }
- goTo(x - strlen("Search") / 2, height / 2 + 3);
- printf(CYN "Search" WHT);
- while (420) {
- in = getch();
- switch (in) {
- case 27: return 1;
- case 13: index = searchPosts(query);
- if (index != -1) {
- Post *p = posts[index];
- postUI(p);
- } return 1;
- default: break;
- }
- }
- getch();
- }
- int addPost() {
- system("cls");
- int i, j, k, x = width / 2;
- for (i = 0; i < 20; i++) {
- goTo(x - i, height / 2 - 7);
- for (j = 0; j < 2 * i + 1; j++) printf(CYN "%c", 196);
- goTo(x - i, height / 2 + 7);
- for (j = 0; j < 2 * i + 1; j++) printf("%c", 196);
- Sleep(20);
- }
- goTo(x - strlen("Title") / 2, height / 2 - 6);
- printf(WHT "Title");
- goTo(x - 13, height / 2 - 4);
- for (i = 0; i < 27; i++) printf("%c", 196);
- goTo(x - strlen("Question") / 2, height / 2 - 3);
- printf("Question");
- for (i = 0; i < 4; i++) {
- goTo(x - 19, height / 2 + i * 2 - 1);
- for (j = 0; j < 39; j++) printf("%c", 196);
- }
- char *title, *content;
- int debounce = 1;
- while (debounce) {
- title = calloc(29, sizeof(char));
- debounce = 0;
- goTo(x - 13, height / 2 - 5);
- for (i = 0; i < 27; i++) {
- char c = getche();
- if (c == 9 || c == 13) break;
- if (c == 8) {
- goTo(x - 13, height / 2 - 5);
- for (j = 0; j < 27; j++) printf(" ");
- debounce = 1;
- break;
- }
- if (c == 27) return 1;
- title[i] = c;
- } title[i] = '\0';
- } debounce = 1;
- while (debounce) {
- content = calloc(300, sizeof(char));
- debounce = 0;
- goTo(x - 19, height / 2 - 2);
- for (i = 0; i < 195; i++) {
- if (!(i % 39)) goTo(x - 19, height / 2 + 2 * (i / 39 - 1));
- char c = getche();
- if (c == 9 || c == 13) break;
- if (c == 8) {
- for (j = 0; j < 5; j++) {
- goTo(x - 19, height / 2 + 2 * (j - 1));
- for (k = 0; k < 39; k++) printf(" ");
- }
- debounce = 1;
- break;
- }
- if (c == 27) return 1;
- content[i] = c;
- } content[i] = '\0';
- }
- goTo(x - strlen("POST") / 2, height / 2 + 8);
- printf(CYN "POST" WHT);
- char in;
- while (420) {
- in = getch();
- switch (in) {
- case 27: return 1;
- case 13: appendPost(loadPost(title, current -> username, currentTime(), content, 0, 1, 0));
- break;
- default: break;
- }
- }
- }
- int login() {
- system("cls");
- int i, j, x = width / 2;
- for (i = 0; i < 20; i++) {
- goTo(x - i, height / 2 - 4);
- for (j = 0; j < 2 * i + 1; j++) printf(CYN "%c", 196);
- goTo(x - i, height / 2 + 4);
- for (j = 0; j < 2 * i + 1; j++) printf("%c", 196);
- Sleep(20);
- }
- goTo(x - strlen("Username") / 2, height / 2 - 3);
- printf(WHT "Username");
- goTo(x - 13, height / 2 - 1);
- for(i = 0; i < 27; i++) printf("%c", 196);
- goTo(x - strlen("Password") / 2, height / 2 + 1);
- printf("Password");
- goTo(x - 13, height / 2 + 3);
- for(i = 0; i < 27; i++) printf("%c", 196);
- goTo(x - 19, height / 2 + 5);
- printf("LOG IN");
- goTo(x + 20 - strlen("SIGN UP"), height / 2 + 5);
- printf("SIGN UP");
- char *username, *password;
- int debounce = 1;
- while (debounce) {
- username = calloc(29, sizeof(char));
- debounce = 0;
- goTo(x - 13, height / 2 - 2);
- for (i = 0; i < 27; i++) {
- char c = getche();
- if (c == 9 || c == 13) break;
- if (c == 8) {
- goTo(x - 13, height / 2 - 2);
- for (j = 0; j < 27; j++) printf(" ");
- debounce = 1;
- break;
- }
- if (c == 27) return 1;
- username[i] = c;
- } username[i] = '\0';
- } debounce = 1;
- while (debounce) {
- password = calloc(29, sizeof(char));
- debounce = 0;
- goTo(x - 13, height / 2 + 2);
- for (i = 0; i < 27; i++) {
- char c = getche();
- if (c == 9 || c == 13) break;
- if (c == 8) {
- goTo(x - 13, height / 2 + 2);
- for (j = 0; j < 27; j++) printf(" ");
- debounce = 1;
- break;
- }
- if (c == 27) return 1;
- password[i] = c;
- } password[i] = '\0';
- }
- goTo(width / 2 - 19, height / 2 + 6);
- for (i = 0 ; i < 6; i++) printf(CYN "%c" WHT, 196);
- int select = 0;
- char in;
- while (420) {
- in = getch();
- switch (in) {
- case -32: switch (getch()) {
- case 77: if (!select) {
- goTo(x - 19, height / 2 + 6);
- for (i = 0; i < 7; i++) printf(" ");
- goTo(x + 19 - strlen("SIGNUP"), height / 2 + 6);
- for (i = 0; i < 7; i++) printf(CYN "%c" WHT, 196);
- select++;
- } break;
- case 75: if (select) {
- goTo(x + 13, height / 2 + 6);
- for (i = 0; i < 7; i++) printf(" ");
- goTo(x - 19, height / 2 + 6);
- for (i = 0; i < 6; i++) printf(CYN "%c" WHT, 196);
- select--;
- } break;
- } break;
- case 27: return 1;
- case 13: goTo(x - 19, height / 2 - 5);
- if (!select) {
- switch(checkUser(username, password)) {
- case 0: printf("Username not found!");
- break;
- case 1: printf("Incorrect password!");
- break;
- case 2: printf("Succesfully logged in as: %s", username);
- updateUser(username, password);
- break;
- }
- }
- else {
- switch(checkUser(username, password)) {
- case 0: printf("Succesfully created account: %s", username);
- appendUser(username, password);
- updateUser(username, password);
- break;
- case 1: printf("Username already exists!");
- break;
- }
- } break;
- default: break;
- }
- }
- }
- void appendUser(char *username, char *password) {
- f = fopen("users.txt", "a+");
- char *pass = strdup(password);
- crypt(pass, strlen(pass));
- fprintf(f, "%s\t%s\n", username, pass);
- fclose(f);
- }
- void updateUser(char *username, char *password) {
- current = (User *) malloc(sizeof(User));
- current -> username = strdup(username);
- current -> password = strdup(password);
- }
- int checkUser(char *username, char *password) {
- int ret = 0;
- f = fopen("users.txt", "a+");
- char user[32], pass[32];
- while (!feof(f)) {
- fscanf(f, "%s\t%s\t\n", user, pass);
- crypt(pass, strlen(pass));
- if (!stricmp(user, username)) {
- ret = 1;
- if (!strcmp(pass, password)) ret = 2;
- fclose(f);
- return ret;
- }
- }
- fclose(f);
- return 0;
- }
- void menu() {
- char in;
- drawMenu(menuItems);
- int debounce = 0;
- while (420) {
- if (debounce) drawMenu(menuItems);
- debounce = 0;
- in = getch();
- switch (in) {
- case -32: switch (getch()) {
- case 72: if (menuCurrent > 0) menuSelect(menuCurrent - 1); break;
- case 77: break; // right arrow
- case 75: break; // left arrow
- case 80: if (menuCurrent < 5) menuSelect(menuCurrent + 1); break;
- } break;
- case 27: eraseMenu();
- return;
- case 13: eraseMenu();
- debounce = (*menuFunctions[menuCurrent])();
- break;
- default: break;
- }
- }
- }
- int postUI(Post *p) {
- char in, *commentString = malloc(12);
- int debounce = 0;
- switch(p -> ansNum) {
- case 0: sprintf(commentString, "No answers");
- break;
- case 1: sprintf(commentString, "1 answer");
- break;
- default: sprintf(commentString, "%d answers", p -> ansNum);
- break;
- }
- drawPostUI(p, commentString);
- while (420) {
- if (debounce) drawPostUI(p, commentString);
- debounce = 0;
- in = getch();
- switch (in) {
- case -32: switch (getch()) {
- case 72: break;
- case 77: if (postCurrent < 2)
- postCurrent++;
- postUISelect(postCurrent, p -> points, commentString);
- break;
- case 75: if (postCurrent > 0)
- postCurrent--;
- postUISelect(postCurrent, p -> points, commentString);
- break;
- case 80: break;
- } break;
- case 27: return 1;
- case 13: switch(postCurrent) {
- case 0: if (!p -> voted) {
- p -> points++;
- updatePoints(p -> id, p -> points);
- p -> voted = 1;
- return 1;
- } break;
- case 1: debounce = addAnswer();
- break;
- case 2: if (signedIn) debounce = printAnswers(p -> answers, 0, p -> ansNum);
- break;
- }
- break;
- default: break;
- }
- }
- }
- void postUISelect(int x, int points, char *commentString) {
- int i, inc = width / 2;
- goTo(width / 2 - 29, height / 2 + 7);
- for (i = 0; i < 60; i++) printf(" ");
- if (!x) {
- goTo(inc + 30 - strlen(commentString), height / 2 + 6);
- printf(WHT "%s", commentString);
- goTo(inc - strlen("Add an answer") / 2, height / 2 + 6);
- printf("Add an answer");
- goTo(inc - 29, height / 2 + 6);
- printf(CYN "%d %s", points, points == 1 ? "Point" : "Points");
- goTo(inc - 29, height / 2 + 7);
- printf("Upvote this post");
- } else if (x == 1) {
- goTo(inc - 29, height / 2 + 6);
- printf(WHT "%d %s", points, points == 1 ? "Point" : "Points");
- goTo(inc + 30 - strlen(commentString), height / 2 + 6);
- printf("%s", commentString);
- goTo(inc - strlen("Add an answer") / 2, height / 2 + 6);
- printf(CYN "Add an answer");
- }
- else if (x == 2) {
- goTo(inc - 29, height / 2 + 6);
- printf(WHT "%d %s", points, points == 1 ? "Point" : "Points");
- goTo(inc - strlen("Add an answer") / 2, height / 2 + 6);
- printf("Add an answer");
- goTo(inc + 30 - strlen(commentString), height / 2 + 6);
- printf(CYN "%s", commentString);
- goTo(inc + 30 - strlen("View answers"), height / 2 + 7);
- printf("View answers");
- }
- }
- void drawPostUI(Post *p, char *commentString) {
- system("cls");
- int i, j, x = width / 2;
- for (i = 0; i < 30; i++) {
- goTo(x - i, height / 2 - 5);
- for (j = 0; j < 2 * i + 1; j++) printf(CYN "%c", 196);
- goTo(x - i, height / 2 + 5);
- for (j = 0; j < 2 * i + 1; j++) printf("%c", 196);
- Sleep(7);
- }
- goTo(x - 29, height / 2 - 6);
- printf(WHT "u/%s", p -> author);
- goTo(x + 6, height / 2 - 6);
- printf("%s", p -> date);
- goTo(x - 29, height / 2 + 6);
- printf("%d %s", p -> points, p -> points == 1 ? "Point" : "Points");
- goTo(x - strlen("Add an answer") / 2, height / 2 + 6);
- printf("Add an answer");
- goTo(x + 30 - strlen(commentString), height / 2 + 6);
- printf("%s", commentString);
- goTo(x - strlen(p -> title) / 2, 11);
- printf("%s", strupr(p -> title));
- goTo(x - 29, 13);
- int len = strlen(p -> content);
- j = 0;
- for (i = 0; i < len; i++) {
- if (j == 59) {
- j = 0;
- goTo(x - 29, 13 + i / 59);
- }
- printf("%c", p -> content[i]);
- j++;
- } postUISelect(postCurrent, p -> points, commentString);
- }
- void menuDeselect(int x) {
- int i, end = (menuRight - menuLeft) / 2 - strlen(menuItems[x]) / 2 - 2;
- goTo(menuLeft, menuTop + 3 * (x + 1));
- for (i = 0; i <= end; i++) printf(" ");
- goTo((menuRight - menuLeft) / 2 + menuLeft + strlen(menuItems[x]) / 2 + 1, menuTop + 3 * (x + 1));
- for (i = 0; i <= end; i++) printf(" ");
- }
- void menuSelect(int x) {
- if (x != menuCurrent) menuDeselect(menuCurrent);
- int i, end = (menuRight - menuLeft) / 2 - strlen(menuItems[x]) / 2 - 2;
- goTo(menuLeft, menuTop + 3 * (x + 1));
- printf(CYN "%c" WHT, 195);
- for (i = 0; i < end; i++) printf("%c", 196);
- goTo((menuRight - menuLeft) / 2 + menuLeft + strlen(menuItems[x]) / 2 + 1, menuTop + 3 * (x + 1));
- for (i = 0; i < end; i++) printf("%c", 196);
- printf(CYN "%c" WHT, 180);
- menuCurrent = x;
- }
- void drawMenu(char *items[6]) {
- system("cls");
- int i, j, x = (menuRight - menuLeft) / 2 + menuLeft - 1;
- for (i = 0; i < (menuRight - menuLeft) / 2; i++) {
- goTo(x - i, menuTop + 1);
- for (j = 0; j < 2 * i + 1; j++) printf(CYN "%c", 196);
- goTo(x - i, menuBottom - 1);
- for (j = 0; j < 2 * i + 1; j++) printf("%c", 196);
- Sleep(22 - i);
- } printf("%c" WHT, 196);
- for (i = 0; i < 6; i++) {
- goTo((menuRight - menuLeft) / 2 + menuLeft - strlen(items[i]) / 2, menuTop + 3 * (i + 1));
- puts(items[i]);
- } menuSelect(menuCurrent);
- }
- void eraseMenu() {
- int i, j, x = (menuRight - menuLeft) / 2 + menuLeft - 1;
- menuDeselect(menuCurrent);
- for (i = 0; i < (menuRight - menuLeft) / 2; i++) {
- goTo(x - i, menuTop + 1);
- for (j = 0; j < 2 * i + 1; j++) printf(" ");
- goTo(x - i, menuBottom - 1);
- for (j = 0; j < 2 * i + 1; j++) printf(" ");
- Sleep(i);
- } system("cls");
- }
- void drawUI() {
- system("cls");
- int i, j, increment = height / partitions;
- goTo(width / 2 - strlen("PRESS S TO SEARCH...") / 2, 0);
- printf(CYN "PRESS S TO SEARCH..." WHT);
- for (i = 0; i < width + 1; i++) {
- for (j = 1; j < height + 1; j += increment) {
- goTo(i, j);
- printf("%c", 196);
- } Sleep(1);
- } UISelect(UICurrent);
- }
- int printAnswers(Answer **answers, int page, int ansNum) {
- system("cls");
- drawUI();
- int i, increment = height / partitions;
- goTo(width / 2 - strlen("PAGE 1 OF 5") / 2, 30);
- printf(CYN "PAGE %d OF %d" WHT, answerPage + 1, 1 + ansNum / 4);
- for (i = 0; i < partitions; i++)
- if (i + (4 * answerPage) < ansNum) {
- Answer *a = answers[i + (4 * answerPage)];
- goTo(0, i * increment + 3);
- printf("%s", a -> content);
- goTo(width - strlen(a -> author) - 10, i * (increment) + 6);
- printf("Posted by: %s", a -> author);
- }
- char in;
- while (420) {
- in = getch();
- if (in == 27) return 1;
- }
- }
- void drawAnswerUI() {
- system("cls");
- int i, j, increment = height / partitions;
- goTo(width / 2 - strlen("PRESS S TO SEARCH...") / 2, 0);
- printf(CYN "PRESS S TO SEARCH..." WHT);
- for (i = 0; i < width + 1; i++) {
- for (j = 1; j < height + 1; j += increment) {
- goTo(i, j);
- printf("%c", 196);
- } Sleep(3);
- }
- }
- void printPosts(int page) {
- int i, increment = height / partitions;
- goTo(width / 2 - strlen("PAGE 1 OF 5") / 2, 30);
- printf(CYN "PAGE %d OF %d" WHT, page + 1, 1 + postCount / 4);
- for (i = 0; i < partitions; i++)
- if (i + (4 * page) < postCount) {
- Post *p = posts[i + (4 * page)];
- goTo(0, i * increment + 3);
- printf("%s (%d)", p -> title, p -> points);
- goTo(width - strlen(p -> author) - 10, i * increment + 3);
- printf("Posted by: %s", p -> author);
- goTo(0, i * (increment) + 6);
- printf("Posted on: %s", p -> date);
- char *string = (char *) malloc(12 * sizeof(char));
- switch(p -> ansNum) {
- case 0: sprintf(string, "No answers");
- break;
- case 1: sprintf(string, "1 answer");
- break;
- default: sprintf(string, "%d answers", p -> ansNum);
- break;
- }
- goTo(width - strlen(string) + 1, i * increment + 6);
- printf("%s", string);
- }
- }
- void eraseUI() {
- int i, j, increment = height / partitions;
- for (i = 0; i < width + 1; i++) {
- for (j = 1; j < height + 1; j += increment) {
- goTo(i, j);
- printf(" ");
- } Sleep(3);
- } system("cls");
- }
- void UISelect(int x) {
- int i;
- for (i = 0; i < width + 1; i += 3) {
- goTo(i, (UICurrent % 4) * 7 + 1);
- printf("%c", 196);
- goTo(i, (UICurrent % 4 + 1) * 7 + 1);
- printf("%c", 196);
- }
- for (i = 0; i < width + 1; i += 3) {
- goTo(i, (x % 4) * 7 + 1);
- printf("%c", 194);
- goTo(i, ((x % 4) + 1) * 7 + 1);
- printf("%c", 193);
- } UICurrent = x;
- }
- void answerUI(Answer **answers, int ansNum) {
- drawAnswerUI();
- printAnswers(answers, 0, ansNum);
- char in;
- while (420) {
- in = getch();
- switch (in) {
- case -32: switch (getch()) {
- case 72: break;
- case 77: if (answerPage < (ansNum / 4)) {
- printf("%d %d\n", answerPage, 1 + ansNum / 4);
- answerPage++;
- drawAnswerUI();
- printAnswers(answers, page, ansNum);
- } break;
- case 75: if (answerPage > 0) {
- answerPage--;
- drawAnswerUI();
- printAnswers(answers, page, ansNum);
- } break;
- case 80: break;
- } break;
- case 27: eraseUI();
- return;
- case 13: eraseUI();
- printf("Selected: %s", menuItems[UICurrent]);
- break;
- default: break;
- }
- }
- }
- int UI() {
- drawUI();
- printPosts(0);
- char in;
- int debounce = 0;
- while (420) {
- if (debounce) {
- drawUI();
- printPosts(0);
- }
- debounce = 0;
- in = getch();
- switch (in) {
- case -32: switch (getch()) {
- case 72: if (UICurrent % 4 > 0) UISelect(UICurrent - 1); break;
- case 77: if (page < (postCount % 4 - 2)) {
- page++;
- drawUI();
- printPosts(page);
- UISelect(4 * page);
- } break;
- case 75: if (page > 0) {
- page--;
- drawUI();
- printPosts(page);
- UISelect(4 * page);
- } break;
- case 80: if (UICurrent % 4 < 3) UISelect(UICurrent + 1); break;
- } break;
- case 27: eraseUI();
- return 1;
- case 13: if (UICurrent < postCount) debounce = postUI(posts[UICurrent]);
- break;
- }
- }
- }
- Post *loadPost(char *title, char *author, char *date, char *content, int points, int increment, int override) {
- Post *p = (Post *) malloc(sizeof(Post));
- p -> title = malloc(32);
- p -> title = strdup(title);
- p -> author = malloc(32);
- p -> author = strdup(author);
- p -> date = malloc(32);
- p -> date = strdup(date);
- p -> content = malloc(300);
- p -> content = strdup(content);
- p -> points = points;
- p -> id = postsAdded;
- p -> ansNum = 0;
- p -> voted = 0;
- if (increment) postsAdded++;
- if (override) p -> id = override;
- return p;
- }
- Post **loadPosts() {
- f = fopen("posts.txt", "a+");
- checkEmpty();
- fclose(f);
- f = fopen("posts.txt", "r+");
- fscanf(f, "%d", &postCount);
- lineValues = calloc(postCount + 1, sizeof(int));
- Post **posts = (Post **) malloc(postCount * sizeof(Post *));
- int i, points, digits = 0, x = postCount;
- do { ++digits; } while (x /= 10);
- fseek(f, digits + 1, 0);
- char title[32], author[32], date[32], content[300];
- for (i = 0; i < postCount; i++) {
- lineValues[i] = ftell(f);
- fscanf(f, "%d %d", &x, &points);
- trim(fgets(title, 32, f));
- trim(fgets(author, 32, f));
- trim(fgets(content, 300, f));
- trim(fgets(date, 32, f));
- posts[i] = loadPost(title, author, date, content, points, 1, 0);
- }
- fclose(f);
- loadAnswers(posts);
- return posts;
- }
- void updatePoints(int id, int points) {
- if (id > postCount) return;
- f = fopen("posts.txt", "r+");
- fseek(f, lineValues[id - 1], 0);
- int trash1, trash2;
- fscanf(f, "%d %d", &trash1, &trash2);
- fseek(f, lineValues[id - 1], 0);
- fprintf(f, "%d %d", id, points);
- posts[id - 1] -> points = points;
- fclose(f);
- }
- void appendPost(Post *post) {
- f = fopen("posts.txt", "r+");
- fprintf(f, "%d", ++postCount);
- fseek(f, 0, 2);
- lineValues[postCount - 1] = ftell(f);
- fprintf(f, "%d %d %s\n%s\n%s\n%s\n", post -> id, post -> points, post -> title, post -> author, post -> content, post -> date);
- posts[postCount - 1] = loadPost(post -> title, post -> author, post -> date, post -> content, post -> points, 1, 0);
- fclose(f);
- }
- Answer *loadAnswer(int post, char *author, char *content) {
- Answer *a = (Answer *) malloc(sizeof(Answer));
- a -> post = post;
- a -> author = malloc(32);
- a -> author = strdup(author);
- a -> content = malloc(300);
- a -> content = strdup(content);
- return a;
- }
- void loadAnswers(Post** posts) {
- f = fopen("answers.txt", "a+");
- checkEmpty();
- fclose(f);
- f = fopen("answers.txt", "r+");
- fscanf(f, "%d", &answerCount);
- int i, digits = 0, x = answerCount;
- do { ++digits; } while (x /= 10);
- fseek(f, digits + 1, 0);
- char author[32], content[300];
- for (i = 0; i < answerCount; i++) {
- fscanf(f, "%d", &x);
- x--;
- trim(fgets(author, 32, f));
- trim(fgets(content, 300, f));
- Post *p = posts[x];
- p -> answers[p -> ansNum] = loadAnswer(x, author, content);
- p -> ansNum++;
- }
- fclose(f);
- }
- void appendAnswer(Post **posts, Answer *answer) {
- f = fopen("answers.txt", "r+");
- fprintf(f, "%d", ++answerCount);
- fseek(f, 0, 2);
- fprintf(f, "%d %s\n%s\n", answer -> post, answer -> author, answer -> content);
- int index = (answer -> post) - 1;
- int ansIndex = posts[index] -> ansNum;
- posts[index] -> answers[ansIndex] = loadAnswer(answer -> post, answer -> author, answer -> content);
- posts[index] -> ansNum++;
- fclose(f);
- }
Add Comment
Please, Sign In to add comment