* Implement <autoregister>
[reactos.git] / reactos / tools / rbuild / wineresource.cpp
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #include "pch.h"
19 #include <assert.h>
20
21 #include "rbuild.h"
22
23 using std::string;
24 using std::vector;
25
26 WineResource::WineResource ( const Project& project,
27 string bin2res )
28 : project ( project ),
29 bin2res ( bin2res )
30 {
31 }
32
33 WineResource::~WineResource ()
34 {
35 }
36
37 bool
38 WineResource::IsSpecFile ( const File& file )
39 {
40 string extension = GetExtension ( file.name );
41 if ( extension == ".spec" || extension == ".SPEC" )
42 return true;
43 return false;
44 }
45
46 bool
47 WineResource::IsWineModule ( const Module& module )
48 {
49 const vector<File*>& files = module.non_if_data.files;
50 for ( size_t i = 0; i < files.size (); i++ )
51 {
52 if ( IsSpecFile ( *files[i] ) )
53 return true;
54 }
55 return false;
56 }
57
58 bool
59 WineResource::IsResourceFile ( const File& file )
60 {
61 string extension = GetExtension ( file.name );
62 if ( extension == ".rc" || extension == ".RC" )
63 return true;
64 return false;
65 }
66
67 string
68 WineResource::GetResourceFilename ( const Module& module )
69 {
70 const vector<File*>& files = module.non_if_data.files;
71 for ( size_t i = 0; i < files.size (); i++ )
72 {
73 if ( IsResourceFile ( *files[i] ) )
74 return files[i]->name;
75 }
76 return "";
77 }
78
79 void
80 WineResource::UnpackResources ( bool verbose )
81 {
82 for ( size_t i = 0; i < project.modules.size (); i++ )
83 {
84 if ( IsWineModule ( *project.modules[i] ) )
85 {
86 UnpackResourcesInModule ( *project.modules[i],
87 verbose );
88 }
89 }
90 }
91
92 void
93 WineResource::UnpackResourcesInModule ( Module& module,
94 bool verbose )
95 {
96 string resourceFilename = GetResourceFilename ( module );
97 if ( resourceFilename.length () == 0 )
98 return;
99
100 if ( verbose )
101 {
102 printf ( "\nUnpacking resources for %s",
103 module.name.c_str () );
104 }
105
106 string outputDirectory = module.GetBasePath ();
107 string parameters = ssprintf ( "-b %s -f -x %s",
108 NormalizeFilename ( outputDirectory ).c_str (),
109 NormalizeFilename ( resourceFilename ).c_str () );
110 string command = FixSeparatorForSystemCommand(bin2res) + " " + parameters;
111 int exitcode = system ( command.c_str () );
112 if ( exitcode != 0 )
113 {
114 throw InvocationFailedException ( command,
115 exitcode );
116 }
117 }