cmXCodeObject.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "cmXCodeObject.h"
  2. #include "cmSystemTools.h"
  3. const char* cmXCodeObject::PBXTypeNames[] = {
  4. "PBXGroup", "PBXBuildStyle", "PBXProject", "PBXHeadersBuildPhase",
  5. "PBXSourcesBuildPhase", "PBXFrameworksBuildPhase", "PBXNativeTarget",
  6. "PBXFileReference", "PBXBuildFile", "PBXContainerItemProxy",
  7. "PBXTargetDependency", "PBXShellScriptBuildPhase",
  8. "PBXResourcesBuildPhase", "PBXApplicationReference",
  9. "PBXExecutableFileReference", "PBXLibraryReference", "PBXToolTarget",
  10. "PBXLibraryTarget", "PBXAggregateTarget", "XCBuildConfiguration",
  11. "XCConfigurationList",
  12. "PBXCopyFilesBuildPhase",
  13. "None"
  14. };
  15. cmXCodeObject::~cmXCodeObject()
  16. {
  17. this->Version = 15;
  18. }
  19. //----------------------------------------------------------------------------
  20. cmXCodeObject::cmXCodeObject(PBXType ptype, Type type)
  21. {
  22. this->Version = 15;
  23. this->PBXTargetDependencyValue = 0;
  24. this->Target = 0;
  25. this->Object =0;
  26. this->IsA = ptype;
  27. if(type == OBJECT)
  28. {
  29. cmOStringStream str;
  30. str << (void*)this;
  31. str << (void*)this;
  32. str << (void*)this;
  33. this->Id = str.str();
  34. }
  35. else
  36. {
  37. this->Id =
  38. "Temporary cmake object, should not be refered to in xcode file";
  39. }
  40. cmSystemTools::ReplaceString(this->Id, "0x", "");
  41. this->Id = cmSystemTools::UpperCase(this->Id);
  42. if(this->Id.size() < 24)
  43. {
  44. int diff = 24 - this->Id.size();
  45. for(int i =0; i < diff; ++i)
  46. {
  47. this->Id += "0";
  48. }
  49. }
  50. this->TypeValue = type;
  51. if(this->TypeValue == OBJECT)
  52. {
  53. this->AddAttribute("isa", 0);
  54. }
  55. }
  56. //----------------------------------------------------------------------------
  57. void cmXCodeObject::Indent(int level, std::ostream& out)
  58. {
  59. while(level)
  60. {
  61. out << " ";
  62. level--;
  63. }
  64. }
  65. //----------------------------------------------------------------------------
  66. void cmXCodeObject::Print(std::ostream& out)
  67. {
  68. std::string separator = "\n";
  69. int indentFactor = 1;
  70. if(this->Version > 15
  71. && (this->IsA == PBXFileReference || this->IsA == PBXBuildFile))
  72. {
  73. separator = " ";
  74. indentFactor = 0;
  75. }
  76. cmXCodeObject::Indent(2*indentFactor, out);
  77. out << this->Id << " ";
  78. if(!(this->IsA == PBXGroup && this->Comment.size() == 0))
  79. {
  80. this->PrintComment(out);
  81. }
  82. out << " = {";
  83. if(separator == "\n")
  84. {
  85. out << separator;
  86. }
  87. std::map<cmStdString, cmXCodeObject*>::iterator i;
  88. cmXCodeObject::Indent(3*indentFactor, out);
  89. out << "isa = " << PBXTypeNames[this->IsA] << ";" << separator;
  90. for(i = this->ObjectAttributes.begin();
  91. i != this->ObjectAttributes.end(); ++i)
  92. {
  93. cmXCodeObject* object = i->second;
  94. if(i->first != "isa")
  95. {
  96. cmXCodeObject::Indent(3*indentFactor, out);
  97. }
  98. else
  99. {
  100. continue;
  101. }
  102. if(object->TypeValue == OBJECT_LIST)
  103. {
  104. out << i->first << " = (" << separator;
  105. for(unsigned int k = 0; k < i->second->List.size(); k++)
  106. {
  107. cmXCodeObject::Indent(4*indentFactor, out);
  108. out << i->second->List[k]->Id << " ";
  109. i->second->List[k]->PrintComment(out);
  110. out << "," << separator;
  111. }
  112. cmXCodeObject::Indent(3*indentFactor, out);
  113. out << ");" << separator;
  114. }
  115. else if(object->TypeValue == ATTRIBUTE_GROUP)
  116. {
  117. std::map<cmStdString, cmXCodeObject*>::iterator j;
  118. out << i->first << " = {" << separator;
  119. for(j = object->ObjectAttributes.begin(); j !=
  120. object->ObjectAttributes.end(); ++j)
  121. {
  122. cmXCodeObject::Indent(4 *indentFactor, out);
  123. out << j->first << " = " << j->second->String << ";";
  124. out << separator;
  125. }
  126. cmXCodeObject::Indent(3 *indentFactor, out);
  127. out << "};" << separator;
  128. }
  129. else if(object->TypeValue == OBJECT_REF)
  130. {
  131. out << i->first << " = " << object->Object->Id;
  132. if(object->Object->HasComment() && i->first != "remoteGlobalIDString")
  133. {
  134. out << " ";
  135. object->Object->PrintComment(out);
  136. }
  137. out << ";" << separator;
  138. }
  139. else if(object->TypeValue == STRING)
  140. {
  141. out << i->first << " = " << object->String << ";" << separator;
  142. }
  143. else
  144. {
  145. out << "what is this?? " << i->first << "\n";
  146. }
  147. }
  148. cmXCodeObject::Indent(2*indentFactor, out);
  149. out << "};\n";
  150. }
  151. //----------------------------------------------------------------------------
  152. void cmXCodeObject::PrintList(std::vector<cmXCodeObject*> const& objs,
  153. std::ostream& out)
  154. {
  155. cmXCodeObject::Indent(1, out);
  156. out << "objects = {\n";
  157. for(unsigned int i = 0; i < objs.size(); ++i)
  158. {
  159. if(objs[i]->TypeValue == OBJECT)
  160. {
  161. objs[i]->Print(out);
  162. }
  163. }
  164. cmXCodeObject::Indent(1, out);
  165. out << "};\n";
  166. }
  167. void cmXCodeObject::CopyAttributes(cmXCodeObject* copy)
  168. {
  169. this->ObjectAttributes = copy->ObjectAttributes;
  170. this->List = copy->List;
  171. this->String = copy->String;
  172. this->Object = copy->Object;
  173. }
  174. void cmXCodeObject::SetString(const char* s)
  175. {
  176. std::string ss = s;
  177. if(ss.size() == 0)
  178. {
  179. this->String = "\"\"";
  180. return;
  181. }
  182. // escape quotes
  183. cmSystemTools::ReplaceString(ss, "\"", "\\\"");
  184. bool needQuote = false;
  185. this->String = "";
  186. if(ss.find_first_of(" <>.+-=@") != ss.npos)
  187. {
  188. needQuote = true;
  189. }
  190. if(needQuote)
  191. {
  192. this->String = "\"";
  193. }
  194. this->String += ss;
  195. if(needQuote)
  196. {
  197. this->String += "\"";
  198. }
  199. }