| 1 |
/* dctrl-tools - Debian control file inspection tools |
| 2 |
Copyright © 2003, 2004, 2005, 2010, 2011, 2015 Antti-Juhani Kaijanaho |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License along |
| 15 |
with this program; if not, write to the Free Software Foundation, Inc., |
| 16 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef PARAGRAPH_H |
| 20 |
#define PARAGRAPH_H |
| 21 |
|
| 22 |
#define MAX_FIELDS 100 |
| 23 |
|
| 24 |
#include <stddef.h> |
| 25 |
#include "fsaf.h" |
| 26 |
#include "fieldtrie.h" |
| 27 |
|
| 28 |
struct field_datum { |
| 29 |
size_t line; |
| 30 |
size_t start, end; /* offsets to the file; [start,end) is the body */ |
| 31 |
size_t name_start, name_end; /* as start and end, but for the name */ |
| 32 |
struct field_datum *next, *prev; |
| 33 |
}; |
| 34 |
|
| 35 |
struct field_data { |
| 36 |
struct field_datum *first; |
| 37 |
struct field_datum *last; |
| 38 |
}; |
| 39 |
|
| 40 |
struct paragraph_parser { |
| 41 |
bool eof; |
| 42 |
bool invalidate_p; |
| 43 |
FSAF * fp; |
| 44 |
size_t loc; |
| 45 |
size_t line; |
| 46 |
|
| 47 |
bool ignore_broken_paras; |
| 48 |
bool register_unknown_fields; |
| 49 |
}; |
| 50 |
|
| 51 |
struct paragraph { |
| 52 |
size_t line; |
| 53 |
struct paragraph_parser * common; |
| 54 |
size_t start, end; /* offsets to the file; [start,end) is the paragraph */ |
| 55 |
size_t nfields, maxfields; |
| 56 |
struct field_data * fields; |
| 57 |
}; |
| 58 |
|
| 59 |
typedef struct paragraph_parser para_parser_t; |
| 60 |
typedef struct paragraph para_t; |
| 61 |
|
| 62 |
/* Initialize the given para_parser_t, associating with it the given FSAF. */ |
| 63 |
void para_parser_init(para_parser_t *, FSAF *, |
| 64 |
bool invalidate_p, bool ignore_broken_paras, |
| 65 |
bool register_unknown_fields); |
| 66 |
|
| 67 |
/* Initialize the given para_t for the given parser. */ |
| 68 |
void para_init(para_parser_t *, para_t *); |
| 69 |
|
| 70 |
void para_parse_next(para_t *); |
| 71 |
|
| 72 |
static inline |
| 73 |
size_t para_num_fields(para_t *p) { |
| 74 |
return p->nfields; |
| 75 |
} |
| 76 |
|
| 77 |
static inline |
| 78 |
struct fsaf_read_rv get_whole_para(para_t * p) |
| 79 |
{ |
| 80 |
return fsaf_read(p->common->fp, p->start, p->end - p->start); |
| 81 |
} |
| 82 |
|
| 83 |
static inline |
| 84 |
struct field_data find_field(const para_t *p, size_t fld_inx) |
| 85 |
{ |
| 86 |
return fld_inx < p->nfields |
| 87 |
? p->fields[fld_inx] |
| 88 |
: (struct field_data){ NULL, NULL }; |
| 89 |
} |
| 90 |
|
| 91 |
static inline |
| 92 |
struct field_data find_field_wr(const para_t *p, |
| 93 |
size_t fld_inx, |
| 94 |
size_t repl_inx) |
| 95 |
{ |
| 96 |
struct field_data fd = find_field(p, fld_inx); |
| 97 |
if (fd.first == NULL && repl_inx != (size_t)(-1)) { |
| 98 |
fd = find_field(p, repl_inx); |
| 99 |
} |
| 100 |
return fd; |
| 101 |
} |
| 102 |
|
| 103 |
// NOTE: get_field finds the FIRST field of the name! |
| 104 |
static inline |
| 105 |
struct fsaf_read_rv get_field(para_t * p, size_t fld_inx, size_t repl_inx) |
| 106 |
{ |
| 107 |
struct field_data fds = find_field_wr(p, fld_inx, repl_inx); |
| 108 |
if (fds.first == NULL) { |
| 109 |
const struct fsaf_read_rv fail = { |
| 110 |
.b = NULL, |
| 111 |
.len = 0 |
| 112 |
}; |
| 113 |
return fail; |
| 114 |
} |
| 115 |
struct field_datum *fd = fds.first; |
| 116 |
return fsaf_read(p->common->fp, fd->start, fd->end - fd->start); |
| 117 |
} |
| 118 |
|
| 119 |
// NOTE: get_field_as finds the FIRST field of the name! |
| 120 |
static inline |
| 121 |
char * get_field_as(const para_t * p, size_t fld_inx) |
| 122 |
{ |
| 123 |
struct field_data fds = find_field(p, fld_inx); |
| 124 |
if (fds.first == NULL) return strdup(""); |
| 125 |
struct field_datum *fd = fds.first; |
| 126 |
return fsaf_getas(p->common->fp, fd->start, fd->end - fd->start); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
static inline |
| 131 |
bool para_eof(para_parser_t * para) { return para->eof; } |
| 132 |
|
| 133 |
#endif /* PARAGRAPH_H */ |