| 234 | } |
| 235 | |
| 236 | static bool hasEmacsCppMarker(const char* path) |
| 237 | { |
| 238 | // TODO: identify is called three times for each file |
| 239 | // Preprocessor::loadFiles() -> createDUI() |
| 240 | // Preprocessor::preprocess() -> createDUI() |
| 241 | // TokenList::createTokens() -> TokenList::determineCppC() |
| 242 | #ifdef LOG_EMACS_MARKER |
| 243 | std::cout << path << '\n'; |
| 244 | #endif |
| 245 | |
| 246 | FILE *fp = fopen(path, "rt"); |
| 247 | if (!fp) |
| 248 | return false; |
| 249 | std::string buf(128, '\0'); |
| 250 | { |
| 251 | #if __cplusplus >= 201703L |
| 252 | // C++17 provides an overload with non-const data() |
| 253 | #define CONST_CAST(x) (x) |
| 254 | #else |
| 255 | #define CONST_CAST(x) const_cast<char*>(x) |
| 256 | #endif |
| 257 | // TODO: read the whole first line only |
| 258 | const char * const res = fgets(CONST_CAST(buf.data()), buf.size(), fp); |
| 259 | #undef CONST_CAST |
| 260 | fclose(fp); |
| 261 | fp = nullptr; |
| 262 | if (!res) |
| 263 | return false; // failed to read file |
| 264 | } |
| 265 | // TODO: replace with regular expression |
| 266 | const auto pos1 = buf.find("-*-"); |
| 267 | if (pos1 == std::string::npos) |
| 268 | return false; // no start marker |
| 269 | const auto pos_nl = buf.find_first_of("\r\n"); |
| 270 | if (pos_nl != std::string::npos && (pos_nl < pos1)) { |
| 271 | #ifdef LOG_EMACS_MARKER |
| 272 | std::cout << path << " - Emacs marker not on the first line" << '\n'; |
| 273 | #endif |
| 274 | return false; // not on first line |
| 275 | } |
| 276 | const auto pos2 = buf.find("-*-", pos1 + 3); |
| 277 | // TODO: make sure we have read the whole line before bailing out |
| 278 | if (pos2 == std::string::npos) { |
| 279 | #ifdef LOG_EMACS_MARKER |
| 280 | std::cout << path << " - Emacs marker not terminated" << '\n'; |
| 281 | #endif |
| 282 | return false; // no end marker |
| 283 | } |
| 284 | #ifdef LOG_EMACS_MARKER |
| 285 | std::cout << "Emacs marker: '" << buf.substr(pos1, (pos2 + 3) - pos1) << "'" << '\n'; |
| 286 | #endif |
| 287 | // TODO: support /* */ comments |
| 288 | const std::string buf_trim = trim(buf); // trim whitespaces |
| 289 | if (buf_trim[0] == '/' && buf_trim[1] == '*') { |
| 290 | const auto pos_cmt = buf.find("*/", 2); |
| 291 | if (pos_cmt != std::string::npos && pos_cmt < (pos2 + 3)) |
| 292 | { |
| 293 | #ifdef LOG_EMACS_MARKER |