ResourceScriptStmt.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  3. // See https://llvm.org/LICENSE.txt for license information.
  4. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. //
  6. //===---------------------------------------------------------------------===//
  7. //
  8. // This implements methods defined in ResourceScriptStmt.h.
  9. //
  10. // Ref: msdn.microsoft.com/en-us/library/windows/desktop/aa380599(v=vs.85).aspx
  11. //
  12. //===---------------------------------------------------------------------===//
  13. #include "ResourceScriptStmt.h"
  14. namespace llvm {
  15. namespace rc {
  16. raw_ostream &operator<<(raw_ostream &OS, const IntOrString &Item) {
  17. if (Item.IsInt)
  18. return OS << Item.Data.Int;
  19. else
  20. return OS << Item.Data.String;
  21. }
  22. raw_ostream &OptionalStmtList::log(raw_ostream &OS) const {
  23. for (const auto &Stmt : Statements) {
  24. OS << " Option: ";
  25. Stmt->log(OS);
  26. }
  27. return OS;
  28. }
  29. raw_ostream &LanguageResource::log(raw_ostream &OS) const {
  30. return OS << "Language: " << Lang << ", Sublanguage: " << SubLang << "\n";
  31. }
  32. StringRef AcceleratorsResource::Accelerator::OptionsStr
  33. [AcceleratorsResource::Accelerator::NumFlags] = {
  34. "ASCII", "VIRTKEY", "NOINVERT", "ALT", "SHIFT", "CONTROL"};
  35. uint32_t AcceleratorsResource::Accelerator::OptionsFlags
  36. [AcceleratorsResource::Accelerator::NumFlags] = {ASCII, VIRTKEY, NOINVERT,
  37. ALT, SHIFT, CONTROL};
  38. raw_ostream &AcceleratorsResource::log(raw_ostream &OS) const {
  39. OS << "Accelerators (" << ResName << "): \n";
  40. OptStatements->log(OS);
  41. for (const auto &Acc : Accelerators) {
  42. OS << " Accelerator: " << Acc.Event << " " << Acc.Id;
  43. for (size_t i = 0; i < Accelerator::NumFlags; ++i)
  44. if (Acc.Flags & Accelerator::OptionsFlags[i])
  45. OS << " " << Accelerator::OptionsStr[i];
  46. OS << "\n";
  47. }
  48. return OS;
  49. }
  50. raw_ostream &BitmapResource::log(raw_ostream &OS) const {
  51. return OS << "Bitmap (" << ResName << "): " << BitmapLoc << "\n";
  52. }
  53. raw_ostream &CursorResource::log(raw_ostream &OS) const {
  54. return OS << "Cursor (" << ResName << "): " << CursorLoc << "\n";
  55. }
  56. raw_ostream &IconResource::log(raw_ostream &OS) const {
  57. return OS << "Icon (" << ResName << "): " << IconLoc << "\n";
  58. }
  59. raw_ostream &HTMLResource::log(raw_ostream &OS) const {
  60. return OS << "HTML (" << ResName << "): " << HTMLLoc << "\n";
  61. }
  62. StringRef MenuDefinition::OptionsStr[MenuDefinition::NumFlags] = {
  63. "CHECKED", "GRAYED", "HELP", "INACTIVE", "MENUBARBREAK", "MENUBREAK"};
  64. uint32_t MenuDefinition::OptionsFlags[MenuDefinition::NumFlags] = {
  65. CHECKED, GRAYED, HELP, INACTIVE, MENUBARBREAK, MENUBREAK};
  66. raw_ostream &MenuDefinition::logFlags(raw_ostream &OS, uint16_t Flags) {
  67. for (size_t i = 0; i < NumFlags; ++i)
  68. if (Flags & OptionsFlags[i])
  69. OS << " " << OptionsStr[i];
  70. return OS;
  71. }
  72. raw_ostream &MenuDefinitionList::log(raw_ostream &OS) const {
  73. OS << " Menu list starts\n";
  74. for (auto &Item : Definitions)
  75. Item->log(OS);
  76. return OS << " Menu list ends\n";
  77. }
  78. raw_ostream &MenuItem::log(raw_ostream &OS) const {
  79. OS << " MenuItem (" << Name << "), ID = " << Id;
  80. logFlags(OS, Flags);
  81. return OS << "\n";
  82. }
  83. raw_ostream &MenuSeparator::log(raw_ostream &OS) const {
  84. return OS << " Menu separator\n";
  85. }
  86. raw_ostream &PopupItem::log(raw_ostream &OS) const {
  87. OS << " Popup (" << Name << ")";
  88. logFlags(OS, Flags);
  89. OS << ":\n";
  90. return SubItems.log(OS);
  91. }
  92. raw_ostream &MenuResource::log(raw_ostream &OS) const {
  93. OS << "Menu (" << ResName << "):\n";
  94. OptStatements->log(OS);
  95. return Elements.log(OS);
  96. }
  97. raw_ostream &StringTableResource::log(raw_ostream &OS) const {
  98. OS << "StringTable:\n";
  99. OptStatements->log(OS);
  100. for (const auto &String : Table) {
  101. OS << " " << String.first << " =>";
  102. for (const auto &S : String.second)
  103. OS << " " << S;
  104. OS << "\n";
  105. }
  106. return OS;
  107. }
  108. const StringMap<Control::CtlInfo> Control::SupportedCtls = {
  109. {"LTEXT", CtlInfo{0x50020000, ClsStatic, true}},
  110. {"CTEXT", CtlInfo{0x50020001, ClsStatic, true}},
  111. {"RTEXT", CtlInfo{0x50020002, ClsStatic, true}},
  112. {"ICON", CtlInfo{0x50000003, ClsStatic, true}},
  113. {"PUSHBUTTON", CtlInfo{0x50010000, ClsButton, true}},
  114. {"DEFPUSHBUTTON", CtlInfo{0x50010001, ClsButton, true}},
  115. {"AUTO3STATE", CtlInfo{0x50010006, ClsButton, true}},
  116. {"AUTOCHECKBOX", CtlInfo{0x50010003, ClsButton, true}},
  117. {"AUTORADIOBUTTON", CtlInfo{0x50000009, ClsButton, true}},
  118. {"CHECKBOX", CtlInfo{0x50010002, ClsButton, true}},
  119. {"GROUPBOX", CtlInfo{0x50000007, ClsButton, true}},
  120. {"RADIOBUTTON", CtlInfo{0x50000004, ClsButton, true}},
  121. {"STATE3", CtlInfo{0x50010005, ClsButton, true}},
  122. {"PUSHBOX", CtlInfo{0x5001000A, ClsButton, true}},
  123. {"EDITTEXT", CtlInfo{0x50810000, ClsEdit, false}},
  124. {"COMBOBOX", CtlInfo{0x50000000, ClsComboBox, false}},
  125. {"LISTBOX", CtlInfo{0x50800001, ClsListBox, false}},
  126. {"SCROLLBAR", CtlInfo{0x50000000, ClsScrollBar, false}},
  127. {"CONTROL", CtlInfo{0x50000000, 0, true}},
  128. };
  129. raw_ostream &Control::log(raw_ostream &OS) const {
  130. OS << " Control (" << ID << "): " << Type << ", title: " << Title
  131. << ", loc: (" << X << ", " << Y << "), size: [" << Width << ", " << Height
  132. << "]";
  133. if (Style)
  134. OS << ", style: " << (*Style).getValue();
  135. if (ExtStyle)
  136. OS << ", ext. style: " << *ExtStyle;
  137. if (HelpID)
  138. OS << ", help ID: " << *HelpID;
  139. return OS << "\n";
  140. }
  141. raw_ostream &DialogResource::log(raw_ostream &OS) const {
  142. OS << "Dialog" << (IsExtended ? "Ex" : "") << " (" << ResName << "): loc: ("
  143. << X << ", " << Y << "), size: [" << Width << ", " << Height
  144. << "], help ID: " << HelpID << "\n";
  145. OptStatements->log(OS);
  146. for (auto &Ctl : Controls)
  147. Ctl.log(OS);
  148. return OS;
  149. }
  150. raw_ostream &VersionInfoBlock::log(raw_ostream &OS) const {
  151. OS << " Start of block (name: " << Name << ")\n";
  152. for (auto &Stmt : Stmts)
  153. Stmt->log(OS);
  154. return OS << " End of block\n";
  155. }
  156. raw_ostream &VersionInfoValue::log(raw_ostream &OS) const {
  157. OS << " " << Key << " =>";
  158. size_t NumValues = Values.size();
  159. for (size_t Id = 0; Id < NumValues; ++Id) {
  160. if (Id > 0 && HasPrecedingComma[Id])
  161. OS << ",";
  162. OS << " " << Values[Id];
  163. }
  164. return OS << "\n";
  165. }
  166. using VersionInfoFixed = VersionInfoResource::VersionInfoFixed;
  167. using VersionInfoFixedType = VersionInfoFixed::VersionInfoFixedType;
  168. const StringRef
  169. VersionInfoFixed::FixedFieldsNames[VersionInfoFixed::FtNumTypes] = {
  170. "", "FILEVERSION", "PRODUCTVERSION", "FILEFLAGSMASK",
  171. "FILEFLAGS", "FILEOS", "FILETYPE", "FILESUBTYPE"};
  172. const StringMap<VersionInfoFixedType> VersionInfoFixed::FixedFieldsInfoMap = {
  173. {FixedFieldsNames[FtFileVersion], FtFileVersion},
  174. {FixedFieldsNames[FtProductVersion], FtProductVersion},
  175. {FixedFieldsNames[FtFileFlagsMask], FtFileFlagsMask},
  176. {FixedFieldsNames[FtFileFlags], FtFileFlags},
  177. {FixedFieldsNames[FtFileOS], FtFileOS},
  178. {FixedFieldsNames[FtFileType], FtFileType},
  179. {FixedFieldsNames[FtFileSubtype], FtFileSubtype}};
  180. VersionInfoFixedType VersionInfoFixed::getFixedType(StringRef Type) {
  181. auto UpperType = Type.upper();
  182. auto Iter = FixedFieldsInfoMap.find(UpperType);
  183. if (Iter != FixedFieldsInfoMap.end())
  184. return Iter->getValue();
  185. return FtUnknown;
  186. }
  187. bool VersionInfoFixed::isTypeSupported(VersionInfoFixedType Type) {
  188. return FtUnknown < Type && Type < FtNumTypes;
  189. }
  190. bool VersionInfoFixed::isVersionType(VersionInfoFixedType Type) {
  191. switch (Type) {
  192. case FtFileVersion:
  193. case FtProductVersion:
  194. return true;
  195. default:
  196. return false;
  197. }
  198. }
  199. raw_ostream &VersionInfoFixed::log(raw_ostream &OS) const {
  200. for (int Type = FtUnknown; Type < FtNumTypes; ++Type) {
  201. if (!isTypeSupported((VersionInfoFixedType)Type))
  202. continue;
  203. OS << " Fixed: " << FixedFieldsNames[Type] << ":";
  204. for (uint32_t Val : FixedInfo[Type])
  205. OS << " " << Val;
  206. OS << "\n";
  207. }
  208. return OS;
  209. }
  210. raw_ostream &VersionInfoResource::log(raw_ostream &OS) const {
  211. OS << "VersionInfo (" << ResName << "):\n";
  212. FixedData.log(OS);
  213. return MainBlock.log(OS);
  214. }
  215. raw_ostream &UserDefinedResource::log(raw_ostream &OS) const {
  216. OS << "User-defined (type: " << Type << ", name: " << ResName << "): ";
  217. if (IsFileResource)
  218. return OS << FileLoc << "\n";
  219. OS << "data = ";
  220. for (auto &Item : Contents)
  221. OS << Item << " ";
  222. return OS << "\n";
  223. }
  224. raw_ostream &CharacteristicsStmt::log(raw_ostream &OS) const {
  225. return OS << "Characteristics: " << Value << "\n";
  226. }
  227. raw_ostream &VersionStmt::log(raw_ostream &OS) const {
  228. return OS << "Version: " << Value << "\n";
  229. }
  230. raw_ostream &CaptionStmt::log(raw_ostream &OS) const {
  231. return OS << "Caption: " << Value << "\n";
  232. }
  233. raw_ostream &ClassStmt::log(raw_ostream &OS) const {
  234. return OS << "Class: " << Value << "\n";
  235. }
  236. raw_ostream &FontStmt::log(raw_ostream &OS) const {
  237. OS << "Font: size = " << Size << ", face = " << Name
  238. << ", weight = " << Weight;
  239. if (Italic)
  240. OS << ", italic";
  241. return OS << ", charset = " << Charset << "\n";
  242. }
  243. raw_ostream &StyleStmt::log(raw_ostream &OS) const {
  244. return OS << "Style: " << Value << "\n";
  245. }
  246. raw_ostream &ExStyleStmt::log(raw_ostream &OS) const {
  247. return OS << "ExStyle: " << Value << "\n";
  248. }
  249. } // namespace rc
  250. } // namespace llvm