home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / get_font.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-17  |  3.0 KB  |  138 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: get_font.c,v 1.1 89/03/17 08:21:09 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/get_font.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/get_font.c,v $$Revision: 1.1 $";
  12.  
  13. /* LRU routines for font management */
  14.  
  15. #include <stdio.h>
  16. #include "bitmap.h"
  17. #include "font.h"
  18.  
  19. #define MAX_FONT        20
  20. #ifndef NULL
  21. #define NULL        ((char *) 0)
  22. #endif
  23. #define NULL_LIST    ((struct list *) 0)
  24.  
  25. static struct list {
  26.    struct list *next, *prev;        /* next or previous list item */
  27.    struct font *font;            /* pointer to font structure */
  28.    char *name;                /* font name */
  29.    };
  30.  
  31. struct list *list_top = NULL_LIST;
  32.  
  33. /* insert an element at the top of the list */
  34.  
  35. insert(ptr)
  36. register struct list *ptr;
  37.    {
  38.    ptr->next = list_top;
  39.    ptr->prev = NULL_LIST;
  40.    if (ptr->next != NULL_LIST)
  41.       ptr->next->prev = ptr;
  42.    list_top = ptr;
  43.    }
  44.  
  45. /* unlink an element from the list */
  46.  
  47. unlink(ptr)
  48. register struct list *ptr;
  49.    {
  50.    if (ptr->next != NULL_LIST)
  51.       ptr->next->prev = ptr->prev;
  52.    if (ptr->prev != NULL_LIST)
  53.       ptr->prev->next = ptr->next;
  54.    }
  55.  
  56. /* get font, name for an element */
  57.  
  58. create(name,ptr)
  59. char *name;
  60. register struct list *ptr;
  61.    {
  62.    char *save_line();
  63.  
  64.    if (ptr->name != NULL)
  65.       free(ptr->name);
  66.    if (ptr->font != (struct font *) 0) 
  67.       free_font(ptr->font);
  68.    ptr->name = save_line(name);
  69.    ptr->font = open_font(name);
  70.    }
  71.  
  72. /* manage the list of fonts (using LRU) */
  73.  
  74. struct font *
  75. get_font(name)
  76. char *name;
  77.    {
  78.    static count = 0;
  79.    register struct list *ptr;
  80.    register int found=0;
  81.    char *alloc();
  82.    
  83.    if (name == (char *) 0 || *name == '\0')
  84.       return((struct font *) 0);
  85.  
  86.    if (count>0 && strcmp(name,list_top->name)==0) {
  87.       return(list_top->font);
  88.       }
  89.    
  90.    for(ptr=list_top;count>0;ptr=ptr->next) {
  91.       if (found=(ptr->name && strcmp(name,ptr->name)==0)) {
  92.          unlink(ptr);
  93.          insert(ptr);
  94.          break;
  95.          }
  96.       if (ptr->next == NULL_LIST)
  97.          break;
  98.       }
  99.    if (!found && count<MAX_FONT) {
  100.       ptr = (struct list *) alloc(sizeof(struct list));
  101.       ptr->name = NULL;
  102.       ptr->font = (struct font *) NULL;
  103.       create(name,ptr);
  104.       insert(ptr);
  105.       count++; 
  106.       }
  107.    else if (!found) {
  108.       unlink(ptr);
  109.       create(name,ptr);
  110.       insert(ptr);
  111.       }
  112.    return(list_top->font);
  113.    }
  114.  
  115. /* misc stuff */
  116.  
  117. char *
  118. alloc(size)
  119. int size;
  120.    {
  121.    char *malloc();
  122.    char *temp;
  123.  
  124.    if ((temp=malloc(size)) == NULL)
  125.       perror("malloc failed - called from alloc()\n");
  126.    return(temp);
  127.    }
  128.  
  129. char *
  130. save_line(s)
  131. char *s;
  132.    {
  133.    char *strcpy(), *alloc();
  134.  
  135.    if (s==NULL) return(s);
  136.    return(strcpy(alloc(strlen(s)+1),s));
  137.    }
  138.