Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileTypeImpl.cpp « parsers « colorer « src « Colorer-library « src « colorer - github.com/elfmz/far2l.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 026d7ff22dfd2d7f41565d56d2d1525762afc14f (plain)
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
#include <colorer/parsers/FileTypeImpl.h>
#include <colorer/unicode/UnicodeTools.h>

#include <memory>

FileTypeImpl::FileTypeImpl(HRCParserImpl* hrcParser): name(nullptr), group(nullptr), description(nullptr)
{
  this->hrcParser = hrcParser;
  protoLoaded = type_loaded = loadDone = load_broken = input_source_loading = false;
  isPackage = false;
  baseScheme = nullptr;
  inputSource = nullptr;
}

FileTypeImpl::~FileTypeImpl(){
  for(auto it : chooserVector){
    delete it;
  }
  chooserVector.clear();

  for(const auto& it: paramsHash){
    delete it.second;
  }
  paramsHash.clear();

  importVector.clear();
}

Scheme* FileTypeImpl::getBaseScheme() {
  if (!type_loaded) hrcParser->loadFileType(this);
  return baseScheme;
}

std::vector<SString> FileTypeImpl::enumParams() const {
  std::vector<SString> r;
  r.reserve(paramsHash.size());
  for (const auto & p : paramsHash)
  {
	  r.push_back(p.first);
  }
  return r;
}

const String* FileTypeImpl::getParamDescription(const String &name) const{
  auto tp = paramsHash.find(name);
  if (tp != paramsHash.end()) return tp->second->description.get();
  return nullptr;
}

const String *FileTypeImpl::getParamValue(const String &name) const{
  auto tp = paramsHash.find(name);
  if (tp != paramsHash.end()){
    if(tp->second->user_value) return tp->second->user_value.get();
    return tp->second->default_value.get();
  }
  return nullptr;
}

int FileTypeImpl::getParamValueInt(const String &name, int def) const{
  int val = def;
  UnicodeTools::getNumber(getParamValue(name), &val);
  return val;
}

const String* FileTypeImpl::getParamDefaultValue(const String &name) const{
  auto tp = paramsHash.find(name);
  if (tp !=paramsHash.end()) {
    return tp->second->default_value.get();
  }
  return nullptr;
}

const String* FileTypeImpl::getParamUserValue(const String &name) const{
  auto tp = paramsHash.find(name);
  if (tp !=paramsHash.end()) {
    return tp->second->user_value.get();
  }
  return nullptr;
}

TypeParameter* FileTypeImpl::addParam(const String *name){
  auto* tp = new TypeParameter;
  tp->name.reset(new SString(name));
  std::pair<SString, TypeParameter*> pp(name, tp);
  paramsHash.emplace(pp);
  return tp;
}

void FileTypeImpl::setParamValue(const String &name, const String *value){
  auto tp = paramsHash.find(name);
  if (tp != paramsHash.end()) {
    if (value) {
      tp->second->user_value.reset(new SString(value));
    }
    else{
      tp->second->user_value.reset();
    }
  }
}

void FileTypeImpl::setParamDefaultValue(const String &name, const String *value){
  auto tp = paramsHash.find(name);
  if (tp != paramsHash.end()) {
    tp->second->default_value.reset(new SString(value));
  }
}

void FileTypeImpl::setParamUserValue(const String &name, const String *value){
  setParamValue(name,value);
}

void FileTypeImpl::setParamDescription(const String &name, const String *value){
  auto tp = paramsHash.find(name);
  if (tp != paramsHash.end()) {
    tp->second->description.reset(new SString(value));
  }
}

void FileTypeImpl::removeParamValue(const String &name){
  paramsHash.erase(name);
}

size_t FileTypeImpl::getParamCount() const{
  return paramsHash.size();
}

size_t FileTypeImpl::getParamUserValueCount() const{
  size_t count=0;
  for (const auto & it : paramsHash){
    if (it.second->user_value) count++;
  }
  return count;
}

double FileTypeImpl::getPriority(const String *fileName, const String *fileContent) const{
  SMatches match;
  double cur_prior = 0;
  for(auto ftc : chooserVector){
    if (fileName != nullptr && ftc->isFileName() && ftc->getRE()->parse(fileName, &match))
      cur_prior += ftc->getPriority();
    if (fileContent != nullptr && ftc->isFileContent() && ftc->getRE()->parse(fileContent, &match))
      cur_prior += ftc->getPriority();
  }
  return cur_prior;
}