1 /*
2  * reftypes.c
3  *
4  * Copyright (c) Chris Putnam 2003-2021
5  *
6  * Source code released under the GPL version 2
7  *
8  */
9 #include <stdio.h>
10 #include <string.h>
11 #include "is_ws.h"
12 #include "fields.h"
13 #include "reftypes.h"
14 
15 int
get_reftype(const char * p,long refnum,char * progname,variants * all,int nall,char * tag,int * is_default,int chattiness)16 get_reftype( const char *p, long refnum, char *progname, variants *all, int nall, char *tag, int *is_default, int chattiness )
17 {
18 	int i;
19 
20 	p = skip_ws( p );
21 
22 	*is_default = 0;
23 
24 	for ( i=0; i<nall; ++i ) {
25 		if ( !strncasecmp( all[i].type, p, strlen(all[i].type) ) )
26 			return i;
27 	}
28 
29 	*is_default = 1;
30 
31 	if ( chattiness==REFTYPE_CHATTY ) {
32 		if ( progname ) fprintf( stderr, "%s: ", progname );
33 		fprintf( stderr, "Did not recognize type '%s' of refnum %ld (%s).\n"
34 			"\tDefaulting to %s.\n", p, refnum, tag, all[0].type );
35 	}
36 
37 	return 0;
38 }
39 
40 int
process_findoldtag(const char * oldtag,int reftype,variants all[],int nall)41 process_findoldtag( const char *oldtag, int reftype, variants all[], int nall )
42 {
43         variants *v;
44         int i;
45 
46         v = &(all[reftype]);
47         for ( i=0; i<v->ntags; ++i ) {
48                 if ( !strcasecmp( (v->tags[i]).oldstr, oldtag ) )
49                         return i;
50 	}
51         return -1;
52 }
53 
54 /* translate_oldtag()
55  */
56 int
translate_oldtag(const char * oldtag,int reftype,variants all[],int nall,int * processingtype,int * level,char ** newtag)57 translate_oldtag( const char *oldtag, int reftype, variants all[], int nall,
58 		int *processingtype, int *level, char **newtag )
59 {
60 	int n;
61 
62 	n = process_findoldtag( oldtag, reftype, all, nall );
63 	if ( n!=-1 ) {
64 		*processingtype = ((all[reftype]).tags[n]).processingtype;
65 		*level          = ((all[reftype]).tags[n]).level;
66 		*newtag         = ((all[reftype]).tags[n]).newstr;
67 		return 1;
68 	}
69 
70 	return 0;
71 }
72