cmCursesPathWidget.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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 "cmCursesPathWidget.h"
  14. #include "cmCursesMainForm.h"
  15. #include "cmSystemTools.h"
  16. #include "cmDirectory.h"
  17. cmCursesPathWidget::cmCursesPathWidget(int width, int height,
  18. int left, int top) :
  19. cmCursesStringWidget(width, height, left, top)
  20. {
  21. m_Type = cmCacheManager::PATH;
  22. m_Cycle = false;
  23. m_CurrentIndex = 0;
  24. }
  25. void GlobDirs(const std::string& fullPath,
  26. std::vector<std::string>& files,
  27. std::ofstream& of)
  28. {
  29. if ( fullPath[fullPath.size()-1] != '*' )
  30. {
  31. files.push_back(fullPath);
  32. return;
  33. }
  34. std::string path = cmSystemTools::GetFilenamePath(fullPath);
  35. std::string ppath = cmSystemTools::GetFilenameName(fullPath);
  36. ppath = ppath.substr(0, ppath.size()-1);
  37. of << "Search in directory: " << path << std::endl;
  38. of << "Search pattern: " << ppath << std::endl;
  39. cmDirectory d;
  40. if (d.Load(path.c_str()))
  41. {
  42. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  43. {
  44. if((std::string(d.GetFile(i)) != ".")
  45. && (std::string(d.GetFile(i)) != ".."))
  46. {
  47. std::string fname = path;
  48. fname +="/";
  49. fname += d.GetFile(i);
  50. std::string sfname = d.GetFile(i);
  51. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  52. {
  53. of << "Compare: " << sfname.substr(0, ppath.size()) << " and "
  54. << ppath << std::endl;
  55. if ( sfname.size() >= ppath.size() &&
  56. sfname.substr(0, ppath.size()) ==
  57. ppath )
  58. {
  59. files.push_back(fname);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. void cmCursesPathWidget::OnType(int& key, cmCursesMainForm* fm, WINDOW* w)
  67. {
  68. m_Cycle = false;
  69. m_CurrentIndex = 0;
  70. m_LastGlob = "";
  71. this->cmCursesStringWidget::OnType(key, fm, w);
  72. }
  73. void cmCursesPathWidget::OnTab(cmCursesMainForm* fm, WINDOW* w)
  74. {
  75. std::ofstream of("lala.log");
  76. std::string::size_type cc;
  77. if ( !this->GetString() )
  78. {
  79. return;
  80. }
  81. FORM* form = fm->GetForm();
  82. form_driver(form, REQ_NEXT_FIELD);
  83. form_driver(form, REQ_PREV_FIELD);
  84. std::string cstr = this->GetString();
  85. cstr = cstr.substr(0, cstr.find_last_not_of(" \t\n\r")+1);
  86. of << "Cstr: " << cstr << " <> " << m_LastString << std::endl;
  87. if ( m_LastString != cstr )
  88. {
  89. m_Cycle = false;
  90. m_CurrentIndex = 0;
  91. m_LastGlob = "";
  92. of << "Reset" << std::endl;
  93. }
  94. std::string glob;
  95. if ( m_Cycle )
  96. {
  97. of << "We are cycling, try same glob" << std::endl;
  98. glob = m_LastGlob;
  99. }
  100. else
  101. {
  102. glob = cstr + "*";
  103. of << "Try new glob: " << glob << std::endl;
  104. }
  105. std::vector<std::string> dirs;
  106. ::GlobDirs(glob.c_str(), dirs, of);
  107. if ( m_CurrentIndex < dirs.size() )
  108. {
  109. cstr = dirs[m_CurrentIndex];
  110. }
  111. if ( cstr[cstr.size()-1] == '*' )
  112. {
  113. cstr = cstr.substr(0, cstr.size()-1);
  114. }
  115. of << "Glob: " << glob << std::endl;
  116. for ( cc =0; cc < dirs.size(); cc ++ )
  117. {
  118. of << "\t" << cc << ": " << dirs[cc] << std::endl;
  119. }
  120. this->SetString(cstr.c_str());
  121. touchwin(w);
  122. wrefresh(w);
  123. form_driver(form, REQ_END_FIELD);
  124. m_LastGlob = glob;
  125. m_LastString = cstr;
  126. m_Cycle = true;
  127. m_CurrentIndex ++;
  128. if ( m_CurrentIndex >= dirs.size() )
  129. {
  130. m_CurrentIndex = 0;
  131. }
  132. }
  133. void cmCursesPathWidget::OnReturn(cmCursesMainForm* fm, WINDOW* w)
  134. {
  135. this->cmCursesStringWidget::OnReturn(fm, w);
  136. }