update dev300-m57
[ooovba.git] / soldep / bootstrp / XmlBuildList.cxx
blob875723a0ed867454529fc5238fc23e484f028cb7
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 #ifdef MACOSX
79 *file=0; // how does this works???
80 sp=0;
81 #endif
84 FullByteStringList::FullByteStringList()
85 : FullByteStingListImpl(),
86 nRef (0)
90 FullByteStringList::~FullByteStringList()
92 ByteString* pStr = First();
93 while (pStr)
95 delete pStr;
96 pStr = Next();
100 ULONG FullByteStringList::GetPos (ByteString& rStr)
102 ULONG nCurPos = GetCurPos();
103 ULONG nPos = 0;
104 ByteString* pStr = First();
105 while (pStr)
107 if (*pStr == rStr)
109 GetObject (nCurPos);
110 return nPos;
112 pStr = Next();
113 nPos ++;
115 GetObject (nCurPos);
116 return LIST_ENTRY_NOTFOUND;
119 FullByteStringListWrapper& FullByteStringListWrapper::operator=( const FullByteStringListWrapper& rFullByteStringListWrapper )
121 if (pStringList)
122 if (pStringList->GetRef() == 0)
123 delete pStringList;
124 else
125 pStringList->DecRef();
127 pStringList = rFullByteStringListWrapper.GetStringList();
128 if (pStringList)
129 pStringList->IncRef();
130 return *this;
133 FullByteStringListWrapper::~FullByteStringListWrapper ()
135 if (pStringList)
137 if (pStringList->GetRef() == 0)
138 delete pStringList;
139 else
140 pStringList->DecRef();
145 // Function initializes Perl
146 // ATTENTION: No check built in - YET TO DO
148 void XmlBuildList::initPerl(const char* ModulePath) {
149 if (PerlInitialized)
150 return;
151 my_perl = perl_alloc();
152 if (!my_perl)
153 throw XmlBuildListException("Cannot initialize perl");
154 perl_construct(my_perl);
155 char* embedding[] = {"", "-e", "0"};
156 int exitstatus = perl_parse(my_perl, xs_init, 3, embedding, (char **)NULL);
157 if (!exitstatus) {
158 dl_init(aTHX);
159 exitstatus = perl_run(my_perl);
161 //perl_run(my_perl);
162 // Create a variable (s. perlguts)
163 SV* sv = get_sv("main::module_path", TRUE);
164 sv_setpv(sv, ModulePath);
165 eval_pv("use lib $main::module_path; use XMLBuildListParser;", FALSE);
166 checkOperationSuccess();
167 eval_pv("$main::build_lst_obj = XMLBuildListParser->new();", FALSE);
168 checkOperationSuccess();
169 eval_pv("@array = ();", FALSE);
170 checkOperationSuccess();
171 PerlInitialized = TRUE;
174 // Function proves if the $@ perl variable is set, if yes -
175 // last operation was unsuccessfull -> throws exception
176 void XmlBuildList::checkOperationSuccess() {
177 char* op_result = SvPV_nolen(get_sv("main::@", FALSE));
178 if (strcmp(op_result, ""))
179 throw XmlBuildListException(op_result);
183 // Function generates a regular array with NULL as last element
184 // from the Perl-object @array
186 FullByteStringList* XmlBuildList::extractArray() {
187 FullByteStringList* pStringList = new FullByteStringList();
188 AV* theArrayObj = get_av("main::array", FALSE);
189 I32 arrayLength = av_len(theArrayObj); // $#array value
190 if (arrayLength == -1)
191 return pStringList;
193 SV** string_ptr;
194 char* pStoredString;
195 // populate vector with strings (char*)
196 for (int i = 0; i <= arrayLength; i++) {
197 string_ptr = av_fetch(theArrayObj, i, NULL);
198 //pStoredString = savepv(SvPV_nolen(*string_ptr));
199 pStoredString = SvPV_nolen(*string_ptr);
200 ByteString* pStr = new ByteString(pStoredString);
201 pStringList->Insert(pStr, LIST_APPEND);
203 return pStringList;
207 char* XmlBuildList::getError() {
208 eval_pv("$main::string1 = $main::build_lst_obj->getError();", FALSE);
209 checkOperationSuccess();
210 return SvPV_nolen(get_sv("main::string1", FALSE));
213 /*****************************************************************************/
214 XmlBuildList::XmlBuildList(const ByteString& rModulePath)
215 /*****************************************************************************/
216 : PerlInitialized (FALSE)
218 initPerl(rModulePath.GetBuffer());
219 string_obj1 = get_sv("main::string1", TRUE);
220 string_obj2 = get_sv("main::string2", TRUE);
221 string_obj3 = get_sv("main::string3", TRUE);
222 if (!(string_obj1 && string_obj2 && string_obj3))
223 throw XmlBuildListException("Cannot initialize Perl string objects");
227 // Function uninitializes Perl
229 /*****************************************************************************/
230 XmlBuildList::~XmlBuildList()
231 /*****************************************************************************/
233 if (!PerlInitialized)
234 return;
235 perl_destruct(my_perl);
236 perl_free(my_perl);
237 PerlInitialized = FALSE;
240 /*****************************************************************************/
241 void XmlBuildList::loadXMLFile(const ByteString& rBuildList)
242 /*****************************************************************************/
244 sv_setpv(string_obj1, rBuildList.GetBuffer());
245 eval_pv("$main::string2 = $main::build_lst_obj->loadXMLFile($main::string1);", FALSE);
246 checkOperationSuccess();
247 if(!SvTRUE(string_obj2)) {
248 const char* Message = getError();
249 throw XmlBuildListException(Message);
253 /*****************************************************************************/
254 FullByteStringListWrapper XmlBuildList::getProducts()
255 /*****************************************************************************/
257 eval_pv("@array = $main::build_lst_obj->getProducts();", FALSE);
258 checkOperationSuccess();
259 FullByteStringList* pList = extractArray();
260 return FullByteStringListWrapper(pList);
263 /*****************************************************************************/
264 FullByteStringListWrapper XmlBuildList::getJobDirectories(const ByteString& rJobType, const ByteString& rJobPlatform)
265 /*****************************************************************************/
267 sv_setpv(string_obj1, rJobType.GetBuffer());
268 sv_setpv(string_obj2, rJobPlatform.GetBuffer());
269 eval_pv("@array = $main::build_lst_obj->getJobDirectories($main::string1, $main::string2);", FALSE);
270 checkOperationSuccess();
271 FullByteStringList* pList = extractArray();
272 return FullByteStringListWrapper(pList);
275 /*****************************************************************************/
276 FullByteStringListWrapper XmlBuildList::getModuleDependencies(const ByteString& rProduct, const ByteString& rDependencyType)
277 /*****************************************************************************/
279 FullByteStringList* pProducts = new FullByteStringList();
280 FullByteStringListWrapper aProducts (pProducts);
281 if (rProduct != "")
282 aProducts.Insert (new ByteString(rProduct), LIST_APPEND);
283 return getModuleDependencies(aProducts, rDependencyType);
286 /*****************************************************************************/
287 FullByteStringListWrapper XmlBuildList::getModuleDependencies(FullByteStringListWrapper& rProducts, const ByteString& rDependencyType)
288 /*****************************************************************************/
290 eval_pv("@products = ();", FALSE);
291 checkOperationSuccess();
292 AV* theArrayObj = get_av("main::products", FALSE);
293 FullByteStringList* pProducts = rProducts.GetStringList();
294 ByteString* pStr = pProducts->First();
295 while (pStr)
297 sv_setpv(string_obj2, pStr->GetBuffer());
298 av_push(theArrayObj, string_obj2);
299 pStr = pProducts->Next();
302 sv_setpv(string_obj1, rDependencyType.GetBuffer());
303 eval_pv("@array = $main::build_lst_obj->getModuleDependencies(\\@products, $main::string1);", FALSE);
304 checkOperationSuccess();
305 FullByteStringList* pList = extractArray();
306 return FullByteStringListWrapper(pList);
309 /*****************************************************************************/
310 FullByteStringListWrapper XmlBuildList::getJobBuildReqs(const ByteString& rJobDir, const ByteString& rBuildReqPlatform)
311 /*****************************************************************************/
313 sv_setpv(string_obj1, rJobDir.GetBuffer());
314 sv_setpv(string_obj2, rBuildReqPlatform.GetBuffer());
315 eval_pv("@array = $main::build_lst_obj->getJobBuildReqs($main::string1, $main::string2);", FALSE);
316 checkOperationSuccess();
317 FullByteStringList* pList = extractArray();
318 return FullByteStringListWrapper(pList);
321 /*****************************************************************************/
322 ByteString XmlBuildList::getModuleDepType(const ByteString& rDepModuleName)
323 /*****************************************************************************/
325 sv_setpv(string_obj1, rDepModuleName.GetBuffer());
326 eval_pv("$main::string1 = $main::build_lst_obj->getModuleDepType($main::string1);", FALSE);
327 checkOperationSuccess();
328 char* pString = SvPV_nolen(get_sv("main::string1", FALSE));
329 ByteString sDependencyType(pString);
330 return sDependencyType;
333 /*****************************************************************************/
334 sal_Bool XmlBuildList::hasModuleDepType(FullByteStringListWrapper& rProducts, const ByteString& rDependencyType)
335 /*****************************************************************************/
337 FullByteStringListWrapper aDepencendModules = getModuleDependencies(rProducts, rDependencyType);
338 if (aDepencendModules.Count()>0)
340 return sal_True;
342 return sal_False;
345 /*****************************************************************************/
346 FullByteStringListWrapper XmlBuildList::getModuleDepTypes(FullByteStringListWrapper& rProducts)
347 /*****************************************************************************/
349 FullByteStringList * pList = new FullByteStringList();
351 ByteString aDepType = ByteString( DEP_MD_SIMPLE_STR );
352 bool bHasType = hasModuleDepType(rProducts, aDepType);
353 if (bHasType)
354 pList->Insert(new ByteString (aDepType));
356 aDepType = ByteString(DEP_MD_ALWAYS_STR);
357 bHasType = hasModuleDepType(rProducts, aDepType);
358 if (bHasType)
359 pList->Insert(new ByteString (aDepType));
361 aDepType = ByteString(DEP_MD_FORCE_STR);
362 bHasType = hasModuleDepType(rProducts, aDepType);
363 if (bHasType)
364 pList->Insert(new ByteString (aDepType));
366 return FullByteStringListWrapper (pList);
369 /*****************************************************************************/
370 FullByteStringListWrapper XmlBuildList::getModuleProducts(const ByteString& rDepModuleName)
371 /*****************************************************************************/
373 sv_setpv(string_obj1, rDepModuleName.GetBuffer());
374 eval_pv("@array = $main::build_lst_obj->getModuleProducts($main::string1);", FALSE);
375 checkOperationSuccess();
376 FullByteStringList* pList = extractArray();
377 return FullByteStringListWrapper(pList);
380 /*****************************************************************************/
381 ByteString XmlBuildList::getModuleName()
382 /*****************************************************************************/
384 eval_pv("$main::string1 = $main::build_lst_obj->getModuleName();", FALSE);
385 checkOperationSuccess();
386 char* pString = SvPV_nolen(get_sv("main::string1", FALSE));
387 ByteString sModuleName(pString);
388 return sModuleName;
391 /*****************************************************************************/
392 FullByteStringListWrapper XmlBuildList::getDirDependencies(const ByteString& rJobDir, const ByteString& rJobType, const ByteString& rJobPlatform)
393 /*****************************************************************************/
395 sv_setpv(string_obj1, rJobDir.GetBuffer());
396 sv_setpv(string_obj2, rJobType.GetBuffer());
397 sv_setpv(string_obj3, rJobPlatform.GetBuffer());
398 eval_pv("@array = $main::build_lst_obj->getDirDependencies($main::string1, $main::string2, $main::string3);", FALSE);
399 checkOperationSuccess();
400 FullByteStringList* pList = extractArray();
401 return FullByteStringListWrapper(pList);
404 /*****************************************************************************/
405 FullByteStringListWrapper XmlBuildList::getJobTypes(const ByteString& rJobDir)
406 /*****************************************************************************/
408 sv_setpv(string_obj1, rJobDir.GetBuffer());
409 eval_pv("@array = $main::build_lst_obj->getJobTypes($main::string1);", FALSE);
410 checkOperationSuccess();
411 FullByteStringList* pList = extractArray();
412 return FullByteStringListWrapper(pList);
415 /*****************************************************************************/
416 FullByteStringListWrapper XmlBuildList::getJobPlatforms(const ByteString& rJobDir)
417 /*****************************************************************************/
419 sv_setpv(string_obj1, rJobDir.GetBuffer());
420 eval_pv("@array = $main::build_lst_obj->getJobPlatforms($main::string1);", FALSE);
421 checkOperationSuccess();
422 FullByteStringList* pList = extractArray();
423 return FullByteStringListWrapper(pList);
426 /*****************************************************************************/
427 FullByteStringListWrapper XmlBuildList::getJobBuildReqPlatforms(const ByteString& rJobDir, const ByteString& rBuildReqName)
428 /*****************************************************************************/
430 sv_setpv(string_obj1, rJobDir.GetBuffer());
431 sv_setpv(string_obj2, rBuildReqName.GetBuffer());
432 eval_pv("@array = $main::build_lst_obj->getJobBuildReqPlatforms($main::string1, $main::string2);", FALSE);
433 checkOperationSuccess();
434 FullByteStringList* pList = extractArray();
435 return FullByteStringListWrapper(pList);