cmFindProgramRule.cxx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #include "cmFindProgramRule.h"
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. static void GetPath(std::vector<std::string>& path)
  15. {
  16. #if defined(_WIN32) && !defined(__CYGWIN__)
  17. char* pathSep = ";";
  18. #else
  19. char* pathSep = ":";
  20. #endif
  21. std::string pathEnv = getenv("PATH");
  22. std::string::size_type start =0;
  23. bool done = false;
  24. while(!done)
  25. {
  26. std::string::size_type endpos = pathEnv.find(pathSep, start);
  27. if(endpos != std::string::npos)
  28. {
  29. path.push_back(pathEnv.substr(start, endpos-start));
  30. start = endpos+1;
  31. }
  32. else
  33. {
  34. done = true;
  35. }
  36. }
  37. }
  38. // cmFindProgramRule
  39. bool cmFindProgramRule::Invoke(std::vector<std::string>& args)
  40. {
  41. if(args.size() < 2 )
  42. {
  43. this->SetError("called with incorrect number of arguments");
  44. return false;
  45. }
  46. std::vector<std::string> path;
  47. GetPath(path);
  48. std::vector<std::string>::iterator i = args.begin();
  49. const char* define = (*i).c_str();
  50. i++;
  51. for(; i != args.end(); ++i)
  52. {
  53. for(int k=0; k < path.size(); k++)
  54. {
  55. std::string tryPath = path[k];
  56. tryPath += "/";
  57. tryPath += *i;
  58. #ifdef _WIN32
  59. tryPath += ".exe";
  60. #endif
  61. if(cmSystemTools::FileExists(tryPath.c_str()))
  62. {
  63. m_Makefile->AddDefinition(define, tryPath.c_str());
  64. return true;
  65. }
  66. }
  67. }
  68. return false;
  69. }