check_include.cxx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. if (p >= line.length() || (line[p] != '"' && line[p] != '<')) {
  38. cerr << "Ignoring invalid #include directive: " << line << "\n";
  39. return string();
  40. }
  41. char close = (line[p] == '"') ? '"' : '>';
  42. p++;
  43. // Now get the filename.
  44. size_t q = p;
  45. while (q < line.length() && line[q] != close) {
  46. q++;
  47. }
  48. if (q >= line.length()) {
  49. cerr << "Ignoring invalid #include directive: " << line << "\n";
  50. return string();
  51. }
  52. return line.substr(p, q - p);
  53. }