File: sopranodirs.cpp

package info (click to toggle)
soprano 2.9.4+dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,008 kB
  • ctags: 5,755
  • sloc: cpp: 38,602; xml: 155; ansic: 143; makefile: 25; java: 14
file content (167 lines) | stat: -rw-r--r-- 5,013 bytes parent folder | download | duplicates (2)
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
/*
 * This file is part of Soprano Project.
 *
 * Copyright (C) 2007-2009 Sebastian Trueg <trueg@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "sopranodirs.h"
#include "soprano-config.h"

#include <QtCore/QLibrary>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>

#ifdef Q_OS_WIN
static inline QString getWinPrefix()
{
    // returns the parent directory of the application
    return QDir( QCoreApplication::applicationDirPath() ).absoluteFilePath( ".." );
}
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif



namespace {
    QStringList makeLibNames( const QString& libname )
    {
        int pos = libname.lastIndexOf('/');
        if (pos < 0) {
            pos = 0;
        }

        QStringList libNames;
        if (libname.indexOf('.', pos) < 0) {
            const char* const extList[] = { ".so", ".dylib", ".bundle", ".sl", ".dll", "d.dll", 0 };
            for ( int i = 0; extList[i]; ++i ) {
                if ( QLibrary::isLibrary( libname + extList[i] ) ) {
                    libNames.append( libname + extList[i] );
                }
                if ( QLibrary::isLibrary( QLatin1String( "lib" ) + libname + extList[i] ) ) {
                    libNames.append( QLatin1String( "lib" ) + libname + extList[i] );
                }
            }
        }
        else {
            libNames.append(libname);
        }
        return libNames;
    }
}


QString Soprano::findLibraryPath( const QString& libName, const QStringList& extraDirs, const QStringList& subDirs_ )
{
    // paths to search for libs
    QStringList dirs = libDirs() + extraDirs;

    // subdirs to search
    QStringList subDirs;
    foreach( const QString& subDir, subDirs_ ) {
        QString s( subDir );
        if ( !s.endsWith( '/' ) )
            s += '/';
        subDirs << s;
    }
    // we add the empty string to be able to handle all in one loop below
    subDirs << QString();

    QStringList libs = makeLibNames( libName );
    Q_FOREACH( const QString& lib, libs ) {
        if ( lib.startsWith( '/' ) ) {
            QFileInfo fi( lib );
            if ( fi.isFile() )
                return fi.absoluteFilePath();
        }
        else {
            foreach( const QString& dir, dirs ) {
                foreach( const QString& subDir, subDirs ) {
                    QFileInfo fi( dir + '/' + subDir + lib );
                    if ( fi.isFile() ) {
                        return fi.absoluteFilePath();
                    }
                }
            }
        }
    }
    return QString();
}


QStringList Soprano::envDirList( const char* var )
{
    QStringList dirs;
    QByteArray varData = qgetenv( var );
    if ( !varData.isEmpty() ) {
        QStringList d = QString::fromLocal8Bit( varData ).split( PATH_SEPARATOR );
        Q_FOREACH( const QString &dir, d ) {
            dirs += QDir::fromNativeSeparators( dir );
        }
    }
    return dirs;
}


QStringList Soprano::libDirs()
{
    QStringList paths = QCoreApplication::libraryPaths();
    paths << QLatin1String( SOPRANO_PREFIX"/lib"SOPRANO_LIB_SUFFIX );
#ifdef Q_OS_WIN
    paths << QLatin1String( SOPRANO_PREFIX"/bin" );
    paths << getWinPrefix() + QLatin1String( "/bin" );
    paths << getWinPrefix() + QLatin1String( "/lib"SOPRANO_LIB_SUFFIX );
#else
    paths << QLatin1String( "/usr/lib"SOPRANO_LIB_SUFFIX );
    paths << QLatin1String( "/usr/local/lib"SOPRANO_LIB_SUFFIX );
#ifdef LIBRARY_ARCHITECTURE
    paths << QLatin1String( "/usr/lib/"LIBRARY_ARCHITECTURE );
    paths << QLatin1String( "/usr/local/lib/"LIBRARY_ARCHITECTURE );
#endif
    paths += Soprano::envDirList( "LD_LIBRARY_PATH" );
#endif
    return paths;
}


QStringList Soprano::dataDirs()
{
    QStringList paths;
    paths << QLatin1String( SOPRANO_PREFIX"/share" )
#ifdef Q_OS_WIN
          << getWinPrefix() + QLatin1String( "/share" )
#endif
          << Soprano::envDirList( "SOPRANO_DIRS" )
          << Soprano::envDirList( "XDG_DATA_DIRS" );
    return paths;
}


QStringList Soprano::exeDirs()
{
    QStringList paths;
    paths << QLatin1String( SOPRANO_PREFIX"/bin" )
#ifdef Q_OS_WIN
          << getWinPrefix() + QLatin1String( "/bin" )
#endif
          << envDirList( "PATH" );
    return paths;
}