improve visual clarity
This commit is contained in:
93
main.cc
93
main.cc
@@ -13,7 +13,8 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include <string>
|
||||
//#include <vector> //try to make the program work without this without doing anything awful but do it later
|
||||
// #include <vector> //try to make the program work without this without doing anything awful but do
|
||||
// it later
|
||||
|
||||
#include "client.h"
|
||||
#include "disp.h"
|
||||
@@ -30,6 +31,7 @@ const int DEFAULT_CUR_X = 2; // the x position of the preferred default cursor
|
||||
const int DEFAULT_CUR_Y = 12; // the x position of the preferred default cursor
|
||||
// position for message entry
|
||||
|
||||
int width, height;
|
||||
chatmode_t mode = NO_MODE; // what mode is this program in. 0 = nothing. 1 = server. 2 = client
|
||||
|
||||
int serverSocketDescriptor; // a global variable for storing the server socket
|
||||
@@ -71,40 +73,42 @@ main(int argc, char *argv[])
|
||||
* userInput: character buffer for the user's next message
|
||||
* modeless: check whether a modeless exit was accidental
|
||||
*/
|
||||
int ch;
|
||||
int ch, inlen;
|
||||
bool exit = false;
|
||||
char *userInput = new char[1024];
|
||||
char *userInputF = new char[1032];
|
||||
bool modeless = false;
|
||||
|
||||
if (argc == 1) {
|
||||
usage(argv[0], &modeless);
|
||||
goto leave;
|
||||
}
|
||||
|
||||
|
||||
while ((ch = getopt(argc, argv, "m:a:p:h")) != -1)
|
||||
switch (ch) {
|
||||
case 'm':
|
||||
if (!strncmp(optarg, "client", 6)) {
|
||||
logFileName = "client.txt";
|
||||
mode = CLIENT_MODE;
|
||||
} else if (!strncmp(optarg, "server", 6)) {
|
||||
logFileName = "server.txt";
|
||||
mode = SERVER_MODE;
|
||||
} else {
|
||||
usage(argv[0], &modeless);
|
||||
goto leave;
|
||||
}
|
||||
break;
|
||||
case 'a':
|
||||
IP_ADDRESS = optarg;
|
||||
break;
|
||||
case 'p':
|
||||
PORT_NUM = atoi(optarg);
|
||||
break;
|
||||
case 'h': default:
|
||||
case 'm':
|
||||
if (!strncmp(optarg, "client", 6)) {
|
||||
logFileName = "client.txt";
|
||||
mode = CLIENT_MODE;
|
||||
} else if (!strncmp(optarg, "server", 6)) {
|
||||
logFileName = "server.txt";
|
||||
mode = SERVER_MODE;
|
||||
} else {
|
||||
usage(argv[0], &modeless);
|
||||
goto leave;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'a':
|
||||
IP_ADDRESS = optarg;
|
||||
break;
|
||||
case 'p':
|
||||
PORT_NUM = atoi(optarg);
|
||||
break;
|
||||
case 'h':
|
||||
default:
|
||||
usage(argv[0], &modeless);
|
||||
goto leave;
|
||||
break;
|
||||
}
|
||||
|
||||
// putting this lower down to circumvent the terminal brick when the
|
||||
@@ -123,12 +127,12 @@ main(int argc, char *argv[])
|
||||
|
||||
switch (mode) {
|
||||
case CLIENT_MODE:
|
||||
writeToFile(logFileName, "CLIENT MODE");
|
||||
writeToFile(logFileName, "SYSTEM: CLIENT MODE");
|
||||
setupClient();
|
||||
linePos++;
|
||||
break;
|
||||
case SERVER_MODE:
|
||||
writeToFile(logFileName, "SERVER MODE");
|
||||
writeToFile(logFileName, "SYSTEM: SERVER MODE");
|
||||
setupServer(PORT_NUM);
|
||||
linePos++;
|
||||
break;
|
||||
@@ -144,7 +148,7 @@ main(int argc, char *argv[])
|
||||
box(stdscr, 0, 0);
|
||||
|
||||
// Get screen size
|
||||
int height, width;
|
||||
// int height, width;
|
||||
getmaxyx(stdscr, height, width);
|
||||
|
||||
// Draw separator line above input
|
||||
@@ -152,7 +156,18 @@ main(int argc, char *argv[])
|
||||
|
||||
// Labels
|
||||
mvprintw(0, 2, " Chat ");
|
||||
mvprintw(height - 3, 2, " Input ");
|
||||
// mvprintw(height - 3, 2, " Input ");
|
||||
switch (mode) {
|
||||
case CLIENT_MODE:
|
||||
mvprintw(height - 3, 2, " Client ");
|
||||
break;
|
||||
case SERVER_MODE:
|
||||
mvprintw(height - 3, 2, " Server ");
|
||||
break;
|
||||
default:
|
||||
mvprintw(height - 3, 2, " Input ");
|
||||
break;
|
||||
}
|
||||
|
||||
// Display Chat Log
|
||||
displayFile(logFileName, linePos, LOG_LENGTH);
|
||||
@@ -166,20 +181,32 @@ main(int argc, char *argv[])
|
||||
move(height - 2, 2);
|
||||
clrtoeol();
|
||||
printw("You: ");
|
||||
move(height - 2, 7);
|
||||
movetobox();
|
||||
|
||||
refresh();
|
||||
|
||||
getstr(userInput);
|
||||
writeToFile(logFileName, userInput);
|
||||
|
||||
if (!strncmp(userInput, "/quit", 5))
|
||||
exit = true;
|
||||
|
||||
if (mode == 1)
|
||||
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
|
||||
else
|
||||
send(clientSocketDescriptor, (char *)userInput, strlen(userInput), 0);
|
||||
inlen = strlen(userInput);
|
||||
|
||||
switch (mode) {
|
||||
case SERVER_MODE:
|
||||
snprintf(userInputF, 1032 * sizeof(char), "Server: %s", userInput);
|
||||
send(clientSocketDescriptor, (char *)userInput, inlen, 0);
|
||||
break;
|
||||
case CLIENT_MODE:
|
||||
snprintf(userInputF, 1032 * sizeof(char), "Client: %s", userInput);
|
||||
send(clientSocketDescriptor, (char *)userInput, inlen, 0);
|
||||
break;
|
||||
default:
|
||||
goto leave;
|
||||
break;
|
||||
}
|
||||
|
||||
writeToFile(logFileName, userInputF);
|
||||
}
|
||||
|
||||
leave:
|
||||
|
||||
Reference in New Issue
Block a user