Statistics
| Branch: | Tag: | Revision:

root / host / lib / utils / paths.cpp @ 98074d0c

History | View | Annotate | Download (4.48 KB)

1
//
2
// Copyright 2010-2012 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
#include <uhd/config.hpp>
19
#include <uhd/utils/paths.hpp>
20
#include <boost/tokenizer.hpp>
21
#include <boost/filesystem.hpp>
22
#include <boost/foreach.hpp>
23
#include <boost/bind.hpp>
24
#include <cstdlib>
25
#include <string>
26
#include <vector>
27
#include <cstdlib> //getenv
28
#include <cstdio>  //P_tmpdir
29
#ifdef BOOST_MSVC
30
#define USE_GET_TEMP_PATH
31
#include <windows.h> //GetTempPath
32
#endif
33

    
34
namespace fs = boost::filesystem;
35

    
36
/***********************************************************************
37
 * Determine the paths separator
38
 **********************************************************************/
39
#ifdef UHD_PLATFORM_WIN32
40
    static const std::string env_path_sep = ";";
41
#else
42
    static const std::string env_path_sep = ":";
43
#endif /*UHD_PLATFORM_WIN32*/
44

    
45
#define path_tokenizer(inp) \
46
    boost::tokenizer<boost::char_separator<char> > \
47
    (inp, boost::char_separator<char>(env_path_sep.c_str()))
48

    
49
/***********************************************************************
50
 * Get a list of paths for an environment variable
51
 **********************************************************************/
52
static std::string get_env_var(const std::string &var_name, const std::string &def_val = ""){
53
    const char *var_value_ptr = std::getenv(var_name.c_str());
54
    return (var_value_ptr == NULL)? def_val : var_value_ptr;
55
}
56

    
57
static std::vector<fs::path> get_env_paths(const std::string &var_name){
58

    
59
    std::string var_value = get_env_var(var_name);
60

    
61
    //convert to filesystem path, filter blank paths
62
    std::vector<fs::path> paths;
63
    if (var_value.empty()) return paths; //FIXME boost tokenizer throws w/ blank strings on some platforms
64
    BOOST_FOREACH(const std::string &path_string, path_tokenizer(var_value)){
65
        if (path_string.empty()) continue;
66
        paths.push_back(fs::system_complete(path_string));
67
    }
68
    return paths;
69
}
70

    
71
/***********************************************************************
72
 * Get a list of special purpose paths
73
 **********************************************************************/
74
std::string uhd::get_pkg_data_path(void)
75
{
76
    return get_env_var("UHD_PKG_DATA_PATH", UHD_PKG_DATA_PATH);
77
}
78

    
79
std::vector<fs::path> get_image_paths(void){
80
    std::vector<fs::path> paths = get_env_paths("UHD_IMAGE_PATH");
81
    paths.push_back(fs::path(uhd::get_pkg_data_path()) / "images");
82
    return paths;
83
}
84

    
85
std::vector<fs::path> get_module_paths(void){
86
    std::vector<fs::path> paths = get_env_paths("UHD_MODULE_PATH");
87
    paths.push_back(fs::path(uhd::get_pkg_data_path()) / "modules");
88
    return paths;
89
}
90

    
91
/***********************************************************************
92
 * Implement the functions in paths.hpp
93
 **********************************************************************/
94
std::string uhd::get_tmp_path(void){
95
    const char *tmp_path = NULL;
96

    
97
    //try the official uhd temp path environment variable
98
    tmp_path = std::getenv("UHD_TEMP_PATH");
99
    if (tmp_path != NULL) return tmp_path;
100

    
101
    //try the windows function if available
102
    #ifdef USE_GET_TEMP_PATH
103
    char lpBuffer[2048];
104
    if (GetTempPath(sizeof(lpBuffer), lpBuffer)) return lpBuffer;
105
    #endif
106

    
107
    //try windows environment variables
108
    tmp_path = std::getenv("TMP");
109
    if (tmp_path != NULL) return tmp_path;
110

    
111
    tmp_path = std::getenv("TEMP");
112
    if (tmp_path != NULL) return tmp_path;
113

    
114
    //try the stdio define if available
115
    #ifdef P_tmpdir
116
    if (P_tmpdir != NULL) return P_tmpdir;
117
    #endif
118

    
119
    //try unix environment variables
120
    tmp_path = std::getenv("TMPDIR");
121
    if (tmp_path != NULL) return tmp_path;
122

    
123
    //give up and use the unix default
124
    return "/tmp";
125
}
126

    
127
std::string uhd::get_app_path(void){
128
    const char *appdata_path = std::getenv("APPDATA");
129
    if (appdata_path != NULL) return appdata_path;
130

    
131
    const char *home_path = std::getenv("HOME");
132
    if (home_path != NULL) return home_path;
133

    
134
    return uhd::get_tmp_path();
135
}