* Implement <autoregister>
[reactos.git] / reactos / tools / rbuild / syssetupgenerator.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 SysSetupGenerator::SysSetupGenerator ( const Project& project )
27 : project ( project )
28 {
29 }
30
31 SysSetupGenerator::~SysSetupGenerator ()
32 {
33 }
34
35 void
36 SysSetupGenerator::Generate ()
37 {
38 HINF inf;
39 unsigned long errorLine;
40
41 string syssetupTemplate = "media" + sSep + "inf" + sSep + "syssetup.inf.tpl";
42 string syssetup = "media" + sSep + "inf" + sSep + "syssetup.inf";
43
44 if ( 0 != InfHostOpenFile ( &inf, syssetupTemplate.c_str (), &errorLine ) )
45 throw new FileNotFoundException ( syssetupTemplate );
46
47 for ( size_t i = 0; i < project.modules.size (); i++ )
48 {
49 const Module& module = *project.modules[i];
50 if ( module.autoRegister != NULL )
51 Generate ( inf, module );
52 }
53
54 if ( 0 != InfHostWriteFile ( inf, syssetup.c_str (), "" ) )
55 {
56 InfHostCloseFile ( inf );
57 throw new AccessDeniedException ( syssetup );
58 }
59
60 InfHostCloseFile ( inf );
61 }
62
63 #define DIRECTORYID_SYSTEM32 "11"
64
65 string
66 SysSetupGenerator::GetDirectoryId ( const Module& module )
67 {
68 if ( ToLower ( module.installBase ) == "system32" )
69 return DIRECTORYID_SYSTEM32;
70 throw InvalidOperationException ( __FILE__,
71 __LINE__ );
72 }
73
74 #define FLG_REGSVR_DLLREGISTER "1"
75 #define FLG_REGSVR_DLLINSTALL "2"
76 #define FLG_REGSVR_BOTH "3"
77
78 string
79 SysSetupGenerator::GetFlags ( const Module& module )
80 {
81 if ( module.autoRegister->type == DllRegisterServer )
82 return FLG_REGSVR_DLLREGISTER;
83 if ( module.autoRegister->type == DllInstall )
84 return FLG_REGSVR_DLLINSTALL;
85 if ( module.autoRegister->type == Both )
86 return FLG_REGSVR_BOTH;
87 throw InvalidOperationException ( __FILE__,
88 __LINE__ );
89 }
90
91 void
92 SysSetupGenerator::Generate ( HINF inf,
93 const Module& module )
94 {
95 PINFCONTEXT context;
96
97 string infSection = module.autoRegister->infSection;
98 if ( 0 != InfHostFindOrAddSection ( inf, infSection.c_str (), &context ) )
99 {
100 throw new Exception ( ".inf section '%s' not found", infSection.c_str () );
101 InfHostCloseFile ( inf );
102 }
103
104 if ( 0 != InfHostAddLine ( context, NULL ) ||
105 0 != InfHostAddField ( context, GetDirectoryId ( module ).c_str () ) ||
106 0 != InfHostAddField ( context, "" ) ||
107 0 != InfHostAddField ( context, module.installName.c_str () ) ||
108 0 != InfHostAddField ( context, GetFlags ( module ).c_str () ) )
109 {
110 InfHostFreeContext ( context );
111 InfHostCloseFile ( inf );
112 throw InvalidOperationException ( __FILE__,
113 __LINE__ );
114 }
115
116 InfHostFreeContext ( context );
117 }