xref: /trunk/main/l10ntools/source/treeconfig.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir #include <vector>
2*cdf0e10cSrcweir #include <string>
3*cdf0e10cSrcweir #include <iostream>
4*cdf0e10cSrcweir #include "treeconfig.hxx"
5*cdf0e10cSrcweir #include "export.hxx"
6*cdf0e10cSrcweir #ifdef WNT
7*cdf0e10cSrcweir #include <direct.h>
8*cdf0e10cSrcweir #include <io.h>
9*cdf0e10cSrcweir #else
10*cdf0e10cSrcweir #include <dirent.h>
11*cdf0e10cSrcweir #endif
12*cdf0e10cSrcweir #include <sys/stat.h>
13*cdf0e10cSrcweir #include <unistd.h>
14*cdf0e10cSrcweir #include <stdio.h>
15*cdf0e10cSrcweir #include <stdlib.h>
16*cdf0e10cSrcweir 
17*cdf0e10cSrcweir using namespace std;
18*cdf0e10cSrcweir 
19*cdf0e10cSrcweir namespace transex3
20*cdf0e10cSrcweir {
21*cdf0e10cSrcweir 
22*cdf0e10cSrcweir bool Treeconfig::parseConfig(){
23*cdf0e10cSrcweir 
24*cdf0e10cSrcweir     string source_config_file = string( static_cast<ByteString>( Export::GetEnv("SOURCE_ROOT_DIR") ).GetBuffer() );
25*cdf0e10cSrcweir     if( source_config_file.empty() )
26*cdf0e10cSrcweir     {
27*cdf0e10cSrcweir         cerr << "Error: no suitable environment set?!?";
28*cdf0e10cSrcweir         exit( -1 );
29*cdf0e10cSrcweir     }
30*cdf0e10cSrcweir     source_config_file += string("/source_config");
31*cdf0e10cSrcweir     if( isConfigFilePresent() )
32*cdf0e10cSrcweir     {
33*cdf0e10cSrcweir         inireader.read( map , source_config_file );
34*cdf0e10cSrcweir         return true;
35*cdf0e10cSrcweir     }
36*cdf0e10cSrcweir     else return false;
37*cdf0e10cSrcweir }
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // ALWAYS add all repositories from source_config file to the container active_repos
40*cdf0e10cSrcweir // if a config_file is present ALWAYS return false
41*cdf0e10cSrcweir // if you are in the root of a repository also add it to the container active_repos
42*cdf0e10cSrcweir // if you are far inside a repository /my/path/ooo/sw/source then don't add it to the container but return true
43*cdf0e10cSrcweir // if you are in some misc place like /tmp then return true
44*cdf0e10cSrcweir // => the application can decide what to do in case the function returns true thus how to handle pwd() path
45*cdf0e10cSrcweir bool Treeconfig::getActiveRepositories( vector<string>& active_repos ){
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir     bool isPresent = isConfigFilePresent();
48*cdf0e10cSrcweir     bool hasPath   = false;
49*cdf0e10cSrcweir     string pwd;
50*cdf0e10cSrcweir     string guessedRepo;
51*cdf0e10cSrcweir     Export::getCurrentDir( pwd );
52*cdf0e10cSrcweir     string source_root = Export::GetEnv( "SOURCE_ROOT_DIR" );
53*cdf0e10cSrcweir     string solarsrc    = Export::GetEnv( "SOLARSRC" );
54*cdf0e10cSrcweir     string partial;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir     // if we are inside of a repository root then active it otherwise let the app handle the return!
57*cdf0e10cSrcweir     string::size_type pos = pwd.find_first_of( source_root );
58*cdf0e10cSrcweir     if( pos != string::npos && ( pos + source_root.length() +1 ) < pwd.length()){  // I am within SOURCE_ROOT_DIR
59*cdf0e10cSrcweir         partial = pwd.substr( pos + source_root.length() +1  , pwd.length());
60*cdf0e10cSrcweir         string::size_type nextPart = partial.find_first_of( "/" );
61*cdf0e10cSrcweir         if( nextPart != string::npos )
62*cdf0e10cSrcweir             hasPath = true;
63*cdf0e10cSrcweir         else
64*cdf0e10cSrcweir             guessedRepo = partial;
65*cdf0e10cSrcweir     }
66*cdf0e10cSrcweir     else                              // I am NOT within SOURCE_ROOT_DIR
67*cdf0e10cSrcweir         hasPath = true;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     if( isPresent )
70*cdf0e10cSrcweir     {
71*cdf0e10cSrcweir         hasPath = false;                // if config_file is present don't care about pwd
72*cdf0e10cSrcweir         stringmap* repos = static_cast<stringmap*>( map[ string("repositories") ] );
73*cdf0e10cSrcweir         if( repos != 0 )
74*cdf0e10cSrcweir         {
75*cdf0e10cSrcweir             for( stringmap::iterator iter = repos->begin() ; iter != repos->end() ; ++iter )
76*cdf0e10cSrcweir             {
77*cdf0e10cSrcweir                 if( static_cast<string>( iter->second ) == string( "active" ) )
78*cdf0e10cSrcweir                 {
79*cdf0e10cSrcweir                     active_repos.push_back( iter->first );
80*cdf0e10cSrcweir                     if( static_cast<string>( iter->first ) == guessedRepo )
81*cdf0e10cSrcweir                     {
82*cdf0e10cSrcweir                         guessedRepo.clear();            // don't add double in case it is present in config_file
83*cdf0e10cSrcweir                     }
84*cdf0e10cSrcweir                 }
85*cdf0e10cSrcweir             }
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir         else
88*cdf0e10cSrcweir         {
89*cdf0e10cSrcweir             cerr << "Error: source_config files doesn't contain a 'repositories' section ?!?";
90*cdf0e10cSrcweir             exit( -1 );
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir     }
93*cdf0e10cSrcweir     if( !guessedRepo.empty() ){
94*cdf0e10cSrcweir         active_repos.push_back( guessedRepo );          // add myrepo
95*cdf0e10cSrcweir     }
96*cdf0e10cSrcweir     return hasPath;                                     // are we deep inside of a source tree or outside of SOURCE_ROOT_DIR?
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir void Treeconfig::getCurrentDir( string& dir )
100*cdf0e10cSrcweir {
101*cdf0e10cSrcweir     char buffer[64000];
102*cdf0e10cSrcweir     if( getcwd( buffer , sizeof( buffer ) ) == 0 ){
103*cdf0e10cSrcweir         cerr << "Error: getcwd failed!\n";
104*cdf0e10cSrcweir         exit( -1 );
105*cdf0e10cSrcweir     }
106*cdf0e10cSrcweir     dir = string( buffer );
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir bool Treeconfig::isConfigFilePresent()
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir     string config_file = Export::GetEnv( "SOURCE_ROOT_DIR" );
112*cdf0e10cSrcweir     config_file += "/source_config";
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     struct stat status;
115*cdf0e10cSrcweir     if( stat( config_file.c_str() , &status ) < 0 )
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         return false;
118*cdf0e10cSrcweir     }
119*cdf0e10cSrcweir #ifdef WNT
120*cdf0e10cSrcweir     return ( status.st_mode & _S_IFREG ) && ( _access( config_file.c_str() , 4 ) >= 0 ) ;
121*cdf0e10cSrcweir #else
122*cdf0e10cSrcweir     return ( status.st_mode & S_IFREG ) && ( access( config_file.c_str() , R_OK ) >= 0 ) ;
123*cdf0e10cSrcweir #endif
124*cdf0e10cSrcweir }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir }
129