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