forked from sarthou/reference_manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
referencer.cpp
91 lines (80 loc) · 1.55 KB
/
referencer.cpp
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
#include "reference_manager/referencer.h"
#include <cstdlib>
#include <iostream>
#include <vector>
Referencer::Referencer()
{
m_references = map<string, string>();
}
bool Referencer::add_reference(string id, string ref)
{
if(!is_referenced(ref))
{
for(map<string, string>::iterator it = m_references.begin(); it != m_references.end();)
{
if(it->second == id)
m_references.erase(it->first);
else
++it;
}
m_references.insert(make_pair(ref, id));
return true;
}
else
return false;
}
bool Referencer::remove_reference(string ref)
{
if(is_referenced(ref))
{
m_references.erase(ref);
return true;
}
else
return false;
}
string Referencer::list_references()
{
string list;
for(map<string, string>::iterator it = m_references.begin(); it != m_references.end(); ++it)
list += it->second + " <=> " + it->first + "\n";
return list;
}
bool Referencer::get_identifier(string ref, string& id)
{
if(is_referenced(ref))
{
id = m_references[ref];
return true;
}
else
{
id = "";
return false;
}
}
bool Referencer::get_reference(string id, string& ref)
{
ref = "";
for(map<string, string>::iterator it = m_references.begin(); it != m_references.end(); ++it)
{
if(it->second == id)
ref = it->first;
}
if(ref == "")
return false;
else
return true;
}
bool Referencer::is_referenced(string ref)
{
if(m_references.size())
{
if(m_references.count(ref) == 0)
return false;
else
return true;
}
else
return false;
}