Directory: | ./ |
---|---|
File: | tmp_project/PhoenixTex2Html/src/PLatexObj/pbiblio_parser.cpp |
Date: | 2025-03-24 18:12:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 70 | 77 | 90.9% |
Branches: | 113 | 146 | 77.4% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | |||
8 | #include "pbiblio_parser.h" | ||
9 | |||
10 | ///Convert a string into an entry type | ||
11 | /** @param str : string to be used | ||
12 | * @return corresponding entry type | ||
13 | */ | ||
14 | 765 | PBiblioEntryType::PBiblioEntryType pbiblio_strToEntryType(const PString & str){ | |
15 |
2/2✓ Branch 1 taken 408 times.
✓ Branch 2 taken 357 times.
|
765 | if(str == "article"){return PBiblioEntryType::ARTICLE;} |
16 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 2 taken 300 times.
|
357 | else if(str == "book"){return PBiblioEntryType::BOOK;} |
17 |
2/2✓ Branch 1 taken 207 times.
✓ Branch 2 taken 93 times.
|
300 | else if(str == "software"){return PBiblioEntryType::SOFTWARE;} |
18 |
2/2✓ Branch 1 taken 45 times.
✓ Branch 2 taken 48 times.
|
93 | else if(str == "inproceedings"){return PBiblioEntryType::INPROCEEDINGS;} |
19 |
2/2✓ Branch 1 taken 15 times.
✓ Branch 2 taken 33 times.
|
48 | else if(str == "phdthesis"){return PBiblioEntryType::PHDTHESIS;} |
20 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 15 times.
|
33 | else if(str == "techreport"){return PBiblioEntryType::TECHREPORT;} |
21 | 15 | else {return PBiblioEntryType::NONE;} | |
22 | } | ||
23 | |||
24 | ///Parse an attribute of the current bibliography entry | ||
25 | /** @param[out] parser : parser to be used | ||
26 | * @param[out] attrName : aatribute name | ||
27 | * @param[out] attrValue : attribute value | ||
28 | */ | ||
29 | 4230 | bool pbiblio_parseAttribute(PFileParser & parser, PString & attrName, PString & attrValue){ | |
30 |
1/1✓ Branch 1 taken 4230 times.
|
4230 | parser.skipWhiteSpace(); |
31 |
2/2✓ Branch 1 taken 4230 times.
✓ Branch 4 taken 4230 times.
|
4230 | attrName = parser.getNextToken(); //Name of the attribute |
32 | //Then we expect an = | ||
33 |
1/1✓ Branch 1 taken 4230 times.
|
4230 | PString token = parser.getNextToken(); |
34 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4230 times.
|
4230 | if(token != "="){ |
35 | ✗ | std::cerr << "pbliblio_parser : error at " << parser.getLocation() << std::endl; | |
36 | ✗ | std::cerr << "\tunexpected token '" << token << "'" << std::endl; | |
37 | ✗ | return false; | |
38 | } | ||
39 | //Now parse the value | ||
40 |
2/2✓ Branch 1 taken 4230 times.
✓ Branch 4 taken 4230 times.
|
4230 | token = parser.getNextToken(); |
41 |
1/1✓ Branch 1 taken 4230 times.
|
4230 | PString tmpAttrValue(""); |
42 |
2/2✓ Branch 1 taken 2808 times.
✓ Branch 2 taken 1422 times.
|
4230 | if(token == "{"){ |
43 |
4/4✓ Branch 1 taken 2808 times.
✓ Branch 4 taken 2808 times.
✓ Branch 7 taken 2808 times.
✓ Branch 10 taken 2808 times.
|
2808 | tmpAttrValue = parser.getUntilKeyWithoutPaternRecurse("}","{"); |
44 |
2/2✓ Branch 1 taken 1338 times.
✓ Branch 2 taken 84 times.
|
1422 | }else if(token == "\""){ |
45 |
1/1✓ Branch 1 taken 1338 times.
|
1338 | parser.setEscapeChar('\\'); |
46 |
3/3✓ Branch 1 taken 1338 times.
✓ Branch 4 taken 1338 times.
✓ Branch 7 taken 1338 times.
|
1338 | tmpAttrValue = parser.getUntilKeyWithoutPatern("\""); |
47 |
1/1✓ Branch 1 taken 1338 times.
|
1338 | parser.setEscapeChar('\0'); |
48 | }else{ | ||
49 |
3/3✓ Branch 1 taken 84 times.
✓ Branch 4 taken 84 times.
✓ Branch 7 taken 84 times.
|
84 | tmpAttrValue = parser.getUntilKeyWithoutPatern(","); |
50 | } | ||
51 |
3/3✓ Branch 1 taken 4230 times.
✓ Branch 4 taken 4230 times.
✓ Branch 7 taken 4230 times.
|
4230 | attrValue = tmpAttrValue.eraseFirstLastChar(" \n\t"); |
52 | |||
53 |
2/2✓ Branch 1 taken 4230 times.
✓ Branch 4 taken 4230 times.
|
4230 | if(parser.isMatch(",")){} //Just skip the next , if there is one |
54 | 4230 | return true; | |
55 | 4230 | } | |
56 | |||
57 | ///Check the attribute name | ||
58 | /** @param attrName : attribute name | ||
59 | * @return modified attribute name if necessary | ||
60 | */ | ||
61 | 4230 | PString pbiblio_checkAttrName(const PString & attrName){ | |
62 |
2/2✓ Branch 1 taken 21 times.
✓ Branch 2 taken 4209 times.
|
4230 | if(attrName == "authors"){return "author";} |
63 | 4209 | return attrName; | |
64 | } | ||
65 | |||
66 | ///Check the attribute value | ||
67 | /** @param attrValue : attribute value | ||
68 | * @return modified attribute value if necessary | ||
69 | */ | ||
70 | 4230 | PString pbiblio_checkAttrValue(const PString & attrValue){ | |
71 |
4/4✓ Branch 2 taken 4230 times.
✓ Branch 5 taken 4230 times.
✓ Branch 8 taken 4230 times.
✓ Branch 11 taken 4230 times.
|
4230 | return attrValue.eraseChar("{}\"%").replace("\\url", ""); |
72 | } | ||
73 | |||
74 | ///Parse the full bibliography file | ||
75 | /** @param[out] parser : parser to be used | ||
76 | * @param[out] mapStrEntry : map of all the bibliography entries | ||
77 | * @param type : type of the biblio entry | ||
78 | * @return true on success, false otherwise | ||
79 | */ | ||
80 | 765 | bool pbliblio_parserEntry(PFileParser & parser, PMapStrEntry & mapStrEntry, PBiblioEntryType::PBiblioEntryType & type){ | |
81 |
2/2✓ Branch 1 taken 765 times.
✓ Branch 4 taken 765 times.
|
765 | parser.getUntilKeyWithoutPatern("@"); //Get the start of the entry |
82 |
5/5✓ Branch 1 taken 765 times.
✓ Branch 4 taken 765 times.
✓ Branch 7 taken 765 times.
✓ Branch 10 taken 765 times.
✓ Branch 13 taken 765 times.
|
1530 | PString entryType(parser.getUntilKeyWithoutPatern("{").eraseChar(" \n\t").toLower()); |
83 |
1/1✓ Branch 1 taken 765 times.
|
765 | type = pbiblio_strToEntryType(entryType); |
84 |
4/4✓ Branch 1 taken 765 times.
✓ Branch 4 taken 765 times.
✓ Branch 7 taken 765 times.
✓ Branch 10 taken 765 times.
|
1530 | PString entryLabel(parser.getUntilKeyWithoutPatern(",").eraseChar(" \n\t")); |
85 |
3/3✓ Branch 1 taken 765 times.
✓ Branch 4 taken 765 times.
✓ Branch 7 taken 765 times.
|
765 | mapStrEntry["label"] = entryLabel; |
86 | |||
87 |
11/13✓ Branch 1 taken 4995 times.
✓ Branch 3 taken 4992 times.
✓ Branch 4 taken 3 times.
✓ Branch 6 taken 4992 times.
✓ Branch 9 taken 4992 times.
✓ Branch 11 taken 4230 times.
✓ Branch 12 taken 762 times.
✓ Branch 13 taken 4992 times.
✓ Branch 14 taken 3 times.
✓ Branch 16 taken 4230 times.
✓ Branch 17 taken 765 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
|
4995 | while(!parser.isEndOfFile() && !parser.isMatch("}")){ |
88 |
2/2✓ Branch 1 taken 4230 times.
✓ Branch 4 taken 4230 times.
|
4230 | PString attrName(""), attrValue(""); |
89 |
2/3✓ Branch 1 taken 4230 times.
✓ Branch 3 taken 4230 times.
✗ Branch 4 not taken.
|
4230 | if(pbiblio_parseAttribute(parser, attrName, attrValue)){ |
90 |
4/4✓ Branch 1 taken 4230 times.
✓ Branch 4 taken 4230 times.
✓ Branch 7 taken 4230 times.
✓ Branch 10 taken 4230 times.
|
4230 | mapStrEntry[pbiblio_checkAttrName(attrName)] = pbiblio_checkAttrValue(attrValue); |
91 | }else{ | ||
92 | ✗ | return false; | |
93 | } | ||
94 |
2/4✓ Branch 1 taken 4230 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4230 times.
✗ Branch 5 not taken.
|
4230 | } |
95 | 765 | return true; | |
96 | 765 | } | |
97 | |||
98 | |||
99 | ///Convert the map of string into a PBiblioEntry | ||
100 | /** @param[out] biblioEntry : output of the conversion | ||
101 | * @param mapStrEntry : map to be converted | ||
102 | */ | ||
103 | 765 | void pbliblio_convertMapStrToBibEntry(PBiblioEntry & biblioEntry, PMapStrEntry & mapStrEntry){ | |
104 |
2/2✓ Branch 2 taken 765 times.
✓ Branch 5 taken 765 times.
|
765 | biblioEntry.setLabel(mapStrEntry["label"]); |
105 |
2/2✓ Branch 2 taken 765 times.
✓ Branch 5 taken 765 times.
|
765 | biblioEntry.setTitle(mapStrEntry["title"]); |
106 |
2/2✓ Branch 2 taken 765 times.
✓ Branch 5 taken 765 times.
|
765 | biblioEntry.setAuthor(mapStrEntry["author"]); |
107 |
2/2✓ Branch 2 taken 765 times.
✓ Branch 5 taken 765 times.
|
765 | biblioEntry.setJournal(mapStrEntry["journal"]); |
108 |
2/2✓ Branch 2 taken 765 times.
✓ Branch 5 taken 765 times.
|
765 | biblioEntry.setYear(mapStrEntry["year"]); |
109 |
2/2✓ Branch 2 taken 765 times.
✓ Branch 5 taken 765 times.
|
765 | biblioEntry.setUrl(mapStrEntry["url"]); |
110 | 765 | } | |
111 | |||
112 | ///Parse the full bibliography file | ||
113 | /** @param[out] parser : parser to be used | ||
114 | * @param[out] mapBiblioEntry : map of all the bibliography entries | ||
115 | * @return true on success, false otherwise | ||
116 | */ | ||
117 | 3 | bool pbliblio_parser(PFileParser & parser, PMapBiblioEntry & mapBiblioEntry){ | |
118 |
2/2✓ Branch 1 taken 765 times.
✓ Branch 2 taken 3 times.
|
768 | while(!parser.isEndOfFile()){ |
119 | 765 | PMapStrEntry mapStrEntry; | |
120 | PBiblioEntryType::PBiblioEntryType type; | ||
121 |
2/3✓ Branch 1 taken 765 times.
✓ Branch 3 taken 765 times.
✗ Branch 4 not taken.
|
765 | if(pbliblio_parserEntry(parser, mapStrEntry, type)){ |
122 |
1/1✓ Branch 1 taken 765 times.
|
765 | PBiblioEntry biblioEntry; |
123 |
1/1✓ Branch 1 taken 765 times.
|
765 | pbliblio_convertMapStrToBibEntry(biblioEntry, mapStrEntry); |
124 |
1/1✓ Branch 2 taken 765 times.
|
765 | biblioEntry.setId(mapBiblioEntry.size()); |
125 |
1/1✓ Branch 1 taken 765 times.
|
765 | biblioEntry.setType(type); |
126 |
3/3✓ Branch 1 taken 765 times.
✓ Branch 4 taken 765 times.
✓ Branch 7 taken 765 times.
|
765 | mapBiblioEntry[biblioEntry.getLabel()] = biblioEntry; |
127 | 765 | }else{ | |
128 | ✗ | return false; | |
129 | } | ||
130 |
1/2✓ Branch 1 taken 765 times.
✗ Branch 2 not taken.
|
765 | } |
131 | 3 | return true; | |
132 | } | ||
133 | |||
134 | ///Parse the full bibliography file | ||
135 | /** @param[out] mapBiblioEntry : map of all the bibliography entries | ||
136 | * @param fileName : name ofthe file to be parsed | ||
137 | * @return true on success, false otherwise | ||
138 | */ | ||
139 | 3 | bool pbliblio_file(PMapBiblioEntry & mapBiblioEntry, const PPath & fileName){ | |
140 |
1/1✓ Branch 1 taken 3 times.
|
3 | PFileParser parser; |
141 |
2/3✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
3 | if(!parser.open(fileName)){ |
142 | ✗ | std::cerr << "pbliblio_file : can't open file '" << fileName << "'" << std::endl; | |
143 | ✗ | return false; | |
144 | } | ||
145 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | parser.setSeparator("()[]{}+=*.;,/%#':<>\""); |
146 | |||
147 |
1/1✓ Branch 1 taken 3 times.
|
3 | return pbliblio_parser(parser, mapBiblioEntry); |
148 | 3 | } | |
149 | |||
150 |