check_include.cxx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Filename: check_include.cxx
  2. // Created by: drose (16Oct00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "check_include.h"
  6. ////////////////////////////////////////////////////////////////////
  7. // Function: check_include
  8. // Description: Checks to see if the given line is a C/C++ #include
  9. // directive. If it is, returns the named filename;
  10. // otherwise, returns the empty string.
  11. ////////////////////////////////////////////////////////////////////
  12. string
  13. check_include(const string &line) {
  14. // Skip initial whitespace on the line.
  15. size_t p = 0;
  16. while (p < line.length() && isspace(line[p])) {
  17. p++;
  18. }
  19. if (p >= line.length() || line[p] != '#') {
  20. // No hash mark.
  21. return string();
  22. }
  23. // We have a hash mark! Skip more whitespace.
  24. p++;
  25. while (p < line.length() && isspace(line[p])) {
  26. p++;
  27. }
  28. if (p >= line.length() || !(line.substr(p, 7) == "include")) {
  29. // Some other directive, not #include.
  30. return string();
  31. }
  32. // It's an #include directive! Skip more whitespace.
  33. p += 7;
  34. while (p < line.length() && isspace(line[p])) {
  35. p++;
  36. }
  37. // note: ppremake cant expand cpp #define vars used as include targets yet
  38. if (p >= line.length() || (line[p] != '"' && line[p] != '<')) {
  39. // if it starts with a capital, assume its a #define var used as
  40. // include tgt, and don't print a warning
  41. if(!((line[p]>='A')&&(line[p]<='Z'))) {
  42. cerr << "Ignoring invalid #include directive: " << line << "\n";
  43. }
  44. return string();
  45. }
  46. char close = (line[p] == '"') ? '"' : '>';
  47. p++;
  48. // Now get the filename.
  49. size_t q = p;
  50. while (q < line.length() && line[q] != close) {
  51. q++;
  52. }
  53. if (q >= line.length()) {
  54. cerr << "Ignoring invalid #include directive: " << line << "\n";
  55. return string();
  56. }
  57. return line.substr(p, q - p);
  58. }