1 /************************************************************************/
2 /*									*/
3 /*  Copy part of one document to another (or the same) document.	*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"docEditConfig.h"
8 
9 #   include	<stdlib.h>
10 #   include	<stdio.h>
11 
12 #   include	<docPropertiesAdmin.h>
13 
14 #   include	<appDebugon.h>
15 
16 #   include	<docBuf.h>
17 #   include	"docDocumentCopyJob.h"
18 #   include	<docParaRulerAdmin.h>
19 #   include	<docBorderPropertyAdmin.h>
20 #   include	<docItemShadingAdmin.h>
21 #   include	<docCellPropertyAdmin.h>
22 
docInitDocumentCopyJob(DocumentCopyJob * dcj)23 void docInitDocumentCopyJob(	DocumentCopyJob *	dcj )
24     {
25     dcj->dcjEditOperation= (EditOperation *)0;
26     docInitSelectionScope( &(dcj->dcjTargetSelectionScope) );
27     dcj->dcjTargetTree= (DocumentTree *)0;
28     dcj->dcjSourceDocument= (BufferDocument *)0;
29     dcj->dcjSourceTree= (DocumentTree *)0;
30     dcj->dcjCopyFields= 0;
31     dcj->dcjFieldMap= (int *)0;
32 
33     docInitDocumentAttributeMap( &(dcj->dcjAttributeMap) );
34     dcj->dcjForceAttributeTo= -1;
35 
36     utilInitMemoryBuffer( &(dcj->dcjRefFileName) );
37 
38     dcj->dcjFieldStack= (FieldCopyStackLevel *)0;
39 
40     dcj->dcjInExternalTree= 0;
41 
42     dcj->dcjCurrentTextAttributeNumberFrom= -1;
43     dcj->dcjCurrentTextAttributeNumberTo= -1;
44 
45     utilInitIndexSet( &(dcj->dcjNoteFieldsCopied) );
46     dcj->dcjBulletsCopied= 0;
47 
48     dcj->dcjCopyHeadParaProperties= 0;
49     dcj->dcjCopyTailParaProperties= 0;
50 
51     return;
52     }
53 
docCleanDocumentCopyJob(DocumentCopyJob * dcj)54 void docCleanDocumentCopyJob(	DocumentCopyJob *	dcj )
55     {
56     if  ( dcj->dcjFieldMap )
57 	{ free( dcj->dcjFieldMap );	}
58 
59     docCleanDocumentAttributeMap( &(dcj->dcjAttributeMap) );
60 
61     utilCleanMemoryBuffer( &(dcj->dcjRefFileName) );
62 
63     while( dcj->dcjFieldStack )
64 	{
65 	FieldCopyStackLevel *	prev= dcj->dcjFieldStack->fcslPrev;
66 
67 	free( dcj->dcjFieldStack );
68 
69 	dcj->dcjFieldStack= prev;
70 	}
71 
72     utilCleanIndexSet( &(dcj->dcjNoteFieldsCopied) );
73 
74     return;
75     }
76 
docPushFieldOnCopyStack(DocumentCopyJob * dcj,DocumentField * df)77 int docPushFieldOnCopyStack(		DocumentCopyJob *	dcj,
78 					DocumentField *		df )
79     {
80     FieldCopyStackLevel *	fcsl;
81 
82     fcsl= (FieldCopyStackLevel *)malloc( sizeof(FieldCopyStackLevel) );
83     if  ( ! fcsl )
84 	{ XDEB(fcsl); return -1;	}
85 
86     fcsl->fcslPrev= dcj->dcjFieldStack;
87     fcsl->fcslField= df;
88     dcj->dcjFieldStack= fcsl;
89 
90     return 0;
91     }
92 
93 /************************************************************************/
94 /*									*/
95 /*  Allocate a mapping for field numbers.				*/
96 /*									*/
97 /************************************************************************/
98 
docAllocateFieldMap(const BufferDocument * bdFrom)99 static int * docAllocateFieldMap(	const BufferDocument *	bdFrom )
100     {
101     int *	fieldMap;
102     int		i;
103     const int	fieldCount= bdFrom->bdFieldList.dflPagedList.plItemCount;
104 
105     fieldMap= (int *)malloc( fieldCount* sizeof(int) );
106     if  ( ! fieldMap )
107 	{ LXDEB(fieldCount,fieldMap); return (int *)0; }
108     for ( i= 0; i < fieldCount; i++ )
109 	{ fieldMap[i]= -1;	}
110 
111     return fieldMap;
112     }
113 
114 /************************************************************************/
115 
docSetAttributeMap(DocumentAttributeMap * dam,BufferDocument * bdTo,BufferDocument * bdFrom)116 static int docSetAttributeMap(	DocumentAttributeMap *		dam,
117 				BufferDocument *		bdTo,
118 				BufferDocument *		bdFrom )
119     {
120     int		rval= 0;
121 
122     int *	fontMap= (int *)0;
123     int *	colorMap= (int *)0;
124     int *	borderMap= (int *)0;
125     int *	shadingMap= (int *)0;
126     int *	frameMap= (int *)0;
127     int *	cellMap= (int *)0;
128     int *	lsmap= (int *)0;
129     int *	rulerMap= (int *)0;
130 
131     const DocumentPropertyLists *	dplFrom= bdFrom->bdPropertyLists;
132     DocumentPropertyLists *		dplTo= bdTo->bdPropertyLists;
133 
134     if  ( docMergeColorTables( &colorMap, bdTo, bdFrom ) )
135 	{ LDEB(1); rval= -1; goto ready;	}
136 
137     if  ( docMergeBorderPropertiesLists( &borderMap, colorMap,
138 					&(dplTo->dplBorderPropertyList),
139 					&(dplFrom->dplBorderPropertyList) ) )
140 	{ LDEB(1); rval= -1; goto ready;	}
141 
142     if  ( docMergeItemShadingLists( &shadingMap, colorMap,
143 					&(dplTo->dplItemShadingList),
144 					&(dplFrom->dplItemShadingList) ) )
145 	{ LDEB(1); rval= -1; goto ready;	}
146 
147     if  ( docMergeFramePropertyLists( &frameMap,
148 					&(dplTo->dplFramePropertyList),
149 					&(dplFrom->dplFramePropertyList) ) )
150 	{ LDEB(1); rval= -1; goto ready;	}
151 
152     if  ( docMergeTabstopListLists( &rulerMap,
153 					&(dplTo->dplTabStopListList),
154 					&(dplFrom->dplTabStopListList) ) )
155 	{ LDEB(1); rval= -1; goto ready;	}
156 
157     if  ( docMergeCellPropertiesLists( &cellMap, borderMap, shadingMap,
158 					&(dplTo->dplCellPropertyList),
159 					&(dplFrom->dplCellPropertyList) ) )
160 	{ LDEB(1); rval= -1; goto ready;	}
161 
162     if  ( docMergeDocumentLists( &fontMap, &lsmap, bdTo, bdFrom,
163 						    colorMap, rulerMap ) )
164 	{ LDEB(1); rval= -1; goto ready;	}
165 
166     if  ( dam->damFontMap )
167 	{ free( dam->damFontMap );	}
168     dam->damFontMap= fontMap; fontMap= (int *)0; /* steal */
169 
170     if  ( dam->damColorMap )
171 	{ free( dam->damColorMap );	}
172     dam->damColorMap= colorMap; colorMap= (int *)0; /* steal */
173 
174     if  ( dam->damRulerMap )
175 	{ free( dam->damRulerMap );	}
176     dam->damRulerMap= rulerMap; rulerMap= (int *)0; /* steal */
177 
178     if  ( dam->damBorderMap )
179 	{ free( dam->damBorderMap );	}
180     dam->damBorderMap= borderMap; borderMap= (int *)0; /* steal */
181 
182     if  ( dam->damShadingMap )
183 	{ free( dam->damShadingMap );	}
184     dam->damShadingMap= shadingMap; shadingMap= (int *)0; /* steal */
185 
186     if  ( dam->damFrameMap )
187 	{ free( dam->damFrameMap );	}
188     dam->damFrameMap= frameMap; frameMap= (int *)0; /* steal */
189 
190     if  ( dam->damCellMap )
191 	{ free( dam->damCellMap );	}
192     dam->damCellMap= cellMap; cellMap= (int *)0; /* steal */
193 
194     if  ( dam->damListStyleMap )
195 	{ free( dam->damListStyleMap );	}
196     dam->damListStyleMap= lsmap; lsmap= (int *)0;/* steal */
197 
198   ready:
199 
200     if  ( fontMap )
201 	{ free( fontMap );	}
202     if  ( colorMap )
203 	{ free( colorMap );	}
204     if  ( borderMap )
205 	{ free( borderMap );	}
206     if  ( shadingMap )
207 	{ free( shadingMap );	}
208     if  ( frameMap )
209 	{ free( frameMap );	}
210     if  ( cellMap )
211 	{ free( cellMap );	}
212     if  ( lsmap )
213 	{ free( lsmap );	}
214     if  ( rulerMap )
215 	{ free( rulerMap );	}
216 
217     return rval;
218     }
219 
220 /************************************************************************/
221 /*									*/
222 /*  Build a copy job for copying within a document.			*/
223 /*									*/
224 /************************************************************************/
225 
docSet1DocumentCopyJob(DocumentCopyJob * dcj,EditOperation * eo,int copyFields)226 int docSet1DocumentCopyJob(	DocumentCopyJob *	dcj,
227 				EditOperation *		eo,
228 				int			copyFields )
229     {
230     int *	fieldMap;
231 
232     dcj->dcjEditOperation= eo;
233     dcj->dcjTargetSelectionScope= eo->eoSelectionScope;
234     dcj->dcjTargetTree= eo->eoTree;
235     dcj->dcjSourceDocument= eo->eoDocument;
236     dcj->dcjSourceTree= eo->eoTree;
237     dcj->dcjCopyFields= copyFields;
238 
239     fieldMap= docAllocateFieldMap( dcj->dcjSourceDocument );
240     if  ( ! fieldMap )
241 	{ XDEB(fieldMap); return -1;	}
242 
243     if  ( dcj->dcjFieldMap )
244 	{ free( dcj->dcjFieldMap );	}
245 
246     dcj->dcjFieldMap= fieldMap;
247 
248     if  ( eo->eoBottomField					&&
249 	  docPushFieldOnCopyStack( dcj, eo->eoBottomField )	)
250 	{ LDEB(1); return -1;	}
251 
252     return 0;
253     }
254 
255 /************************************************************************/
256 /*									*/
257 /*  Build a copy job for copying within a document.			*/
258 /*									*/
259 /************************************************************************/
260 
docSetTraceDocumentCopyJob(DocumentCopyJob * dcj,EditOperation * eo,BufferDocument * bdFrom)261 int docSetTraceDocumentCopyJob(	DocumentCopyJob *	dcj,
262 				EditOperation *		eo,
263 				BufferDocument *	bdFrom )
264     {
265     int *	fieldMap= (int *)0;
266 
267     dcj->dcjEditOperation= eo;
268     dcj->dcjTargetSelectionScope= eo->eoSelectionScope;
269     dcj->dcjTargetTree= eo->eoTree;
270     dcj->dcjSourceDocument= bdFrom;
271     dcj->dcjSourceTree= &(bdFrom->bdBody);
272     dcj->dcjCopyFields= 1;
273 
274     fieldMap= docAllocateFieldMap( dcj->dcjSourceDocument );
275     if  ( ! fieldMap )
276 	{ XDEB(fieldMap); return -1;	}
277 
278     if  ( dcj->dcjFieldMap )
279 	{ free( dcj->dcjFieldMap );	}
280 
281     dcj->dcjFieldMap= fieldMap;
282 
283     if  ( eo->eoBottomField					&&
284 	  docPushFieldOnCopyStack( dcj, eo->eoBottomField )	)
285 	{ LDEB(1); return -1;	}
286 
287     return 0;
288     }
289 
290 /************************************************************************/
291 /*									*/
292 /*  Build a copy job for copying from one document to another.		*/
293 /*									*/
294 /************************************************************************/
295 
docSet2DocumentCopyJob(DocumentCopyJob * dcj,EditOperation * eo,BufferDocument * bdFrom,DocumentTree * eiFrom,const MemoryBuffer * refFileName,int forceAttributeTo)296 int docSet2DocumentCopyJob(	DocumentCopyJob *	dcj,
297 				EditOperation *		eo,
298 				BufferDocument *	bdFrom,
299 				DocumentTree *		eiFrom,
300 				const MemoryBuffer *	refFileName,
301 				int			forceAttributeTo )
302     {
303     int *		fldmap;
304 
305     BufferDocument *	bdTo= eo->eoDocument;
306 
307     dcj->dcjEditOperation= eo;
308     dcj->dcjTargetSelectionScope= eo->eoSelectionScope;
309     dcj->dcjTargetTree= eo->eoTree;
310     dcj->dcjSourceDocument= bdFrom;
311     dcj->dcjSourceTree= eiFrom;
312     dcj->dcjCopyFields= 1;
313     dcj->dcjForceAttributeTo= forceAttributeTo;
314 
315     fldmap= docAllocateFieldMap( dcj->dcjSourceDocument );
316     if  ( ! fldmap )
317 	{ XDEB(fldmap); return -1;	}
318     if  ( dcj->dcjFieldMap )
319 	{ free( dcj->dcjFieldMap );	}
320     dcj->dcjFieldMap= fldmap;
321 
322     if  ( docSetAttributeMap( &(dcj->dcjAttributeMap), bdTo, bdFrom ) )
323 	{ LDEB(1); return -1;	}
324 
325     if  ( refFileName )
326 	{
327 	if  ( utilCopyMemoryBuffer( &(dcj->dcjRefFileName), refFileName ) )
328 	    { LDEB(1); return -1;	}
329 	}
330     else{
331 	utilEmptyMemoryBuffer( &(dcj->dcjRefFileName) );
332 	}
333 
334     if  ( eo->eoBottomField					&&
335 	  docPushFieldOnCopyStack( dcj, eo->eoBottomField )	)
336 	{ LDEB(1); return -1;	}
337 
338     return 0;
339     }
340