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