fix compile problem under g++-4.3
[hirshfeld.git] / src / base_directory.cpp
blob8963b46b2c2dce7bdc11ad72b3c01d6bb9c4e80d
1 #include "config.h"
3 #include "base_directory.hpp"
4 #include <vector>
5 #include <cstdlib>
6 #include <unistd.h>
7 #include "dirname.h"
9 using namespace std;
11 namespace hirshfeld {
13 class BaseDirectory {
14 public:
15 static const BaseDirectory* get_instance() {
16 if(not m_instance) {
17 m_instance = new BaseDirectory();
19 return m_instance;
22 const string& home() const {return m_home;}
23 const string& xdg_data_home() const {return m_xdg_data_home;}
24 const vector<string>& xdg_data_dirs() const {return m_xdg_data_dirs;}
25 private:
26 BaseDirectory() {
27 const char* tmp;
29 tmp = getenv("HOME");
30 if(tmp) {
31 m_home = tmp;
32 } else {
33 m_home = "/";
36 tmp = getenv("XDG_DATA_DIRS");
37 if(tmp) {
38 m_xdg_data_home = tmp;
39 } else {
40 m_xdg_data_home = m_home + "/.local/share";
43 tmp = getenv("XDG_DATA_DIRS");
44 if(not tmp) tmp = "/usr/local/share:/usr/share";
46 string tmp2 = tmp;
47 while(not tmp2.empty() and tmp2[0] == ':') {
48 tmp2 = tmp2.substr(1);
51 while(not tmp2.empty() and tmp2[tmp2.size()-1] == ':') {
52 tmp2 = tmp2.substr(0, tmp2.size()-1);
55 m_xdg_data_dirs.push_back(m_xdg_data_home);
56 size_t pos = 0;
57 while(pos != string::npos) {
58 size_t nextpos = tmp2.find(':', pos);
59 if(nextpos == string::npos) {
60 m_xdg_data_dirs.push_back(tmp2.substr(pos));
61 pos = nextpos;
62 } else {
63 m_xdg_data_dirs.push_back(tmp2.substr(pos, nextpos-pos));
64 pos = nextpos+1;
69 string m_home;
70 string m_xdg_data_home;
71 vector<string> m_xdg_data_dirs;
72 static BaseDirectory* m_instance;
74 BaseDirectory* BaseDirectory::m_instance = 0;
76 static string home;
77 static string xdg_data_home;
78 static vector<string> xdg_data_dirs;
80 std::string
81 load_first_data(const std::string& resource)
83 const BaseDirectory& bd = *(BaseDirectory::get_instance());
84 const vector<string>& xdg_data_dirs = bd.xdg_data_dirs();
86 for(int i = 0; i < int(xdg_data_dirs.size()); ++i) {
87 string res = xdg_data_dirs[i] + "/" + resource;
88 if(access(res.c_str(), R_OK) == 0) return res;
90 return "";