File: docDocpropField.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (271 lines) | stat: -rw-r--r-- 6,849 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/************************************************************************/
/*									*/
/*  Buffer administration routines.					*/
/*									*/
/************************************************************************/

#   include	"tedConfig.h"

#   include	<stdlib.h>
#   include	<string.h>
#   include	<stdio.h>

#   include	<appDebugon.h>

#   include	<appUnit.h>
#   include	"docBuf.h"
#   include	"docEvalField.h"

/************************************************************************/
/*									*/
/*  Evaluate fields to document properties.				*/
/*									*/
/************************************************************************/

#   define	FIC_COUNT	15

/************************************************************************/
/*									*/
/*  Return the value of one of the document dates.			*/
/*									*/
/************************************************************************/

static int docIsMergeformat(		const unsigned char *		bytes,
					FieldInstructionsComponent *	fic,
					int				n,
					int				comp )
    {
    if  ( comp < n-1					&&
	  fic[comp].ficSize == 2				&&
	  ! memcmp( bytes+ fic[comp].ficOffset, "\\*", 2 )	&&
	  fic[comp+ 1].ficSize == 11				)
	{
	const char *		mergeformat= "mergeformat";
	int			i;

	for ( i= 0; i < 11; i++ )
	    {
	    if  ( tolower( bytes[fic[comp+1].ficOffset+ i] ) != mergeformat[i] )
		{ break;	}
	    }

	if  ( i == 11 )
	    { return 1;	}
	}

    return 0;
    }

int docCalculateDocDateFieldString(	int *			pCalculated,
					int *			pNewSize,
					unsigned char *		target,
					int			targetSize,
					BufferDocument *	bd,
					const BufferItem *	paraBi,
					int			part,
					int			partCount,
					const DocumentField *	df )
    {
    FieldInstructionsComponent	fic[FIC_COUNT];
    int				n;
    int				comp;
    unsigned char *		bytes= df->dfInstructions.mbBytes;

    const DocumentProperties *	dp= &(bd->bdProperties);
    const struct tm *		tm;

    int				mergeFormat= 0;
    int				pictureComp= -1;

    n= docSplitFieldInstructions( &(df->dfInstructions), fic, FIC_COUNT );
    if  ( n < 2 )
	{ LDEB(n); return -1;	}

    for ( comp= 1; comp < n; comp++ )
	{
	if  ( docIsMergeformat( bytes, fic, n, comp ) )
	    { comp++; continue;	}

	if  ( comp < n-1					&&
	      fic[comp].ficSize == 2				&&
	      ! memcmp( bytes+ fic[comp].ficOffset, "\\@", 2 )	)
	    { pictureComp= comp+ 1; comp++; continue; }

	LSDEB(comp,(char *)bytes+ fic[comp].ficOffset);
	}

    if  ( targetSize < 40 )
	{ LDEB(targetSize); return -1;	}

    switch( df->dfKind )
	{
	case DOCfkCREATEDATE:	tm= &(dp->dpCreatim);	break;
	case DOCfkSAVEDATE:	tm= &(dp->dpRevtim);	break;
	case DOCfkPRINTDATE:	tm= &(dp->dpPrintim);	break;

	default:
	    LDEB(df->dfKind); *pCalculated= 0; return 0;
	}

    if  ( tm->tm_mday == 0 )
	{ /*LDEB(tm->tm_mday);*/ *pCalculated= 0; return 0;	}

    if  ( pictureComp >= 0 )
	{
	char		scratch[50+1];

	if  ( fic[pictureComp].ficSize > sizeof(scratch)- 1 )
	    {
	    LLDEB(fic[pictureComp].ficSize,sizeof(scratch));
	    *pCalculated= 0; return 0;
	    }
	memcpy( scratch, bytes+ fic[pictureComp].ficOffset,
						    fic[pictureComp].ficSize );
	scratch[fic[pictureComp].ficSize]= '\0';

	appWordFormatDate( (char *)target, targetSize, tm, scratch );
	*pNewSize= strlen( (char *)target );
	*pCalculated= 1; return 0;
	}

    if  ( mergeFormat )
	{
	/*  GCC: Sut Up! */
	const char *	frm= "%c";
	if  ( strftime( (char *)target, targetSize, frm, tm ) < 1 )
	    { *pCalculated= 0; return 0;	}

	*pNewSize= strlen( (char *)target );
	*pCalculated= 1; return 0;
	}


    *pCalculated= 0; return 0;
    }

/************************************************************************/
/*									*/
/*  Return the value of one of the document properties.			*/
/*									*/
/************************************************************************/

int docCalculateDocStringFieldString(	int *			pCalculated,
					int *			pNewSize,
					unsigned char *		target,
					int			targetSize,
					BufferDocument *	bd,
					const BufferItem *	paraBi,
					int			part,
					int			partCount,
					const DocumentField *	df )
    {
    const DocumentProperties *	dp= &(bd->bdProperties);
    unsigned char *		val;
    int				len;
    
    switch( df->dfKind )
	{
	case DOCfkAUTHOR:	val= dp->dpAuthor;	break;
	case DOCfkCOMMENTS:	val= dp->dpComment;	break;
	case DOCfkKEYWORDS:	val= dp->dpKeywords;	break;
	case DOCfkSUBJECT:	val= dp->dpSubject;	break;
	case DOCfkTITLE:	val= dp->dpTitle;	break;
	case DOCfkFILENAME:	val= dp->dpFilename;	break;

	default:
	    LDEB(df->dfKind); *pCalculated= 0; return 0;
	}

    if  ( ! val )
	{ val= (unsigned char *)"";	}
    len= strlen( (char *)val );

    if  ( len > targetSize )
	{ LLDEB(len,targetSize); *pCalculated= 0; return 0;	}

    strcpy( (char *)target, (char *)val );
    *pNewSize= len;

    *pCalculated= 1; return 0;
    }

/************************************************************************/
/*									*/
/*  Return the file name of the document.				*/
/*									*/
/*  The file name on the document proprties is assumed to be a full	*/
/*  absolute name. (From "/")						*/
/*									*/
/************************************************************************/

int docCalculateFilenameFieldString(	int *			pCalculated,
					int *			pNewSize,
					unsigned char *		target,
					int			targetSize,
					BufferDocument *	bd,
					const BufferItem *	paraBi,
					int			part,
					int			partCount,
					const DocumentField *	df )
    {
    FieldInstructionsComponent	fic[FIC_COUNT];
    int				n;
    unsigned char *		bytes= df->dfInstructions.mbBytes;

    const DocumentProperties *	dp= &(bd->bdProperties);

    int				fullPath= 0;
    unsigned char *		file= dp->dpFilename;
    unsigned char *		relative= (unsigned char *)0;

    int				size;
    int				comp;

    n= docSplitFieldInstructions( &(df->dfInstructions), fic, FIC_COUNT );
    if  ( n < 1 )
	{ LDEB(n); return -1;	}

    for ( comp= 1; comp < n; comp++ )
	{
	if  ( docIsMergeformat( bytes, fic, n, comp ) )
	    { comp++; continue;	}

	if  ( comp < n-1					&&
	      fic[comp].ficSize == 2				&&
	      ! memcmp( bytes+ fic[comp].ficOffset, "\\p", 2 )	)
	    { fullPath= 1; continue;	}

	LSDEB(comp,(char *)bytes+ fic[comp].ficOffset);
	}

    if  ( ! file )
	{ file= (unsigned char *)"";	}

    if  ( ! fullPath )
	{ relative= (unsigned char *)strrchr( (char *)file, '/' );	}

    if  ( fullPath || ! relative )
	{
	size= strlen( (char *)file );
	if  ( size > targetSize )
	    { LLDEB(size,targetSize); *pCalculated= 0; return 0; }

	memcpy( target, file, size );
	}
    else{
	relative++;

	size= strlen( (char *)relative );
	if  ( size > targetSize )
	    { LLDEB(size,targetSize); *pCalculated= 0; return 0; }

	memcpy( target, relative, size );
	}

    target[size]= '\0';
    *pNewSize= size;
    *pCalculated= 1;

    return 0;
    }