File: entrydef.C

package info (click to toggle)
gbib 0.1.2-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 812 kB
  • ctags: 657
  • sloc: cpp: 3,535; sh: 2,816; ansic: 2,209; makefile: 267; sed: 93
file content (327 lines) | stat: -rw-r--r-- 6,161 bytes parent folder | download | duplicates (2)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* Gnome BibTeX containers.C
 *    Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
 *    Felipe Bergo <bergo@seul.org>
 * 
 *    This program is free software; you can redistribute it and'or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2, or (at your option)
 *    any later version.
 */

#include <iostream>
#include <map>
#include <vector>
#if (__GNUC__ < 3)
#include <string>
#endif /* __GNUC__ < 3 */
#include <algorithm>
#include <cstring>
#include <cstdio>
#include "gbib.h"

#include <ctype.h>

using namespace std;

int nstyles = 0;
char *bibstyle;

map<string, ApBibEntryDef, less<string> > entrydefs;

// Just to store the entrydef names, for some reason unaccesible from map
vector_string entrydefTab;

//
vector_string fieldNameTab;

//
vector_string extraFieldNameTab;

vector_string commandTab;


static void Stolower(char *s) {
   for (int i=0; s[i] > ' '; i++)
     s[i] = tolower(s[i]);
}


static void Stoupper(char *s) {
   for (int i=0; s[i] > ' '; i++)
     s[i] = toupper(s[i]);
}


static
int get_fieldname_idx(char * n) 
{
    for (unsigned int i=0; i<fieldNameTab.size(); i++) {
	if (strcmp(n, fieldNameTab[i].c_str())==0) {
	    return i;
	}
    }
    
    return -1;
}


static
int insert_fieldname(char * n) 
{
    for (unsigned int i=0; i<fieldNameTab.size(); i++) {
	if (strcmp(n, fieldNameTab[i].c_str())==0) {
	    return i;
	}
    } 
    
    fieldNameTab.push_back(n);
	
    return fieldNameTab.size() - 1;
}


void new_entrydef(char* name, char* required, char* optional)
{
    int i, rl, ol;
    BibEntryDef *def = new BibEntryDef;
    char *aux;
    char s[250];
    int iaux[100];
    
    for(i=0;i<strlen(name);i++)
      name[i]=tolower(name[i]);

    //    cerr << "newdef: " << name << endl;

    def->name = name;
    rl = strlen(required);
    ol = strlen(optional);
    
    // Number of required entries
    def->nreq = 0;
    strcpy(s, required);    
    //    cerr << "required " << required << "\n";
    if (rl > 0) {
	aux = &s[0];
	for (i=0; i < rl; i++) {	    
	    if (required[i] == ' ' || required[i] == '\0') {
		s[i] = '\0';
		iaux[def->nreq++] = insert_fieldname(aux);
		aux = &s[i+1];
	    }
	}
	iaux[def->nreq++] = insert_fieldname(aux);
	def->required = new int[def->nreq];
	for (i=0; i < def->nreq; i++) 	    
	  def->required[i] = iaux[i];
    }

    // Number of optional entries    
    def->nopt = 0;    
    strcpy(s, optional);
    //    cerr << "optional " << optional << "\n";
    if (ol > 0) {
	aux = &s[0];
	for (i=0; i < ol; i++) {
	    if (optional[i] == ' ' || optional[i] == '\0') {
		s[i] = '\0';
		iaux[def->nopt++] = insert_fieldname(aux);
		aux = &s[i+1];
	    }
	}	
	iaux[def->nopt++] = insert_fieldname(aux);
	def->optional = new int[def->nopt];
	for (i=0; i < def->nopt; i++) 	    
	  def->optional[i] = iaux[i];
    }

    entrydefs[name] = def;
    entrydefTab.push_back(def->name);
    //    cerr << "newdef: [" << name << "]\n";
}


void new_bibstyles(char* s) 
{
    int l;
    
    l = strlen(s);
    bibstyle = new char[l+1];

    if (l > 0) {
	int i;
	
	nstyles = 1;
	for (i=0; i < l; i++) {
	    if (s[i] == ' ') {
		bibstyle[i] = '\0';
		nstyles++;
	    } else 
	      bibstyle[i] = (unsigned char)s[i];
	}
	bibstyle[i] = '\0';
    } else  
      nstyles = 0;
}


BibEntryDef *getBibEntryDef(char *n)
{
    int i;
    char s[255];
    
    // Non-case sensitive
    for (i=0; n[i] > ' '; i++)
	s[i] = tolower(n[i]);
    s[i] = '\0';
    
    if (entrydefs.find(s) != entrydefs.end()) {
	return entrydefs[s];
    }

    //    cerr << "didn't find1 [" << s << "]" << endl;
    //    cerr << "didn't find2 [" << n << "]" << endl;
    return 0;
}



char *BibEntryDef::getRequired(int i)
{
    if (i > nreq || i < 0) 
      return 0;
    
    return( const_cast<char*>(fieldNameTab[required[i]].c_str()) );
}


char *BibEntryDef::getOptional(int i)
{ 
    if (i > nopt || i < 0) 
      return 0;
    
    return( const_cast<char*>(fieldNameTab[optional[i]].c_str()) );
}


int BibEntryDef::getFieldIdx(char *fx)
{
    int k = get_fieldname_idx(fx);

    if (k >= 0) {
	for (int i=0; i < nreq; i++)
	  if (k == required[i])
	    return i;
		
	for (int i=0; i < nopt; i++)
	  if (k == optional[i])
	    return i+nreq;
    }

    return -1;
}


char *get_entrytype_name(int i)
{
  unsigned int ui;
  ui=(unsigned int)i;
  if (0 <= ui && ui < entrydefTab.size()) {
    return ( const_cast<char*>(entrydefTab[ui].c_str()));
  }
  
  return 0;
}   


char *get_bibtex_style(int i)
{
        if (0 <= i && i < nstyles) {
	    int k=0, j=0;
	    
	    for (k=0; j<i; k++) { 
	      if (bibstyle[k]=='\0') {
		  j++;
	      }
	    }
	    return &bibstyle[k];
	}
    
        return 0;
}


int set_globalfield_name(char *n)
{
    string name = n;
    
    // return the corresponding index if the name already exists
    for (unsigned int i=0; i<fieldNameTab.size(); i++) {
	if (name == fieldNameTab[i]) {
	    return i;
	}
    }
    
    for (unsigned int i=0; i<extraFieldNameTab.size(); i++) {
	if (name == extraFieldNameTab[i]) {
	    return i + fieldNameTab.size();
	}
    }

    // it doesn't exist
    extraFieldNameTab.push_back(name);
   
   return fieldNameTab.size() + extraFieldNameTab.size() - 1; 
}
 

char *get_globalfield_name(int idx)
{
  unsigned int ui;
  ui=(unsigned int)idx;
    if (ui < fieldNameTab.size())
      return (const_cast<char*>(fieldNameTab[ui].c_str()));
    
    ui -= fieldNameTab.size();
    
    if (ui < extraFieldNameTab.size())
      return (const_cast<char*>(extraFieldNameTab[ui].c_str()));
    
    return 0;
}


void new_commands(char* s) 
{
   int i, l;
   char *sx;
   
   l = strlen(s);
   
   if (l > 0) {
      sx = &s[0];
      for (i=0; i < l; i++) {
	 if (s[i] == ' ') {
	    s[i] = '\0';
	    Stoupper(sx);
	    commandTab.push_back(sx);
	    sx = &s[i+1];
	 }
      }
      s[i] = '\0';
      Stoupper(sx);
      commandTab.push_back(sx);
   }
}

bool isCommand(char *n) {
   
   vector_string::iterator result;

   Stoupper(n);

   result = find(commandTab.begin(), commandTab.end(), n);

   return (result != commandTab.end());
}