sync master with lastest vba changes
[ooovba.git] / soldep / bootstrp / XmlBuildList.cxx
blob287da8aebd7ff2b8db9d40f3805a16f60e625002
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XmlBuildList.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <EXTERN.h> /* from the Perl distribution */
33 #ifdef WNT
34 #include <tools/prewin.h>
35 #include "perl.h"
36 #include <tools/postwin.h>
37 #else
38 #include "perl.h"
39 #endif
40 #undef Copy /* from Perl handy.h */
42 #include <soldep/XmlBuildList.hxx>
43 #include "XmlBuildListDef.hxx"
45 #ifdef __cplusplus
46 # define EXTERN_C extern "C"
47 #else
48 # define EXTERN_C extern
49 #endif
51 static PerlInterpreter *my_perl; /*** The Perl interpreter ***/
52 static const char * DEP_MD_SIMPLE_STR = "md-simple";
54 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
55 static void xs_init (pTHX);
56 static void dl_init (pTHX);
58 static void xs_init(pTHX)
60 char *file = __FILE__;
61 dXSUB_SYS;
63 /* DynaLoader is a special case */
64 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
68 static void dl_init(pTHX)
70 char *file = __FILE__;
71 dTARG;
72 dSP;
73 /* Dynamicboot strapping code*/
74 SAVETMPS;
75 targ=sv_newmortal();
76 FREETMPS;
77 /* end Dynamic bootstrapping code */
78 *file=0;
79 sp=0;
82 FullByteStringList::FullByteStringList()
83 : FullByteStingListImpl(),
84 nRef (0)
88 FullByteStringList::~FullByteStringList()
90 ByteString* pStr = First();
91 while (pStr)
93 delete pStr;
94 pStr = Next();
98 ULONG FullByteStringList::GetPos (ByteString& rStr)
100 ULONG nCurPos = GetCurPos();
101 ULONG nPos = 0;
102 ByteString* pStr = First();
103 while (pStr)
105 if (*pStr == rStr)
107 GetObject (nCurPos);
108 return nPos;
110 pStr = Next();
111 nPos ++;
113 GetObject (nCurPos);
114 return LIST_ENTRY_NOTFOUND;
117 FullByteStringListWrapper& FullByteStringListWrapper::operator=( const FullByteStringListWrapper& rFullByteStringListWrapper )
119 if (pStringList)
120 if (pStringList->GetRef() == 0)
121 delete pStringList;
122 else
123 pStringList->DecRef();
125 pStringList = rFullByteStringListWrapper.GetStringList();
126 if (pStringList)
127 pStringList->IncRef();
128 return *this;
131 FullByteStringListWrapper::~FullByteStringListWrapper ()
133 if (pStringList)
135 if (pStringList->GetRef() == 0)
136 delete pStringList;
137 else
138 pStringList->DecRef();
143 // Function initializes Perl
144 // ATTENTION: No check built in - YET TO DO
146 void XmlBuildList::initPerl(const char* ModulePath) {
147 if (PerlInitialized)
148 return;
149 my_perl = perl_alloc();
150 if (!my_perl)
151 throw XmlBuildListException("Cannot initialize perl");
152 perl_construct(my_perl);
153 char* embedding[] = {"", "-e", "0"};
154 int exitstatus = perl_parse(my_perl, xs_init, 3, embedding, (char **)NULL);
155 if (!exitstatus) {
156 dl_init(aTHX);
157 exitstatus = perl_run(my_perl);
159 //perl_run(my_perl);
160 // Create a variable (s. perlguts)
161 SV* sv = get_sv("main::module_path", TRUE);
162 sv_setpv(sv, ModulePath);
163 eval_pv("use lib $main::module_path; use XMLBuildListParser;", FALSE);
164 checkOperationSuccess();
165 eval_pv("$main::build_lst_obj = XMLBuildListParser->new();", FALSE);
166 checkOperationSuccess();
167 eval_pv("@array = ();", FALSE);
168 checkOperationSuccess();
169 PerlInitialized = TRUE;
172 // Function proves if the $@ perl variable is set, if yes -
173 // last operation was unsuccessfull -> throws exception
174 void XmlBuildList::checkOperationSuccess() {
175 char* op_result = SvPV_nolen(get_sv("main::@", FALSE));
176 if (strcmp(op_result, ""))
177 throw XmlBuildListException(op_result);
181 // Function generates a regular array with NULL as last element
182 // from the Perl-object @array
184 FullByteStringList* XmlBuildList::extractArray() {
185 FullByteStringList* pStringList = new FullByteStringList();
186 AV* theArrayObj = get_av("main::array", FALSE);
187 I32 arrayLength = av_len(theArrayObj); // $#array value
188 if (arrayLength == -1)
189 return pStringList;
191 SV** string_ptr;
192 char* pStoredString;
193 // populate vector with strings (char*)
194 for (int i = 0; i <= arrayLength; i++) {
195 string_ptr = av_fetch(theArrayObj, i, NULL);
196 //pStoredString = savepv(SvPV_nolen(*string_ptr));
197 pStoredString = SvPV_nolen(*string_ptr);
198 ByteString* pStr = new ByteString(pStoredString);
199 pStringList->Insert(pStr, LIST_APPEND);
201 return pStringList;
205 char* XmlBuildList::getError() {
206 eval_pv("$main::string1 = $main::build_lst_obj->getError();", FALSE);
207 checkOperationSuccess();
208 return SvPV_nolen(get_sv("main::string1", FALSE));
211 /*****************************************************************************/
212 XmlBuildList::XmlBuildList(const ByteString& rModulePath)
213 /*****************************************************************************/
214 : PerlInitialized (FALSE)
216 initPerl(rModulePath.GetBuffer());
217 string_obj1 = get_sv("main::string1", TRUE);
218 string_obj2 = get_sv("main::string2", TRUE);
219 string_obj3 = get_sv("main::string3", TRUE);
220 if (!(string_obj1 && string_obj2 && string_obj3))
221 throw XmlBuildListException("Cannot initialize Perl string objects");
225 // Function uninitializes Perl
227 /*****************************************************************************/
228 XmlBuildList::~XmlBuildList()
229 /*****************************************************************************/
231 if (!PerlInitialized)
232 return;
233 perl_destruct(my_perl);
234 perl_free(my_perl);
235 PerlInitialized = FALSE;
238 /*****************************************************************************/
239 void XmlBuildList::loadXMLFile(const ByteString& rBuildList)
240 /*****************************************************************************/
242 sv_setpv(string_obj1, rBuildList.GetBuffer());
243 eval_pv("$main::string2 = $main::build_lst_obj->loadXMLFile($main::string1);", FALSE);
244 checkOperationSuccess();
245 if(!SvTRUE(string_obj2)) {
246 const char* Message = getError();
247 throw XmlBuildListException(Message);
251 /*****************************************************************************/
252 FullByteStringListWrapper XmlBuildList::getProducts()
253 /*****************************************************************************/
255 eval_pv("@array = $main::build_lst_obj->getProducts();", FALSE);
256 checkOperationSuccess();
257 FullByteStringList* pList = extractArray();
258 return FullByteStringListWrapper(pList);
261 /*****************************************************************************/
262 FullByteStringListWrapper XmlBuildList::getJobDirectories(const ByteString& rJobType, const ByteString& rJobPlatform)
263 /*****************************************************************************/
265 sv_setpv(string_obj1, rJobType.GetBuffer());
266 sv_setpv(string_obj2, rJobPlatform.GetBuffer());
267 eval_pv("@array = $main::build_lst_obj->getJobDirectories($main::string1, $main::string2);", FALSE);
268 checkOperationSuccess();
269 FullByteStringList* pList = extractArray();
270 return FullByteStringListWrapper(pList);
273 /*****************************************************************************/
274 FullByteStringListWrapper XmlBuildList::getModuleDependencies(const ByteString& rProduct, const ByteString& rDependencyType)
275 /*****************************************************************************/
277 FullByteStringList* pProducts = new FullByteStringList();
278 FullByteStringListWrapper aProducts (pProducts);
279 if (rProduct != "")
280 aProducts.Insert (new ByteString(rProduct), LIST_APPEND);
281 return getModuleDependencies(aProducts, rDependencyType);
284 /*****************************************************************************/
285 FullByteStringListWrapper XmlBuildList::getModuleDependencies(FullByteStringListWrapper& rProducts, const ByteString& rDependencyType)
286 /*****************************************************************************/
288 eval_pv("@products = ();", FALSE);
289 checkOperationSuccess();
290 AV* theArrayObj = get_av("main::products", FALSE);
291 FullByteStringList* pProducts = rProducts.GetStringList();
292 ByteString* pStr = pProducts->First();
293 while (pStr)
295 sv_setpv(string_obj2, pStr->GetBuffer());
296 av_push(theArrayObj, string_obj2);
297 pStr = pProducts->Next();
300 sv_setpv(string_obj1, rDependencyType.GetBuffer());
301 eval_pv("@array = $main::build_lst_obj->getModuleDependencies(\\@products, $main::string1);", FALSE);
302 checkOperationSuccess();
303 FullByteStringList* pList = extractArray();
304 return FullByteStringListWrapper(pList);
307 /*****************************************************************************/
308 FullByteStringListWrapper XmlBuildList::getJobBuildReqs(const ByteString& rJobDir, const ByteString& rBuildReqPlatform)
309 /*****************************************************************************/
311 sv_setpv(string_obj1, rJobDir.GetBuffer());
312 sv_setpv(string_obj2, rBuildReqPlatform.GetBuffer());
313 eval_pv("@array = $main::build_lst_obj->getJobBuildReqs($main::string1, $main::string2);", FALSE);
314 checkOperationSuccess();
315 FullByteStringList* pList = extractArray();
316 return FullByteStringListWrapper(pList);
319 /*****************************************************************************/
320 ByteString XmlBuildList::getModuleDepType(const ByteString& rDepModuleName)
321 /*****************************************************************************/
323 sv_setpv(string_obj1, rDepModuleName.GetBuffer());
324 eval_pv("$main::string1 = $main::build_lst_obj->getModuleDepType($main::string1);", FALSE);
325 checkOperationSuccess();
326 char* pString = SvPV_nolen(get_sv("main::string1", FALSE));
327 ByteString sDependencyType(pString);
328 return sDependencyType;
331 /*****************************************************************************/
332 sal_Bool XmlBuildList::hasModuleDepType(FullByteStringListWrapper& rProducts, const ByteString& rDependencyType)
333 /*****************************************************************************/
335 FullByteStringListWrapper aDepencendModules = getModuleDependencies(rProducts, rDependencyType);
336 if (aDepencendModules.Count()>0)
338 return sal_True;
340 return sal_False;
343 /*****************************************************************************/
344 FullByteStringListWrapper XmlBuildList::getModuleDepTypes(FullByteStringListWrapper& rProducts)
345 /*****************************************************************************/
347 FullByteStringList * pList = new FullByteStringList();
349 ByteString aDepType = ByteString( DEP_MD_SIMPLE_STR );
350 bool bHasType = hasModuleDepType(rProducts, aDepType);
351 if (bHasType)
352 pList->Insert(new ByteString (aDepType));
354 aDepType = ByteString(DEP_MD_ALWAYS_STR);
355 bHasType = hasModuleDepType(rProducts, aDepType);
356 if (bHasType)
357 pList->Insert(new ByteString (aDepType));
359 aDepType = ByteString(DEP_MD_FORCE_STR);
360 bHasType = hasModuleDepType(rProducts, aDepType);
361 if (bHasType)
362 pList->Insert(new ByteString (aDepType));
364 return FullByteStringListWrapper (pList);
367 /*****************************************************************************/
368 FullByteStringListWrapper XmlBuildList::getModuleProducts(const ByteString& rDepModuleName)
369 /*****************************************************************************/
371 sv_setpv(string_obj1, rDepModuleName.GetBuffer());
372 eval_pv("@array = $main::build_lst_obj->getModuleProducts($main::string1);", FALSE);
373 checkOperationSuccess();
374 FullByteStringList* pList = extractArray();
375 return FullByteStringListWrapper(pList);
378 /*****************************************************************************/
379 ByteString XmlBuildList::getModuleName()
380 /*****************************************************************************/
382 eval_pv("$main::string1 = $main::build_lst_obj->getModuleName();", FALSE);
383 checkOperationSuccess();
384 char* pString = SvPV_nolen(get_sv("main::string1", FALSE));
385 ByteString sModuleName(pString);
386 return sModuleName;
389 /*****************************************************************************/
390 FullByteStringListWrapper XmlBuildList::getDirDependencies(const ByteString& rJobDir, const ByteString& rJobType, const ByteString& rJobPlatform)
391 /*****************************************************************************/
393 sv_setpv(string_obj1, rJobDir.GetBuffer());
394 sv_setpv(string_obj2, rJobType.GetBuffer());
395 sv_setpv(string_obj3, rJobPlatform.GetBuffer());
396 eval_pv("@array = $main::build_lst_obj->getDirDependencies($main::string1, $main::string2, $main::string3);", FALSE);
397 checkOperationSuccess();
398 FullByteStringList* pList = extractArray();
399 return FullByteStringListWrapper(pList);
402 /*****************************************************************************/
403 FullByteStringListWrapper XmlBuildList::getJobTypes(const ByteString& rJobDir)
404 /*****************************************************************************/
406 sv_setpv(string_obj1, rJobDir.GetBuffer());
407 eval_pv("@array = $main::build_lst_obj->getJobTypes($main::string1);", FALSE);
408 checkOperationSuccess();
409 FullByteStringList* pList = extractArray();
410 return FullByteStringListWrapper(pList);
413 /*****************************************************************************/
414 FullByteStringListWrapper XmlBuildList::getJobPlatforms(const ByteString& rJobDir)
415 /*****************************************************************************/
417 sv_setpv(string_obj1, rJobDir.GetBuffer());
418 eval_pv("@array = $main::build_lst_obj->getJobPlatforms($main::string1);", FALSE);
419 checkOperationSuccess();
420 FullByteStringList* pList = extractArray();
421 return FullByteStringListWrapper(pList);
424 /*****************************************************************************/
425 FullByteStringListWrapper XmlBuildList::getJobBuildReqPlatforms(const ByteString& rJobDir, const ByteString& rBuildReqName)
426 /*****************************************************************************/
428 sv_setpv(string_obj1, rJobDir.GetBuffer());
429 sv_setpv(string_obj2, rBuildReqName.GetBuffer());
430 eval_pv("@array = $main::build_lst_obj->getJobBuildReqPlatforms($main::string1, $main::string2);", FALSE);
431 checkOperationSuccess();
432 FullByteStringList* pList = extractArray();
433 return FullByteStringListWrapper(pList);