-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectConfig.cpp
More file actions
166 lines (145 loc) · 3.51 KB
/
ProjectConfig.cpp
File metadata and controls
166 lines (145 loc) · 3.51 KB
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
#include "ProjectConfig.h"
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
static string trim( const string &s )
{
size_t start = s.find_first_not_of( " \t\r\n" );
if ( start == string::npos )
return "";
size_t end = s.find_last_not_of( " \t\r\n" );
return s.substr( start, end - start + 1 );
}
static string unquote( const string &s )
{
string t = trim( s );
if ( t.size() >= 2 && t.front() == '"' && t.back() == '"' )
return t.substr( 1, t.size() - 2 );
return t;
}
static void parseInlineTable( const string &name, const string &body, vector<Dependency> &deps )
{
Dependency dep;
dep.name = name;
// Split on commas, parse each key = "value" pair
string content = body;
while ( !content.empty() )
{
size_t comma = string::npos;
bool inQuote = false;
for ( size_t i = 0; i < content.size(); i++ )
{
if ( content[i] == '"' )
inQuote = !inQuote;
else if ( content[i] == ',' && !inQuote )
{
comma = i;
break;
}
}
string pair;
if ( comma != string::npos )
{
pair = content.substr( 0, comma );
content = content.substr( comma + 1 );
}
else
{
pair = content;
content.clear();
}
size_t eq = pair.find( '=' );
if ( eq == string::npos )
continue;
string key = trim( pair.substr( 0, eq ) );
string value = unquote( pair.substr( eq + 1 ) );
if ( key == "path" )
dep.path = value;
else if ( key == "git" )
dep.gitUrl = value;
else if ( key == "tag" )
dep.tag = value;
}
deps.push_back( dep );
}
ProjectConfig *ProjectConfig::loadFromFile( const string &path )
{
ifstream file( path );
if ( !file.is_open() )
return nullptr;
ProjectConfig *config = new ProjectConfig();
string currentSection;
string line;
int lineNum = 0;
while ( getline( file, line ) )
{
lineNum++;
// Strip comments
{
bool inQuote = false;
for ( size_t i = 0; i < line.size(); i++ )
{
if ( line[i] == '"' )
inQuote = !inQuote;
else if ( line[i] == '#' && !inQuote )
{
line = line.substr( 0, i );
break;
}
}
}
line = trim( line );
if ( line.empty() )
continue;
// Section header
if ( line.front() == '[' && line.back() == ']' )
{
currentSection = trim( line.substr( 1, line.size() - 2 ) );
continue;
}
// Key = value
size_t eq = line.find( '=' );
if ( eq == string::npos )
{
cerr << path << ":" << lineNum << ": parse error: expected key = value" << endl;
continue;
}
string key = trim( line.substr( 0, eq ) );
string value = trim( line.substr( eq + 1 ) );
if ( currentSection == "project" )
{
string val = unquote( value );
if ( key == "name" )
config->mName = val;
else if ( key == "version" )
config->mVersion = val;
else if ( key == "type" )
config->mType = val;
}
else if ( currentSection == "deps" )
{
// Expect inline table: name = { key = "val", ... }
size_t braceOpen = value.find( '{' );
size_t braceClose = value.rfind( '}' );
if ( braceOpen != string::npos && braceClose != string::npos && braceClose > braceOpen )
{
string body = value.substr( braceOpen + 1, braceClose - braceOpen - 1 );
parseInlineTable( key, body, config->mDeps );
}
else
{
cerr << path << ":" << lineNum << ": parse error: expected inline table for dependency '" << key << "'" << endl;
}
}
}
return config;
}
ProjectConfig *ProjectConfig::loadFromDirectory( const string &dir )
{
string path = dir;
if ( !path.empty() && path.back() != '/' )
path += '/';
path += "blang.toml";
return loadFromFile( path );
}