cmUseMangledMesaCommand.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmUseMangledMesaCommand.h"
  14. #include "cmSystemTools.h"
  15. // cmUseMangledMesaCommand
  16. bool cmUseMangledMesaCommand::InitialPass(std::vector<std::string> const& args)
  17. {
  18. // expected two arguments:
  19. // arguement one: the full path to gl_mangle.h
  20. // arguement two : directory for output of edited headers
  21. if(args.size() < 2)
  22. {
  23. this->SetError("called with incorrect number of arguments");
  24. return false;
  25. }
  26. const char* inputDir = args[0].c_str();
  27. const char* destDir = args[1].c_str();
  28. std::vector<std::string> files;
  29. cmSystemTools::Glob(inputDir, "\\.h$", files);
  30. if(files.size() == 0)
  31. {
  32. cmSystemTools::Error("Could not open Mesa Directory ", inputDir);
  33. return false;
  34. }
  35. cmSystemTools::MakeDirectory(destDir);
  36. for(std::vector<std::string>::iterator i = files.begin();
  37. i != files.end(); ++i)
  38. {
  39. std::string path = inputDir;
  40. path += "/";
  41. path += *i;
  42. this->CopyAndFullPathMesaHeader(path.c_str(), destDir);
  43. }
  44. return true;
  45. }
  46. void
  47. cmUseMangledMesaCommand::
  48. CopyAndFullPathMesaHeader(const char* source,
  49. const char* outdir)
  50. {
  51. std::string dir, file;
  52. cmSystemTools::SplitProgramPath(source, dir, file);
  53. std::string outFile = outdir;
  54. outFile += "/";
  55. outFile += file;
  56. std::string tempOutputFile = outFile;
  57. tempOutputFile += ".tmp";
  58. std::ofstream fout(tempOutputFile.c_str());
  59. if(!fout)
  60. {
  61. cmSystemTools::Error("Could not open file for write in copy operatation: ",
  62. tempOutputFile.c_str(), outdir);
  63. return;
  64. }
  65. std::ifstream fin(source);
  66. if(!fin)
  67. {
  68. cmSystemTools::Error("Could not open file for read in copy operatation",
  69. source);
  70. return;
  71. }
  72. // now copy input to output and expand varibles in the
  73. // input file at the same time
  74. const int bufSize = 4096;
  75. char buffer[bufSize];
  76. std::string inLine;
  77. // regular expression for any #include line
  78. cmRegularExpression includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  79. // regular expression for gl/ or GL/ in a file (match(1) of above)
  80. cmRegularExpression glDirLine("(gl|GL)(/|\\\\)([^<\"]+)");
  81. // regular expression for gl GL or xmesa in a file (match(1) of above)
  82. cmRegularExpression glLine("(gl|GL|xmesa)");
  83. while(fin)
  84. {
  85. fin.getline(buffer, bufSize);
  86. if(fin)
  87. {
  88. inLine = buffer;
  89. if(includeLine.find(inLine.c_str()))
  90. {
  91. std::string includeFile = includeLine.match(1);
  92. if(glDirLine.find(includeFile.c_str()))
  93. {
  94. std::string file = glDirLine.match(3);
  95. fout << "#include \"" << outdir << "/" << file.c_str() << "\"\n";
  96. }
  97. else if(glLine.find(includeFile.c_str()))
  98. {
  99. fout << "#include \"" << outdir << "/" << includeLine.match(1).c_str() << "\"\n";
  100. }
  101. else
  102. {
  103. fout << inLine << "\n";
  104. }
  105. }
  106. else
  107. {
  108. fout << inLine << "\n";
  109. }
  110. }
  111. }
  112. // close the files before attempting to copy
  113. fin.close();
  114. fout.close();
  115. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  116. outFile.c_str());
  117. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  118. }