cmCableDefineSetCommand.cxx 3.9 KB

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