summaryrefslogtreecommitdiff
path: root/clients/xzwrite/yank.c
blob: 2aba0e817abee9c989cd0cfde1e1caaa3ec6a814 (plain)
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
#include "xzwrite.h"

static Yank yank_buffer;
extern Defaults defs;

static int read_index, write_index, highest;

void yank_init()
{
     yank_buffer = (Yank) Malloc(defs.max_yanks*sizeof(YankRec),
				   "while allocating yank buffer", NULL);
     bzero((char *) yank_buffer, defs.max_yanks*sizeof(YankRec));

     read_index = write_index = 0;
     highest = -1;
}

Yank yank_prev()
{
     if (highest == -1)
	  return NULL;
     
     if (--read_index < 0) read_index = highest;
     return &yank_buffer[read_index];
}

Yank yank_next()
{
     if (highest == -1)
	  return NULL;
     
     if (++read_index > highest) read_index = 0;
     return &yank_buffer[read_index];
}

void yank_store(dest, msg)
   Dest dest;
   char *msg;
{
     yank_buffer[write_index].dest = *dest;
     if (yank_buffer[write_index].msg)
	 free(yank_buffer[write_index].msg);
     yank_buffer[write_index].msg = (char *) Malloc(strlen(msg) + 1,
						    "while yanking message",
						    NULL);
     strcpy(yank_buffer[write_index].msg, msg);

     /*
      * read_index  = write_index + 1 so that if I follow the store by
      * a yank_prev I will get the message just stored (since
      * read_index is decremented before being used).  If I do a
      * yank_next, then read_index will be > highest and reset to zero.
      */
     read_index = write_index + 1;
     if (write_index > highest)
	  highest = write_index;
     write_index = (write_index + 1) % defs.max_yanks;
}