-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathEditorconfig.cpp
More file actions
171 lines (155 loc) · 4.26 KB
/
Editorconfig.cpp
File metadata and controls
171 lines (155 loc) · 4.26 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
167
168
169
170
171
/*
* Copyright 2018 Kacper Kasper <[email protected]>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "Editorconfig.h"
#include <algorithm>
#include <fstream>
#include <regex>
#include <sstream>
#include <string>
#include <Directory.h>
#include <Entry.h>
#include <String.h>
bool
Editorconfig::Find(BPath* filePath, BPath* editorconfigPath)
{
if(filePath == nullptr) return false;
bool found = false;
BPath parentPath(*filePath);
BDirectory parentDir;
BEntry editorconfigFile;
do {
parentPath.GetParent(&parentPath);
parentDir.SetTo(parentPath.Path());
editorconfigFile.SetTo(&parentDir, ".editorconfig");
found = editorconfigFile.Exists();
} while(!found && !parentDir.IsRootDirectory());
if(found && editorconfigPath != nullptr) {
editorconfigFile.GetPath(editorconfigPath);
}
return found;
}
bool
Editorconfig::Parse(const char* filename, BMessage* propertiesDict)
{
if(propertiesDict == nullptr) return false;
std::ifstream file(filename, std::ifstream::in);
if(!file.is_open()) return false;
std::string currentSectionName;
BMessage currentSection;
std::string line;
do {
std::getline(file, line);
if(line.empty() || line[0] == '#' || line[0] == ';') continue;
if(line[0] == '[' && line[line.size() - 1] == ']') {
// section
if(!currentSectionName.empty() && !currentSection.IsEmpty()) {
propertiesDict->AddMessage(currentSectionName.c_str(), ¤tSection);
currentSection.MakeEmpty();
}
currentSectionName = line.substr(1, line.size() - 2);
} else {
// property
std::string name, value;
line.erase(std::remove_if(line.begin(), line.end(), [](char x) { return std::isspace(x); }), line.end());
std::istringstream linestream(line);
std::getline(linestream, name, '=');
std::getline(linestream, value, '=');
currentSection.AddString(name.c_str(), value.c_str());
}
} while(!file.eof());
if(!currentSectionName.empty() && !currentSection.IsEmpty()) {
propertiesDict->AddMessage(currentSectionName.c_str(), ¤tSection);
}
file.close();
return true;
}
void
Editorconfig::MatchFilename(const char* filename, const BMessage* allProperties,
BMessage* properties)
{
if(allProperties == nullptr) return;
char* name;
uint32 type;
int32 count;
for(int32 i = 0;
allProperties->GetInfo(B_MESSAGE_TYPE, i, &name, &type, &count) == B_OK;
i++) {
BString regexStr(name);
bool dirSpecific = false;
int32 slashIndex = regexStr.FindFirst("/");
if(slashIndex != B_ERROR && slashIndex > 0 && regexStr[slashIndex - 1] != '\\')
dirSpecific = true;
int32 inBraceCount = 0;
int32 c = 0;
while(c < regexStr.Length()) {
if(regexStr[c] == '*') {
if(c > 0 && regexStr[c - 1] == '\\') {
c++;
continue;
}
// **
if(c < regexStr.Length() - 1 && regexStr[c + 1] == '*') {
regexStr.Replace("**", "(.+)", 1, c);
c += strlen("(.+)");
} else {
BString s;
if(dirSpecific) s = "([^/]+)";
else s = "(.+)";
regexStr.Replace("*", s, 1, c);
c += s.Length();
}
continue;
} else if(regexStr[c] == '!') {
// FIXME: only if in []?
if(c > 0 && regexStr[c - 1] == '\\') {
c++;
continue;
}
regexStr.Replace("!", "^", 1, c);
} else if(regexStr[c] == '.') {
regexStr.Replace(".", "\\.", 1, c);
c += 2;
continue;
}
if(regexStr[c] == '{') {
if(c > 0 && regexStr[c - 1] == '\\') {
c++;
continue;
}
inBraceCount++;
regexStr.Replace("{", "(", 1, c);
}
if(inBraceCount > 0) {
if(c > 0 && regexStr[c - 1] == '\\') {
c++;
continue;
}
if(regexStr[c] == ',')
regexStr.Replace(",", "|", 1, c);
if(regexStr[c] == '}') {
inBraceCount--;
regexStr.Replace("}", ")", 1, c);
}
}
c++;
}
if(inBraceCount != 0) return;
std::regex expr(regexStr.String());
if(properties != nullptr && std::regex_match(filename, expr)) {
BMessage globProperties;
if(allProperties->FindMessage(name, &globProperties) == B_OK) {
char* stringName;
for(int32 j = 0;
globProperties.GetInfo(B_STRING_TYPE, j, &stringName, &type, &count) == B_OK;
j++) {
BString value;
globProperties.FindString(stringName, &value);
properties->RemoveName(stringName);
properties->AddString(stringName, value);
}
}
}
}
}