1 /************************************************************************/
2 /*									*/
3 /*  Buffer administration routines.					*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"docBaseConfig.h"
8 
9 #   include	<stdlib.h>
10 #   include	<string.h>
11 
12 #   include	<appDebugon.h>
13 #   include	<utilTree.h>
14 
15 #   include	"docStyleSheet.h"
16 #   include	"docListDepth.h"
17 
18 /************************************************************************/
19 /*									*/
20 /*  Copy a style sheet. This is done in reverse order to avoid		*/
21 /*  reallocations of the array for every style.				*/
22 /*									*/
23 /************************************************************************/
24 
docCopyStyleSheet(DocumentStyleSheet * to,const DocumentStyleSheet * from,const DocumentAttributeMap * dam)25 int docCopyStyleSheet(	DocumentStyleSheet *		to,
26 			const DocumentStyleSheet *	from,
27 			const DocumentAttributeMap *	dam )
28     {
29     int			i;
30 
31     docCleanStyleSheet( to );
32     docInitStyleSheet( to );
33 
34     for ( i= from->dssStyleCount- 1; i >= 0; i-- )
35 	{
36 	DocumentStyle *	dsTo;
37 
38 	if  ( ! from->dssStyles[i] )
39 	    { continue;	}
40 
41 	dsTo= docInsertStyle( to, i, from->dssStyles[i], dam );
42 	if  ( ! dsTo )
43 	    { LPDEB(i,dsTo); return -1;	}
44 	}
45 
46     return 0;
47     }
48 
49 /************************************************************************/
50 /*									*/
51 /*  Remove a style from the style names administration.			*/
52 /*									*/
53 /************************************************************************/
54 
docDeleteStyleFromNames(DocumentStyleSheet * dss,const DocumentStyle * ds)55 static int docDeleteStyleFromNames(	DocumentStyleSheet *	dss,
56 					const DocumentStyle *	ds )
57     {
58     char *	styleNames= ds->dsName;
59 
60     while( styleNames && styleNames[0] )
61 	{
62 	char *	comma;
63 
64 	comma= strchr( styleNames, ',' );
65 	if  ( comma )
66 	    {
67 	    *comma= '\0';
68 
69 	    utilTreeDeleteEQ( dss->dssStylesByName, styleNames,
70 					(UTIL_TREE_CALLBACK)0, (void *)0 );
71 
72 	    *comma= ',';
73 	    styleNames= comma+ 1;
74 	    }
75 	else{
76 	    utilTreeDeleteEQ( dss->dssStylesByName, styleNames,
77 					(UTIL_TREE_CALLBACK)0, (void *)0 );
78 	    break;
79 	    }
80 	}
81 
82     return 0;
83     }
84 
85 /************************************************************************/
86 /*									*/
87 /*  Insert a style into the style names administration.			*/
88 /*  Also do some administration based on the name fragments.		*/
89 /*									*/
90 /************************************************************************/
91 
docInsertStyleByName(DocumentStyleSheet * dss,DocumentStyle * ds,const char * name,int nameLen)92 static int docInsertStyleByName(	DocumentStyleSheet *	dss,
93 					DocumentStyle *		ds,
94 					const char *		name,
95 					int			nameLen )
96     {
97     if  ( utilTreeStoreValue( dss->dssStylesByName,
98 				(void **)0, (const char **)0, name, ds ) )
99 	{ SDEB(name); return -1;	}
100 
101     if  ( nameLen == 9					&&
102 	  ! strncmp( name, "heading ", 8 )		&&
103 	  name[8] >= '1'				&&
104 	  name[8] <= '9'				)
105 	{ ds->dsOutlineLevel=  name[8]- '1';	}
106 
107     return 0;
108     }
109 
docInsertStyleIntoNames(DocumentStyleSheet * dss,DocumentStyle * ds)110 static int docInsertStyleIntoNames(	DocumentStyleSheet *	dss,
111 					DocumentStyle *		ds )
112     {
113     char *	styleNames= ds->dsName;
114     int		rval= 0;
115 
116     while( styleNames && styleNames[0] )
117 	{
118 	char *	comma;
119 
120 	comma= strchr( styleNames, ',' );
121 	if  ( comma )
122 	    {
123 	    *comma= '\0';
124 
125 	    if  ( docInsertStyleByName( dss, ds,
126 					    styleNames, comma- styleNames ) )
127 		{ SDEB(styleNames); rval= 1;	}
128 
129 	    *comma= ',';
130 	    styleNames= comma+ 1;
131 	    }
132 	else{
133 	    if  ( docInsertStyleByName( dss, ds,
134 					    styleNames, strlen( styleNames ) ) )
135 		{ SDEB(styleNames); rval= 1;	}
136 
137 	    break;
138 	    }
139 	}
140 
141     return rval;
142     }
143 
docInsertStyle(DocumentStyleSheet * dss,int n,const DocumentStyle * dsFrom,const DocumentAttributeMap * dam)144 DocumentStyle * docInsertStyle(	DocumentStyleSheet *		dss,
145 				int				n,
146 				const DocumentStyle *		dsFrom,
147 				const DocumentAttributeMap *	dam )
148     {
149     DocumentStyle *	ds;
150 
151     if  ( n < 0 )
152 	{ n= dss->dssStyleCount;	}
153 
154     if  ( ! dss->dssStylesByName )
155 	{
156 	const int ownKeys= 1;
157 
158 	dss->dssStylesByName= utilTreeMakeTree( ownKeys );
159 	if  ( ! dss->dssStylesByName )
160 	    { XDEB(dss->dssStylesByName); return (DocumentStyle *)0;	}
161 	}
162 
163     if  ( n >= dss->dssStyleCount )
164 	{
165 	DocumentStyle **	fresh;
166 
167 	fresh= (DocumentStyle **)realloc( dss->dssStyles,
168 				    ( n + 1 ) * sizeof(DocumentStyle *) );
169 
170 	if  ( ! fresh )
171 	    { LLDEB(dss->dssStyleCount,fresh); return (DocumentStyle *)0; }
172 	dss->dssStyles= fresh;
173 
174 	while( dss->dssStyleCount <= n )
175 	    { fresh[dss->dssStyleCount++]= (DocumentStyle *)0;	}
176 	}
177 
178     if  ( dss->dssStyles[n] )
179 	{ docDeleteStyleFromNames( dss, dss->dssStyles[n] );	}
180     else{
181 	dss->dssStyles[n]= (DocumentStyle *)malloc( sizeof(DocumentStyle) );
182 	if  ( ! dss->dssStyles[n] )
183 	    { XDEB(dss->dssStyles[n]); return (DocumentStyle *)0;	}
184 
185 	docInitDocumentStyle( dss->dssStyles[n] );
186 	}
187 
188     ds= dss->dssStyles[n];
189 
190     if  ( docCopyStyle( ds, dsFrom, dam ) )
191 	{ LDEB(n); return (DocumentStyle *)0; }
192 
193     ds->dsStyleNumber= n;
194     ds->dsOutlineLevel= PPoutlineBODYTEXT;
195 
196     if  ( PROPmaskISSET( &(ds->dsParaMask), PPpropOUTLINELEVEL ) )
197 	{
198 	ds->dsOutlineLevel= ds->dsParaProps.ppOutlineLevel;
199 	}
200 
201     if  ( docInsertStyleIntoNames( dss, ds ) )
202 	{ LDEB(n);	}
203 
204     return ds;
205     }
206 
docCleanStyleSheet(DocumentStyleSheet * dss)207 void docCleanStyleSheet(	DocumentStyleSheet *	dss )
208     {
209     int			i;
210 
211     for ( i= 0; i < dss->dssStyleCount; i++ )
212 	{
213 	if  ( dss->dssStyles[i] )
214 	    {
215 	    docCleanDocumentStyle( dss->dssStyles[i] );
216 	    free( dss->dssStyles[i] );
217 	    }
218 	}
219 
220     if  ( dss->dssStyles )
221 	{ free( dss->dssStyles );	}
222 
223     if  ( dss->dssStylesByName )
224 	{
225 	utilTreeFreeTree( dss->dssStylesByName,
226 				    (UTIL_TREE_CALLBACK)0, (void *)0 );
227 	}
228 
229     return;
230     }
231 
docInitStyleSheet(DocumentStyleSheet * dss)232 void docInitStyleSheet(	DocumentStyleSheet *	dss )
233     {
234     dss->dssStyles= (DocumentStyle **)0;
235     dss->dssStyleCount= 0;
236     dss->dssStylesByName= (void *)0;
237     }
238 
docGetStyleByName(const DocumentStyleSheet * dss,const char * styleName)239 DocumentStyle * docGetStyleByName(
240 				const DocumentStyleSheet *	dss,
241 				const char *			styleName )
242     {
243 
244     if  ( dss->dssStylesByName )
245 	{
246 	return (DocumentStyle *)utilTreeGetEQ( dss->dssStylesByName,
247 						(const char **)0, styleName );
248 	}
249 
250     return (DocumentStyle *)0;
251     }
252 
253