-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdependency.cpp
140 lines (128 loc) · 4.02 KB
/
dependency.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
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
//@ {
//@ "targets":[{"name":"dependency.o","type":"object"}]
//@ }
#include "dependency.hpp"
#include "resourceobject.hpp"
#include "stringkey.hpp"
#include "errormessage.hpp"
#include "exceptionhandler.hpp"
#include "variant.hpp"
#include "pathutils.hpp"
#include "writebuffer.hpp"
#include "stdstream.hpp"
#include <cstdlib>
#include <cstring>
using namespace Maike;
static Dependency::Relation relation(const char* string)
{
auto key=Stringkey(string);
if(key==Stringkey("implementation"))
{return Dependency::Relation::IMPLEMENTATION;}
if(key==Stringkey("external"))
{return Dependency::Relation::EXTERNAL;}
if(key==Stringkey("internal"))
{return Dependency::Relation::INTERNAL;}
if(key==Stringkey("leaf"))
{return Dependency::Relation::LEAF;}
if(key==Stringkey("generated"))
{
WriteBuffer(StdStream::error()).write("Warning: The relation `generated` is deprecated. "
"Use `misc` instead.\n");
return Dependency::Relation::MISC;
}
if(key==Stringkey("include_generated"))
{
WriteBuffer(StdStream::error()).write("Warning: The relation `include_generated` is deprecated. "
"Use `include_extra` instead.\n");
return Dependency::Relation::INCLUDE_EXTRA;
}
if(key==Stringkey("include_extra"))
{return Dependency::Relation::INCLUDE_EXTRA;}
if(key==Stringkey("file"))
{
WriteBuffer(StdStream::error()).write("Warning: The relation `file` is deprecated. "
"Use `misc` instead.\n");
return Dependency::Relation::MISC;
}
if(key==Stringkey("include"))
{return Dependency::Relation::INCLUDE;}
if(key==Stringkey("tool"))
{return Dependency::Relation::TOOL;}
if(key==Stringkey("misc"))
{return Dependency::Relation::MISC;}
if(key==Stringkey("runtime"))
{return Dependency::Relation::RUNTIME;}
if(key==Stringkey("external_resource"))
{return Dependency::Relation::EXTERNAL_RESOURCE;}
exceptionRaise(ErrorMessage("Unknown relation type #0;",{string}));
}
static const char* relation(Dependency::Relation rel)
{
switch(rel)
{
case Dependency::Relation::IMPLEMENTATION:
return "implementation";
case Dependency::Relation::EXTERNAL:
return "external";
case Dependency::Relation::INTERNAL:
return "internal";
case Dependency::Relation::LEAF:
return "leaf";
case Dependency::Relation::MISC:
return "misc";
case Dependency::Relation::INCLUDE:
return "include";
case Dependency::Relation::TOOL:
return "tool";
case Dependency::Relation::INCLUDE_EXTRA:
return "include_extra";
case Dependency::Relation::RUNTIME:
return "runtime";
case Dependency::Relation::EXTERNAL_RESOURCE:
return "external_resource";
}
return nullptr;
}
const char* Dependency::relationGetStr() const
{return relation(relationGet());}
Dependency::Dependency(const ResourceObject& obj,const char* in_dir,const char* root):
m_name(nullptr),r_target(nullptr)
,m_rel(relation(static_cast<const char*>(obj.objectGet("rel"))))
{
assert(m_rel!=Relation::INTERNAL && m_rel!=Relation::LEAF);
// EXTERNAL, TOOL, RUNTIME, and EXTERNAL_RESOURCE refers to stuff outside the
// source repo. This means that current directory should not be used in this case.
if(m_rel==Relation::EXTERNAL || m_rel==Relation::TOOL || m_rel==Relation::RUNTIME
|| m_rel==Relation::EXTERNAL_RESOURCE)
{nameSet(rootStrip(static_cast<const char*>(obj.objectGet("ref")),root).c_str());}
else
{
auto name_temp=rootStrip(dircat(in_dir,static_cast<const char*>(obj.objectGet("ref"))),root);
nameSet(name_temp.c_str(),name_temp.size());
}
}
void Dependency::dump(ResourceObject& dependency) const
{
dependency.objectSet("ref",dependency.create(nameGet()))
.objectSet("rel",dependency.create(relation(relationGet())));
}
void Dependency::nameSet(const char* name)
{
nameSet(name,strlen(name));
}
void Dependency::nameSet(const char* name,size_t size)
{
auto N=size + 1;
m_name=static_cast<char*>( realloc(m_name,N*sizeof(N)) );
if(m_name==NULL)
{exceptionRaise(ErrorMessage("Out of memory",{}));}
memcpy(m_name,name,N);
}
void Dependency::nameFree() noexcept
{
if(m_name!=nullptr)
{
free(m_name);
m_name=nullptr;
}
}