cmCableDefineSetCommand.cxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // If the element cannot be added, return an error.
  31. // This can occur when a tag is not specified and can't be generated.
  32. if(!this->AddElement(*arg))
  33. { return false; }
  34. }
  35. // Write this command's configuration output.
  36. this->WriteConfiguration();
  37. return true;
  38. }
  39. /**
  40. * Write the CABLE configuration code to define this Set.
  41. */
  42. void cmCableDefineSetCommand::WriteConfiguration() const
  43. {
  44. cmRegularExpression needCdataBlock("[&<>]");
  45. // Get the ouptut information from the cmCableData.
  46. std::ostream& os = m_CableData->GetOutputStream();
  47. cmCableData::Indentation indent = m_CableData->GetIndentation();
  48. // Output the code.
  49. os << indent << "<Set name=\"" << m_SetName.c_str() << "\">" << std::endl;
  50. for(Elements::const_iterator e = m_Elements.begin();
  51. e != m_Elements.end(); ++e)
  52. {
  53. os << indent << " <Element";
  54. // Only output the tag if it is not the empty string.
  55. if(e->first.length() > 0)
  56. {
  57. os << " tag=\"" << e->first.c_str() << "\"";
  58. }
  59. os << ">";
  60. if(needCdataBlock.find(e->second.c_str()))
  61. {
  62. os << "<![CDATA[" << e->second.c_str() << "]]>";
  63. }
  64. else
  65. {
  66. os << e->second.c_str();
  67. }
  68. os << "</Element>" << std::endl;
  69. }
  70. os << indent << "</Set>" << std::endl;
  71. }
  72. /**
  73. * Add an element to the set. The given string is the argument to the
  74. * command describing the element. There are two formats allowed:
  75. * "code" = The code describing the element to CABLE is simply given.
  76. * The GenerateTag() method will guess at a good tag for the
  77. * code.
  78. * "tag:code" = The left side of a single colon is text describing the tag.
  79. * GenerateTag() will not be called.
  80. */
  81. bool cmCableDefineSetCommand::AddElement(const std::string& arg)
  82. {
  83. // A regular expression to match the tagged element specification.
  84. cmRegularExpression tagGiven("^([A-Za-z_0-9]*)[ \t]*:[ \t]*([^:].*|::.*)$");
  85. std::string tag;
  86. std::string code;
  87. if(tagGiven.find(arg.c_str()))
  88. {
  89. // A tag was given. Use it.
  90. tag = tagGiven.match(1);
  91. code = tagGiven.match(2);
  92. }
  93. else
  94. {
  95. // No tag was given. Try to generate one.
  96. if(!this->GenerateTag(arg, tag))
  97. { return false; }
  98. code = arg;
  99. }
  100. // Add an element with the given tag and code.
  101. m_Elements.push_back(Element(tag, code));
  102. return true;
  103. }
  104. /**
  105. * Given the string representing a set element, automatically generate
  106. * the CABLE element tag for it.
  107. *
  108. * **This function determines how the output language of all
  109. * CABLE-generated wrappers will look!**
  110. */
  111. bool
  112. cmCableDefineSetCommand::GenerateTag(const std::string& element,
  113. std::string& tag)
  114. {
  115. // Hold the regular expressions for matching against the element.
  116. cmRegularExpression regex;
  117. // If the element's code begins in a $, it is referring to a set name.
  118. // The set's elements have their own tags, so we don't need one.
  119. regex.compile("^[ \t]*\\$");
  120. if(regex.find(element))
  121. { tag = ""; return true; }
  122. // Test for simple integer
  123. regex.compile("^[ \t]*([0-9]*)[ \t]*$");
  124. if(regex.find(element))
  125. {
  126. tag = "_";
  127. tag.append(regex.match(1));
  128. return true;
  129. }
  130. // Test for basic integer type
  131. regex.compile("^[ \t]*(unsigned[ ]|signed[ ])?[ \t]*(char|short|int|long|long[ ]long)[ \t]*$");
  132. if(regex.find(element))
  133. {
  134. tag = "_";
  135. if(regex.match(1) == "unsigned ")
  136. { tag.append("u"); }
  137. if(regex.match(2) == "long long")
  138. { tag.append("llong"); }
  139. else
  140. { tag.append(regex.match(2)); }
  141. return true;
  142. }
  143. // Test for basic floating-point type
  144. regex.compile("^[ \t]*(long[ ])?[ \t]*(float|double)[ \t]*$");
  145. if(regex.find(element))
  146. {
  147. tag = "_";
  148. if(regex.match(1) == "long ")
  149. tag.append("l");
  150. tag.append(regex.match(2));
  151. return true;
  152. }
  153. // Test for basic wide-character type
  154. regex.compile("^[ \t]*(wchar_t)[ \t]*$");
  155. if(regex.find(element))
  156. {
  157. tag = "_wchar";
  158. return true;
  159. }
  160. // Test for plain type name (without template arguments).
  161. regex.compile("^[ \t]*([A-Za-z_][A-Za-z0-9_]*)[ \t]*$");
  162. if(regex.find(element))
  163. {
  164. // The tag is the same as the type.
  165. tag = regex.match(1);
  166. return true;
  167. }
  168. // Test for template class instance.
  169. regex.compile("^[ \t]*([A-Za-z_][A-Za-z0-9_]*)<.*[ \t]*$");
  170. if(regex.find(element))
  171. {
  172. // The tag is the type without arguments (the arguments may have
  173. // their own tags).
  174. tag = regex.match(1);
  175. return true;
  176. }
  177. // We can't generate a tag.
  178. std::string err =
  179. ("doesn't know how to generate tag for element \""+element+"\" in set \""
  180. +m_SetName+"\"\nPlease specify one with the \"tag:element\" syntax.");
  181. this->SetError(err.c_str());
  182. tag = "";
  183. return false;
  184. }