| 1 |
|
| /* ------------------------------------------------------------------------ |
| 2 |
|
| @NAME : BibTeX.xs |
| 3 |
|
| @DESCRIPTION: Glue between the C library (src/) and the Perl module |
| 4 |
|
| Text::BibTeX. |
| 5 |
|
| |
| 6 |
|
| The C library never prints, exits or croaks: every call |
| 7 |
|
| collects its errors in a bt_errors list. Once the C work is |
| 8 |
|
| done and its memory freed, the errors are turned into |
| 9 |
|
| Text::BibTeX::Error objects and handed to |
| 10 |
|
| Text::BibTeX::Error::_dispatch, which reports them through |
| 11 |
|
| Perl's warn/die (so they can be caught, redirected or |
| 12 |
|
| silenced like any other Perl warning or exception). |
| 13 |
|
| |
| 14 |
|
| Input is read through PerlIO, so any Perl filehandle works |
| 15 |
|
| (layers, in-memory and tied handles included). |
| 16 |
|
| @CREATED : Jan/Feb 1997, Greg Ward |
| 17 |
|
| -------------------------------------------------------------------------- */ |
| 18 |
|
| |
| 19 |
|
| #define PERL_NO_GET_CONTEXT |
| 20 |
|
| #include "EXTERN.h" |
| 21 |
|
| #include "perl.h" |
| 22 |
|
| #include "XSUB.h" |
| 23 |
|
| |
| 24 |
|
| #include "btparse.h" |
| 25 |
|
| |
| 26 |
|
| /* Memory allocated by btparse must be released by btparse (bt_free() and |
| 27 |
|
| * the bt_free_* functions): on Windows, free() here is Perl's allocator. */ |
| 28 |
|
| |
| 29 |
|
| #define MY_CXT_KEY "Text::BibTeX::_guts" XS_VERSION |
| 30 |
|
| |
| 31 |
|
| typedef struct |
| 32 |
|
| { |
| 33 |
|
| bt_macros * macros; /* the global macro table */ |
| 34 |
|
| } my_cxt_t; |
| 35 |
|
| |
| 36 |
|
| START_MY_CXT |
| 37 |
|
| |
| 38 |
|
| /* A parser reading from a Perl filehandle */ |
| 39 |
|
| typedef struct |
| 40 |
|
| { |
| 41 |
|
| bt_parser * parser; |
| 42 |
|
| SV * fh; /* RV to the handle (we hold a ref) */ |
| 43 |
|
| bt_macros * private_macros; /* NULL: use the global table */ |
| 44 |
|
| int utf8; |
| 45 |
|
| SV * read_died; /* exception thrown by a tied READ */ |
| 46 |
|
| #ifdef MULTIPLICITY |
| 47 |
|
| PerlInterpreter * perl; |
| 48 |
|
| #endif |
| 49 |
|
| } btxs_parser; |
| 50 |
|
| |
| 51 |
|
| /* Wrappers for the C structures behind Name and NameFormat objects */ |
| 52 |
|
| typedef struct { bt_name * name; } btxs_name; |
| 53 |
|
| typedef struct { bt_name_format * format; } btxs_format; |
| 54 |
|
| |
| 55 |
|
| typedef btxs_parser * Text__BibTeX__Parser; |
| 56 |
|
| typedef btxs_name * Text__BibTeX__Name__Struct; |
| 57 |
|
| typedef btxs_format * Text__BibTeX__NameFormat__Struct; |
| 58 |
|
| |
| 59 |
|
| |
| 60 |
|
| static void |
| 61 |
0 |
| btxs_oom (void) |
| 62 |
|
| { |
| 63 |
|
| dTHX; |
| 64 |
0 |
| croak ("Out of memory in Text::BibTeX"); |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
|
| |
| 68 |
|
| /* ---------------------------------------------------------------------- |
| 69 |
|
| * Strings |
| 70 |
|
| */ |
| 71 |
|
| |
| 72 |
|
| /* Text of an SV as a C string (NULL for undef), and whether it is UTF-8. |
| 73 |
|
| * A UTF-8 flagged string's buffer is already UTF-8, so it never needs |
| 74 |
|
| * upgrading (which would fail on read-only values). */ |
| 75 |
|
| static const char * |
| 76 |
1206 |
| sv_text_len (pTHX_ SV *sv, int *utf8, STRLEN *len) |
| 77 |
|
| { |
| 78 |
|
| const char * s; |
| 79 |
|
| STRLEN l; |
| 80 |
|
| |
| 81 |
1206 |
▶ | if (utf8) *utf8 = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (utf8) *utf8 = 0; |
493 |
713 |
1206 |
|
| 82 |
1206 |
▶ | if (len) *len = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) *len = 0; |
38 |
1168 |
1206 |
|
| 83 |
1206 |
▶ | if (sv == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv == NULL) |
226 |
980 |
1206 |
|
| 84 |
226 |
| return NULL; |
| 85 |
980 |
| SvGETMAGIC (sv); |
| 86 |
980 |
▶ | if (!SvOK (sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK (sv)) |
147 |
833 |
980 |
|
| 87 |
147 |
| return NULL; |
| 88 |
833 |
| s = SvPV_nomg (sv, l); |
| 89 |
833 |
▶ | if (utf8) *utf8 = SvUTF8 (sv) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (utf8) *utf8 = SvUTF8 (sv) ? 1 : 0; |
492 |
341 |
833 |
|
| 90 |
833 |
▶ | if (len) *len = l; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) *len = l; |
38 |
795 |
833 |
|
| 91 |
833 |
| return s; |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| #define sv_text(sv, utf8) sv_text_len (aTHX_ sv, utf8, NULL) |
| 95 |
|
| |
| 96 |
|
| /* New SV for a C string; flagged as UTF-8 if asked (and if valid) */ |
| 97 |
|
| static SV * |
| 98 |
2294 |
| new_text (pTHX_ const char *s, int utf8) |
| 99 |
|
| { |
| 100 |
|
| STRLEN len; |
| 101 |
|
| |
| 102 |
2294 |
▶ | if (s == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s == NULL) |
0 |
2294 |
2294 |
|
| 103 |
0 |
| return newSV (0); |
| 104 |
2294 |
| len = strlen (s); |
| 105 |
2294 |
▶ | if (utf8 && !is_utf8_string ((const U8 *) s, len)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (utf8 && !is_utf8_string ((const U8 *) s, len)) |
2007 |
287 |
2294 |
| if (utf8 && !is_utf8_string ((const U8 *) s, len)) |
0 |
2007 |
2007 |
|
| 106 |
0 |
| utf8 = 0; |
| 107 |
2294 |
▶ | return newSVpvn_flags (s, len, utf8 ? SVf_UTF8 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| return newSVpvn_flags (s, len, utf8 ? SVf_UTF8 : 0); |
2007 |
287 |
2294 |
|
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
|
| |
| 111 |
|
| /* ---------------------------------------------------------------------- |
| 112 |
|
| * Errors |
| 113 |
|
| */ |
| 114 |
|
| |
| 115 |
|
| /* Convert errs to Text::BibTeX::Error objects, clear it, and pass them |
| 116 |
|
| * (with the entry they belong to, if any) to the Perl dispatcher, which |
| 117 |
|
| * may die. Call this only when no C memory is left to free. */ |
| 118 |
|
| static void |
| 119 |
689 |
| dispatch_errors (pTHX_ bt_errors *errs, int utf8, SV *entry_ref) |
| 120 |
|
| { |
| 121 |
689 |
| dSP; |
| 122 |
|
| AV * av; |
| 123 |
|
| HV * stash; |
| 124 |
|
| bt_error * e; |
| 125 |
|
| |
| 126 |
689 |
▶ | if (errs->head == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (errs->head == NULL) |
629 |
60 |
689 |
|
| 127 |
629 |
| return; |
| 128 |
|
| |
| 129 |
60 |
| av = newAV (); |
| 130 |
60 |
| stash = gv_stashpv ("Text::BibTeX::Error", GV_ADD); |
| 131 |
142 |
▶ | for (e = errs->head; e; e = e->next) |
Branch: 1 branch
| Branch | True | False | Total |
| for (e = errs->head; e; e = e->next) |
82 |
60 |
142 |
|
| 132 |
|
| { |
| 133 |
82 |
| HV * hv = newHV (); |
| 134 |
82 |
| hv_stores (hv, "errclass", newSViv (e->errclass)); |
| 135 |
82 |
| hv_stores (hv, "message", new_text (aTHX_ e->message, utf8)); |
| 136 |
82 |
▶ | if (e->filename) |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->filename) |
18 |
64 |
82 |
|
| 137 |
18 |
| hv_stores (hv, "filename", new_text (aTHX_ e->filename, utf8)); |
| 138 |
82 |
▶ | if (e->line > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->line > 0) |
57 |
25 |
82 |
|
| 139 |
57 |
| hv_stores (hv, "line", newSViv (e->line)); |
| 140 |
82 |
▶ | if (e->item_desc && e->item > 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (e->item_desc && e->item > 0) |
23 |
59 |
82 |
| if (e->item_desc && e->item > 0) |
7 |
16 |
23 |
|
| 141 |
|
| { |
| 142 |
7 |
| hv_stores (hv, "item_desc", new_text (aTHX_ e->item_desc, utf8)); |
| 143 |
7 |
| hv_stores (hv, "item", newSViv (e->item)); |
| 144 |
|
| } |
| 145 |
82 |
| av_push (av, sv_bless (newRV_noinc ((SV *) hv), stash)); |
| 146 |
|
| } |
| 147 |
60 |
| bt_errors_clear (errs); |
| 148 |
|
| |
| 149 |
60 |
| ENTER; |
| 150 |
60 |
| SAVETMPS; |
| 151 |
60 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
60 |
60 |
|
| 152 |
60 |
▶ | XPUSHs (sv_2mortal (newRV_noinc ((SV *) av))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (sv_2mortal (newRV_noinc ((SV *) av))); |
0 |
60 |
60 |
|
| 153 |
60 |
▶ | XPUSHs (entry_ref ? entry_ref : &PL_sv_undef); |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHs (entry_ref ? entry_ref : &PL_sv_undef); |
0 |
60 |
60 |
| XPUSHs (entry_ref ? entry_ref : &PL_sv_undef); |
38 |
22 |
60 |
|
| 154 |
60 |
| PUTBACK; |
| 155 |
60 |
| call_pv ("Text::BibTeX::Error::_dispatch", G_DISCARD); |
| 156 |
58 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
58 |
0 |
58 |
|
| 157 |
58 |
| LEAVE; |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
|
| |
| 161 |
|
| /* ---------------------------------------------------------------------- |
| 162 |
|
| * Reading from a Perl filehandle |
| 163 |
|
| */ |
| 164 |
|
| |
| 165 |
|
| static long |
| 166 |
28 |
| read_tied (pTHX_ btxs_parser *xp, IO *io, MAGIC *mg, char *buf, size_t n) |
| 167 |
|
| { |
| 168 |
28 |
| dSP; |
| 169 |
28 |
| SV * bufsv = sv_2mortal (newSVpvs ("")); |
| 170 |
28 |
| long got = -1; |
| 171 |
|
| int count; |
| 172 |
|
| |
| 173 |
28 |
| ENTER; |
| 174 |
28 |
| SAVETMPS; |
| 175 |
28 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
28 |
28 |
|
| 176 |
28 |
▶ | XPUSHs (SvTIED_obj ((SV *) io, mg)); |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHs (SvTIED_obj ((SV *) io, mg)); |
0 |
28 |
28 |
| XPUSHs (SvTIED_obj ((SV *) io, mg)); |
28 |
0 |
28 |
|
| 177 |
28 |
▶ | XPUSHs (bufsv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (bufsv); |
0 |
28 |
28 |
|
| 178 |
28 |
▶ | XPUSHs (sv_2mortal (newSVuv (n))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (sv_2mortal (newSVuv (n))); |
0 |
28 |
28 |
|
| 179 |
28 |
| PUTBACK; |
| 180 |
28 |
| count = call_method ("READ", G_SCALAR | G_EVAL); |
| 181 |
28 |
| SPAGAIN; |
| 182 |
28 |
▶ | if (count == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (count == 1) |
28 |
0 |
28 |
|
| 183 |
|
| { |
| 184 |
28 |
| SV * ret = POPs; |
| 185 |
28 |
▶ | if (SvTRUE (ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE (ERRSV)) |
28 |
0 |
28 |
| if (SvTRUE (ERRSV)) |
1 |
27 |
28 |
|
| 186 |
|
| { |
| 187 |
1 |
▶ | if (!xp->read_died) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!xp->read_died) |
1 |
0 |
1 |
|
| 188 |
1 |
▶ | xp->read_died = newSVsv (ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| xp->read_died = newSVsv (ERRSV); |
1 |
0 |
1 |
|
| 189 |
|
| } |
| 190 |
27 |
▶ | else if (SvOK (ret)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (SvOK (ret)) |
27 |
0 |
27 |
|
| 191 |
|
| { |
| 192 |
|
| STRLEN len; |
| 193 |
|
| const char * s; |
| 194 |
|
| |
| 195 |
27 |
▶ | if (SvUTF8 (bufsv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8 (bufsv)) |
0 |
27 |
27 |
|
| 196 |
0 |
| xp->utf8 = 1; |
| 197 |
27 |
| s = SvPV (bufsv, len); |
| 198 |
27 |
▶ | if (len > n) /* READ returned more than asked */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > n) /* READ returned more than asked */ |
0 |
27 |
27 |
|
| 199 |
0 |
| len = n; |
| 200 |
27 |
| memcpy (buf, s, len); |
| 201 |
27 |
| got = (long) len; |
| 202 |
|
| } |
| 203 |
|
| } |
| 204 |
28 |
| PUTBACK; |
| 205 |
28 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
28 |
0 |
28 |
|
| 206 |
28 |
| LEAVE; |
| 207 |
28 |
| return got; |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
|
| static long |
| 211 |
89 |
| xs_read (void *userdata, char *buf, size_t n) |
| 212 |
|
| { |
| 213 |
89 |
| btxs_parser * xp = (btxs_parser *) userdata; |
| 214 |
|
| IO * io; |
| 215 |
|
| MAGIC * mg; |
| 216 |
|
| PerlIO * f; |
| 217 |
|
| SSize_t got; |
| 218 |
|
| #ifdef MULTIPLICITY |
| 219 |
|
| PerlInterpreter * my_perl = xp->perl; |
| 220 |
|
| #endif |
| 221 |
|
| |
| 222 |
89 |
▶ | io = GvIO ((GV *) SvRV (xp->fh)); |
Branch: 4 branches
| Branch | True | False | Total |
| io = GvIO ((GV *) SvRV (xp->fh)); |
89 |
0 |
89 |
| io = GvIO ((GV *) SvRV (xp->fh)); |
0 |
89 |
89 |
| io = GvIO ((GV *) SvRV (xp->fh)); |
0 |
0 |
0 |
| io = GvIO ((GV *) SvRV (xp->fh)); |
89 |
0 |
89 |
|
| 223 |
89 |
▶ | if (io == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (io == NULL) |
0 |
89 |
89 |
|
| 224 |
0 |
| return -1; |
| 225 |
89 |
▶ | if ((mg = SvTIED_mg ((SV *) io, PERL_MAGIC_tiedscalar))) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((mg = SvTIED_mg ((SV *) io, PERL_MAGIC_tiedscalar))) |
28 |
61 |
89 |
| if ((mg = SvTIED_mg ((SV *) io, PERL_MAGIC_tiedscalar))) |
28 |
61 |
89 |
|
| 226 |
28 |
| return read_tied (aTHX_ xp, io, mg, buf, n); |
| 227 |
|
| |
| 228 |
61 |
| f = IoIFP (io); |
| 229 |
61 |
▶ | if (f == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f == NULL) |
0 |
61 |
61 |
|
| 230 |
0 |
| return -1; |
| 231 |
61 |
| got = PerlIO_read (f, buf, n); |
| 232 |
61 |
▶ | if (got < 0 || (got == 0 && PerlIO_error (f))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (got < 0 || (got == 0 && PerlIO_error (f))) |
61 |
0 |
61 |
| if (got < 0 || (got == 0 && PerlIO_error (f))) |
28 |
33 |
61 |
| if (got < 0 || (got == 0 && PerlIO_error (f))) |
0 |
28 |
28 |
|
| 233 |
0 |
| return -1; |
| 234 |
61 |
| return (long) got; |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
|
| |
| 238 |
|
| /* ---------------------------------------------------------------------- |
| 239 |
|
| * Converting a btparse entry AST to a Perl structure |
| 240 |
|
| */ |
| 241 |
|
| |
| 242 |
|
| static SV * |
| 243 |
696 |
| convert_value (pTHX_ AST *top, int preserve, int utf8) |
| 244 |
|
| { |
| 245 |
|
| AST * value; |
| 246 |
|
| bt_nodetype nodetype; |
| 247 |
|
| char * text; |
| 248 |
|
| |
| 249 |
696 |
| value = bt_next_value (top, NULL, &nodetype, &text); |
| 250 |
|
| |
| 251 |
696 |
▶ | if (preserve) |
Branch: 1 branch
| Branch | True | False | Total |
| if (preserve) |
1 |
695 |
696 |
|
| 252 |
|
| { |
| 253 |
1 |
| HV * val_stash = gv_stashpv ("Text::BibTeX::Value", GV_ADD); |
| 254 |
1 |
| HV * sval_stash = gv_stashpv ("Text::BibTeX::SimpleValue", GV_ADD); |
| 255 |
1 |
| AV * compound = newAV (); |
| 256 |
|
| |
| 257 |
3 |
▶ | while (value) |
Branch: 1 branch
| Branch | True | False | Total |
| while (value) |
2 |
1 |
3 |
|
| 258 |
|
| { |
| 259 |
2 |
| AV * simple = newAV (); |
| 260 |
2 |
| av_push (simple, newSViv ((IV) nodetype)); |
| 261 |
2 |
| av_push (simple, new_text (aTHX_ text, utf8)); |
| 262 |
2 |
| av_push (compound, sv_bless (newRV_noinc ((SV *) simple), sval_stash)); |
| 263 |
2 |
| value = bt_next_value (top, value, &nodetype, &text); |
| 264 |
|
| } |
| 265 |
1 |
| return sv_bless (newRV_noinc ((SV *) compound), val_stash); |
| 266 |
|
| } |
| 267 |
|
| |
| 268 |
|
| /* After full post-processing a value is a single string (or NULL for |
| 269 |
|
| * an undefined macro); if not, paste what there is. */ |
| 270 |
695 |
▶ | if (value && value->right) |
Branch: 2 branches
| Branch | True | False | Total |
| if (value && value->right) |
683 |
12 |
695 |
| if (value && value->right) |
0 |
683 |
683 |
|
| 271 |
|
| { |
| 272 |
0 |
| SV * sv = newSVpvs (""); |
| 273 |
0 |
▶ | if (utf8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (utf8) |
0 |
0 |
0 |
|
| 274 |
0 |
| SvUTF8_on (sv); |
| 275 |
0 |
▶ | for (; value; value = bt_next_value (top, value, NULL, &text)) |
Branch: 1 branch
| Branch | True | False | Total |
| for (; value; value = bt_next_value (top, value, NULL, &text)) |
0 |
0 |
0 |
|
| 276 |
0 |
▶ | if (text) |
Branch: 1 branch
| Branch | True | False | Total |
| if (text) |
0 |
0 |
0 |
|
| 277 |
0 |
| sv_catsv (sv, sv_2mortal (new_text (aTHX_ text, utf8))); |
| 278 |
0 |
| return sv; |
| 279 |
|
| } |
| 280 |
695 |
▶ | return text ? new_text (aTHX_ text, utf8) : newSV (0); |
Branch: 1 branch
| Branch | True | False | Total |
| return text ? new_text (aTHX_ text, utf8) : newSV (0); |
673 |
22 |
695 |
|
| 281 |
|
| } |
| 282 |
|
| |
| 283 |
|
| static void |
| 284 |
160 |
| convert_assigned_entry (pTHX_ AST *top, HV *entry, int preserve, int utf8) |
| 285 |
|
| { |
| 286 |
160 |
| AV * flist = newAV (); |
| 287 |
160 |
| HV * values = newHV (); |
| 288 |
160 |
| HV * lines = newHV (); |
| 289 |
|
| AST * field; |
| 290 |
|
| char * field_name; |
| 291 |
160 |
| int last_line = top->line; |
| 292 |
|
| |
| 293 |
|
| /* The STOP line is the line of the last field (the closing brace is |
| 294 |
|
| * not kept in the AST) */ |
| 295 |
160 |
| hv_stores (lines, "START", newSViv (top->line)); |
| 296 |
|
| |
| 297 |
160 |
| for (field = bt_next_field (top, NULL, &field_name); |
| 298 |
849 |
▶ | field; |
Branch: 1 branch
| Branch | True | False | Total |
| field; |
689 |
160 |
849 |
|
| 299 |
689 |
| field = bt_next_field (top, field, &field_name)) |
| 300 |
|
| { |
| 301 |
|
| SV * name; |
| 302 |
|
| |
| 303 |
689 |
▶ | if (!field_name) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!field_name) |
0 |
689 |
689 |
|
| 304 |
0 |
| continue; |
| 305 |
689 |
| name = new_text (aTHX_ field_name, utf8); |
| 306 |
689 |
| av_push (flist, name); |
| 307 |
689 |
| (void) hv_store_ent (values, name, convert_value (aTHX_ field, preserve, utf8), 0); |
| 308 |
689 |
| (void) hv_store_ent (lines, name, newSViv (field->line), 0); |
| 309 |
689 |
| last_line = field->line; |
| 310 |
|
| } |
| 311 |
160 |
| hv_stores (lines, "STOP", newSViv (last_line)); |
| 312 |
|
| |
| 313 |
160 |
| hv_stores (entry, "fields", newRV_noinc ((SV *) flist)); |
| 314 |
160 |
| hv_stores (entry, "values", newRV_noinc ((SV *) values)); |
| 315 |
160 |
| hv_stores (entry, "lines", newRV_noinc ((SV *) lines)); |
| 316 |
160 |
| } |
| 317 |
|
| |
| 318 |
|
| static void |
| 319 |
7 |
| convert_value_entry (pTHX_ AST *top, HV *entry, int preserve, int utf8) |
| 320 |
|
| { |
| 321 |
7 |
| HV * lines = newHV (); |
| 322 |
7 |
| AST * item = NULL, *last = NULL; |
| 323 |
|
| |
| 324 |
7 |
| hv_stores (lines, "START", newSViv (top->line)); |
| 325 |
13 |
▶ | while ((item = bt_next_value (top, item, NULL, NULL))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((item = bt_next_value (top, item, NULL, NULL))) |
6 |
7 |
13 |
|
| 326 |
6 |
| last = item; |
| 327 |
7 |
▶ | hv_stores (lines, "STOP", newSViv (last ? last->line : top->line)); |
Branch: 1 branch
| Branch | True | False | Total |
| hv_stores (lines, "STOP", newSViv (last ? last->line : top->line)); |
5 |
2 |
7 |
|
| 328 |
7 |
| hv_stores (entry, "lines", newRV_noinc ((SV *) lines)); |
| 329 |
7 |
| hv_stores (entry, "value", convert_value (aTHX_ top, preserve, utf8)); |
| 330 |
7 |
| } |
| 331 |
|
| |
| 332 |
|
| /* Fill in an entry hash from an AST (and free the AST) */ |
| 333 |
|
| static void |
| 334 |
167 |
| ast_to_hash (pTHX_ HV *entry, AST *top, int status, int preserve, |
| 335 |
|
| bt_macros *macros, bt_errors *errs, int utf8) |
| 336 |
|
| { |
| 337 |
167 |
| bt_metatype metatype = bt_entry_metatype (top); |
| 338 |
|
| btshort options; |
| 339 |
|
| char * key; |
| 340 |
|
| |
| 341 |
|
| /* |
| 342 |
|
| * Clear out all hash values that might not be replaced in this |
| 343 |
|
| * conversion (in case the user parses into an existing |
| 344 |
|
| * Text::BibTeX::Entry object). |
| 345 |
|
| */ |
| 346 |
167 |
| (void) hv_deletes (entry, "key", G_DISCARD); |
| 347 |
167 |
| (void) hv_deletes (entry, "fields", G_DISCARD); |
| 348 |
167 |
| (void) hv_deletes (entry, "lines", G_DISCARD); |
| 349 |
167 |
| (void) hv_deletes (entry, "values", G_DISCARD); |
| 350 |
167 |
| (void) hv_deletes (entry, "value", G_DISCARD); |
| 351 |
|
| |
| 352 |
|
| /* |
| 353 |
|
| * Post-process (the parser returned the raw entry): with 'preserve' |
| 354 |
|
| * we keep the values as they are; otherwise expand macros, paste |
| 355 |
|
| * strings and (except for @string values) collapse whitespace. This |
| 356 |
|
| * also stores the macros defined by an @string entry. |
| 357 |
|
| */ |
| 358 |
167 |
▶ | if (preserve) |
Branch: 1 branch
| Branch | True | False | Total |
| if (preserve) |
1 |
166 |
167 |
|
| 359 |
1 |
| options = BTO_MINIMAL; |
| 360 |
|
| else |
| 361 |
166 |
▶ | options = (metatype == BTE_MACRODEF) ? BTO_MACRO : BTO_FULL; |
Branch: 1 branch
| Branch | True | False | Total |
| options = (metatype == BTE_MACRODEF) ? BTO_MACRO : BTO_FULL; |
18 |
148 |
166 |
|
| 362 |
167 |
| bt_postprocess_entry (macros, errs, top, options); |
| 363 |
|
| |
| 364 |
167 |
| hv_stores (entry, "type", new_text (aTHX_ bt_entry_type (top), utf8)); |
| 365 |
167 |
| hv_stores (entry, "metatype", newSViv (metatype)); |
| 366 |
167 |
▶ | if ((key = bt_entry_key (top))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((key = bt_entry_key (top))) |
142 |
25 |
167 |
|
| 367 |
142 |
| hv_stores (entry, "key", new_text (aTHX_ key, utf8)); |
| 368 |
167 |
| hv_stores (entry, "status", newSViv (status ? 1 : 0)); |
| 369 |
|
| |
| 370 |
167 |
| switch (metatype) |
| 371 |
|
| { |
| 372 |
160 |
| case BTE_MACRODEF: |
| 373 |
|
| case BTE_REGULAR: |
| 374 |
160 |
| convert_assigned_entry (aTHX_ top, entry, preserve, utf8); |
| 375 |
160 |
| break; |
| 376 |
7 |
| case BTE_COMMENT: |
| 377 |
|
| case BTE_PREAMBLE: |
| 378 |
7 |
| convert_value_entry (aTHX_ top, entry, preserve, utf8); |
| 379 |
7 |
| break; |
| 380 |
0 |
| default: |
| 381 |
0 |
| break; |
| 382 |
|
| } |
| 383 |
167 |
| } |
| 384 |
|
| |
| 385 |
|
| static HV * |
| 386 |
236 |
| entry_hash (pTHX_ SV *entry_ref) |
| 387 |
|
| { |
| 388 |
236 |
▶ | if (! (SvROK (entry_ref) && SvTYPE (SvRV (entry_ref)) == SVt_PVHV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (! (SvROK (entry_ref) && SvTYPE (SvRV (entry_ref)) == SVt_PVHV)) |
236 |
0 |
236 |
| if (! (SvROK (entry_ref) && SvTYPE (SvRV (entry_ref)) == SVt_PVHV)) |
0 |
236 |
236 |
|
| 389 |
0 |
| croak ("entry_ref must be a hash ref"); |
| 390 |
236 |
| return (HV *) SvRV (entry_ref); |
| 391 |
|
| } |
| 392 |
|
| |
| 393 |
|
| /* Parse one entry with p into entry_ref; returns true if one was found */ |
| 394 |
|
| static int |
| 395 |
198 |
| parse_into (pTHX_ SV *entry_ref, bt_parser *p, int preserve, int utf8) |
| 396 |
|
| { |
| 397 |
198 |
| HV * entry = entry_hash (aTHX_ entry_ref); |
| 398 |
|
| bt_errors errs; |
| 399 |
|
| boolean status; |
| 400 |
|
| AST * top; |
| 401 |
|
| |
| 402 |
198 |
| (void) hv_deletes (entry, "errors", G_DISCARD); /* fresh error list */ |
| 403 |
198 |
| bt_errors_init (&errs); |
| 404 |
198 |
| top = bt_parse_entry (p, &errs, BTO_NOPOSTPROCESS, &status); |
| 405 |
198 |
▶ | if (top) |
Branch: 1 branch
| Branch | True | False | Total |
| if (top) |
167 |
31 |
198 |
|
| 406 |
|
| { |
| 407 |
167 |
| ast_to_hash (aTHX_ entry, top, status, preserve, |
| 408 |
|
| bt_parser_macros (p), &errs, utf8); |
| 409 |
167 |
| bt_free_ast (top); |
| 410 |
|
| } |
| 411 |
198 |
| dispatch_errors (aTHX_ &errs, utf8, entry_ref); |
| 412 |
197 |
| return top != NULL; |
| 413 |
|
| } |
| 414 |
|
| |
| 415 |
|
| |
| 416 |
|
| /* ---------------------------------------------------------------------- |
| 417 |
|
| * Converting lists of C strings to Perl |
| 418 |
|
| */ |
| 419 |
|
| |
| 420 |
|
| static void |
| 421 |
396 |
| store_stringlist (pTHX_ HV *hash, const char *key, char **list, int n, |
| 422 |
|
| int utf8) |
| 423 |
|
| { |
| 424 |
396 |
▶ | if (list) |
Branch: 1 branch
| Branch | True | False | Total |
| if (list) |
202 |
194 |
396 |
|
| 425 |
|
| { |
| 426 |
202 |
| AV * av = newAV (); |
| 427 |
|
| int i; |
| 428 |
|
| |
| 429 |
456 |
▶ | for (i = 0; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) |
254 |
202 |
456 |
|
| 430 |
254 |
▶ | av_push (av, list[i] ? new_text (aTHX_ list[i], utf8) : newSV (0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_push (av, list[i] ? new_text (aTHX_ list[i], utf8) : newSV (0)); |
254 |
0 |
254 |
|
| 431 |
202 |
| (void) hv_store (hash, key, strlen (key), newRV_noinc ((SV *) av), 0); |
| 432 |
|
| } |
| 433 |
|
| else |
| 434 |
194 |
| (void) hv_delete (hash, key, strlen (key), G_DISCARD); |
| 435 |
396 |
| } |
| 436 |
|
| |
| 437 |
|
| |
| 438 |
|
| static int |
| 439 |
16 |
| constant (const char *name, IV *arg) |
| 440 |
|
| { |
| 441 |
|
| static const struct { const char *name; IV value; } constants[] = |
| 442 |
|
| { |
| 443 |
|
| { "BTE_UNKNOWN", BTE_UNKNOWN }, |
| 444 |
|
| { "BTE_REGULAR", BTE_REGULAR }, |
| 445 |
|
| { "BTE_COMMENT", BTE_COMMENT }, |
| 446 |
|
| { "BTE_PREAMBLE", BTE_PREAMBLE }, |
| 447 |
|
| { "BTE_MACRODEF", BTE_MACRODEF }, |
| 448 |
|
| { "BTAST_STRING", BTAST_STRING }, |
| 449 |
|
| { "BTAST_NUMBER", BTAST_NUMBER }, |
| 450 |
|
| { "BTAST_MACRO", BTAST_MACRO }, |
| 451 |
|
| { "BTN_FIRST", BTN_FIRST }, |
| 452 |
|
| { "BTN_VON", BTN_VON }, |
| 453 |
|
| { "BTN_LAST", BTN_LAST }, |
| 454 |
|
| { "BTN_JR", BTN_JR }, |
| 455 |
|
| { "BTN_NONE", BTN_NONE }, |
| 456 |
|
| { "BTJ_MAYTIE", BTJ_MAYTIE }, |
| 457 |
|
| { "BTJ_SPACE", BTJ_SPACE }, |
| 458 |
|
| { "BTJ_FORCETIE", BTJ_FORCETIE }, |
| 459 |
|
| { "BTJ_NOTHING", BTJ_NOTHING }, |
| 460 |
|
| }; |
| 461 |
|
| size_t i; |
| 462 |
|
| |
| 463 |
149 |
▶ | for (i = 0; i < sizeof (constants) / sizeof (constants[0]); i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < sizeof (constants) / sizeof (constants[0]); i++) |
149 |
0 |
149 |
|
| 464 |
149 |
▶ | if (strEQ (name, constants[i].name)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ (name, constants[i].name)) |
16 |
133 |
149 |
|
| 465 |
|
| { |
| 466 |
16 |
| *arg = constants[i].value; |
| 467 |
16 |
| return 1; |
| 468 |
|
| } |
| 469 |
0 |
| return 0; |
| 470 |
|
| } |
| 471 |
|
| |
| 472 |
|
| |
| 473 |
|
| MODULE = Text::BibTeX PACKAGE = Text::BibTeX |
| 474 |
|
| |
| 475 |
|
| PROTOTYPES: DISABLE |
| 476 |
|
| |
| 477 |
|
| BOOT: |
| 478 |
|
| { |
| 479 |
|
| MY_CXT_INIT; |
| 480 |
18 |
| MY_CXT.macros = bt_macros_new (NULL); |
| 481 |
18 |
| bt_set_oom_handler (btxs_oom); |
| 482 |
|
| } |
| 483 |
|
| |
| 484 |
|
| void |
| 485 |
|
| CLONE (...) |
| 486 |
|
| CODE: |
| 487 |
|
| /* the new interpreter starts with a copy of the parent's macros */ |
| 488 |
|
| MY_CXT_CLONE; |
| 489 |
0 |
| MY_CXT.macros = bt_macros_clone (MY_CXT.macros, NULL); |
| 490 |
|
| |
| 491 |
|
| SV * |
| 492 |
|
| constant (name) |
| 493 |
|
| const char * name |
| 494 |
|
| CODE: |
| 495 |
|
| IV i; |
| 496 |
16 |
▶ | if (constant (name, &i)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (constant (name, &i)) |
16 |
0 |
16 |
|
| 497 |
16 |
| RETVAL = newSViv (i); |
| 498 |
|
| else |
| 499 |
0 |
| RETVAL = &PL_sv_undef; |
| 500 |
|
| OUTPUT: |
| 501 |
|
| RETVAL |
| 502 |
|
| |
| 503 |
|
| const char * |
| 504 |
|
| unicode_version () |
| 505 |
|
| CODE: |
| 506 |
2 |
| RETVAL = bt_unicode_version (); |
| 507 |
|
| OUTPUT: |
| 508 |
|
| RETVAL |
| 509 |
|
| |
| 510 |
|
| void |
| 511 |
|
| initialize () |
| 512 |
|
| CODE: |
| 513 |
|
| /* nothing to do: kept for compatibility */ |
| 514 |
|
| |
| 515 |
|
| void |
| 516 |
|
| cleanup () |
| 517 |
|
| CODE: |
| 518 |
|
| dMY_CXT; |
| 519 |
0 |
| bt_delete_all_macros (MY_CXT.macros); |
| 520 |
|
| |
| 521 |
|
| void |
| 522 |
|
| isplit_list (string, delim, filename=NULL, line=0, description=NULL) |
| 523 |
|
| SV * string |
| 524 |
|
| const char * delim |
| 525 |
|
| SV * filename |
| 526 |
|
| int line |
| 527 |
|
| SV * description |
| 528 |
|
| PREINIT: |
| 529 |
|
| bt_stringlist * list; |
| 530 |
|
| bt_errors errs; |
| 531 |
|
| int utf8, i; |
| 532 |
|
| const char * s; |
| 533 |
|
| AV * result; |
| 534 |
|
| PPCODE: |
| 535 |
38 |
| s = sv_text (string, &utf8); |
| 536 |
38 |
| bt_errors_init (&errs); |
| 537 |
38 |
| list = bt_split_list (&errs, s, delim, sv_text (filename, NULL), |
| 538 |
|
| line, sv_text (description, NULL)); |
| 539 |
38 |
| result = (AV *) sv_2mortal ((SV *) newAV ()); |
| 540 |
38 |
▶ | if (list) |
Branch: 1 branch
| Branch | True | False | Total |
| if (list) |
38 |
0 |
38 |
|
| 541 |
|
| { |
| 542 |
147 |
▶ | for (i = 0; i < list->num_items; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < list->num_items; i++) |
109 |
38 |
147 |
|
| 543 |
109 |
▶ | av_push (result, list->items[i] |
Branch: 1 branch
| Branch | True | False | Total |
| av_push (result, list->items[i] |
104 |
5 |
109 |
|
| 544 |
|
| ? new_text (aTHX_ list->items[i], utf8) : newSV (0)); |
| 545 |
38 |
| bt_free_list (list); |
| 546 |
|
| } |
| 547 |
38 |
| dispatch_errors (aTHX_ &errs, utf8, NULL); |
| 548 |
38 |
▶ | EXTEND (SP, av_len (result) + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND (SP, av_len (result) + 1); |
38 |
0 |
38 |
| EXTEND (SP, av_len (result) + 1); |
0 |
38 |
38 |
|
| 549 |
147 |
▶ | for (i = 0; i <= av_len (result); i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= av_len (result); i++) |
109 |
38 |
147 |
|
| 550 |
109 |
| PUSHs (sv_2mortal (SvREFCNT_inc (*av_fetch (result, i, 0)))); |
| 551 |
|
| |
| 552 |
|
| SV * |
| 553 |
|
| purify_string (instr, options=0) |
| 554 |
|
| SV * instr |
| 555 |
|
| int options |
| 556 |
|
| PREINIT: |
| 557 |
|
| const char * s; |
| 558 |
|
| char * pure; |
| 559 |
|
| int utf8; |
| 560 |
|
| CODE: |
| 561 |
73 |
▶ | if ((s = sv_text (instr, &utf8)) == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((s = sv_text (instr, &utf8)) == NULL) |
1 |
72 |
73 |
|
| 562 |
1 |
| XSRETURN_UNDEF; |
| 563 |
72 |
| pure = bt_purify_string (s, (btshort) options); |
| 564 |
72 |
| RETVAL = new_text (aTHX_ pure, utf8); |
| 565 |
72 |
| bt_free (pure); |
| 566 |
|
| OUTPUT: |
| 567 |
|
| RETVAL |
| 568 |
|
| |
| 569 |
|
| SV * |
| 570 |
|
| change_case (transform, string, options=0) |
| 571 |
|
| char transform |
| 572 |
|
| SV * string |
| 573 |
|
| int options |
| 574 |
|
| PREINIT: |
| 575 |
|
| const char * s; |
| 576 |
|
| char * changed; |
| 577 |
|
| bt_errors errs; |
| 578 |
|
| int utf8; |
| 579 |
|
| CODE: |
| 580 |
19 |
▶ | if ((s = sv_text (string, &utf8)) == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((s = sv_text (string, &utf8)) == NULL) |
0 |
19 |
19 |
|
| 581 |
0 |
| XSRETURN_UNDEF; |
| 582 |
19 |
| bt_errors_init (&errs); |
| 583 |
19 |
| changed = bt_change_case (&errs, transform, s, (btshort) options); |
| 584 |
19 |
▶ | RETVAL = changed ? new_text (aTHX_ changed, utf8) : newSV (0); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = changed ? new_text (aTHX_ changed, utf8) : newSV (0); |
18 |
1 |
19 |
|
| 585 |
19 |
| bt_free (changed); |
| 586 |
19 |
| sv_2mortal (RETVAL); |
| 587 |
19 |
| dispatch_errors (aTHX_ &errs, utf8, NULL); |
| 588 |
18 |
▶ | SvREFCNT_inc_simple_void (RETVAL); |
Branch: 1 branch
| Branch | True | False | Total |
| SvREFCNT_inc_simple_void (RETVAL); |
18 |
0 |
18 |
|
| 589 |
|
| OUTPUT: |
| 590 |
|
| RETVAL |
| 591 |
|
| |
| 592 |
|
| void |
| 593 |
|
| add_macro_text (macro, text, filename=NULL, line=0) |
| 594 |
|
| SV * macro |
| 595 |
|
| SV * text |
| 596 |
|
| SV * filename |
| 597 |
|
| int line |
| 598 |
|
| PREINIT: |
| 599 |
|
| bt_errors errs; |
| 600 |
|
| int utf8; |
| 601 |
|
| CODE: |
| 602 |
|
| dMY_CXT; |
| 603 |
219 |
| bt_errors_init (&errs); |
| 604 |
219 |
| bt_add_macro_text (MY_CXT.macros, &errs, sv_text (macro, &utf8), |
| 605 |
|
| sv_text (text, NULL), |
| 606 |
|
| sv_text (filename, NULL), line); |
| 607 |
219 |
| dispatch_errors (aTHX_ &errs, utf8, NULL); |
| 608 |
|
| |
| 609 |
|
| void |
| 610 |
|
| delete_macro (macro) |
| 611 |
|
| SV * macro |
| 612 |
|
| CODE: |
| 613 |
|
| dMY_CXT; |
| 614 |
4 |
▶ | if (SvOK (macro)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK (macro)) |
4 |
0 |
4 |
|
| 615 |
4 |
| bt_delete_macro (MY_CXT.macros, sv_text (macro, NULL)); |
| 616 |
|
| |
| 617 |
|
| void |
| 618 |
|
| delete_all_macros () |
| 619 |
|
| CODE: |
| 620 |
|
| dMY_CXT; |
| 621 |
0 |
| bt_delete_all_macros (MY_CXT.macros); |
| 622 |
|
| |
| 623 |
|
| int |
| 624 |
|
| macro_length (macro) |
| 625 |
|
| SV * macro |
| 626 |
|
| CODE: |
| 627 |
|
| dMY_CXT; |
| 628 |
10 |
▶ | RETVAL = SvOK (macro) |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = SvOK (macro) |
0 |
10 |
10 |
|
| 629 |
10 |
▶ | ? bt_macro_length (MY_CXT.macros, sv_text (macro, NULL)) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| ? bt_macro_length (MY_CXT.macros, sv_text (macro, NULL)) : 0; |
10 |
0 |
10 |
|
| 630 |
|
| OUTPUT: |
| 631 |
|
| RETVAL |
| 632 |
|
| |
| 633 |
|
| SV * |
| 634 |
|
| macro_text (macro, filename=NULL, line=0) |
| 635 |
|
| SV * macro |
| 636 |
|
| SV * filename |
| 637 |
|
| int line |
| 638 |
|
| PREINIT: |
| 639 |
|
| bt_errors errs; |
| 640 |
|
| const char * text; |
| 641 |
|
| int utf8; |
| 642 |
|
| CODE: |
| 643 |
|
| dMY_CXT; |
| 644 |
7 |
| bt_errors_init (&errs); |
| 645 |
7 |
| text = bt_macro_text (MY_CXT.macros, &errs, sv_text (macro, &utf8), |
| 646 |
|
| sv_text (filename, NULL), line); |
| 647 |
7 |
▶ | RETVAL = sv_2mortal (text ? newSVpv (text, 0) : newSV (0)); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = sv_2mortal (text ? newSVpv (text, 0) : newSV (0)); |
3 |
4 |
7 |
|
| 648 |
7 |
| dispatch_errors (aTHX_ &errs, utf8, NULL); |
| 649 |
7 |
▶ | SvREFCNT_inc_simple_void (RETVAL); |
Branch: 1 branch
| Branch | True | False | Total |
| SvREFCNT_inc_simple_void (RETVAL); |
7 |
0 |
7 |
|
| 650 |
|
| OUTPUT: |
| 651 |
|
| RETVAL |
| 652 |
|
| |
| 653 |
|
| |
| 654 |
|
| MODULE = Text::BibTeX PACKAGE = Text::BibTeX::Parser |
| 655 |
|
| |
| 656 |
|
| Text::BibTeX::Parser |
| 657 |
|
| new (class, fh, filename=NULL, utf8=0, private_macros=0) |
| 658 |
|
| const char * class |
| 659 |
|
| SV * fh |
| 660 |
|
| SV * filename |
| 661 |
|
| int utf8 |
| 662 |
|
| int private_macros |
| 663 |
|
| PREINIT: |
| 664 |
|
| btxs_parser * xp; |
| 665 |
35 |
▶ | GV * gv = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| GV * gv = NULL; |
0 |
35 |
35 |
|
| 666 |
|
| CODE: |
| 667 |
|
| PERL_UNUSED_VAR (class); |
| 668 |
35 |
▶ | if (SvROK (fh) && SvTYPE (SvRV (fh)) == SVt_PVGV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK (fh) && SvTYPE (SvRV (fh)) == SVt_PVGV) |
35 |
0 |
35 |
| if (SvROK (fh) && SvTYPE (SvRV (fh)) == SVt_PVGV) |
35 |
0 |
35 |
|
| 669 |
35 |
| gv = (GV *) SvRV (fh); |
| 670 |
0 |
▶ | else if (SvTYPE (fh) == SVt_PVGV) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (SvTYPE (fh) == SVt_PVGV) |
0 |
0 |
0 |
|
| 671 |
0 |
| gv = (GV *) fh; |
| 672 |
0 |
▶ | else if (SvROK (fh) && SvTYPE (SvRV (fh)) == SVt_PVIO) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (SvROK (fh) && SvTYPE (SvRV (fh)) == SVt_PVIO) |
0 |
0 |
0 |
| else if (SvROK (fh) && SvTYPE (SvRV (fh)) == SVt_PVIO) |
0 |
0 |
0 |
|
| 673 |
0 |
| croak ("Text::BibTeX::Parser::new: pass a glob or glob reference, " |
| 674 |
|
| "not an IO object"); |
| 675 |
35 |
▶ | if (gv == NULL || GvIO (gv) == NULL) |
Branch: 6 branches
| Branch | True | False | Total |
| if (gv == NULL || GvIO (gv) == NULL) |
35 |
0 |
35 |
| if (gv == NULL || GvIO (gv) == NULL) |
35 |
0 |
35 |
| if (gv == NULL || GvIO (gv) == NULL) |
0 |
35 |
35 |
| if (gv == NULL || GvIO (gv) == NULL) |
0 |
0 |
0 |
| if (gv == NULL || GvIO (gv) == NULL) |
35 |
0 |
35 |
| if (gv == NULL || GvIO (gv) == NULL) |
0 |
35 |
35 |
|
| 676 |
0 |
| croak ("Text::BibTeX::Parser::new: not a filehandle"); |
| 677 |
|
| { |
| 678 |
|
| dMY_CXT; |
| 679 |
35 |
| Newxz (xp, 1, btxs_parser); |
| 680 |
|
| #ifdef MULTIPLICITY |
| 681 |
|
| xp->perl = aTHX; |
| 682 |
|
| #endif |
| 683 |
35 |
| xp->fh = newRV_inc ((SV *) gv); |
| 684 |
35 |
| xp->utf8 = utf8; |
| 685 |
35 |
▶ | if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
Branch: 10 branches
| Branch | True | False | Total |
| if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
35 |
0 |
35 |
| if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
0 |
35 |
35 |
| if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
0 |
0 |
0 |
| if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
35 |
0 |
35 |
| if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
33 |
2 |
35 |
| if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
33 |
0 |
33 |
| if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
0 |
33 |
33 |
| if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
0 |
0 |
0 |
| if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
33 |
0 |
33 |
| if (IoIFP (GvIO (gv)) && PerlIO_isutf8 (IoIFP (GvIO (gv)))) |
2 |
31 |
33 |
|
| 686 |
2 |
| xp->utf8 = 1; |
| 687 |
35 |
| xp->parser = bt_parser_new_reader (xs_read, xp, |
| 688 |
|
| sv_text (filename, NULL), |
| 689 |
35 |
| xp->utf8 ? BTP_UTF8 : 0); |
| 690 |
35 |
▶ | if (private_macros) |
Branch: 1 branch
| Branch | True | False | Total |
| if (private_macros) |
1 |
34 |
35 |
|
| 691 |
1 |
| xp->private_macros = bt_macros_new (MY_CXT.macros); |
| 692 |
35 |
▶ | bt_parser_set_macros (xp->parser, xp->private_macros |
Branch: 1 branch
| Branch | True | False | Total |
| bt_parser_set_macros (xp->parser, xp->private_macros |
1 |
34 |
35 |
|
| 693 |
|
| ? xp->private_macros : MY_CXT.macros); |
| 694 |
|
| } |
| 695 |
35 |
| RETVAL = xp; |
| 696 |
|
| OUTPUT: |
| 697 |
|
| RETVAL |
| 698 |
|
| |
| 699 |
|
| int |
| 700 |
|
| eof (xp) |
| 701 |
|
| Text::BibTeX::Parser xp |
| 702 |
|
| CODE: |
| 703 |
3 |
| RETVAL = bt_parser_eof (xp->parser); |
| 704 |
|
| OUTPUT: |
| 705 |
|
| RETVAL |
| 706 |
|
| |
| 707 |
|
| int |
| 708 |
|
| utf8 (xp) |
| 709 |
|
| Text::BibTeX::Parser xp |
| 710 |
|
| CODE: |
| 711 |
0 |
▶ | RETVAL = xp->utf8; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = xp->utf8; |
0 |
0 |
0 |
|
| 712 |
|
| OUTPUT: |
| 713 |
|
| RETVAL |
| 714 |
|
| |
| 715 |
|
| int |
| 716 |
|
| CLONE_SKIP (...) |
| 717 |
|
| CODE: |
| 718 |
0 |
▶ | RETVAL = 1; /* C state can't be shared */ |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = 1; /* C state can't be shared */ |
0 |
0 |
0 |
|
| 719 |
|
| OUTPUT: |
| 720 |
|
| RETVAL |
| 721 |
|
| |
| 722 |
|
| void |
| 723 |
|
| DESTROY (xp) |
| 724 |
|
| Text::BibTeX::Parser xp |
| 725 |
|
| CODE: |
| 726 |
35 |
| bt_parser_free (xp->parser); |
| 727 |
35 |
| bt_macros_free (xp->private_macros); |
| 728 |
35 |
| SvREFCNT_dec (xp->fh); |
| 729 |
35 |
| SvREFCNT_dec (xp->read_died); |
| 730 |
35 |
| Safefree (xp); |
| 731 |
|
| |
| 732 |
|
| |
| 733 |
|
| MODULE = Text::BibTeX PACKAGE = Text::BibTeX::Entry |
| 734 |
|
| |
| 735 |
|
| int |
| 736 |
|
| _parse (entry_ref, parser, preserve=0) |
| 737 |
|
| SV * entry_ref |
| 738 |
|
| Text::BibTeX::Parser parser |
| 739 |
|
| int preserve |
| 740 |
|
| CODE: |
| 741 |
160 |
| RETVAL = parse_into (aTHX_ entry_ref, parser->parser, preserve, |
| 742 |
|
| parser->utf8); |
| 743 |
160 |
▶ | if (parser->read_died) |
Branch: 1 branch
| Branch | True | False | Total |
| if (parser->read_died) |
1 |
159 |
160 |
|
| 744 |
|
| { |
| 745 |
1 |
| SV * err = sv_2mortal (parser->read_died); |
| 746 |
1 |
| parser->read_died = NULL; |
| 747 |
1 |
| croak_sv (err); |
| 748 |
|
| } |
| 749 |
|
| OUTPUT: |
| 750 |
|
| RETVAL |
| 751 |
|
| |
| 752 |
|
| int |
| 753 |
|
| _parse_s (entry_ref, text, preserve=0, want_utf8=0) |
| 754 |
|
| SV * entry_ref |
| 755 |
|
| SV * text |
| 756 |
|
| int preserve |
| 757 |
|
| int want_utf8 |
| 758 |
|
| PREINIT: |
| 759 |
|
| bt_parser * p; |
| 760 |
|
| const char * s; |
| 761 |
|
| STRLEN len; |
| 762 |
|
| int utf8; |
| 763 |
|
| CODE: |
| 764 |
|
| dMY_CXT; |
| 765 |
38 |
| (void) entry_hash (aTHX_ entry_ref); |
| 766 |
38 |
| s = sv_text_len (aTHX_ text, &utf8, &len); |
| 767 |
38 |
▶ | if (s == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s == NULL) |
0 |
38 |
38 |
|
| 768 |
0 |
| XSRETURN_NO; |
| 769 |
38 |
▶ | utf8 = utf8 || want_utf8; |
Branch: 2 branches
| Branch | True | False | Total |
| utf8 = utf8 || want_utf8; |
26 |
12 |
38 |
| utf8 = utf8 || want_utf8; |
26 |
0 |
26 |
|
| 770 |
38 |
| p = bt_parser_new_string (s, len, NULL, 1, utf8 ? BTP_UTF8 : 0); |
| 771 |
38 |
| bt_parser_set_macros (p, MY_CXT.macros); |
| 772 |
38 |
| SAVEDESTRUCTOR ((void (*) (void *)) bt_parser_free, p); |
| 773 |
38 |
| RETVAL = parse_into (aTHX_ entry_ref, p, preserve, utf8); |
| 774 |
|
| OUTPUT: |
| 775 |
|
| RETVAL |
| 776 |
|
| |
| 777 |
|
| |
| 778 |
|
| MODULE = Text::BibTeX PACKAGE = Text::BibTeX::Name |
| 779 |
|
| |
| 780 |
|
| void |
| 781 |
|
| _split (name_hashref, name, filename, line, name_num, keep_cstruct) |
| 782 |
|
| SV * name_hashref |
| 783 |
|
| SV * name |
| 784 |
|
| SV * filename |
| 785 |
|
| int line |
| 786 |
|
| int name_num |
| 787 |
|
| int keep_cstruct |
| 788 |
|
| PREINIT: |
| 789 |
|
| HV * name_hash; |
| 790 |
|
| bt_name * split; |
| 791 |
|
| bt_errors errs; |
| 792 |
|
| int utf8; |
| 793 |
|
| CODE: |
| 794 |
99 |
▶ | if (! (SvROK (name_hashref) && SvTYPE (SvRV (name_hashref)) == SVt_PVHV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (! (SvROK (name_hashref) && SvTYPE (SvRV (name_hashref)) == SVt_PVHV)) |
99 |
0 |
99 |
| if (! (SvROK (name_hashref) && SvTYPE (SvRV (name_hashref)) == SVt_PVHV)) |
0 |
99 |
99 |
|
| 795 |
0 |
| croak ("name_hashref is not a hash reference"); |
| 796 |
99 |
| name_hash = (HV *) SvRV (name_hashref); |
| 797 |
99 |
| (void) hv_deletes (name_hash, "_cstruct", G_DISCARD); |
| 798 |
|
| |
| 799 |
99 |
| bt_errors_init (&errs); |
| 800 |
99 |
| split = bt_split_name (&errs, sv_text (name, &utf8), |
| 801 |
|
| sv_text (filename, NULL), line, name_num); |
| 802 |
99 |
| store_stringlist (aTHX_ name_hash, "first", split->parts[BTN_FIRST], |
| 803 |
|
| split->part_len[BTN_FIRST], utf8); |
| 804 |
99 |
| store_stringlist (aTHX_ name_hash, "von", split->parts[BTN_VON], |
| 805 |
|
| split->part_len[BTN_VON], utf8); |
| 806 |
99 |
| store_stringlist (aTHX_ name_hash, "last", split->parts[BTN_LAST], |
| 807 |
|
| split->part_len[BTN_LAST], utf8); |
| 808 |
99 |
| store_stringlist (aTHX_ name_hash, "jr", split->parts[BTN_JR], |
| 809 |
|
| split->part_len[BTN_JR], utf8); |
| 810 |
|
| |
| 811 |
99 |
▶ | if (keep_cstruct) |
Branch: 1 branch
| Branch | True | False | Total |
| if (keep_cstruct) |
85 |
14 |
99 |
|
| 812 |
|
| { |
| 813 |
|
| btxs_name * xn; |
| 814 |
85 |
| SV * obj = newSV (0); |
| 815 |
85 |
| Newx (xn, 1, btxs_name); |
| 816 |
85 |
| xn->name = split; |
| 817 |
85 |
| sv_setref_pv (obj, "Text::BibTeX::Name::Struct", xn); |
| 818 |
85 |
| (void) hv_stores (name_hash, "_cstruct", obj); |
| 819 |
|
| } |
| 820 |
|
| else |
| 821 |
14 |
| bt_free_name (split); |
| 822 |
99 |
| dispatch_errors (aTHX_ &errs, utf8, NULL); |
| 823 |
|
| |
| 824 |
|
| void |
| 825 |
|
| free (name_hashref) |
| 826 |
|
| SV * name_hashref |
| 827 |
|
| CODE: |
| 828 |
0 |
▶ | if (SvROK (name_hashref) && SvTYPE (SvRV (name_hashref)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK (name_hashref) && SvTYPE (SvRV (name_hashref)) == SVt_PVHV) |
0 |
0 |
0 |
| if (SvROK (name_hashref) && SvTYPE (SvRV (name_hashref)) == SVt_PVHV) |
0 |
0 |
0 |
|
| 829 |
0 |
| (void) hv_deletes ((HV *) SvRV (name_hashref), "_cstruct", G_DISCARD); |
| 830 |
|
| |
| 831 |
|
| |
| 832 |
|
| MODULE = Text::BibTeX PACKAGE = Text::BibTeX::Name::Struct |
| 833 |
|
| |
| 834 |
|
| int |
| 835 |
|
| CLONE_SKIP (...) |
| 836 |
|
| CODE: |
| 837 |
0 |
▶ | RETVAL = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = 1; |
0 |
0 |
0 |
|
| 838 |
|
| OUTPUT: |
| 839 |
|
| RETVAL |
| 840 |
|
| |
| 841 |
|
| void |
| 842 |
|
| DESTROY (xn) |
| 843 |
|
| Text::BibTeX::Name::Struct xn |
| 844 |
|
| CODE: |
| 845 |
85 |
| bt_free_name (xn->name); |
| 846 |
85 |
| Safefree (xn); |
| 847 |
|
| |
| 848 |
|
| |
| 849 |
|
| MODULE = Text::BibTeX PACKAGE = Text::BibTeX::NameFormat |
| 850 |
|
| |
| 851 |
|
| Text::BibTeX::NameFormat::Struct |
| 852 |
|
| create (parts="fvlj", abbrev_first=0) |
| 853 |
|
| const char * parts |
| 854 |
|
| int abbrev_first |
| 855 |
|
| PREINIT: |
| 856 |
|
| bt_errors errs; |
| 857 |
|
| bt_name_format * format; |
| 858 |
|
| CODE: |
| 859 |
43 |
| bt_errors_init (&errs); |
| 860 |
43 |
| format = bt_create_name_format (&errs, parts, abbrev_first); |
| 861 |
43 |
| dispatch_errors (aTHX_ &errs, 0, NULL); /* dies on bad parts */ |
| 862 |
43 |
▶ | if (format == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (format == NULL) |
0 |
43 |
43 |
|
| 863 |
0 |
| croak ("invalid name format \"%s\"", parts); |
| 864 |
43 |
| Newx (RETVAL, 1, btxs_format); |
| 865 |
43 |
| RETVAL->format = format; |
| 866 |
|
| OUTPUT: |
| 867 |
|
| RETVAL |
| 868 |
|
| |
| 869 |
|
| void |
| 870 |
|
| free (format) |
| 871 |
|
| SV * format |
| 872 |
|
| CODE: |
| 873 |
|
| /* the structure is freed when its last reference goes away */ |
| 874 |
|
| PERL_UNUSED_VAR (format); |
| 875 |
|
| |
| 876 |
|
| void |
| 877 |
|
| _set_text (format, part, pre_part, post_part, pre_token, post_token) |
| 878 |
|
| Text::BibTeX::NameFormat::Struct format |
| 879 |
|
| int part |
| 880 |
|
| SV * pre_part |
| 881 |
|
| SV * post_part |
| 882 |
|
| SV * pre_token |
| 883 |
|
| SV * post_token |
| 884 |
|
| CODE: |
| 885 |
11 |
▶ | if (part < 0 || part >= BT_MAX_NAMEPARTS) |
Branch: 2 branches
| Branch | True | False | Total |
| if (part < 0 || part >= BT_MAX_NAMEPARTS) |
11 |
0 |
11 |
| if (part < 0 || part >= BT_MAX_NAMEPARTS) |
0 |
11 |
11 |
|
| 886 |
0 |
| croak ("invalid name part %d", part); |
| 887 |
11 |
| bt_set_format_text (format->format, (bt_namepart) part, |
| 888 |
|
| sv_text (pre_part, NULL), |
| 889 |
|
| sv_text (post_part, NULL), |
| 890 |
|
| sv_text (pre_token, NULL), |
| 891 |
|
| sv_text (post_token, NULL)); |
| 892 |
|
| |
| 893 |
|
| void |
| 894 |
|
| _set_options (format, part, abbrev, join_tokens, join_part) |
| 895 |
|
| Text::BibTeX::NameFormat::Struct format |
| 896 |
|
| int part |
| 897 |
|
| int abbrev |
| 898 |
|
| int join_tokens |
| 899 |
|
| int join_part |
| 900 |
|
| CODE: |
| 901 |
14 |
▶ | if (part < 0 || part >= BT_MAX_NAMEPARTS) |
Branch: 2 branches
| Branch | True | False | Total |
| if (part < 0 || part >= BT_MAX_NAMEPARTS) |
14 |
0 |
14 |
| if (part < 0 || part >= BT_MAX_NAMEPARTS) |
0 |
14 |
14 |
|
| 902 |
0 |
| croak ("invalid name part %d", part); |
| 903 |
14 |
▶ | if (join_tokens < BTJ_MAYTIE || join_tokens > BTJ_NOTHING |
Branch: 2 branches
| Branch | True | False | Total |
| if (join_tokens < BTJ_MAYTIE || join_tokens > BTJ_NOTHING |
14 |
0 |
14 |
| if (join_tokens < BTJ_MAYTIE || join_tokens > BTJ_NOTHING |
14 |
0 |
14 |
|
| 904 |
14 |
▶ | || join_part < BTJ_MAYTIE || join_part > BTJ_NOTHING) |
Branch: 2 branches
| Branch | True | False | Total |
| || join_part < BTJ_MAYTIE || join_part > BTJ_NOTHING) |
14 |
0 |
14 |
| || join_part < BTJ_MAYTIE || join_part > BTJ_NOTHING) |
0 |
14 |
14 |
|
| 905 |
0 |
| croak ("invalid join method"); |
| 906 |
14 |
| bt_set_format_options (format->format, (bt_namepart) part, abbrev, |
| 907 |
|
| (bt_joinmethod) join_tokens, |
| 908 |
|
| (bt_joinmethod) join_part); |
| 909 |
|
| |
| 910 |
|
| SV * |
| 911 |
|
| format_name (name, format, utf8=0) |
| 912 |
|
| Text::BibTeX::Name::Struct name |
| 913 |
|
| Text::BibTeX::NameFormat::Struct format |
| 914 |
|
| int utf8 |
| 915 |
|
| PREINIT: |
| 916 |
|
| bt_errors errs; |
| 917 |
|
| char * formatted; |
| 918 |
|
| CODE: |
| 919 |
66 |
| bt_errors_init (&errs); |
| 920 |
66 |
| formatted = bt_format_name (&errs, name->name, format->format); |
| 921 |
66 |
| RETVAL = sv_2mortal (new_text (aTHX_ formatted, utf8)); |
| 922 |
66 |
| bt_free (formatted); |
| 923 |
66 |
| dispatch_errors (aTHX_ &errs, utf8, NULL); |
| 924 |
66 |
▶ | SvREFCNT_inc_simple_void (RETVAL); |
Branch: 1 branch
| Branch | True | False | Total |
| SvREFCNT_inc_simple_void (RETVAL); |
66 |
0 |
66 |
|
| 925 |
|
| OUTPUT: |
| 926 |
|
| RETVAL |
| 927 |
|
| |
| 928 |
|
| |
| 929 |
|
| MODULE = Text::BibTeX PACKAGE = Text::BibTeX::NameFormat::Struct |
| 930 |
|
| |
| 931 |
|
| int |
| 932 |
|
| CLONE_SKIP (...) |
| 933 |
|
| CODE: |
| 934 |
0 |
▶ | RETVAL = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = 1; |
0 |
0 |
0 |
|
| 935 |
|
| OUTPUT: |
| 936 |
|
| RETVAL |
| 937 |
|
| |
| 938 |
|
| void |
| 939 |
|
| DESTROY (xf) |
| 940 |
|
| Text::BibTeX::NameFormat::Struct xf |
| 941 |
|
| CODE: |
| 942 |
43 |
| bt_free_name_format (xf->format); |
| 943 |
43 |
| Safefree (xf); |