1 /*
2  * fields.h
3  *
4  * Copyright (c) Chris Putnam 2003-2021
5  *
6  * Source code released under the GPL version 2
7  *
8  */
9 #ifndef FIELDS_H
10 #define FIELDS_H
11 
12 #define FIELDS_OK            (0)
13 #define FIELDS_ERR_MEMERR   (-1)
14 #define FIELDS_ERR_NOTFOUND (-2)
15 
16 #define FIELDS_NOTFOUND  (-1)
17 
18 #define LEVEL_ORIG   (-2)
19 #define LEVEL_ANY    (-1)
20 #define LEVEL_MAIN    (0)
21 #define LEVEL_HOST    (1)
22 #define LEVEL_SERIES  (2)
23 
24 #include <stdarg.h>
25 #include "str.h"
26 #include "vplist.h"
27 
28 typedef struct fields_entry {
29 	str tag;
30 	str value;
31 	str language;
32 	int level;
33 	int used;
34 } fields_entry;
35 
36 typedef struct fields {
37 	fields_entry **entries;
38 	int n, max;
39 } fields;
40 
41 void    fields_init( fields *f );
42 fields *fields_new( void );
43 fields *fields_dupl( fields *f );
44 void    fields_delete( fields *f );
45 void    fields_free( fields *f );
46 
47 int     fields_remove( fields *f, int n );
48 
49 #define FIELDS_CAN_DUP (0)
50 #define FIELDS_NO_DUPS (1)
51 
52 #define fields_add( a, b, c, d )                   _fields_add( a, b, c, NULL, d, FIELDS_NO_DUPS )
53 #define fields_add_can_dup( a, b, c, d )           _fields_add( a, b, c, NULL, d, FIELDS_CAN_DUP )
54 #define fields_add_suffix( a, b, c, d, e )         _fields_add_suffix( a, b, c, d, NULL, e, FIELDS_NO_DUPS )
55 #define fields_add_suffix_can_dup( a, b, c, d, e ) _fields_add_suffix( a, b, c, d, NULL, e, FIELDS_CAN_DUP )
56 
57 #define fields_add_lang( a, b, c, d, e )           _fields_add( a, b, c, d, e, FIELDS_NO_DUPS )
58 
59 int  _fields_add       ( fields *f, const char *tag,                     const char *value, const char *lang, int level, int mode );
60 int  _fields_add_suffix( fields *f, const char *tag, const char *suffix, const char *value, const char *lang, int level, int mode );
61 
62 int  fields_maxlevel( fields *f );
63 void fields_clear_used( fields *f );
64 void fields_set_used( fields *f, int n );
65 int  fields_replace_or_add( fields *f, const char *tag, const char *value, int level );
66 
67 int fields_num( fields *f );
68 int fields_used( fields *f, int n );
69 int fields_no_tag( fields *f, int n );
70 int fields_no_value( fields *f, int n );
71 int fields_has_value( fields *f, int n );
72 
73 int fields_match_level( fields *f, int n, int level );
74 int fields_match_tag( fields *f, int n, const char *tag );
75 int fields_match_casetag( fields *f, int n, const char *tag );
76 int fields_match_tag_level( fields *f, int n, const char *tag, int level );
77 int fields_match_casetag_level( fields *f, int n, const char *tag, int level );
78 
79 void fields_report( fields *f, FILE *fp );
80 
81 #define FIELDS_STRP_FLAG     (2)
82 #define FIELDS_POSP_FLAG     (4)
83 #define FIELDS_NOLENOK_FLAG  (8)
84 #define FIELDS_SETUSE_FLAG  (16)
85 
86 #define FIELDS_CHRP        (FIELDS_SETUSE_FLAG                                         )
87 #define FIELDS_STRP        (FIELDS_SETUSE_FLAG | FIELDS_STRP_FLAG                      )
88 #define FIELDS_POSP        (FIELDS_SETUSE_FLAG | FIELDS_POSP_FLAG                      )
89 #define FIELDS_CHRP_NOLEN  (FIELDS_SETUSE_FLAG |                    FIELDS_NOLENOK_FLAG)
90 #define FIELDS_STRP_NOLEN  (FIELDS_SETUSE_FLAG | FIELDS_STRP_FLAG | FIELDS_NOLENOK_FLAG)
91 #define FIELDS_POSP_NOLEN  (FIELDS_SETUSE_FLAG | FIELDS_POSP_FLAG | FIELDS_NOLENOK_FLAG)
92 #define FIELDS_CHRP_NOUSE  (                            0                              )
93 #define FIELDS_STRP_NOUSE  (                     FIELDS_STRP_FLAG                      )
94 
95 void *fields_tag( fields *f, int n, int mode );
96 void *fields_value( fields *f, int n, int mode );
97 int   fields_level( fields *f, int n );
98 
99 int   fields_find( fields *f, const char *searchtag, int level );
100 
101 void *fields_findv( fields *f, int level, int mode, const char *tag );
102 void *fields_findv_firstof( fields *f, int level, int mode, ... );
103 
104 int   fields_findv_each( fields *f, int level, int mode, vplist *a, const char *tag );
105 int   fields_findv_eachof( fields *f, int level, int mode, vplist *a, ... );
106 
107 #endif
108