PhoenixLecture  2.0.0
Set of tools to make lectures
pbiblio_parser.cpp
Go to the documentation of this file.
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 
11 
15  if(str == "article"){return PBiblioEntryType::ARTICLE;}
16  else if(str == "book"){return PBiblioEntryType::BOOK;}
17  else if(str == "software"){return PBiblioEntryType::SOFTWARE;}
18  else if(str == "inproceedings"){return PBiblioEntryType::INPROCEEDINGS;}
19  else if(str == "phdthesis"){return PBiblioEntryType::PHDTHESIS;}
20  else if(str == "techreport"){return PBiblioEntryType::TECHREPORT;}
21  else {return PBiblioEntryType::NONE;}
22 }
23 
25 
29 bool pbiblio_parseAttribute(PFileParser & parser, PString & attrName, PString & attrValue){
30  parser.skipWhiteSpace();
31  attrName = parser.getNextToken(); //Name of the attribute
32  //Then we expect an =
33  PString token = parser.getNextToken();
34  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  token = parser.getNextToken();
41  PString tmpAttrValue("");
42  if(token == "{"){
43  tmpAttrValue = parser.getUntilKeyWithoutPaternRecurse("}","{");
44  }else if(token == "\""){
45  parser.setEscapeChar('\\');
46  tmpAttrValue = parser.getUntilKeyWithoutPatern("\"");
47  parser.setEscapeChar('\0');
48  }else{
49  tmpAttrValue = parser.getUntilKeyWithoutPatern(",");
50  }
51  attrValue = tmpAttrValue.eraseFirstLastChar(" \n\t");
52 
53  if(parser.isMatch(",")){} //Just skip the next , if there is one
54  return true;
55 }
56 
58 
62  if(attrName == "authors"){return "author";}
63  return attrName;
64 }
65 
67 
71  return attrValue.eraseChar("{}\"%").replace("\\url", "");
72 }
73 
75 
81  parser.getUntilKeyWithoutPatern("@"); //Get the start of the entry
82  PString entryType(parser.getUntilKeyWithoutPatern("{").eraseChar(" \n\t").toLower());
83  type = pbiblio_strToEntryType(entryType);
84  PString entryLabel(parser.getUntilKeyWithoutPatern(",").eraseChar(" \n\t"));
85  mapStrEntry["label"] = entryLabel;
86 
87  while(!parser.isEndOfFile() && !parser.isMatch("}")){
88  PString attrName(""), attrValue("");
89  if(pbiblio_parseAttribute(parser, attrName, attrValue)){
90  mapStrEntry[pbiblio_checkAttrName(attrName)] = pbiblio_checkAttrValue(attrValue);
91  }else{
92  return false;
93  }
94  }
95  return true;
96 }
97 
98 
100 
103 void pbliblio_convertMapStrToBibEntry(PBiblioEntry & biblioEntry, PMapStrEntry & mapStrEntry){
104  biblioEntry.setLabel(mapStrEntry["label"]);
105  biblioEntry.setTitle(mapStrEntry["title"]);
106  biblioEntry.setAuthor(mapStrEntry["author"]);
107  biblioEntry.setJournal(mapStrEntry["journal"]);
108  biblioEntry.setYear(mapStrEntry["year"]);
109  biblioEntry.setUrl(mapStrEntry["url"]);
110 }
111 
113 
117 bool pbliblio_parser(PFileParser & parser, PMapBiblioEntry & mapBiblioEntry){
118  while(!parser.isEndOfFile()){
119  PMapStrEntry mapStrEntry;
121  if(pbliblio_parserEntry(parser, mapStrEntry, type)){
122  PBiblioEntry biblioEntry;
123  pbliblio_convertMapStrToBibEntry(biblioEntry, mapStrEntry);
124  biblioEntry.setId(mapBiblioEntry.size());
125  biblioEntry.setType(type);
126  mapBiblioEntry[biblioEntry.getLabel()] = biblioEntry;
127  }else{
128  return false;
129  }
130  }
131  return true;
132 }
133 
135 
139 bool pbliblio_file(PMapBiblioEntry & mapBiblioEntry, const PPath & fileName){
140  PFileParser parser;
141  if(!parser.open(fileName)){
142  std::cerr << "pbliblio_file : can't open file '" << fileName << "'" << std::endl;
143  return false;
144  }
145  parser.setSeparator("()[]{}+=*.;,/%#':<>\"");
146 
147  return pbliblio_parser(parser, mapBiblioEntry);
148 }
149 
Entry in the bibliography.
Definition: PLatexObj.h:409
void setLabel(const PString &label)
Sets the label of the PBiblioEntry.
Definition: PLatexObj.cpp:1627
const PString & getLabel() const
Gets the label of the PBiblioEntry.
Definition: PLatexObj.cpp:1921
void setId(size_t id)
Sets the id of the PBiblioEntry.
Definition: PLatexObj.cpp:1613
void setTitle(const PString &title)
Sets the title of the PBiblioEntry.
Definition: PLatexObj.cpp:1641
void setJournal(const PString &journal)
Sets the journal of the PBiblioEntry.
Definition: PLatexObj.cpp:1648
void setYear(const PString &year)
Sets the year of the PBiblioEntry.
Definition: PLatexObj.cpp:1683
void setType(const PBiblioEntryType::PBiblioEntryType &type)
Sets the type of the PBiblioEntry.
Definition: PLatexObj.cpp:1620
void setAuthor(const PString &author)
Sets the author of the PBiblioEntry.
Definition: PLatexObj.cpp:1634
void setUrl(const PString &url)
Sets the url of the PBiblioEntry.
Definition: PLatexObj.cpp:1704
classe qui permet de parser des fichiers texte en renvoyant les tokens les uns après les autres
Definition: PFileParser.h:20
void setSeparator(const PString &separator)
Initialise la liste des caractères séparateurs.
Definition: PFileParser.cpp:43
bool open(const PPath &fileName)
Fonction qui ouvre le fichier que l'on va parser.
Definition: PFileParser.cpp:24
PString getNextToken()
Get the next token.
PString getUntilKeyWithoutPatern(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern exclu.
void setEscapeChar(char escapeChar)
Sets the escape character of the PFileParser.
Definition: PFileParser.cpp:58
PString getUntilKeyWithoutPaternRecurse(const PString &patern, const PString &beginPatern, const PString &allowedCharAfterBegin)
Get the string until end sequence and take account recursive patern (embeded strings)
bool isMatch(const PString &patern)
Says if the patern match with the current caracters of the PFileParser.
PLocation getLocation() const
Fonction qui renvoie la PLocation du PFileParser.
void skipWhiteSpace()
Skip the white space if there is at the current caracter position.
bool isEndOfFile() const
Dit si on est à la fin du fichier.
Definition: PFileParser.cpp:88
Path of a directory or a file.
Definition: PPath.h:17
Extends the std::string.
Definition: PString.h:16
PString replace(const PString &pattern, const PString &replaceStr) const
Replace a PString into an other PString.
Definition: PString.cpp:204
PString eraseChar(char ch) const
Erase char ch of current string.
Definition: PString.cpp:478
PString toLower() const
Convert PString in lower case.
Definition: PString.cpp:598
PString eraseFirstLastChar(const PString &vecChar) const
Erase first and last char in a string.
Definition: PString.cpp:545
PString pbiblio_checkAttrName(const PString &attrName)
Check the attribute name.
bool pbliblio_parser(PFileParser &parser, PMapBiblioEntry &mapBiblioEntry)
Parse the full bibliography file.
PString pbiblio_checkAttrValue(const PString &attrValue)
Check the attribute value.
bool pbiblio_parseAttribute(PFileParser &parser, PString &attrName, PString &attrValue)
Parse an attribute of the current bibliography entry.
bool pbliblio_parserEntry(PFileParser &parser, PMapStrEntry &mapStrEntry, PBiblioEntryType::PBiblioEntryType &type)
Parse the full bibliography file.
void pbliblio_convertMapStrToBibEntry(PBiblioEntry &biblioEntry, PMapStrEntry &mapStrEntry)
Convert the map of string into a PBiblioEntry.
PBiblioEntryType::PBiblioEntryType pbiblio_strToEntryType(const PString &str)
Convert a string into an entry type.
bool pbliblio_file(PMapBiblioEntry &mapBiblioEntry, const PPath &fileName)
Parse the full bibliography file.
std::map< PString, PString > PMapStrEntry
Describes one bibliograpy entry in string.
std::map< PString, PBiblioEntry > PMapBiblioEntry
Describes the map for all PBiblioEntry.