openMSX
Completer.cc
Go to the documentation of this file.
1 #include "Completer.hh"
2 #include "Interpreter.hh"
3 #include "InterpreterOutput.hh"
4 #include "FileContext.hh"
5 #include "FileOperations.hh"
6 #include "ReadDir.hh"
7 #include "ranges.hh"
8 #include "stl.hh"
9 #include "strCat.hh"
10 #include "stringsp.hh"
11 #include "TclObject.hh"
12 #include "utf8_unchecked.hh"
13 #include "view.hh"
14 
15 using std::vector;
16 using std::string;
17 using std::string_view;
18 
19 namespace openmsx {
20 
21 Completer::Completer(string_view name_)
22  : name(std::string(name_)) // TODO take std::string parameter instead and move()
23 {
24 }
25 
26 static bool formatHelper(const vector<string_view>& input, size_t columnLimit,
27  vector<string>& result)
28 {
29  size_t column = 0;
30  auto it = begin(input);
31  do {
32  size_t maxcolumn = column;
33  for (size_t i = 0; (i < result.size()) && (it != end(input));
34  ++i, ++it) {
35  auto curSize = utf8::unchecked::size(result[i]);
36  strAppend(result[i], spaces(column - curSize), *it);
37  maxcolumn = std::max(maxcolumn,
38  utf8::unchecked::size(result[i]));
39  if (maxcolumn > columnLimit) return false;
40  }
41  column = maxcolumn + 2;
42  } while (it != end(input));
43  return true;
44 }
45 
46 static vector<string> format(const vector<string_view>& input, size_t columnLimit)
47 {
48  vector<string> result;
49  for (size_t lines = 1; lines < input.size(); ++lines) {
50  result.assign(lines, string());
51  if (formatHelper(input, columnLimit, result)) {
52  return result;
53  }
54  }
55  append(result, input);
56  return result;
57 }
58 
59 vector<string> Completer::formatListInColumns(const vector<string_view>& input)
60 {
61  return format(input, output->getOutputColumns() - 1);
62 }
63 
64 bool Completer::equalHead(string_view s1, string_view s2, bool caseSensitive)
65 {
66  if (s2.size() < s1.size()) return false;
67  if (caseSensitive) {
68  return memcmp(s1.data(), s2.data(), s1.size()) == 0;
69  } else {
70  return strncasecmp(s1.data(), s2.data(), s1.size()) == 0;
71  }
72 }
73 
74 bool Completer::completeImpl(string& str, vector<string_view> matches,
75  bool caseSensitive)
76 {
77  assert(ranges::all_of(matches, [&](auto& m) {
78  return equalHead(str, m, caseSensitive);
79  }));
80 
81  if (matches.empty()) {
82  // no matching values
83  return false;
84  }
85  if (matches.size() == 1) {
86  // only one match
87  str = matches.front();
88  return true;
89  }
90 
91  // Sort and remove duplicates.
92  // For efficiency it's best if the list doesn't contain duplicates to
93  // start with. Though sometimes this is hard to avoid. E.g. when doing
94  // filename completion + some extra allowed strings and one of these
95  // extra strings is the same as one of the filenames.
96  ranges::sort(matches);
97  matches.erase(ranges::unique(matches), end(matches));
98 
99  bool expanded = false;
100  while (true) {
101  auto it = begin(matches);
102  if (str.size() == it->size()) {
103  // match is as long as first word
104  goto out; // TODO rewrite this
105  }
106  // expand with one char and check all strings
107  auto b = begin(*it);
108  auto e = b + str.size();
109  utf8::unchecked::next(e); // one more utf8 char
110  string_view string2(&*b, e - b);
111  for (; it != end(matches); ++it) {
112  if (!equalHead(string2, *it, caseSensitive)) {
113  goto out; // TODO rewrite this
114  }
115  }
116  // no conflict found
117  str = string2;
118  expanded = true;
119  }
120  out:
121  if (!expanded && output) {
122  // print all possibilities
123  for (auto& line : formatListInColumns(matches)) {
124  output->output(line);
125  }
126  }
127  return false;
128 }
129 
130 void Completer::completeFileName(vector<string>& tokens,
131  const FileContext& context)
132 {
133  completeFileNameImpl(tokens, context, vector<string_view>());
134 }
135 
136 void Completer::completeFileNameImpl(vector<string>& tokens,
137  const FileContext& context,
138  vector<string_view> matches)
139 {
140  string& filename = tokens.back();
141  filename = FileOperations::expandTilde(filename);
142  filename = FileOperations::expandCurrentDirFromDrive(filename);
143  string_view dirname1 = FileOperations::getDirName(filename);
144 
145  vector<string> paths;
146  if (FileOperations::isAbsolutePath(filename)) {
147  paths.emplace_back();
148  } else {
149  paths = context.getPaths();
150  }
151 
152  vector<string> filenames;
153  for (auto& p : paths) {
154  string dirname = FileOperations::join(p, dirname1);
156  while (dirent* de = dir.getEntry()) {
157  string name = FileOperations::join(dirname, de->d_name);
158  if (FileOperations::exists(name)) {
159  string nm = FileOperations::join(dirname1, de->d_name);
160  if (FileOperations::isDirectory(name)) {
161  nm += '/';
162  }
164  if (equalHead(filename, nm, true)) {
165  filenames.push_back(nm);
166  }
167  }
168  }
169  }
170  append(matches, filenames);
171  bool t = completeImpl(filename, matches, true);
172  if (t && !filename.empty() && (filename.back() != '/')) {
173  // completed filename, start new token
174  tokens.emplace_back();
175  }
176 }
177 
178 void Completer::checkNumArgs(span<const TclObject> tokens, unsigned exactly, const char* errMessage) const
179 {
180  checkNumArgs(tokens, exactly, Prefix{exactly - 1}, errMessage);
181 }
182 
183 void Completer::checkNumArgs(span<const TclObject> tokens, AtLeast atLeast, const char* errMessage) const
184 {
185  checkNumArgs(tokens, atLeast, Prefix{atLeast.min - 1}, errMessage);
186 }
187 
188 void Completer::checkNumArgs(span<const TclObject> tokens, Between between, const char* errMessage) const
189 {
190  checkNumArgs(tokens, between, Prefix{between.min - 1}, errMessage);
191 }
192 
193 void Completer::checkNumArgs(span<const TclObject> tokens, unsigned exactly, Prefix prefix, const char* errMessage) const
194 {
195  if (tokens.size() == exactly) return;
196  getInterpreter().wrongNumArgs(prefix.n, tokens, errMessage);
197 }
198 
199 void Completer::checkNumArgs(span<const TclObject> tokens, AtLeast atLeast, Prefix prefix, const char* errMessage) const
200 {
201  if (tokens.size() >= atLeast.min) return;
202  getInterpreter().wrongNumArgs(prefix.n, tokens, errMessage);
203 }
204 
205 void Completer::checkNumArgs(span<const TclObject> tokens, Between between, Prefix prefix, const char* errMessage) const
206 {
207  if (tokens.size() >= between.min && tokens.size() <= between.max) return;
208  getInterpreter().wrongNumArgs(prefix.n, tokens, errMessage);
209 }
210 
211 } // namespace openmsx
bool isAbsolutePath(string_view path)
auto unique(ForwardRange &&range)
Definition: ranges.hh:137
Definition: span.hh:34
void checkNumArgs(span< const TclObject > tokens, unsigned exactly, const char *errMessage) const
Definition: Completer.cc:178
uint32_t next(octet_iterator &it)
STL namespace.
vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:292
size_t size(std::string_view utf8)
void strAppend(std::string &result, Ts &&...ts)
Definition: strCat.hh:644
static void completeFileName(std::vector< std::string > &tokens, const FileContext &context, const RANGE &extra)
Definition: Completer.hh:139
static std::vector< std::string > formatListInColumns(const std::vector< std::string_view > &input)
Definition: Completer.cc:59
void append(Result &)
Definition: stl.hh:340
void wrongNumArgs(unsigned argc, span< const TclObject > tokens, const char *message)
Definition: Interpreter.cc:452
std::unique_ptr< Context > context
Definition: GLContext.cc:9
constexpr const char *const filename
virtual void output(std::string_view text)=0
void sort(RandomAccessRange &&range)
Definition: ranges.hh:35
string getNativePath(string_view path)
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
virtual unsigned getOutputColumns() const =0
Completer(std::string_view name)
Definition: Completer.cc:21
string expandCurrentDirFromDrive(string_view path)
bool exists(string_view filename)
std::vector< std::string > getPaths() const
Definition: FileContext.cc:104
bool isDirectory(const Stat &st)
bool all_of(InputRange &&range, UnaryPredicate pred)
Definition: ranges.hh:119
string getConventionalPath(string_view path)
string_view getDirName(string_view path)
string expandTilde(string_view path)
Simple wrapper around openmdir() / readdir() / closedir() functions.
Definition: ReadDir.hh:15
strCatImpl::ConcatSpaces spaces(size_t n)
Definition: strCat.hh:675
string join(string_view part1, string_view part2)
virtual Interpreter & getInterpreter() const =0
struct dirent * getEntry()
Get directory entry for next file.
Definition: ReadDir.cc:17
TclObject t