Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- hw2.h
- COMP1917 Computing 1
- Program supplied as a starting point for
- Assignment 2: Message Board
- UNSW Session 2, 2012
- */
- #define TRUE 1
- #define FALSE 0
- #define MAX_LINE 256
- #define MAX_TEXT 4096
- typedef struct date Date;
- typedef struct time Time;
- typedef struct msgNode MsgNode;
- struct date {
- int day;
- int month;
- int year;
- };
- struct time {
- int hour;
- int minute;
- int second;
- };
- struct msgNode {
- int messageNum;
- int inReplyTo;
- int indent;
- int deleted;
- char *name;
- Date date;
- Time time;
- char *text;
- MsgNode *next;
- };
- // INSERT NEW FUNCTION PROTOTYPES, AS APPROPRIATE
- void printPrompt();
- void printHelp();
- MsgNode *getNode( void );
- char *getName( void );
- char *getText( void );
- void getDate( Date *d );
- int scanDate( Date *d );
- void getTime( Time *t );
- int scanTime( Time *t );
- int dateOK( Date *d );
- int timeOK( Time *t );
- void printPadded( int n, char ch );
- void printDate( Date d );
- void printTime( Time t );
- void printBrief( MsgNode * msg );
- void printFull( MsgNode * msg );
- void freeList( MsgNode *list );
- /*
- hw2.c
- COMP1917 Computing 1
- Program supplied as a starting point for
- Assignment 2: Message Board
- UNSW Session 2, 2012
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <unistd.h>
- //#include "hw2.h"
- int globalMessageNum = 1;
- int main( void )
- {
- MsgNode *list = NULL;
- MsgNode *node;
- MsgNode *location = NULL;
- char command[MAX_LINE];
- char c;
- int n;
- printPrompt();
- // enter a loop, reading and executing commands from the user
- while( fgets(command,MAX_LINE,stdin) != NULL ) {
- char *p;
- // replace newline with end-of-string character
- if(( p = strchr(command,'\n')) != NULL ) {
- *p = '\0';
- }
- p = command;
- while( isspace(*p)) { // skip any initial spaces
- p++;
- }
- c = *p;
- if( isdigit(c)) {
- MsgNode *temp = list;
- sscanf( command,"%d",&n );
- while(temp!= NULL){
- if(n == location->messageNum){
- printf(" %d %d\n", n, location->messageNum);
- printf("Y %d %d\n", n, temp->messageNum);
- }else{
- printf(" %d %d\n", n, location->messageNum);
- printf("Y %d %d\n", n, temp->messageNum);
- }
- temp = temp->next;
- }
- }
- else switch( c ) {
- case 'a': case 'A': // Add item
- {
- node = getNode();
- printFull( node );
- if (list == NULL) {
- list = node;
- } else {
- MsgNode *current = list;
- while (current->next != NULL) {
- current = current->next;
- }
- current->next = node;
- }
- location = node;
- break;
- }
- case 'l': case 'L':
- {
- MsgNode *current = list;
- while(current != NULL) {
- if ((current->next) == NULL) {
- printf("-> ");
- } else {
- printf(" ");
- }
- printf("%2d ",current->messageNum);
- printBrief(current);
- current = current->next;
- }
- break;
- }
- case 'p': case 'P':
- {
- MsgNode *current = list;
- while(current != NULL) {
- if (current->next == NULL) {
- printf("%2d ",current->messageNum);
- printFull(current);
- }
- current = current->next;
- }
- break;
- }
- case 'd': case 'D':
- {
- //set current to delete
- break;
- }
- case 'b': case 'B':
- {
- break;
- }
- case 's': case 'S':
- {
- MsgNode *current = list;
- char search[256];
- printf("Search Text:\n");
- fgets(search,256,stdin);
- printf("Searching for: %s\n", search);
- while( current != NULL){
- if((strstr(search, current->text) || (strstr(search, current->text))) != NULL){
- printf("Yes\n");
- }else{
- printf("No\n");
- }
- current = current->next;
- }
- break;
- }
- case 'h': case 'H': // Help
- printHelp();
- break;
- case 'q': case 'Q': // Quit
- freeList( list );
- printf("Bye!\n");
- return 0;
- break;
- }
- printPrompt();
- }
- return 0;
- }
- // INSERT NEW FUNCTIONS, AS APPROPRIATE
- /************************************************************
- Print prompt only if output goes to screen
- */
- void printPrompt()
- {
- if (isatty(fileno(stdin))) {
- printf("Enter command (A,F,B,P,L,D,R,T,S,U,Q, H for Help): ");
- }
- }
- /************************************************************
- Print the list of commands available to the user,
- and a brief summary of what each command does.
- */
- void printHelp()
- {
- printf("\n");
- printf(" A - Add\n" );
- printf(" L - List\n" );
- printf(" P - Print\n" );
- printf(" F - Forward\n" );
- printf(" B - Back\n" );
- printf("<k>- jump to message k\n");
- printf(" D - Delete\n");
- printf(" R - Reply\n");
- printf(" T - Threads\n");
- printf(" S - Search\n");
- printf(" U - Undo\n" );
- printf(" Q - Quit\n");
- printf(" H - Help\n");
- printf("\n");
- }
- /************************************************************
- allocate space for a new message and get the
- name, date, time and text from standard input.
- */
- MsgNode * getNode( void )
- {
- MsgNode * new_node;
- new_node = (MsgNode *)malloc(sizeof(MsgNode));
- if( new_node == NULL ) {
- printf("Error: could not allocate memory.\n");
- exit( 1 );
- }
- new_node->messageNum= globalMessageNum++;
- new_node->inReplyTo = 0;
- new_node->indent = 0;
- new_node->deleted = FALSE;
- new_node->name = getName();
- getDate( &new_node->date );
- getTime( &new_node->time );
- new_node->text = getText();
- new_node->next = NULL;
- return( new_node );
- }
- /************************************************************
- Read one line of text from standard input,
- store it in a string and return a pointer to the string.
- */
- char * getName( void )
- {
- char buffer[MAX_LINE];
- char *name;
- int length;
- int ch;
- int i;
- // prompt user for input
- printf( "Name: " );
- // skip any intial newline character
- if(( ch = getchar()) == '\n' ) {
- ch = getchar();
- }
- // read text initially into a buffer
- i=0;
- while( i < MAX_LINE && ch != '\n' && ch != EOF ) {
- buffer[i++] = ch;
- ch = getchar();
- }
- // trim of any trailing whitespace
- while( isspace( buffer[i] )) {
- i--;
- }
- // allocate just enough space to store the string
- length = i;
- name = (char *)malloc((length+1)*sizeof(char));
- if( name == NULL ) {
- printf("Error: could not allocate memory.\n");
- exit( 1 );
- }
- // copy text from buffer into new string
- for( i=0; i < length; i++ ) {
- name[i] = buffer[i];
- }
- name[i] = '\0'; // add end-of-string marker
- return( name );
- }
- /************************************************************
- Read several lines of text from standard input,
- store them in a string and return a pointer to the string.
- */
- char * getText( void )
- {
- char buffer[MAX_TEXT];
- char *text;
- int length;
- int ch;
- int i;
- printf("Text: ");
- ch = getchar();
- i=0;
- while(( i < MAX_TEXT )&&( ch != EOF )) {
- buffer[i++] = ch;
- ch = getchar();
- // stop when you encounter a dot on a line by itself
- if( i > 1 && ch == '\n' && buffer[i-1] == '.'
- && buffer[i-2] == '\n' ) {
- ch = EOF;
- i = i-2; // strip off the dot and newlines
- }
- }
- length = i;
- // allocate just enough space to store the string
- text = (char *)malloc((length+1)*sizeof(char));
- if( text == NULL ) {
- printf("Error: could not allocate memory.\n");
- exit( 1 );
- }
- // copy text from buffer to new string
- for( i=0; i<length; i++ ) {
- text[i] = buffer[i];
- }
- text[i] = '\0'; // add end-of-string marker
- return( text );
- }
- /************************************************************
- Get date from standard input;
- if date is invalid, ask the user to re-enter it.
- */
- void getDate( Date *d )
- {
- printf("Date: ");
- while( !scanDate( d ) || !dateOK( d )) {
- printf("Re-enter date in format dd/mm/yy: ");
- }
- }
- /************************************************************
- scan date in the format dd/mm/yyyy
- */
- int scanDate( Date *d )
- {
- char s[MAX_LINE];
- fgets( s, MAX_LINE, stdin );
- if(sscanf(s,"%d/%d/%d",&d->day,&d->month,&d->year)<3){
- return FALSE;
- }
- if( d->year < 100 ) { // turn /12 into /2012, etc.
- d->year = 2000 + d->year;
- }
- return TRUE;
- }
- /************************************************************
- Get time from standard input;
- if time is invalid, ask the user to re-enter it.
- */
- void getTime( Time *t )
- {
- printf("Time: ");
- while( !scanTime( t ) || !timeOK( t )) {
- printf("Re-enter time in format hh:mm:ss: ");
- }
- }
- /************************************************************
- scan time in the format hh:mm:ss
- */
- int scanTime( Time *t )
- {
- char s[MAX_LINE];
- fgets( s, MAX_LINE, stdin );
- return(
- sscanf(s,"%d:%d:%d",&t->hour,&t->minute,&t->second)==3);
- }
- /************************************************************
- Return TRUE if date is valid; FALSE otherwise.
- */
- int dateOK( Date *d )
- {
- int leap=0;
- if((d->year%4)==0){
- leap=1;
- if((d->year%100)==0){
- leap=0;
- if((d->year%400)==0){
- leap=1;
- }
- }
- }
- if((d->month == 2) && (leap == 1)){
- return( d->day >= 1 && d->day <= 29);
- }
- if((d->month == 2) && (leap == 0)){
- return( d->day >= 1 && d->day <= 28);
- }
- if(d->month == 1){
- return( d->day >= 1 && d->day <= 31);
- }
- if(d->month == 3){
- return( d->day >= 1 && d->day <= 31);
- }
- if(d->month == 5){
- return( d->day >= 1 && d->day <= 31);
- }
- if(d->month == 7){
- return( d->day >= 1 && d->day <= 31);
- }
- if(d->month == 8){
- return( d->day >= 1 && d->day <= 31);
- }
- if(d->month == 10){
- return( d->day >= 1 && d->day <= 31);
- }
- if(d->month == 12){
- return( d->day >= 1 && d->day <= 31);
- }
- if(d->month == 4){
- return( d->day >= 1 && d->day <= 30);
- }
- if(d->month == 6){
- return( d->day >= 1 && d->day <= 30);
- }
- if(d->month == 9){
- return( d->day >= 1 && d->day <= 30);
- }
- if(d->month == 11){
- return( d->day >= 1 && d->day <= 30);
- }
- if(d->year < 1582){
- return FALSE;
- }
- return TRUE;
- }
- /************************************************************
- Return TRUE if time is valid; FALSE otherwise.
- */
- int timeOK( Time *t )
- {
- return( t->hour >= 0 && t->hour < 24
- && t->minute >= 0 && t->minute < 60
- && t->second >= 0 && t->second < 60 );
- }
- // INSERT NEW FUNCTIONS, AS APPROPRIATE
- /************************************************************
- Print the specified integer in two digits (prefixed with '0'
- if necessary), followed by the specified character.
- */
- void printPadded( int n, char ch )
- {
- if( n < 10 ) {
- putchar('0');
- }
- printf("%d%c",n,ch );
- }
- /************************************************************
- Print date in the format dd/mm/yyyy
- */
- void printDate( Date d )
- {
- printPadded( d.day, '/');
- printPadded( d.month,'/');
- printf("%d ", d.year );
- }
- /************************************************************
- Print time in the format hh:mm:ss
- */
- void printTime( Time t )
- {
- printPadded( t.hour, ':');
- printPadded( t.minute,':');
- printPadded( t.second,' ');
- }
- /************************************************************
- Print the Name, followed by the first line of the Text.
- */
- void printBrief( MsgNode * msg )
- {
- char *text=msg->text;
- int i=0,j=0;
- if( msg->deleted ) {
- printf("[deleted]\n");
- }
- else {
- printf("%s: ", msg->name );
- while( isspace( text[i] )) {
- i++;
- }
- while( j < 40 && text[i+j] != '\0'
- && text[i+j] != '\n' ) {
- putchar( text[i+j] );
- j++;
- }
- putchar('\n');
- }
- }
- /************************************************************
- Print message in Full
- */
- void printFull( MsgNode * msg )
- {
- if( msg != NULL ) {
- printf("Message %d", msg->messageNum );
- if( msg->deleted ) {
- printf(" has been deleted.\n");
- }
- else {
- printf("\nDate: ");
- printDate( msg->date );
- printf("\nTime: ");
- printTime( msg->time );
- printf("\nName: %s\n", msg->name );
- printf("Text: %s\n", msg->text );
- }
- }
- }
- /************************************************************
- Free all memory occupied by linked list of messages
- */
- void freeList( MsgNode *head )
- {
- MsgNode *node;
- while( head != NULL ) {
- node = head;
- head = head->next;
- free( node->name );
- free( node->text );
- free( node );
- }
- }
Add Comment
Please, Sign In to add comment