Skip to content

Commit e7db3e0

Browse files
committed
Move canonicalize_path into its own file
I plan on reimplementing canonicalize_path, and the reimplementation will be really long and complicated. Move canonicalize_path and its related class into its own .cpp file. This commit should not change behavior.
1 parent 721cb2b commit e7db3e0

File tree

3 files changed

+103
-60
lines changed

3 files changed

+103
-60
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ quick_lint_js_add_library(
4040
error-collector.cpp
4141
error-formatter.cpp
4242
error-list.cpp
43+
file-canonical.cpp
4344
file-handle.cpp
4445
file-path.cpp
4546
file.cpp

src/file-canonical.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (C) 2020 Matthew Glazar
2+
// See end of file for extended copyright information.
3+
4+
#include <algorithm>
5+
#include <cerrno>
6+
#include <cstddef>
7+
#include <cstdio>
8+
#include <cstdlib>
9+
#include <cstring>
10+
#include <quick-lint-js/assert.h>
11+
#include <quick-lint-js/char8.h>
12+
#include <quick-lint-js/file.h>
13+
#include <quick-lint-js/have.h>
14+
#include <string>
15+
16+
#if QLJS_HAVE_STD_FILESYSTEM
17+
#include <filesystem>
18+
#endif
19+
20+
#if QLJS_HAVE_SYS_STAT_H
21+
#include <sys/stat.h>
22+
#endif
23+
24+
namespace quick_lint_js {
25+
canonical_path_result::canonical_path_result() {}
26+
27+
canonical_path_result::canonical_path_result(const char *path) : path_(path) {}
28+
29+
std::string_view canonical_path_result::path() const &noexcept {
30+
QLJS_ASSERT(this->ok());
31+
return this->path_;
32+
}
33+
34+
std::string &&canonical_path_result::path() && noexcept {
35+
QLJS_ASSERT(this->ok());
36+
return std::move(this->path_);
37+
}
38+
39+
const char *canonical_path_result::c_str() const noexcept {
40+
QLJS_ASSERT(this->ok());
41+
return this->path_.c_str();
42+
}
43+
44+
std::string &&canonical_path_result::error() && noexcept {
45+
QLJS_ASSERT(!this->ok());
46+
return std::move(this->error_);
47+
}
48+
49+
canonical_path_result canonical_path_result::failure(std::string &&error) {
50+
canonical_path_result result;
51+
result.error_ = std::move(error);
52+
QLJS_ASSERT(!result.ok());
53+
return result;
54+
}
55+
56+
canonical_path_result canonicalize_path(const char *path) {
57+
#if QLJS_HAVE_STD_FILESYSTEM
58+
std::error_code error;
59+
std::filesystem::path canonical = std::filesystem::canonical(path, error);
60+
if (error) {
61+
return canonical_path_result::failure(
62+
std::string("failed to canonicalize path ") + path + ": " +
63+
error.message());
64+
}
65+
return canonical_path_result(canonical.string().c_str());
66+
#elif QLJS_HAVE_REALPATH
67+
char *allocated_canonical = ::realpath(path, nullptr);
68+
if (!allocated_canonical) {
69+
return canonical_path_result::failure(
70+
std::string("failed to canonicalize path ") + path + ": " +
71+
std::strerror(errno));
72+
}
73+
canonical_path_result result(allocated_canonical);
74+
std::free(allocated_canonical);
75+
return result;
76+
#else
77+
#error "Unsupported platform"
78+
#endif
79+
}
80+
81+
canonical_path_result canonicalize_path(const std::string &path) {
82+
return canonicalize_path(path.c_str());
83+
}
84+
}
85+
86+
// quick-lint-js finds bugs in JavaScript programs.
87+
// Copyright (C) 2020 Matthew Glazar
88+
//
89+
// This file is part of quick-lint-js.
90+
//
91+
// quick-lint-js is free software: you can redistribute it and/or modify
92+
// it under the terms of the GNU General Public License as published by
93+
// the Free Software Foundation, either version 3 of the License, or
94+
// (at your option) any later version.
95+
//
96+
// quick-lint-js is distributed in the hope that it will be useful,
97+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
98+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
99+
// GNU General Public License for more details.
100+
//
101+
// You should have received a copy of the GNU General Public License
102+
// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>.

src/file.cpp

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -276,66 +276,6 @@ void write_file(const char *path, string8_view content) {
276276

277277
std::fclose(file);
278278
}
279-
280-
canonical_path_result::canonical_path_result() {}
281-
282-
canonical_path_result::canonical_path_result(const char *path) : path_(path) {}
283-
284-
std::string_view canonical_path_result::path() const &noexcept {
285-
QLJS_ASSERT(this->ok());
286-
return this->path_;
287-
}
288-
289-
std::string &&canonical_path_result::path() && noexcept {
290-
QLJS_ASSERT(this->ok());
291-
return std::move(this->path_);
292-
}
293-
294-
const char *canonical_path_result::c_str() const noexcept {
295-
QLJS_ASSERT(this->ok());
296-
return this->path_.c_str();
297-
}
298-
299-
std::string &&canonical_path_result::error() && noexcept {
300-
QLJS_ASSERT(!this->ok());
301-
return std::move(this->error_);
302-
}
303-
304-
canonical_path_result canonical_path_result::failure(std::string &&error) {
305-
canonical_path_result result;
306-
result.error_ = std::move(error);
307-
QLJS_ASSERT(!result.ok());
308-
return result;
309-
}
310-
311-
canonical_path_result canonicalize_path(const char *path) {
312-
#if QLJS_HAVE_STD_FILESYSTEM
313-
std::error_code error;
314-
std::filesystem::path canonical = std::filesystem::canonical(path, error);
315-
if (error) {
316-
return canonical_path_result::failure(
317-
std::string("failed to canonicalize path ") + path + ": " +
318-
error.message());
319-
}
320-
return canonical_path_result(canonical.string().c_str());
321-
#elif QLJS_HAVE_REALPATH
322-
char *allocated_canonical = ::realpath(path, nullptr);
323-
if (!allocated_canonical) {
324-
return canonical_path_result::failure(
325-
std::string("failed to canonicalize path ") + path + ": " +
326-
std::strerror(errno));
327-
}
328-
canonical_path_result result(allocated_canonical);
329-
std::free(allocated_canonical);
330-
return result;
331-
#else
332-
#error "Unsupported platform"
333-
#endif
334-
}
335-
336-
canonical_path_result canonicalize_path(const std::string &path) {
337-
return canonicalize_path(path.c_str());
338-
}
339279
}
340280

341281
// quick-lint-js finds bugs in JavaScript programs.

0 commit comments

Comments
 (0)