Mercurial > minori
annotate dep/anitomy/anitomy/anitomy.cpp @ 391:c3f717b7321b
filesystem: only iterate over the list once when erasing deleted files
:)
| author | Paper <paper@tflc.us> |
|---|---|
| date | Fri, 07 Nov 2025 07:09:31 -0500 |
| parents | a0aa8c8c4307 |
| children |
| rev | line source |
|---|---|
| 9 | 1 /* |
| 2 ** Copyright (c) 2014-2017, Eren Okka | |
| 3 ** | |
| 4 ** This Source Code Form is subject to the terms of the Mozilla Public | |
| 5 ** License, v. 2.0. If a copy of the MPL was not distributed with this | |
| 6 ** file, You can obtain one at https://mozilla.org/MPL/2.0/. | |
| 7 */ | |
| 8 | |
|
347
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
9
diff
changeset
|
9 #include <codecvt> |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
9
diff
changeset
|
10 #include <locale> |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
9
diff
changeset
|
11 |
| 9 | 12 #include "anitomy.h" |
| 13 #include "keyword.h" | |
| 14 #include "parser.h" | |
| 15 #include "string.h" | |
| 16 #include "tokenizer.h" | |
| 17 | |
| 18 namespace anitomy { | |
| 19 | |
| 20 bool Anitomy::Parse(string_t filename) { | |
| 21 elements_.clear(); | |
| 22 tokens_.clear(); | |
| 23 | |
| 24 if (options_.parse_file_extension) { | |
| 25 string_t extension; | |
| 26 if (RemoveExtensionFromFilename(filename, extension)) | |
| 27 elements_.insert(kElementFileExtension, extension); | |
| 28 } | |
| 29 | |
| 30 if (!options_.ignored_strings.empty()) | |
| 31 RemoveIgnoredStrings(filename); | |
| 32 | |
| 33 if (filename.empty()) | |
| 34 return false; | |
| 35 elements_.insert(kElementFileName, filename); | |
| 36 | |
| 37 Tokenizer tokenizer(filename, elements_, options_, tokens_); | |
| 38 if (!tokenizer.Tokenize()) | |
| 39 return false; | |
| 40 | |
| 41 Parser parser(elements_, options_, tokens_); | |
| 42 if (!parser.Parse()) | |
| 43 return false; | |
| 44 | |
| 45 return true; | |
| 46 } | |
| 47 | |
|
347
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
9
diff
changeset
|
48 /* assumes UTF-8 */ |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
9
diff
changeset
|
49 bool Anitomy::Parse(const std::string& filename) { |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
9
diff
changeset
|
50 static std::wstring_convert<std::codecvt_utf8_utf16<char_t>, char_t> ucs4conv; |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
9
diff
changeset
|
51 return Parse(ucs4conv.from_bytes(filename)); |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
9
diff
changeset
|
52 } |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
9
diff
changeset
|
53 |
| 9 | 54 //////////////////////////////////////////////////////////////////////////////// |
| 55 | |
| 56 bool Anitomy::RemoveExtensionFromFilename(string_t& filename, string_t& extension) const { | |
| 57 const size_t position = filename.find_last_of(L'.'); | |
| 58 | |
| 59 if (position == string_t::npos) | |
| 60 return false; | |
| 61 | |
| 62 extension = filename.substr(position + 1); | |
| 63 | |
| 64 const size_t max_length = 4; | |
| 65 if (extension.length() > max_length) | |
| 66 return false; | |
| 67 | |
| 68 if (!IsAlphanumericString(extension)) | |
| 69 return false; | |
| 70 | |
| 71 auto keyword = keyword_manager.Normalize(extension); | |
| 72 if (!keyword_manager.Find(kElementFileExtension, keyword)) | |
| 73 return false; | |
| 74 | |
| 75 filename.resize(position); | |
| 76 | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 void Anitomy::RemoveIgnoredStrings(string_t& filename) const { | |
| 81 for (const auto& str : options_.ignored_strings) { | |
| 82 EraseString(filename, str); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 //////////////////////////////////////////////////////////////////////////////// | |
| 87 | |
| 88 Elements& Anitomy::elements() { | |
| 89 return elements_; | |
| 90 } | |
| 91 | |
| 92 Options& Anitomy::options() { | |
| 93 return options_; | |
| 94 } | |
| 95 | |
| 96 const token_container_t& Anitomy::tokens() const { | |
| 97 return tokens_; | |
| 98 } | |
| 99 | |
| 100 } // namespace anitomy |
