cmCableDefineSetCommand.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "cmCableDefineSetCommand.h"
  12. #include "cmCacheManager.h"
  13. #include "cmRegularExpression.h"
  14. // cmCableDefineSetCommand
  15. bool cmCableDefineSetCommand::Invoke(std::vector<std::string>& args)
  16. {
  17. if(args.size() < 2)
  18. {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. // This command needs access to the Cable data.
  23. this->SetupCableData();
  24. std::vector<std::string>::const_iterator arg = args.begin();
  25. // The first argument is the name of the set.
  26. m_SetName = *arg++;
  27. // The rest of the arguments are the elements to be placed in the set.
  28. for(; arg != args.end(); ++arg)
  29. {
  30. m_Elements.push_back(Element(this->GenerateTag(*arg), *arg));
  31. }
  32. // Write this command's configuration output.
  33. this->WriteConfiguration();
  34. return true;
  35. }
  36. /**
  37. * Write the CABLE configuration code to define this Set.
  38. */
  39. void cmCableDefineSetCommand::WriteConfiguration() const
  40. {
  41. cmRegularExpression needCdataBlock("[&<>]");
  42. // Get the ouptut information from the cmCableData.
  43. std::ostream& os = m_CableData->GetOutputStream();
  44. cmCableData::Indentation indent = m_CableData->GetIndentation();
  45. // Output the code.
  46. os << indent << "<Set name=\"" << m_SetName.c_str() << "\">" << std::endl;
  47. for(Elements::const_iterator e = m_Elements.begin();
  48. e != m_Elements.end(); ++e)
  49. {
  50. os << indent << " <Element";
  51. // Only output the tag if it is not the empty string.
  52. if(e->first.length() > 0)
  53. {
  54. os << " tag=\"" << e->first.c_str() << "\"";
  55. }
  56. os << ">";
  57. if(needCdataBlock.find(e->second.c_str()))
  58. {
  59. os << "<![CDATA[" << e->second.c_str() << "]]>";
  60. }
  61. else
  62. {
  63. os << e->second.c_str();
  64. }
  65. os << "</Element>" << std::endl;
  66. }
  67. os << indent << "</Set>" << std::endl;
  68. }
  69. /**
  70. * Given the string representing a set element, automatically generate
  71. * the CABLE element tag for it.
  72. *
  73. * **This function determines how the output language of all
  74. * CABLE-generated wrappers will look!**
  75. */
  76. std::string
  77. cmCableDefineSetCommand::GenerateTag(const std::string& element) const
  78. {
  79. // Hold the regular expressions for matching against the element.
  80. cmRegularExpression regex;
  81. // If the element's code begins in a $, it is referring to a set name.
  82. // The set's elements have their own tags, so we don't need one.
  83. regex.compile("^[ \t]*\\$");
  84. if(regex.find(element))
  85. { return ""; }
  86. // Test for simple integer
  87. regex.compile("^[ \t]*([0-9]*)[ \t]*$");
  88. if(regex.find(element))
  89. {
  90. std::string tag = "_";
  91. tag.append(regex.match(1));
  92. return tag;
  93. }
  94. // Test for basic integer type
  95. regex.compile("^[ \t]*(unsigned[ ]|signed[ ])?[ \t]*(char|short|int|long|long[ ]long)[ \t]*$");
  96. if(regex.find(element))
  97. {
  98. std::string tag = "_";
  99. if(regex.match(1) == "unsigned ")
  100. { tag.append("u"); }
  101. if(regex.match(2) == "long long")
  102. { tag.append("llong"); }
  103. else
  104. { tag.append(regex.match(2)); }
  105. return tag;
  106. }
  107. // Test for basic floating-point type
  108. regex.compile("^[ \t]*(long[ ])?[ \t]*(float|double)[ \t]*$");
  109. if(regex.find(element))
  110. {
  111. std::string tag = "_";
  112. if(regex.match(1) == "long ")
  113. tag.append("l");
  114. tag.append(regex.match(2));
  115. return tag;
  116. }
  117. // Test for basic wide-character type
  118. regex.compile("^[ \t]*(wchar_t)[ \t]*$");
  119. if(regex.find(element))
  120. {
  121. return "_wchar";
  122. }
  123. // Test for plain type name (without template arguments).
  124. regex.compile("^[ \t]*([A-Za-z_][A-Za-z0-9_]*)[ \t]*$");
  125. if(regex.find(element))
  126. {
  127. // The tag is the same as the type.
  128. return regex.match(1);
  129. }
  130. // Test for template class instance.
  131. regex.compile("^[ \t]*([A-Za-z_][A-Za-z0-9_]*)<.*[ \t]*$");
  132. if(regex.find(element))
  133. {
  134. // The tag is the type without arguments (the arguments may have
  135. // their own tags).
  136. return regex.match(1);
  137. }
  138. return "NO_AUTO_TAG";
  139. }