-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazyBib.c++
331 lines (276 loc) · 8.92 KB
/
lazyBib.c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <iomanip> // std::setw, std::left
using std::cout;
using std::endl;
using std::string;
typedef std::map<std::string, std::map<std::string, int> > map2d;
void cleanString(std::string &str, std::vector<char> charToClean);
class Citation
{
public :
Citation(std::string const& outputBib, std::vector<std::string> const& citePattern);
void run(std::string nameFile);
friend std::ostream& operator<<(std::ostream& os, Citation const& cite);
private :
// member data
std::map<std::string, std::map<std::string, int> > m_listRef; // map2d m_listRef; // map <string nameRef, map <string fromFile, int count> >
std::string m_outputBib;
bool m_firstCall;
std::vector<std::string> const m_citePattern;
// member functions
void makeList(string nameFile, string const citePattern, string const fromFile);
bool splitComa(std::string const &str, std::string const &fromFile);
bool bibFile(string const nameLibrary, std::ios::openmode mode = std::ios::out);
void addRef(std::string const nameRef, std::string const fromFile);
};
Citation::Citation(std::string const& outputBib, std::vector<std::string> const& citePattern) :
m_outputBib(outputBib), m_firstCall(true), m_citePattern(citePattern)
{
std::vector<std::string>::const_iterator it = m_citePattern.cbegin();
std::cout << "Using the following citing patterns:" << std::endl;
for (; it != m_citePattern.cend(); ++it)
std::cout << "<" << *it << ">" << std::endl;
}
std::ostream& operator<<(std::ostream& os, Citation const& cite)
{
map2d::const_iterator it = cite.m_listRef.begin();
std::map<std::string, int>::const_iterator it_map;
int count = 0;
for (it = cite.m_listRef.begin(); it != cite.m_listRef.end(); ++it)
{
os << it->first << " :" <<endl;
for (it_map = (it->second).begin(); it_map != (it->second).end(); ++it_map)
{
os << " - " << std::setw(40) << it_map->first << " \t " << it_map->second << endl;
count += it_map->second;
}
}
cout << "total number of citations = " << count << endl;
cout << "number of references = " << (cite.m_listRef).size() << endl;
return os;
}
void Citation::addRef(std::string const nameRef, std::string const fromFile)
{
map2d::iterator it = m_listRef.find(nameRef);
if (it != m_listRef.end())
{
std::map<std::string, int>::iterator it_map = (it->second).find(fromFile);
if (it_map != (it->second).end() )
{
it_map->second += 1;
}
else
{
(it->second).insert(it_map, std::pair<std::string, int>(fromFile, 1));
}
}
else
m_listRef[nameRef][fromFile] = 1;
}
void Citation::makeList(string nameFile, string const citePattern, string const fromFile)
{
std::vector<char> vecChar(1,'"');
cleanString(nameFile, vecChar);
if (nameFile.size() < 4)
nameFile += ".tex";
else if (nameFile.substr(nameFile.size() - 4, 4) != ".tex")
nameFile += ".tex";
std::ifstream ifs(nameFile.c_str());
if (!ifs)
throw string("*** ERROR (from readFile) *** : cannot open file : ''" + nameFile + "''\nthis input is from ''" + fromFile + "''");
string nameFile_recursiveCalling;
string nameRef;
string readingLine;
size_t inputPos (0), citePos(0), citePos_sqBracket_opening(0), citePos_sqBracket_closing(0), length_sub_str(0);
if (m_firstCall) // To skip everything before \beging{document}
{
while (getline(ifs, readingLine))
{
if (readingLine == "\\begin{document}")
break;
}
m_firstCall = false;
}
while ((getline(ifs, readingLine)) && !(ifs.eof()))
{
while ((readingLine[0] == '%') && !(ifs.eof())) // to drop all comment lines
getline(ifs, readingLine);
while ( (inputPos = readingLine.find("input{", inputPos)) != string::npos) // recursive call
{
inputPos += 6;
length_sub_str = readingLine.find("}", inputPos) - inputPos;
nameFile_recursiveCalling = readingLine.substr(inputPos, length_sub_str);
this->makeList(nameFile_recursiveCalling, citePattern, nameFile);
}
while ( (citePos = readingLine.find(citePattern, citePos)) != string::npos) // Searches the string for the first occurrence of the sequence specified by its arguments.
{
citePos_sqBracket_opening = readingLine.find("[", citePos);
citePos_sqBracket_closing = readingLine.find("]", citePos);
citePos = readingLine.find("{", citePos) + 1;
if ((citePos_sqBracket_opening < citePos) && (citePos < citePos_sqBracket_closing))
citePos = readingLine.find("{", citePos_sqBracket_closing) + 1;
length_sub_str = readingLine.find("}", citePos) - citePos;
nameRef = readingLine.substr(citePos, length_sub_str);
if (!this->splitComa(nameRef, nameFile))
this->addRef(nameRef, nameFile);
citePos++;
}
inputPos = 0;
citePos = 0;
}
}
bool Citation::splitComa(std::string const &str, std::string const &fromFile)
{
bool comaDetected = false;
std::string sub_str;
size_t comaPos (0), oldComaPos (0), length_sub_str (0);
std::vector<char> charToDel(2); charToDel[0] = ' '; charToDel[1] = ',';
while ( (comaPos = str.find(",", comaPos)) != string::npos)
{
comaDetected = true;
length_sub_str = comaPos - oldComaPos;
sub_str = str.substr(oldComaPos, length_sub_str);
cleanString(sub_str, charToDel);
this->addRef(sub_str, fromFile);
oldComaPos = comaPos;
comaPos++;
}
if (comaDetected) // to get the ref after the last coma
{
length_sub_str = str.length() - oldComaPos;
sub_str = str.substr(oldComaPos, length_sub_str);
cleanString(sub_str, charToDel);
this->addRef(sub_str, fromFile);
}
return comaDetected;
}
void cleanString(std::string &str, std::vector<char> charToClean)
{
bool cleaner = true;
while (cleaner)
{
cleaner = false;
for (unsigned int i=0; i<charToClean.size(); i++)
{
while ( *(str.end() - 1) == charToClean[i])
{
str = str.substr(0,str.size() - 1);
cleaner = true;
}
while ( *(str.begin()) == charToClean[i])
{
str = str.substr(1, str.size());
cleaner = true;
}
}
}
}
void Citation::run(std::string nameFile)
{
bool success = false;
std::vector<std::string>::const_iterator citePattern_it = m_citePattern.cbegin();
try
{
for (; citePattern_it != m_citePattern.cend(); ++citePattern_it)
this->makeList(nameFile, *citePattern_it, nameFile);
cout << *this << endl;
}
catch (const string& strErrMakeList)
{
std::cerr << strErrMakeList << endl;
}
success = this->bibFile("/Users/mistral/Documents/mendeley_bib/library.bib");
if (!success)
{
cout << "******************************************" << endl;
cout << "Some references have been undetected" << endl;
cout << "Trying to find them in: `/Users/mistral/ownCloud/library/bibtexMendeley/otherBib.bib´" << endl;
success = this->bibFile("/Users/mistral/ownCloud/library/bibtexMendeley/otherBib.bib", std::ios::app);
if (!success)
{
cout << "Following references undetected : " << endl;
cout << *this << endl;
}
else
cout << "They have been found" << endl;
cout << "******************************************" << endl;
}
else
cout << "Every citation has been found" << endl;
}
bool Citation::bibFile(string const nameLibrary, std::ios::openmode mode)
{
std::ifstream ifs(nameLibrary.c_str());
if (!ifs)
throw string("*** ERROR (from Citation::bibFile) *** : cannot open file : ''" + nameLibrary + "'");
std::ofstream ofs(m_outputBib.c_str(), mode);
if (!ofs)
throw string("*** ERROR (from Citation::bibFile) *** : cannot create file : ''" + m_outputBib + "'");
map2d::iterator it;
string str_at, str;
size_t posBracket, posComa, length_substr; // because format is always @...{nameRef,
while (getline(ifs, str_at) && !m_listRef.empty())
{
if (str_at[0] == '@')
{
it = m_listRef.begin();
posBracket = str_at.find("{") + 1;
posComa = str_at.find(",");
length_substr = posComa - posBracket;
while (it != m_listRef.end() && str_at.substr(posBracket, length_substr) != it->first)
++it;
if (it != m_listRef.end())
{
ofs << str_at << endl;
while(str != "}")
{
getline(ifs, str);
ofs << str << endl;
}
m_listRef.erase(it);
}
it = m_listRef.begin();
str = "";
}
}
return (m_listRef.empty());
}
int main(int argc, char *argv[])
{
try
{
if (argc < 3)
throw string("*** ERROR (from main) : *** 2 args are required (+ optional) \n ./exe main.tex output.bib (citePattern1 citePattern2 ... citePatternZ)");
std::map<string, string> listRef;
cout << "you are running " << argv[0] << " with input latex file = " << argv[1] << " and output bibliography file " << argv[2] << endl;
try
{
std::vector<std::string> citingArgs;
if (argc == 3)
{
std::cout << "Using the default citing pattern: \\cite" << std::endl;
citingArgs.push_back("\\cite");
}
if (argc > 3)
{
for (unsigned int i = 3; i < argc; ++i)
citingArgs.push_back(argv[i]);
}
Citation cite(argv[2], citingArgs);
cite.run(argv[1]);
}
catch (const string& strErrCitation)
{
std::cerr << strErrCitation << endl;
}
}
catch (const string& strErrMain)
{
std::cerr << strErrMain << endl;
}
return 0;
}