home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / msj / msjv2_4 / by_time / by_time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-02  |  7.1 KB  |  200 lines

  1. /*
  2.  
  3. Microsoft Systems Journal
  4. Volume 2; Issue 4; September, 1987
  5.  
  6. Code Listings For:
  7.  
  8.     BY_TIME
  9.     pp. 35-45
  10.  
  11. Author(s): Steve Schustack
  12. Title:     Dynamic Allocation Techniques for Memory Management in C Programs
  13.  
  14.  
  15.  
  16. Figure 15
  17. =========
  18.  
  19. */
  20.  
  21. /****************************************************************
  22.  * BY_TIME.C: Filter sorts DIR output in linked list, most recent 
  23.  * first. This is the main function of the program.
  24.  */
  25.  
  26. #include <stdio.h>                      /* For BUFSIZ symbol   */
  27. #include "by_time.h"                    /* Linked list struct  */
  28. main()
  29.         {
  30.         struct s_dir_node head;         /* First node in list  */
  31.         char in_buf[BUFSIZ];            /* Input buffer for    */
  32.                                         /*   DIR output        */
  33.         bgn_list(&head);                /* Initialize list     */
  34.         while (get_dir(in_buf))         /* Input DIR info for  */
  35.                                         /*   next file         */
  36.                 sav_dir(in_buf, &head); /* Save file info in   */
  37.                                         /*  sorted linked list */
  38.         sho_list(&head);                /* Show sorted list    */
  39.         }
  40.  
  41. /*
  42.  
  43. Figure 16
  44. =========
  45.  
  46. */
  47.  
  48. /****************************************************************
  49.  * BGN_LIST.C: Initialize head of list to dummy highest value  
  50.  */
  51. #include <stdio.h>              /* For NULL (zero) pointer     */
  52. #include "by_time.h"            /* For linked list struct      */
  53.  
  54. bgn_list(p_head)
  55. struct s_dir_node *p_head;      /* Pointer to head of list     */
  56.         {
  57.         /* Date in head is greater than DIR date for any file  */
  58.         strcpy(p_head->dir_line, 
  59.                 "ZZZZZZZZ ZZZ    99999  99-99-99  99:99p");
  60.         p_head->next = NULL;    /* No next node - empty list   */
  61.         }
  62.  
  63. /*
  64.  
  65. Figure 17
  66. =========
  67.  
  68. */
  69.  
  70. /****************************************************************
  71.  * GET_DIR.C:  Input DIR info for next fil
  72. e from stdin
  73.  */
  74. #include <stdio.h>              /* For NULL (zero) pointer     */
  75.  
  76. int get_dir(buf)        
  77. char *buf;              /* Buffer for read and pass line back  */
  78.         {
  79.         char *rtn;                      /* save gets() return  */
  80.  
  81.         /* Loop:  Input lines until no more input or got line  */
  82.         /*   starting with an uppercase letter (has file data) */
  83.         while ((rtn = gets(buf)) &&     /* Input a DIR line    */
  84.                 (buf[0] < 'A' || buf[0] > 'Z'))   /* For file? */
  85.                 ;
  86.         return (rtn != NULL);  /* Return 1 if got data, else 0 */
  87.         }        
  88.  
  89. /*
  90.  
  91. Figure 18
  92. =========
  93.  
  94. */
  95.  
  96. /****************************************************************
  97.  * SAV_DIR.C: Allocate new node in list, save DIR info in it
  98.  */
  99. #include <stdio.h>              /* For NULL (zero) pointer     */
  100. #include "by_time.h"            /* For linked list struct      */
  101.  
  102. sav_dir(buf, p_head)
  103. char *buf;                      /* Line of DIR output to save  */
  104. struct s_dir_node *p_head;      /* Pointer to head of list     */
  105.         {
  106.         struct s_dir_node *p_next;      /* Pointer to next     */
  107.                                         /*   node in list      */
  108.         struct s_dir_node *old_p_next;  /* Pointer to previous */
  109.                 /*   next node, parent of current next node    */
  110.         struct s_dir_node *p_new;       /* Pointer to new node */
  111.  
  112.         /* Loop: for each node in list until end of list or    */
  113.         /*   insert point that will keep list sorted is found  */
  114.         for (p_next = p_head->next, old_p_next = p_head;
  115.                 p_next && time_b4(buf, p_next);         
  116.                 old_p_next = p_next, p_next = p_next->next)
  117.                 ;
  118.         /* Dynamically allocate memory for new node - DIR output 
  119.          *   line. Note use of the cast (struct s_dir_node *) 
  120.          *   operator in the assignment to avoid this message: 
  121.          *         warning 47: '=' : different levels of 
  122.  
  123.          *                 indirection
  124.          */
  125.         p_new = (struct s_dir_node *) 
  126.                 malloc(sizeof (struct s_dir_node));
  127.         if (p_new == NULL)      /* malloc() failed, out of RAM */
  128.                 {
  129.                 puts("Out of memory!!!");
  130.                 return;
  131.                 }
  132.         strcpy(p_new->dir_line, buf);   /* Save DIR line in    */
  133.                                         /*  newly alloc'd node */
  134.         p_new->next = old_p_next->next; /* New node points to  */
  135.                                         /*   rest of list      */
  136.         old_p_next->next = p_new;       /* Insert new node in  */
  137.                                         /*   list              */
  138.         }
  139.  
  140. /*
  141.  
  142. Figure 19
  143. =========
  144.  
  145. */
  146.  
  147. /****************************************************************
  148.  * TIME_B4.C: Return 1 if date and time in buf is before date 
  149.  *   and time in node p_next points to, otherwise return 0.
  150.  */
  151. #include "by_time.h"            /* For linked list struct      */
  152. int time_b4(buf, p_next)
  153. char *buf;                      /* Line of DIR output to find  */ 
  154.                                 /*   insert point in list for  */
  155. struct s_dir_node *p_next;      /* Pointer to node in list to  */
  156.         {                       /*   compare time in buf with  */
  157.         int rtn;                /* Return value from strncmp() */
  158.  
  159.         /* compare year, month, day, am/pm, hour, and minute   */
  160.  
  161.         if (rtn = strncmp(&buf[29], &(p_next->dir_line)[29], 2))
  162.                  return (rtn < 0);      /* Years differ        */
  163.         if (rtn = strncmp(&buf[23], &(p_next->dir_line)[23], 2)) 
  164.                  return (rtn < 0);      /* Months differ       */
  165.         if (rtn = strncmp(&buf[26], &(p_next->dir_line)[26], 2)) 
  166.                  return (rtn < 0);      /* Days differ         */
  167.         if (buf[38] != (p_next->dir_line)[38])  /* am/pm's     */ 
  168.                 return (buf[38] == 'a');        /* differ      */
  169.         if (rtn = strncmp(&buf[33], &(p_next->dir_line)[33], 2)) 
  170.                  return (rtn < 0);      /* Hours differ        */
  171.         if (rtn = strncmp(&buf[36], &(p_next->dir_line)[36], 2)) 
  172.                  return (rtn < 0);      /*  Minutes differ     */
  173.         return (0);             /* Dates and times are equal   */
  174.         }
  175.  
  176. */
  177.  
  178. Figure 20
  179. =========
  180.  
  181. */
  182.  
  183. /****************************************************************
  184.  * SHO_LIST.C: Show sorted linked list - output it to stdout   
  185.  */
  186. #include <stdio.h>              /* For NULL (zero) pointer     */
  187. #include "by_time.h"            /* Linked list struct          */
  188.  
  189. sho_list(p_head)
  190. struct s_dir_node *p_head;      /* Pointer to head of list     */
  191.         {
  192.         struct s_dir_node *p_next;      /* Pointer to next     */
  193.                                         /*   node in list      */
  194.  
  195.         for (p_next = p_head->next;     /* Start at first node */
  196.                 p_next != NULL;         /* Still more list?    */
  197.                 p_next = p_next->next)  /* Move down a node    */
  198.  
  199.                 puts(p_next->dir_line); /* Output a DIR line   */
  200.         }