home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / mt / chain.c < prev    next >
C/C++ Source or Header  |  1989-01-12  |  3KB  |  115 lines

  1. /* chain.c - screen file storage and display */
  2. /* uses linked lists with each node defined as a struct strchain */
  3. /* `MIDI Sequencing In C', Jim Conger, M&T Books, 1989 */
  4.  
  5. /* #define TURBOC 1   Define if using TURBOC, leave out for Microsoft */
  6.  
  7. #include <stdio.h>
  8.  
  9. #ifdef TURBOC
  10.     #include <alloc.h>      /* Turbo C library file name */
  11. #else
  12.     #include <malloc.h>     /* Microsoft C library file name */
  13. #endif
  14.  
  15. #include "standard.h"
  16. #include "screenf.h"        /* contains structure definition */
  17.  
  18.  
  19. /* read file into a linked list (chain) in near memory.  Returns a pointer */
  20. /* to the start of the linked list. */
  21. struct strchain 
  22. *inpchain(char *file, int maxlen)
  23. {
  24.     FILE *stream;
  25.     struct strchain *root;
  26.     char *strbuf;
  27.  
  28.     strbuf = (char *) malloc(maxlen + 1);       /* set asside input buffer */
  29.  
  30.     stream = fopen(file, "r");
  31.     
  32.     root = NULL;
  33.     while(fgets(strbuf, maxlen, stream) != NULL){
  34.         root = chain(root, strbuf);
  35.     }
  36.     
  37.     fclose(stream);
  38.     free(strbuf);               /* free buff for other use */
  39.     return(root);
  40. }
  41.  
  42.  
  43. /* add a string recursively to the end of a linked list.  Returns a */
  44. /* pointer to the new node. */
  45. struct strchain 
  46. *chain(struct strchain *p, char *w)
  47. {
  48.     if(p == NULL){
  49.         p = chainalloc();
  50.         p->line = strsave(w);
  51.         p->next = NULL;
  52.     }
  53.     else{
  54.         p->next = chain(p->next, w);
  55.     }
  56.     return(p);
  57. }
  58.  
  59.  
  60. /* free all memory reserved for the linked list (chain) pointed to by p */
  61. void
  62. dechain(struct strchain *p)
  63. {
  64.     struct strchain *q;
  65.     if(p != NULL){
  66.         q = p->next;
  67.         free(p->line);
  68.         free(p);
  69.         dechain(q);
  70.     }
  71. }
  72.  
  73.  
  74. /* write all lines of the linked list (chain) to stdout starting on line y. */
  75. /* Standard console output version.  Characters written with color attrib. */
  76. void
  77. dispchain(struct strchain *p, int y, int attrib)
  78. {
  79.     if(p){
  80.         writeword(p->line, 1, y, attrib);
  81.         dispchain(p->next, ++y, attrib);
  82.     }
  83. }
  84.  
  85.  
  86. /* Reserve memory space for next node in the linked list. */
  87. struct strchain 
  88. *chainalloc(void)
  89. {
  90.     return((struct strchain *) malloc(sizeof(struct strchain)));
  91. }
  92.  
  93.  
  94. /* Save a string to memory.  Reserves memory and returns a pointer to start */
  95. char 
  96. *strsave(char *s)
  97. {
  98.     char *p;
  99.     if((p = (char *) malloc(strlen(s) + 1)) != NULL)
  100.         strcpy(p,s);
  101.     return(p);
  102. }
  103.  
  104.  
  105. /* Write all lines of list to console, FAST VERSION, y is start line */
  106. /* p is pointer to start of linked list, mode is video mode. */
  107. void
  108. fdispchain(struct strchain *p, int y, int attrib, int mode)
  109. {
  110.     if(p){
  111.         fwriteword(p->line, 1, y, attrib, mode);
  112.         fdispchain(p->next, ++y, attrib, mode);
  113.     }
  114. }
  115.