summaryrefslogtreecommitdiff
path: root/soldep/bootstrp/prodmap.cxx
blob: 0c5ce1e66f14e446aa7e6818d79914582e1f95f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
 /*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include "prodmap.hxx"
#include <tools/geninfo.hxx>
#include <tools/fsys.hxx>
#include "minormk.hxx"

#include <stdio.h>

#define PRODUCT_KEY     "TARGETDESCRIPTION/PRODUCTS"
#define DEPENDS_ON_KEY  "TARGETDESCRIPTION/DEPENDSON"
#define BASED_ON_KEY    "TARGETDESCRIPTION/BASEDON"

//
// class ProductMapper
//

/*****************************************************************************/
ProductMapper::ProductMapper()
/*****************************************************************************/
                : pVersionList( NULL ),
                pProductList( NULL )
{
}

/*****************************************************************************/
ProductMapper::ProductMapper( GenericInformationList *pVerList )
/*****************************************************************************/
                : pVersionList( pVerList ),
                pProductList( NULL )
{
    if ( pVerList )
        CreateProductList( pVerList );
}

/*****************************************************************************/
ProductMapper::~ProductMapper()
/*****************************************************************************/
{
    delete pProductList;
}

/*****************************************************************************/
void ProductMapper::CreateProductList( GenericInformationList *pVerList )
/*****************************************************************************/
{
    /*
    creates a list of the following format:

    ProductName Workspace                // 6.0 Final SRC641
    {                                    // {
        DependsOn                        //     DependsOn
        {                                //     {
            product1                     //
            product2                     //
            ...                          //
        }                                //
        BasedOn                          //
        {                                //
            productX                     //
            productY                     //
            ...                          //
        }                                //
    }                                    //
    */

    delete pProductList;
    pProductList = NULL;

    pVersionList = pVerList;

    if ( pVersionList ) {
        ByteString sProductKey( PRODUCT_KEY );
        ByteString sDependsOnKey( DEPENDS_ON_KEY );
        ByteString sBasedOnKey( BASED_ON_KEY );

        for ( sal_uIntPtr i = 0; i < pVersionList->Count(); i++ ) {
            GenericInformation *pVersion = pVersionList->GetObject( i );

            GenericInformation *pProducts = pVersion->GetSubInfo( sProductKey, sal_True );
            if ( pProducts ) {
                ByteString sProducts = pProducts->GetValue();

                ByteString sDependsOn;
                GenericInformation *pDependsOn = pVersion->GetSubInfo( sDependsOnKey, sal_True );
                if ( pDependsOn )
                    sDependsOn = pDependsOn->GetValue();

                ByteString sBasedOn;
                GenericInformation *pBasedOn = pVersion->GetSubInfo( sBasedOnKey, sal_True );
                if ( pBasedOn )
                    sBasedOn = pBasedOn->GetValue();

                for ( sal_uInt16 x = 0; x < sProducts.GetTokenCount( ';' ); x++ ) {
                    ByteString sProduct( sProducts.GetToken( x, ';' ));
                    if( sProduct.Len()) {
                        if ( !pProductList )
                            pProductList = new GenericInformationList();

                        pProductList->InsertInfo( sProduct, *pVersion, sal_True, sal_True );

                        for ( sal_uInt16 y = 0; y < sDependsOn.GetTokenCount( ';' ); y++ ) {
                            ByteString sDependsOnKey_l = sProduct;
                            sDependsOnKey_l += "/DependsOn/";
                            sDependsOnKey_l += sDependsOn.GetToken( y, ';' );

                            pProductList->InsertInfo( sDependsOnKey_l, "", sal_True, sal_True );
                        }
                        for ( sal_uInt16 z = 0; z < sBasedOn.GetTokenCount( ';' ); z++ ) {
                            ByteString sBasedOnKey_l = sProduct;
                            sBasedOnKey_l += "/BasedOn/";
                            sBasedOnKey_l += sBasedOn.GetToken( z, ';' );

                            pProductList->InsertInfo( sBasedOnKey_l, "", sal_True, sal_True );
                        }
                    }
                }
            }
        }
    }
}

/*****************************************************************************/
sal_uInt16 ProductMapper::GetProductInformation(
    const ByteString &rProduct, GenericInformation *& pProductInfo )
/*****************************************************************************/
{
    pProductInfo = NULL;

    if ( !pVersionList )
        return PRODUCT_MAPPER_NO_VERSION_INFORMATION;

    if ( !pProductList )
        return PRODUCT_MAPPER_NO_PRODUCT;

    ByteString sProductKey( rProduct );
    pProductInfo = pProductList->GetInfo( sProductKey, sal_True );

    if ( !pProductInfo )
        return PRODUCT_MAPPER_NO_PRODUCT;

    return PRODUCT_MAPPER_OK;
}

/*****************************************************************************/
sal_uInt16 ProductMapper::PrintDependentTargets(
    const ByteString &rProduct, sal_uInt16 nLevel )
/*****************************************************************************/
{
    GenericInformation *pProductInfo;

    sal_uInt16 nReturn = GetProductInformation( rProduct, pProductInfo );

    if ( nReturn == PRODUCT_MAPPER_OK ) {
        for ( sal_uInt16 i = 0; i < nLevel; i++ )
            fprintf( stdout, "    " );
        fprintf( stdout, "%s (%s)\n", pProductInfo->GetBuffer(),
            pProductInfo->GetValue().GetBuffer());
        aPrintedList.PutString( new ByteString( *pProductInfo ));

        for ( sal_uIntPtr j = 0; j < pProductList->Count(); j++ ) {
            GenericInformation *pCandidate = pProductList->GetObject( j );
            ByteString sKey( "DEPENDSON/" );
            sKey += rProduct;
            GenericInformation *pDependsOn = pCandidate->GetSubInfo( sKey, sal_True );
            if ( pDependsOn )
                PrintDependentTargets( *pCandidate, nLevel + 1 );
        }
        if ( !nLevel ) {
            ByteString sKey( "BASEDON" );
            GenericInformation *pBasedOn = pProductInfo->GetSubInfo( sKey );
            if ( pBasedOn ) {
                GenericInformationList *pBases = pBasedOn->GetSubList();
                if ( pBases ) {
                    for ( sal_uIntPtr k = 0; k < pBases->Count(); k++ ) {
                        aBaseList.PutString( new ByteString( *pBases->GetObject( k )));
                    }
                }
            }
        }
    }

    return nReturn;
}

/*****************************************************************************/
sal_uInt16 ProductMapper::PrintAndDeleteBaseList()
/*****************************************************************************/
{
    if ( aBaseList.Count()) {
        fprintf( stdout, "\nbased on\n" );
        while ( aBaseList.Count()) {
            ByteString sProduct( *aBaseList.GetObject(( sal_uIntPtr ) 0 ));
            if ( aPrintedList.IsString( aBaseList.GetObject(( sal_uIntPtr ) 0 )) == NOT_THERE ) {
                aPrintedList.PutString( aBaseList.GetObject(( sal_uIntPtr ) 0 ));
                PrintDependentTargets( sProduct );
            }
            else
                delete aBaseList.GetObject(( sal_uIntPtr ) 0 );

            aBaseList.Remove(( sal_uIntPtr ) 0 );
        }
        while ( aPrintedList.Count())
            delete aPrintedList.Remove(( sal_uIntPtr ) 0 );

        fprintf( stdout, "\n" );
    }
    return PRODUCT_MAPPER_OK;
}

/*****************************************************************************/
sal_uInt16 ProductMapper::PrintDependencies( const ByteString &rProduct )
/*****************************************************************************/
{
    sal_uInt16 nResult = PrintDependentTargets( rProduct );
    PrintAndDeleteBaseList();
    return nResult;
}

/*****************************************************************************/
sal_uInt16 ProductMapper::PrintProductList()
/*****************************************************************************/
{
    if ( !pVersionList )
        return PRODUCT_MAPPER_NO_VERSION_INFORMATION;

    if ( !pProductList || !pProductList->Count())
        return PRODUCT_MAPPER_NO_PRODUCT;

    if ( pProductList->Count()) {
        for ( sal_uIntPtr i = 0; i < pProductList->Count(); i++ )
            fprintf( stdout, "%s (%s)\n",
                pProductList->GetObject( i )->GetBuffer(),
                pProductList->GetObject( i )->GetValue().GetBuffer());
        fprintf( stdout, "\n" );
    }

    return PRODUCT_MAPPER_OK;
}

/*****************************************************************************/
SByteStringList *ProductMapper::GetMinorList(
    const ByteString &rVersion, const ByteString &rEnvironment )
/*****************************************************************************/
{
    SByteStringList *pList = NULL;

    if ( pVersionList ) {
        String sRoot( GetVersionRoot( pVersionList, rVersion ));
        if ( sRoot.Len()) {
            DirEntry aEntry( sRoot );
            aEntry += DirEntry( String( rEnvironment, RTL_TEXTENCODING_ASCII_US ));
            String sWildcard( String::CreateFromAscii( "inc.*" ));
            aEntry += DirEntry( sWildcard );

            Dir aDir( aEntry, FSYS_KIND_DIR );
            for ( sal_uInt16 i = 0; i < aDir.Count(); i++ ) {
                ByteString sInc( aDir[ i ].GetName(), RTL_TEXTENCODING_ASCII_US );
                if ( sInc.GetTokenCount( '.' ) > 1 ) {
                    if ( !pList )
                        pList = new SByteStringList();
                    pList->PutString( new ByteString( sInc.GetToken( 1, '.' )));
                }
            }
        }
    }
    return pList;
}

/*****************************************************************************/
String ProductMapper::GetVersionRoot(
    GenericInformationList *pList, const ByteString &rVersion )
/*****************************************************************************/
{
    ByteString sKey( rVersion );
    GenericInformation *pVersion = pList->GetInfo( sKey );
    if ( pVersion ) {
#ifdef UNX
        sKey = "drives/o:/unixvolume";
        GenericInformation *pUnixVolume = pVersion->GetSubInfo( sKey, sal_True );
        ByteString sPath;
        if ( pUnixVolume )
            sPath = pUnixVolume->GetValue();
        sPath += "/";
#else
        ByteString sPath( "o:\\" );
#endif
        sKey = "settings/path";
        GenericInformation *pPath = pVersion->GetSubInfo( sKey, sal_True );
        if ( pPath ) {
            sPath += pPath->GetValue().GetToken( 0, '\\' );
            sPath += "/";
        }
#ifdef UNX
        sPath.SearchAndReplaceAll( "\\", "/" );
        while( sPath.SearchAndReplace( "//", "/" ) != STRING_NOTFOUND ) {};
#else
        sPath.SearchAndReplaceAll( "/", "\\" );
        while( sPath.SearchAndReplace( "\\\\", "\\" ) != STRING_NOTFOUND ) {};
#endif

        return String( sPath, RTL_TEXTENCODING_ASCII_US );
    }
    return String();
}

/*****************************************************************************/
BaseProductList *ProductMapper::GetBases(
    GenericInformation *pProductInfo, sal_uInt16 nLevel,
    BaseProductList *pBases )
/*****************************************************************************/
{
    if ( !pBases )
        pBases = new BaseProductList();

    if ( pProductInfo ) {
        ByteString sCandidate( *pProductInfo );
        sCandidate += " (";
        sCandidate += pProductInfo->GetValue();
        sCandidate += ")";

        ByteString sKey( "BASEDON" );
        GenericInformation *pBasedOn = pProductInfo->GetSubInfo( sKey );
        if ( pBasedOn ) {
            GenericInformationList *pBasesInfo = pBasedOn->GetSubList();
            if ( pBasesInfo ) {
                for ( sal_uIntPtr k = 0; k < pBasesInfo->Count(); k++ ) {
                    GenericInformation *pBaseProduct;
                    if ( GetProductInformation( *pBasesInfo->GetObject( k ), pBaseProduct ) == PRODUCT_MAPPER_OK )
                        GetBases( pBaseProduct, ++ nLevel, pBases );
                }
            }
        }
        sal_Bool bFound = sal_False;
        ByteString sUpperCandidate( sCandidate );
        sUpperCandidate.ToUpperAscii();
        for ( sal_uInt16 i = 0; i < pBases->Count() && !bFound; i++ ) {
            ByteString sTest( *pBases->GetObject( i ));
            if ( sTest.ToUpperAscii() == sUpperCandidate )
                bFound = sal_True;
        }
        if ( !bFound )
            pBases->Insert( new ByteString( sCandidate ), ( sal_uIntPtr ) 0 );
    }
    return pBases;
}

/*****************************************************************************/
sal_uInt16 ProductMapper::PrintMinorList(
    const ByteString rProduct, const ByteString rEnvironment )
/*****************************************************************************/
{
    if ( !pVersionList )
        return PRODUCT_MAPPER_NO_VERSION_INFORMATION;

    if ( !pProductList || !pProductList->Count())
        return PRODUCT_MAPPER_NO_PRODUCT;

    GenericInformation *pProductInfo;
    GetProductInformation( rProduct, pProductInfo );
    if ( !pProductInfo )
        return PRODUCT_MAPPER_NO_PRODUCT;

    BaseProductList *pBases = GetBases( pProductInfo );
    if ( pBases->Count()) {
        if ( pBases->Count() > 1 )
            fprintf( stdout, "Product \"%s\" based on ", pBases->GetObject(( sal_uIntPtr ) 0 )->GetBuffer());
        else
            fprintf( stdout, "Product \"%s\" based on no other products", pBases->GetObject(( sal_uIntPtr ) 0 )->GetBuffer());

        for ( sal_uIntPtr i = 1; i < pBases->Count(); i++ ) {
            fprintf( stdout, "\"%s\"", pBases->GetObject( i )->GetBuffer());
            if ( i < pBases->Count() - 1 )
                fprintf( stdout, ", " );
        }
        fprintf( stdout, "\n\n" );
    }
    sal_uInt16 nResult = PRODUCT_MAPPER_OK;

    if ( rEnvironment.Len())
        nResult = PrintSingleMinorList( pProductInfo, pBases, rEnvironment );
    else {
        ByteString sEnvKey( pProductInfo->GetValue());
        sEnvKey += "/Environments";

        GenericInformation *pEnvironmentInfo = pVersionList->GetInfo( sEnvKey, sal_True );
        if ( pEnvironmentInfo ) {
            GenericInformationList *pEnvironmentList = pEnvironmentInfo->GetSubList();
            if ( pEnvironmentList ) {
                for ( sal_uIntPtr i = 0; i < pEnvironmentList->Count(); i++ ) {
                    sal_uInt16 nTmp = PrintSingleMinorList( pProductInfo, pBases, *pEnvironmentList->GetObject( i ));
                    if ( nTmp != PRODUCT_MAPPER_OK )
                        nResult = nTmp;
                }
            }
        }
    }

    for ( sal_uIntPtr m = 0; m < pBases->Count(); m++ )
        delete pBases->GetObject( m );
    delete pBases;

    return nResult;
}

/*****************************************************************************/
sal_uInt16 ProductMapper::PrintSingleMinorList(
    GenericInformation *pProductInfo, BaseProductList *pBases,
    const ByteString rEnvironment )
/*****************************************************************************/
{
    DirEntry aRoot( GetVersionRoot( pVersionList, pProductInfo->GetValue()));
    aRoot += DirEntry( String( rEnvironment, RTL_TEXTENCODING_ASCII_US ));
    if ( !aRoot.Exists())
        return PRODUCT_MAPPER_OK;

    SByteStringList *pMinors = GetMinorList( pProductInfo->GetValue(), rEnvironment );
    if ( !pMinors )
        pMinors = new SByteStringList();
    pMinors->Insert( new ByteString( "" ), LIST_APPEND );

    SByteStringList aOutputList;
    sal_Bool bUnknownMinor = sal_False;
    for ( sal_uIntPtr i = 0; i < pMinors->Count(); i++ ) {
        ByteString sOutput;
        ByteString sProductVersion;

        for ( sal_uIntPtr j = 0; j < pBases->Count(); j++ ) {
            ByteString sCurProduct( *pBases->GetObject( j ));
            ByteString sVersion( sCurProduct.GetToken( sCurProduct.GetTokenCount( '(' ) - 1, '(' ).GetToken( 0, ')' ));
            if ( !j )
                sProductVersion = sVersion;

            MinorMk *pMinorMk = new MinorMk(
                pVersionList, sProductVersion, sVersion, rEnvironment, *pMinors->GetObject( i ));

            ByteString sMinor( pMinorMk->GetLastMinor().GetBuffer());
            if ( !sMinor.Len()) {
                sMinor = "!";
                bUnknownMinor = sal_True;
            }
            if ( j == 0 ) {
                sOutput += pMinorMk->GetBuildNr();
                sOutput += " ";

                if ( i == pMinors->Count() - 1 )
                    sOutput += "flat: ";
                else
                    sOutput += "      ";
            }
            sOutput += sVersion;
            sOutput += ".";
            sOutput += sMinor;
            sOutput += "(";
            sOutput += pMinorMk->GetBuildNr();
            sOutput += ")  ";
        }
        aOutputList.PutString( new ByteString( sOutput ));
    }
    ByteString sOldMinor;

    if ( aOutputList.Count())
        fprintf( stdout, "Available builds on %s:\n", rEnvironment.GetBuffer());

    for ( sal_uIntPtr o = 0; o < aOutputList.Count(); o++ ) {
        ByteString sOutput( *aOutputList.GetObject( o ));
        sOutput = sOutput.Copy( sOutput.GetToken( 0, ' ' ).Len() + 1 );

        ByteString sCurMinor( sOutput.GetToken( 1, '.' ).GetToken( 0, '(' ));
        if ( sOldMinor.Len() && sCurMinor < sOldMinor ) {
            fprintf( stdout, "      ----------\n" );
        }
        sOldMinor = sCurMinor;

        fprintf( stdout, "%s\n", sOutput.GetBuffer());
        delete aOutputList.GetObject( o );
    }
    if ( bUnknownMinor )
        fprintf( stdout, "Symbol ! indcates that at least one minor could not be found\n\n" );
    else if ( aOutputList.Count())
        fprintf( stdout, "\n" );

    for ( sal_uIntPtr l = 0; l < pMinors->Count(); l++ )
        delete pMinors->GetObject( l );
    delete pMinors;

    return PRODUCT_MAPPER_OK;
}