1 /************************************************************************/
2 /* */
3 /* Calculate 'Hyperlink' fields. */
4 /* */
5 /************************************************************************/
6
7 # include "docBaseConfig.h"
8
9 # include <appDebugon.h>
10
11 # include "docDocumentField.h"
12 # include "docHyperlinkField.h"
13
docInitHyperlinkField(HyperlinkField * hf)14 void docInitHyperlinkField( HyperlinkField * hf )
15 {
16 utilInitMemoryBuffer( &(hf->hfFile) );
17 utilInitMemoryBuffer( &(hf->hfBookmark) );
18 utilInitMemoryBuffer( &(hf->hfFont) );
19 }
20
docCleanHyperlinkField(HyperlinkField * hf)21 void docCleanHyperlinkField( HyperlinkField * hf )
22 {
23 utilCleanMemoryBuffer( &(hf->hfFile) );
24 utilCleanMemoryBuffer( &(hf->hfBookmark) );
25 utilCleanMemoryBuffer( &(hf->hfFont) );
26 }
27
28 /************************************************************************/
29 /* */
30 /* Make a hyperlink to the same document relative: Remove the absolute */
31 /* document name if it is equal to the name of the document that the */
32 /* link appears in. */
33 /* */
34 /************************************************************************/
35
docMakeHyperlinkRelative(DocumentField * dfTo,const MemoryBuffer * refFileName)36 int docMakeHyperlinkRelative( DocumentField * dfTo,
37 const MemoryBuffer * refFileName )
38 {
39 int rval= 0;
40 HyperlinkField hf;
41
42 docInitHyperlinkField( &hf );
43
44 if ( docGetHyperlinkField( &hf, dfTo ) )
45 { LDEB(1); rval= -1; goto ready; }
46
47 if ( ! utilMemoryBufferIsEmpty( &(hf.hfBookmark) ) ||
48 ! utilEqualMemoryBuffer( &(hf.hfFile), refFileName ) )
49 { goto ready; }
50
51 utilEmptyMemoryBuffer( &(hf.hfFile) );
52
53 if ( docSetHyperlinkField( &(dfTo->dfInstructions), &hf ) )
54 { rval= -1; goto ready; }
55
56 ready:
57
58 docCleanHyperlinkField( &hf );
59
60 return rval;
61 }
62
63 /************************************************************************/
64
docSetHyperlinkField(FieldInstructions * fi,const HyperlinkField * hf)65 int docSetHyperlinkField( FieldInstructions * fi,
66 const HyperlinkField * hf )
67 {
68 int rval= 0;
69
70 if ( docStartFieldInstructions( fi, "HYPERLINK", 9 ) )
71 { LDEB(9); return -1; }
72
73 if ( ! utilMemoryBufferIsEmpty( &(hf->hfFile) ) )
74 {
75 if ( docFieldInstructionsAddComponent( fi, &(hf->hfFile) ) )
76 { LDEB(1); return -1; }
77 }
78
79 if ( docFieldInstructionsAddArgFlagIfSet( fi, 'l', &(hf->hfBookmark) ) )
80 { CDEB('l'); return -1; }
81
82 if ( docFieldInstructionsAddArgFlagIfSet( fi, 'o', &(hf->hfFont) ) )
83 { CDEB('o'); return -1; }
84
85 return rval;
86 }
87
88 /************************************************************************/
89 /* */
90 /* Extract the destination from a hyperlink field. */
91 /* */
92 /* Return the file name and the bookmark. */
93 /* The strings are NOT '\0' terminated. */
94 /* */
95 /************************************************************************/
96
docGetHyperlinkField(HyperlinkField * hf,const DocumentField * df)97 int docGetHyperlinkField( HyperlinkField * hf,
98 const DocumentField * df )
99 {
100 const FieldInstructions * fi= &(df->dfInstructions);
101 const InstructionsComponent * ic;
102 int comp;
103
104 if ( df->dfKind != DOCfkHYPERLINK )
105 { return 1; }
106
107 ic= fi->fiComponents+ 1;
108 for ( comp= 1; comp < fi->fiComponentCount; ic++, comp++ )
109 {
110 if ( comp == 1 && ! ic->icIsFlag )
111 {
112 if ( utilCopyMemoryBuffer( &(hf->hfFile), &(ic->icBuffer) ) )
113 { CDEB('o'); return -1; }
114
115 continue;
116 }
117
118 if ( docComponentIsArgFlag( fi, comp, 'o' ) )
119 {
120 ic++, comp++;
121
122 if ( utilCopyMemoryBuffer( &(hf->hfFont), &(ic->icBuffer) ) )
123 { CDEB('o'); return -1; }
124
125 continue;
126 }
127
128 if ( docComponentIsArgFlag( fi, comp, 'l' ) )
129 {
130 ic++, comp++;
131
132 if ( utilCopyMemoryBuffer( &(hf->hfBookmark), &(ic->icBuffer) ) )
133 { CDEB('l'); return -1; }
134
135 continue;
136 }
137
138 LLSDEB(comp,ic->icIsFlag,utilMemoryBufferGetString(&(ic->icBuffer)));
139 }
140
141 return 0;
142 }
143
docHyperlinkFieldIsLink(const HyperlinkField * hf)144 int docHyperlinkFieldIsLink( const HyperlinkField * hf )
145 {
146 return ! utilMemoryBufferIsEmpty( &(hf->hfFile) ) ||
147 ! utilMemoryBufferIsEmpty( &(hf->hfBookmark) );
148 }
149
docHyperlinkFieldIsExtern(const HyperlinkField * hf)150 int docHyperlinkFieldIsExtern( const HyperlinkField * hf )
151 {
152 return ! utilMemoryBufferIsEmpty( &(hf->hfFile) );
153 }
154
docEqualHyperlinkFields(const HyperlinkField * hf1,const HyperlinkField * hf2)155 int docEqualHyperlinkFields( const HyperlinkField * hf1,
156 const HyperlinkField * hf2 )
157 {
158 if ( ! utilEqualMemoryBuffer( &(hf1->hfFile), &(hf2->hfFile) ) )
159 { return 0; }
160 if ( ! utilEqualMemoryBuffer( &(hf1->hfBookmark), &(hf2->hfBookmark) ) )
161 { return 0; }
162 if ( ! utilEqualMemoryBuffer( &(hf1->hfFont), &(hf2->hfFont) ) )
163 { return 0; }
164
165 return 1;
166 }
167
docCopyHyperlinkField(HyperlinkField * to,const HyperlinkField * from)168 int docCopyHyperlinkField( HyperlinkField * to,
169 const HyperlinkField * from )
170 {
171 if ( utilCopyMemoryBuffer( &(to->hfFile), &(from->hfFile) ) )
172 { return -1; }
173 if ( utilCopyMemoryBuffer( &(to->hfBookmark), &(from->hfBookmark) ) )
174 { return -1; }
175 if ( utilCopyMemoryBuffer( &(to->hfFont), &(from->hfFont) ) )
176 { return -1; }
177
178 return 0;
179 }
180