-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_records.c
138 lines (133 loc) · 3.76 KB
/
copy_records.c
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
/*
* Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
*
* This file is part of ED.
*
* ED is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation.
*
* ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with ED
* (see the file COPYING). If not, write to the Free Software Foundation, 675
* Mass Ave, Cambridge, MA 02139, USA.
*/
#include "opsys.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "memory.h"
#include "rec.h"
#include "window.h"
#include "ed_dec.h"
/******************************************************************************\
|Routine: copy_records
|Callby: wincom
|Purpose: Copies the contents of an existing set of records.
|Arguments:
| source is the header of the existing record queue.
| base is the header of the new, copied queue.
| binsource is a flag that the source buffer is binary.
| bindest is a flag that the destination buffer is binary.
\******************************************************************************/
Int copy_records(source,dest,binsource,bindest)
rec_ptr source,dest;
Int binsource,bindest;
{
register rec_ptr old,end,new;
register Int i,j,count;
Char buf[BINARY_RECSIZE + 1];
dest->next = dest->prev = dest;
count = 0;
/* source and dest are the same type */
if(binsource == bindest)
for(old = source->next,end = (rec_ptr)&source->next;old != end;old = old->next)
{
i = old->length;
new = (rec_ptr)imalloc(sizeof(rec_node));
insq(new,dest->prev);
new->data = (Char *)imalloc(i + 1);
if(i > 0)
memcpy(new->data,old->data,i);
new->data[i] = 0;
new->length = i;
new->recflags = 1; /* it is a freeable buffer */
count++;
}
/* source is binary, dest is not. this never happens with the current setup, since there isn't a -notb option */
/* else if(binsource)
{
for(i = 0,old = source->next,end = (rec_ptr)&source->next;old != end;old = old->next)
{
for(j = 0;i < sizeof(buf) && j < old->length;j++)
if((buf[i++] = old->data[j]) == '\n')
{
buf[--i] = '\0';
new = (rec_ptr)imalloc(sizeof(rec_node));
insq(new,dest->prev);
new->data = (Char *)imalloc(i + 1);
if(i > 0)
memcpy(new->data,buf,i);
new->data[i] = 0;
new->length = i;
new->recflags = 1;
count++;
i = 0;
}
}
if(i)
{
new = (rec_ptr)imalloc(sizeof(rec_node));
insq(new,dest->prev);
new->data = (Char *)imalloc(i + 1);
if(i > 0)
memcpy(new->data,buf,i);
new->data[i] = 0;
new->length = i;
new->recflags = 1;
count++;
}
}*/
/* source is not binary, dest is binary */
else
{
for(i = 0,old = source->next,end = (rec_ptr)&source->next;old != end;old = old->next)
{
for(j = 0;j <= old->length;j++)
{
if(j == old->length)
buf[i++] = '\n';
else
buf[i++] = old->data[j];
if(i >= BINARY_RECSIZE)
{
buf[i] = '\0';
new = (rec_ptr)imalloc(sizeof(rec_node));
insq(new,dest->prev);
new->data = (Char *)imalloc(BINARY_RECSIZE + 1);
memcpy(new->data,buf,BINARY_RECSIZE);
new->data[BINARY_RECSIZE] = 0;
new->length = BINARY_RECSIZE;
new->recflags = 1; /* it is a freeable buffer */
count++;
i = 0;
}
}
}
if(i)
{
new = (rec_ptr)imalloc(sizeof(rec_node));
insq(new,dest->prev);
new->data = (Char *)imalloc(i + 1);
if(i > 0)
memcpy(new->data,buf,i);
new->data[i] = 0;
new->length = i;
new->recflags = 1; /* it is a freeable buffer */
count++;
}
}
return(count);
}