Parser.xs

filter: Statementstmt 100% 20 / 20 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 32 / 32 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/ELFF/Parser.pm ↑ Summary
1 /*
2 * ELFF-Parser is a perl module for parsing ELFF formatted log files.
3 *
4 * Copyright (C) 2007-2010 Mark Warren <mwarren42@gmail.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20  
21 #include "EXTERN.h"
22 #include "perl.h"
23 #include "XSUB.h"
24  
25 MODULE = ELFF::Parser PACKAGE = ELFF::Parser
26  
27 AV*
28 tokenize(value)
29 SV* value
30 PROTOTYPE: $
31 CODE:
32 STRLEN len;
33 unsigned int l;
34 char *line, *ptr;
35 SV* field;
36  
37 // bail if value is undef or not a string
38 25 ▶ if(!SvPOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvPOK(value)) { 4 21 25
39 4 croak("wrong type of scalar for tokenize(), must be a string");
40 }
41  
42 // set up pointers
43 21 ptr = line = SvPV(value, len);
44  
45 // allocate a new array
46 21 RETVAL = newAV();
47 21 sv_2mortal((SV*)RETVAL);
48  
49 // trim trailing white space from the line
50 24 ▶ while(isspace(line[len - 1])) {
Branch: 1 branch
BranchTrueFalseTotal
while(isspace(line[len - 1])) { 3 21 24
51 3 line[len - 1] = '\0';
52 3 len--;
53 }
54  
55 72 ▶ while(ptr - line < len) {
Branch: 1 branch
BranchTrueFalseTotal
while(ptr - line < len) { 53 19 72
56 // is this a quoted field?
57 53 int quotes = *ptr == '"' ? 1 : 0;
58  
59 // find the ptr to the first index of either '"' or ' ',
60 // depending on whether or not the field is quoted
61 53 ▶ char *end = strchr(ptr + quotes, quotes == 1 ? '"' : ' ');
Branch: 1 branch
BranchTrueFalseTotal
char *end = strchr(ptr + quotes, quotes == 1 ? '"' : ' '); 17 36 53
62  
63 // find the length of the field
64 53 ▶ if(end == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(end == NULL) { 15 38 53
65 15 ▶ if(quotes == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(quotes == 1) { 2 13 15
66 // last field is missing trailing quote, malformed line
67 2 croak("malformed line, missing trailing quote");
68 }
69  
70 13 l = strlen(ptr);
71 13 end = ptr + l;
72 }
73 else
74 38 l = end - ptr - quotes;
75  
76 // push the field into the result array
77 51 field = newSVpvn(ptr + quotes, l);
78 51 av_push((AV*)RETVAL, field);
79  
80 // set ptr to end + quotes and space
81 51 ptr = ptr + quotes + l + quotes + 1;
82 }
83  
84 OUTPUT:
85 RETVAL
86  
« blib/lib/ELFF/Parser.pm ↑ Summary