| 284 | } |
| 285 | |
| 286 | void ScriptFile::ExpandIncludePaths() { |
| 287 | std::string& script = contents; |
| 288 | |
| 289 | file_range.clear(); |
| 290 | file_range.push_back(IncludeFileRange(1, GetNumLines(script), 0, file_path)); |
| 291 | |
| 292 | // Get the position of the first letter of the first "#include" |
| 293 | // directive in the script |
| 294 | size_t found_pos = script.find("#include"); |
| 295 | while (found_pos != std::string::npos) { |
| 296 | bool disabled_include = false; |
| 297 | for (int i = found_pos - 1; i >= 0; i--) { |
| 298 | if (script[i] != ' ' && script[i] != '\t') { |
| 299 | if (script[i] == '\n' || script[i] == '\r') { |
| 300 | break; |
| 301 | } else { |
| 302 | if (script[i] != '/') { |
| 303 | LOGE << "Unexpected character \'" << script[i] << "\' before #include in " << file_path << std::endl; |
| 304 | int line_count = 0; |
| 305 | for (int k = 0; k < i; k++) { |
| 306 | if (script[k] == '\n') { |
| 307 | line_count++; |
| 308 | } |
| 309 | } |
| 310 | LOGE << " line " << line_count << std::endl; |
| 311 | } else { |
| 312 | disabled_include = true; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | size_t path_start = found_pos + 10; // Get pos of first letter of path |
| 318 | size_t path_end = script.find('\"', path_start); // Find end of path |
| 319 | std::string path = std::string(script_dir_path) + |
| 320 | script.substr(path_start, path_end - path_start); |
| 321 | |
| 322 | // If include file has not already been added, then replace #include |
| 323 | // directive with contents of included file. Otherwise, just remove |
| 324 | // the directive. |
| 325 | Path include_path = FindFilePath(path, kDataPaths | kModPaths); |
| 326 | if (disabled_include == false && !AlreadyAddedIncludeFileInParentHierarchy(this, include_path) && !AlreadyAddedIncludeFile(include_path)) { |
| 327 | dependencies.resize(dependencies.size() + 1); |
| 328 | dependencies.back().path = include_path; |
| 329 | if (include_path.isValid()) { |
| 330 | dependencies.back().modified = GetDateModifiedInt64(include_path.GetFullPath()); |
| 331 | |
| 332 | std::string new_script = GetScriptFileRecursive(this, include_path)->unexpanded_contents; |
| 333 | |
| 334 | if (config["dump_include_scripts"].toBool()) { |
| 335 | new_script = |
| 336 | +"/*include - START - " + path + " */\n" + new_script + "/*include - END - " + path + " */\n"; |
| 337 | } |
| 338 | |
| 339 | script.replace(found_pos, path_end + 1 - found_pos, new_script); |
| 340 | |
| 341 | unsigned line_number = GetLineNumber(script, found_pos); |
| 342 | unsigned include_length = GetNumLines(new_script); |
| 343 | InsertFileRange(file_range, line_number, include_length, include_path); |
no test coverage detected