File: ibuf.c

package info (click to toggle)
conquest 8.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,984 kB
  • ctags: 3,086
  • sloc: ansic: 39,393; sh: 8,540; yacc: 446; makefile: 296; lex: 146
file content (186 lines) | stat: -rw-r--r-- 3,469 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "c_defs.h"

/************************************************************************
 *
 * ibuf.c - input buffering to support Function key macros
 * Jon Trulson 3/30/96
 *
 * $Id: ibuf.c,v 1.8 2004/05/08 21:01:41 jon Exp $
 *
 * Copyright 1999-2004 Jon Trulson under the ARTISTIC LICENSE. (See LICENSE).
 **********************************************************************/

/**********************************************************************/
/* Unix/C specific porting and supporting code Copyright (C)1994-1996 */
/* by Jon Trulson <jon@radscan.com> under the same terms and          */
/* conditions of the original copyright by Jef Poskanzer and Craig    */
/* Leres.                                                             */
/*                                                                    */
/**********************************************************************/

#include "conf.h"
#include "global.h"
#include "defs.h"
#include "ibuf.h"

/* the buffer */
static unsigned int *rp, *wp;
static int ndata;
static unsigned int data[IBUFMAX];

/* iBufInit - intialize the input buffer */

void iBufInit(void)
{
  data[0] = 0;
  rp = wp = data;
  ndata = 0;

  return;
}


/* iBufEmpty - return TRUE or FALSE depending on whether the buffer is 
 * empty 
 */

int iBufCount(void)
{
  return ndata;
}

static int putRing(int *buf, int len)
{
  int left, wlen = len, i;
  int *rptr = buf;

  left = IBUFMAX - ndata; /* max space available */

  /* a null buf means to simply return the space left */

  if (!rptr)
    return(left);

  if (wlen > left)
    wlen = left;

  if (rptr != NULL)
    {
      for (i=0; i < wlen; i++, rptr++)
        {
          if ( wp >= (data + IBUFMAX) ) 
            wp = data;

          *wp = *rptr;
          ndata++;
	  wp++;
        }
    }

  return(wlen);
}


static int getRing(int *buf, int len, int update)
{
  unsigned int *wptr = buf, *rptr = rp;
  int rlen = len, tlen, numdata = ndata;

  if (rlen > ndata)
    rlen = ndata;

  tlen = rlen;

  while (rlen--)
    {
      if (rptr >= (data + IBUFMAX)) 
        rptr = data;

      if (wptr)                 /* NULL buf doesn't copy data - Pop */
        {
          *wptr = *rptr;
          wptr++;
        }
      rptr++;
      numdata--;
    }

  if (update)
    {
      rp = rptr;
      ndata = numdata;
    }

  return(tlen);
}


/* iBufPut - put a string into the buffer */

void iBufPut(char *thestr)
{
  int i;
  int n = strlen(thestr);
  int idata[IBUFMAX];

  /* cvt to int array */
  for (i=0; i<n; i++)
    idata[i] = thestr[i] & 0xff;

  putRing(idata, n);

  return;
}

/* iBufPutc - put a char into the buffer */

void iBufPutc(unsigned int thechar)
{
  putRing(&thechar, 1);

  return;
}


/* iBufGetCh - return next char from the input buffer */

unsigned int iBufGetCh(void)
{
  static int c;

  if (!iBufCount())
    {
      /*      clog("IBUF GETC EMPTY, returning 0\n");*/
      return('\0');
    }
  getRing(&c, 1, TRUE);

  return(c);
}
  
/* DoMacro - stuff the buffer if an fkey pressed */

int DoMacro(int fkey)
{
  if (fkey < 0 || fkey >= MAX_MACROS)
  {
#ifdef DEBUG_MACROS
    
    clog("DoMacro(): KEY_F(0) = %d, KEY_F(MAX_MACROS) = %d ch = %d, fkey = %d", 
	 KEY_F(0), KEY_F(MAX_MACROS), ch, fkey);
    clog("DoMacro(): got invalid Fkey: %d", ch);
#endif

    return(FALSE);
  }

  iBufPut(UserConf.MacrosF[fkey]);	

#ifdef DEBUG_MACROS
  clog("DoMacro(): got an FKey: %d", ch);
#endif
  
  return(TRUE);

}