check_include.cxx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // it it starts with a capital, assume its a #define var used as include tgt,
  40. // and dont print a warning
  41. if(!((line[p]>='A')&&(line[p]<='Z')))
  42. cerr << "Ignoring invalid #include directive: " << line << "\n";
  43. return string();
  44. }
  45. char close = (line[p] == '"') ? '"' : '>';
  46. p++;
  47. // Now get the filename.
  48. size_t q = p;
  49. while (q < line.length() && line[q] != close) {
  50. q++;
  51. }
  52. if (q >= line.length()) {
  53. cerr << "Ignoring invalid #include directive: " << line << "\n";
  54. return string();
  55. }
  56. return line.substr(p, q - p);
  57. }