1 /* fields.c - handle MS Word fields
2 
3 Copyright (C) 2008 The Free Software Foundation
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19 This file is available from http://sourceforge.net/projects/latex2rtf/
20 
21 Authors:
22     2008 Scott Prahl
23 */
24 
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include "main.h"
29 #include "fields.h"
30 
31 #define FIELD_MAX_DEPTH 20
32 
33 int g_equation_field_EQ_in_effect = 0;
34 
35 int g_fields_use_EQ       = 1;
36 int g_fields_use_REF      = 1;
37 int g_fields_use_SYMBOL   = 1;
38 int g_fields_use_PAGE     = 1;
39 int g_fields_use_PAGE_REF = 1;
40 int g_fields_use_COMMENT  = 1;
41 
42 int g_fields_allowed      = 1;
43 
44 char g_field_separator  = ',';
45 
46 int  g_field_depth = -1;
47 int  g_field[FIELD_MAX_DEPTH];
48 
49 /* OK there are a bunch of possible states
50 
51 g_fields_allowed   :   should fields be used at all
52 g_fields_use_EQ    :   is the EQ type of field allowed
53 g_fields_use_REF   :   is the REF type of field allowed
54 g_fields_use_SYMBOL:   is the SYMBOL type of field allowed
55 
56 g_field_depth      :   0 if not processing field
57 g_field_type       :
58 
59 
60 EQ fields should never be nested
61 */
62 
EQ_field_active(void)63 int EQ_field_active(void)
64 {
65     int i;
66     for (i=0; i<=g_field_depth; i++) {
67         if (g_field[i] == FIELD_EQ)
68             return 1;
69     }
70     return 0;
71 }
startField(int type)72 void startField(int type)
73 {
74     if (!g_fields_allowed) return;
75 
76     switch (type) {
77         case FIELD_EQ:
78             diagnostics(4,"starting EQ field");
79             diagnostics(6, "EQ fields allowed = %d",g_fields_use_EQ);
80             if (!g_fields_use_EQ) return;
81             if (EQ_field_active())
82                 diagnostics(1,"nested EQ fields ???");
83 
84             fprintRTF("{\\field{\\*\\fldinst{ EQ ");
85             break;
86 
87         case FIELD_REF:
88             diagnostics(4,"starting REF field");
89             if (!g_fields_use_REF) return;
90             break;
91 
92         case FIELD_SYMBOL:
93             diagnostics(4,"starting SYMBOL field");
94             if (!g_fields_use_SYMBOL) return;
95             break;
96 
97         case FIELD_PAGE:
98             diagnostics(4,"starting PAGE field");
99             if (!g_fields_use_PAGE) return;
100             break;
101 
102         case FIELD_PAGE_REF:
103             diagnostics(4,"starting PAGE_REF field");
104             if (!g_fields_use_PAGE_REF) return;
105             break;
106 
107         case FIELD_COMMENT:
108             diagnostics(4,"starting COMMENT field");
109             if (!g_fields_use_COMMENT) return;
110             fprintRTF("{\\field{\\*\\fldinst{ COMMENTS \" ");
111             break;
112     }
113 
114     g_field_depth++;
115     g_field[g_field_depth] = type;
116     if (g_field_depth >= FIELD_MAX_DEPTH)
117         diagnostics(0, "Nesting of fields is excessive!");
118 
119 }
120 
endCurrentField(void)121 void endCurrentField(void)
122 {
123     diagnostics(4, "end Field");
124     if (!g_fields_allowed) return;
125     if (g_field_depth < 0) {
126         diagnostics(1, "oops, looks like fields are too shallow!");
127         return;
128     }
129 
130     switch (g_field[g_field_depth]) {
131         case FIELD_EQ:
132             if (!g_fields_use_EQ) return;
133             fprintRTF("}}{\\fldrslt }}\n");
134             break;
135 
136         case FIELD_REF:
137             if (!g_fields_use_REF) return;
138             break;
139 
140         case FIELD_SYMBOL:
141             if (!g_fields_use_SYMBOL) return;
142             break;
143 
144         case FIELD_PAGE:
145             if (!g_fields_use_PAGE) return;
146             break;
147 
148         case FIELD_PAGE_REF:
149             if (!g_fields_use_PAGE_REF) return;
150             break;
151 
152         case FIELD_COMMENT:
153             if (!g_fields_use_COMMENT) return;
154             fprintRTF("\" }}{\\fldrslt }}\n");
155             break;
156     }
157 
158     g_field_depth--;
159 }
160 
endAllFields(void)161 void endAllFields(void)
162 {
163     if (!g_fields_allowed) return;
164     while (g_field_depth >= 0)
165         endCurrentField();
166 }
167 
fprintfRTF_field_separator(void)168 void fprintfRTF_field_separator(void)
169 {
170     fprintRTF("%c", g_field_separator);
171 }
172 
set_field_separator(char c)173 void set_field_separator(char c)
174 {
175     g_field_separator = c;
176 }
177 
set_fields_use_REF(int i)178 void set_fields_use_REF(int i)
179 {
180     g_fields_use_REF = i;
181 }
182 
fields_use_REF(void)183 int fields_use_REF(void)
184 {
185     return g_fields_use_REF;
186 }
187 
set_fields_use_EQ(int i)188 void set_fields_use_EQ(int i)
189 {
190     g_fields_use_EQ = i;
191 }
192 
fields_use_EQ(void)193 int fields_use_EQ(void)
194 {
195     return g_fields_use_EQ;
196 }
197 
processing_fields(void)198 int processing_fields(void)
199 {
200     if (g_field_depth >= 0)
201         return 1;
202     else
203         return 0;
204 }
205 
206