Guest User

Untitled

a guest
Jan 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.14 KB | None | 0 0
  1. /**
  2.    hw2.h
  3.  
  4.    COMP1917 Computing 1
  5.  
  6.    Program supplied as a starting point for
  7.    Assignment 2: Message Board
  8.  
  9.    UNSW Session 2, 2012
  10. */
  11.  
  12. #define TRUE           1
  13. #define FALSE          0
  14.  
  15. #define MAX_LINE     256
  16. #define MAX_TEXT    4096
  17.  
  18. typedef struct date    Date;
  19. typedef struct time    Time;
  20. typedef struct msgNode MsgNode;
  21.  
  22. struct date {
  23.   int day;
  24.   int month;
  25.   int year;
  26. };
  27.  
  28. struct time {
  29.   int hour;
  30.   int minute;
  31.   int second;
  32. };
  33.  
  34. struct msgNode {
  35.   int   messageNum;
  36.   int   inReplyTo;
  37.   int   indent;
  38.   int   deleted;
  39.   char *name;
  40.   Date  date;
  41.   Time  time;
  42.   char *text;
  43.   MsgNode *next;
  44.  
  45. };
  46.  
  47. // INSERT NEW FUNCTION PROTOTYPES, AS APPROPRIATE
  48.  
  49. void printPrompt();
  50. void   printHelp();
  51. MsgNode *getNode( void );
  52. char    *getName( void );
  53. char    *getText( void );
  54. void     getDate( Date *d );
  55. int     scanDate( Date *d );
  56. void     getTime( Time *t );
  57. int     scanTime( Time *t );
  58. int       dateOK( Date *d );
  59. int       timeOK( Time *t );
  60. void printPadded( int n, char ch );
  61. void   printDate( Date d );
  62. void   printTime( Time t );
  63. void  printBrief( MsgNode * msg );
  64. void   printFull( MsgNode * msg );
  65. void    freeList( MsgNode *list );
  66.  
  67.  
  68. /*
  69.    hw2.c
  70.  
  71.    COMP1917 Computing 1
  72.  
  73.    Program supplied as a starting point for
  74.    Assignment 2: Message Board
  75.  
  76.    UNSW Session 2, 2012
  77. */
  78.  
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <string.h>
  82. #include <ctype.h>
  83. #include <unistd.h>
  84.  
  85. //#include "hw2.h"
  86.  
  87.  
  88. int globalMessageNum = 1;
  89.  
  90.  
  91. int main( void )
  92. {
  93.   MsgNode *list = NULL;
  94.   MsgNode *node;
  95.   MsgNode *location = NULL;
  96.   char command[MAX_LINE];
  97.   char c;
  98.   int n;
  99.  
  100.  
  101.   printPrompt();
  102.  
  103.   // enter a loop, reading and executing commands from the user
  104.   while( fgets(command,MAX_LINE,stdin) != NULL ) {
  105.     char *p;
  106.  
  107.     // replace newline with end-of-string character
  108.     if(( p = strchr(command,'\n')) != NULL ) {
  109.       *p = '\0';
  110.     }
  111.     p = command;
  112.     while( isspace(*p)) { // skip any initial spaces
  113.       p++;
  114.     }
  115.     c = *p;
  116.  
  117.     if( isdigit(c)) {
  118.  
  119.  
  120.     MsgNode *temp = list;
  121.     sscanf( command,"%d",&n );
  122.         while(temp!= NULL){
  123.             if(n == location->messageNum){
  124.                 printf(" %d %d\n", n, location->messageNum);
  125.                 printf("Y %d %d\n", n, temp->messageNum);
  126.             }else{
  127.                 printf(" %d %d\n", n, location->messageNum);
  128.                 printf("Y %d %d\n", n, temp->messageNum);
  129.                
  130.  
  131.              }
  132.              temp = temp->next;
  133.         }
  134.  
  135.     }
  136.  
  137.  
  138.     else switch( c ) {
  139.  
  140.  
  141.     case 'a': case 'A': // Add item
  142.     {
  143.       node = getNode();
  144.       printFull( node );
  145.       if (list == NULL) {
  146.         list = node;
  147.       } else {
  148.         MsgNode *current = list;
  149.         while (current->next != NULL) {
  150.             current = current->next;
  151.         }
  152.         current->next = node;
  153.       }
  154.       location = node;
  155.       break;
  156.      }
  157.     case 'l': case 'L':
  158.     {
  159.         MsgNode *current = list;
  160.         while(current != NULL) {
  161.             if ((current->next) == NULL) {
  162.                 printf("-> ");
  163.             } else {
  164.                 printf("   ");
  165.             }
  166.             printf("%2d ",current->messageNum);
  167.             printBrief(current);
  168.             current = current->next;
  169.         }
  170.         break;
  171.     }
  172.  
  173.     case 'p': case 'P':
  174.     {
  175.         MsgNode *current = list;
  176.         while(current != NULL) {
  177.            if (current->next == NULL) {
  178.  
  179.             printf("%2d ",current->messageNum);
  180.             printFull(current);
  181.            }
  182.             current = current->next;
  183.  
  184.         }
  185.  
  186.         break;
  187.     }
  188.  
  189.  
  190.       case 'd': case 'D':
  191.     {
  192.       //set current to delete
  193.         break;
  194.     }
  195.  
  196.  
  197.     case 'b': case 'B':
  198.     {
  199.  
  200.  
  201.  
  202.         break;
  203.     }
  204.  
  205.     case 's': case 'S':
  206.     {
  207.         MsgNode *current = list;
  208.         char search[256];
  209.  
  210.         printf("Search Text:\n");
  211.         fgets(search,256,stdin);
  212.         printf("Searching for: %s\n", search);
  213.  
  214.         while( current != NULL){
  215.             if((strstr(search, current->text) || (strstr(search, current->text))) != NULL){
  216.                printf("Yes\n");
  217.             }else{
  218.                 printf("No\n");
  219.             }
  220.             current = current->next;
  221.  
  222.         }
  223.  
  224.  
  225.        break;
  226.     }
  227.  
  228.  
  229.  
  230.     case 'h': case 'H': // Help
  231.       printHelp();
  232.       break;
  233.  
  234.     case 'q': case 'Q': // Quit
  235.       freeList( list );
  236.       printf("Bye!\n");
  237.       return 0;
  238.       break;
  239.     }
  240.  
  241.     printPrompt();
  242.   }
  243.  
  244.   return 0;
  245. }
  246.  
  247. // INSERT NEW FUNCTIONS, AS APPROPRIATE
  248.  
  249. /************************************************************
  250.    Print prompt only if output goes to screen
  251. */
  252. void printPrompt()
  253. {
  254.   if (isatty(fileno(stdin))) {
  255.     printf("Enter command (A,F,B,P,L,D,R,T,S,U,Q, H for Help): ");
  256.   }
  257. }
  258.  
  259. /************************************************************
  260.    Print the list of commands available to the user,
  261.    and a brief summary of what each command does.
  262. */
  263. void printHelp()
  264. {
  265.   printf("\n");
  266.   printf(" A - Add\n" );
  267.   printf(" L - List\n" );
  268.   printf(" P - Print\n" );
  269.   printf(" F - Forward\n" );
  270.   printf(" B - Back\n" );
  271.   printf("<k>- jump to message k\n");
  272.   printf(" D - Delete\n");
  273.   printf(" R - Reply\n");
  274.   printf(" T - Threads\n");
  275.   printf(" S - Search\n");
  276.   printf(" U - Undo\n" );
  277.   printf(" Q - Quit\n");
  278.   printf(" H - Help\n");
  279.   printf("\n");
  280. }
  281.  
  282. /************************************************************
  283.    allocate space for a new message and get the
  284.    name, date, time and text from standard input.
  285. */
  286. MsgNode * getNode( void )
  287. {
  288.   MsgNode * new_node;
  289.   new_node = (MsgNode *)malloc(sizeof(MsgNode));
  290.   if( new_node == NULL ) {
  291.      printf("Error: could not allocate memory.\n");
  292.      exit( 1 );
  293.   }
  294.   new_node->messageNum= globalMessageNum++;
  295.   new_node->inReplyTo = 0;
  296.   new_node->indent    = 0;
  297.   new_node->deleted   = FALSE;
  298.   new_node->name      = getName();
  299.   getDate( &new_node->date );
  300.   getTime( &new_node->time );
  301.   new_node->text      = getText();
  302.   new_node->next      = NULL;
  303.  
  304.   return( new_node );
  305. }
  306.  
  307. /************************************************************
  308.    Read one line of text from standard input,
  309.    store it in a string and return a pointer to the string.
  310. */
  311. char * getName( void )
  312. {
  313.   char buffer[MAX_LINE];
  314.   char *name;
  315.   int length;
  316.   int ch;
  317.   int i;
  318.  
  319.   // prompt user for input
  320.   printf( "Name: " );
  321.   // skip any intial newline character
  322.   if(( ch = getchar()) == '\n' ) {
  323.      ch = getchar();
  324.   }
  325.   // read text initially into a buffer
  326.   i=0;
  327.   while( i < MAX_LINE && ch != '\n' && ch != EOF ) {
  328.      buffer[i++] = ch;
  329.      ch = getchar();
  330.   }
  331.   // trim of any trailing whitespace
  332.   while( isspace( buffer[i] )) {
  333.     i--;
  334.   }
  335.   // allocate just enough space to store the string
  336.   length = i;
  337.   name = (char *)malloc((length+1)*sizeof(char));
  338.   if( name == NULL ) {
  339.      printf("Error: could not allocate memory.\n");
  340.      exit( 1 );
  341.   }
  342.   // copy text from buffer into new string
  343.   for( i=0; i < length; i++ ) {
  344.      name[i] = buffer[i];
  345.   }
  346.   name[i] = '\0'; // add end-of-string marker
  347.  
  348.   return( name );
  349. }
  350.  
  351. /************************************************************
  352.    Read several lines of text from standard input,
  353.    store them in a string and return a pointer to the string.
  354. */
  355. char * getText( void )
  356. {
  357.   char buffer[MAX_TEXT];
  358.   char *text;
  359.   int length;
  360.   int ch;
  361.   int i;
  362.  
  363.   printf("Text: ");
  364.   ch = getchar();
  365.   i=0;
  366.   while(( i < MAX_TEXT )&&( ch != EOF )) {
  367.      buffer[i++] = ch;
  368.      ch = getchar();
  369.      // stop when you encounter a dot on a line by itself
  370.      if( i > 1 && ch == '\n' && buffer[i-1] == '.'
  371.                              && buffer[i-2] == '\n' ) {
  372.         ch = EOF;
  373.         i  = i-2; // strip off the dot and newlines
  374.      }
  375.   }
  376.   length = i;
  377.   // allocate just enough space to store the string
  378.   text = (char *)malloc((length+1)*sizeof(char));
  379.   if( text == NULL ) {
  380.      printf("Error: could not allocate memory.\n");
  381.      exit( 1 );
  382.   }
  383.   // copy text from buffer to new string
  384.   for( i=0; i<length; i++ ) {
  385.      text[i] = buffer[i];
  386.   }
  387.   text[i] = '\0'; // add end-of-string marker
  388.  
  389.   return( text );
  390. }
  391.  
  392. /************************************************************
  393.    Get date from standard input;
  394.    if date is invalid, ask the user to re-enter it.
  395. */
  396. void getDate( Date *d )
  397. {
  398.   printf("Date: ");
  399.   while( !scanDate( d ) || !dateOK( d )) {
  400.      printf("Re-enter date in format dd/mm/yy: ");
  401.   }
  402. }
  403.  
  404. /************************************************************
  405.    scan date in the format dd/mm/yyyy
  406. */
  407. int scanDate( Date *d )
  408. {
  409.   char s[MAX_LINE];
  410.  
  411.   fgets( s, MAX_LINE, stdin );
  412.   if(sscanf(s,"%d/%d/%d",&d->day,&d->month,&d->year)<3){
  413.     return FALSE;
  414.   }
  415.   if( d->year < 100 ) { // turn /12 into /2012, etc.
  416.     d->year = 2000 + d->year;
  417.   }
  418.   return TRUE;
  419. }
  420.  
  421. /************************************************************
  422.    Get time from standard input;
  423.    if time is invalid, ask the user to re-enter it.
  424. */
  425. void getTime( Time *t )
  426. {
  427.   printf("Time: ");
  428.   while( !scanTime( t ) || !timeOK( t )) {
  429.      printf("Re-enter time in format hh:mm:ss: ");
  430.   }
  431. }
  432.  
  433. /************************************************************
  434.    scan time in the format hh:mm:ss
  435. */
  436. int scanTime( Time *t )
  437. {
  438.   char s[MAX_LINE];
  439.  
  440.   fgets( s, MAX_LINE, stdin );
  441.   return(
  442.      sscanf(s,"%d:%d:%d",&t->hour,&t->minute,&t->second)==3);
  443. }
  444.  
  445. /************************************************************
  446.    Return TRUE if date is valid; FALSE otherwise.
  447. */
  448. int dateOK( Date *d )
  449. {
  450.    int leap=0;
  451.  
  452.  
  453.    if((d->year%4)==0){
  454.       leap=1;
  455.       if((d->year%100)==0){
  456.          leap=0;
  457.          if((d->year%400)==0){
  458.             leap=1;
  459.          }
  460.      }
  461.    }
  462.  
  463.    if((d->month == 2) && (leap == 1)){
  464.       return( d->day >= 1 && d->day <= 29);
  465.    }
  466.    if((d->month == 2) && (leap == 0)){
  467.       return( d->day >= 1 && d->day <= 28);
  468.    }
  469.    if(d->month == 1){
  470.       return( d->day >= 1 && d->day <= 31);
  471.    }
  472.    if(d->month == 3){
  473.       return( d->day >= 1 && d->day <= 31);
  474.    }
  475.    if(d->month == 5){
  476.       return( d->day >= 1 && d->day <= 31);
  477.    }
  478.    if(d->month == 7){
  479.       return( d->day >= 1 && d->day <= 31);
  480.    }
  481.    if(d->month == 8){
  482.       return( d->day >= 1 && d->day <= 31);
  483.    }
  484.    if(d->month == 10){
  485.       return( d->day >= 1 && d->day <= 31);
  486.    }
  487.    if(d->month == 12){
  488.       return( d->day >= 1 && d->day <= 31);
  489.    }
  490.    if(d->month == 4){
  491.       return( d->day >= 1 && d->day <= 30);
  492.    }
  493.    if(d->month == 6){
  494.       return( d->day >= 1 && d->day <= 30);
  495.    }
  496.    if(d->month == 9){
  497.       return( d->day >= 1 && d->day <= 30);
  498.    }
  499.    if(d->month == 11){
  500.       return( d->day >= 1 && d->day <= 30);
  501.    }
  502.    if(d->year < 1582){
  503.       return FALSE;
  504.    }
  505.  
  506.  
  507.   return TRUE;
  508. }
  509.  
  510. /************************************************************
  511.    Return TRUE if time is valid; FALSE otherwise.
  512. */
  513. int timeOK( Time *t )
  514. {
  515.   return(   t->hour   >= 0 && t->hour   < 24
  516.          && t->minute >= 0 && t->minute < 60
  517.          && t->second >= 0 && t->second < 60 );
  518. }
  519.  
  520. // INSERT NEW FUNCTIONS, AS APPROPRIATE
  521.  
  522. /************************************************************
  523.   Print the specified integer in two digits (prefixed with '0'
  524.   if necessary), followed by the specified character.
  525. */
  526. void printPadded( int n, char ch )
  527. {
  528.   if( n < 10 ) {
  529.     putchar('0');
  530.   }
  531.   printf("%d%c",n,ch );
  532. }
  533.  
  534. /************************************************************
  535.   Print date in the format dd/mm/yyyy
  536. */
  537. void printDate( Date d )
  538. {
  539.   printPadded( d.day,  '/');
  540.   printPadded( d.month,'/');
  541.   printf("%d ", d.year );
  542. }
  543.  
  544. /************************************************************
  545.   Print time in the format hh:mm:ss
  546. */
  547. void printTime( Time t )
  548. {
  549.   printPadded( t.hour,  ':');
  550.   printPadded( t.minute,':');
  551.   printPadded( t.second,' ');
  552. }
  553.  
  554. /************************************************************
  555.   Print the Name, followed by the first line of the Text.
  556. */
  557. void printBrief( MsgNode * msg )
  558. {
  559.   char *text=msg->text;
  560.   int i=0,j=0;
  561.   if( msg->deleted ) {
  562.     printf("[deleted]\n");
  563.   }
  564.   else {
  565.     printf("%s: ", msg->name );
  566.     while( isspace( text[i] )) {
  567.       i++;
  568.     }
  569.     while( j < 40 && text[i+j] != '\0'
  570.                   && text[i+j] != '\n' ) {
  571.       putchar( text[i+j] );
  572.       j++;
  573.     }
  574.     putchar('\n');
  575.   }
  576. }
  577.  
  578. /************************************************************
  579.   Print message in Full
  580. */
  581. void printFull( MsgNode * msg )
  582. {
  583.   if( msg != NULL ) {
  584.     printf("Message %d", msg->messageNum );
  585.     if( msg->deleted ) {
  586.       printf(" has been deleted.\n");
  587.     }
  588.     else {
  589.       printf("\nDate: ");
  590.       printDate( msg->date );
  591.       printf("\nTime: ");
  592.       printTime( msg->time );
  593.       printf("\nName: %s\n", msg->name );
  594.       printf("Text: %s\n", msg->text );
  595.     }
  596.   }
  597. }
  598.  
  599. /************************************************************
  600.   Free all memory occupied by linked list of messages
  601. */
  602. void freeList( MsgNode *head )
  603. {
  604.   MsgNode *node;
  605.   while( head != NULL ) {
  606.     node = head;
  607.     head = head->next;
  608.     free( node->name );
  609.     free( node->text );
  610.     free( node );
  611.   }
  612. }
Add Comment
Please, Sign In to add comment