File: settings.cpp

package info (click to toggle)
source-highlight 3.1.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,332 kB
  • ctags: 5,233
  • sloc: sh: 11,270; cpp: 10,206; ansic: 9,515; makefile: 1,865; lex: 1,200; yacc: 1,021; php: 213; perl: 211; awk: 98; erlang: 94; lisp: 90; java: 75; ruby: 69; python: 61; asm: 43; ml: 38; ada: 36; haskell: 27; xml: 23; cs: 11; sql: 8; tcl: 6; sed: 4
file content (200 lines) | stat: -rw-r--r-- 4,676 bytes parent folder | download | duplicates (3)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * settings.cpp
 *
 *  Created on: Apr 18, 2009
 *      Author: Lorenzo Bettini <http://www.lorenzobettini.it>, (C) 2008
 *  Copyright: See COPYING file that comes with this distribution
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "settings.h"

#ifndef USE_MSVC
// msvc does not provide this header
#include <dirent.h>
#else
#include <io.h>
#include <direct.h>
#define mkdir(path,mode) _mkdir (path)
#include <compat_dirent.h>
#endif

#include <sys/stat.h>

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <boost/regex.hpp>

#include "fileutil.h"
#include "verbosity.h"

#ifdef USE_MINGW
// mingw's mkdir() function has 1 argument, but we pass 2 arguments.
// so we use its version _mkdir()
#undef mkdir
#define mkdir(path,mode) _mkdir (path)
#endif

using namespace std;

namespace srchilite {

/// if set (i.e., not empty), retrieveDataDir() always returns this value
string globalDataDir = "";

/// the regular expression for the conf file syntax
boost::regex
        datadir_exp(
                "[[:blank:]]*(datadir)[[:blank:]]*=[[:blank:]]*\"([^[:blank:]\\r]+)\"[[:blank:]\\r]*|([[:space:]]+)|([[:space:]]*#.*)");

#define DEFAULT_CONF_DIR ".source-highlight"
#define DEFAULT_CONF_FILE "source-highlight.conf"

static string findHomeDirectory() {
    const char *home = getenv("HOME");
    if (home) {
        return home;
    }

    // let's try other variables in windows
    char * homedrive = getenv("HOMEDRIVE");	// Drive containing Windows
    char * homepath = getenv("HOMEPATH");	// Path to the user directory into the Windows drive
    if (homedrive && homepath) {
        return string(homedrive) + string(homepath);
    }

    // last chance in windows
    home = getenv("USERPROFILE");
    if (home) {
        return home;
    }

    // nothing found...
    return "";
}

Settings::Settings() :
    confFileName(DEFAULT_CONF_FILE),
    testFileName("lang.map"), dataDir(ABSOLUTEDATADIR) {
    const string homeDir = findHomeDirectory();

    confDir = (homeDir != "" ? homeDir + "/" + DEFAULT_CONF_DIR + "/" : string(
            DEFAULT_CONF_DIR) + "/");
}

Settings::~Settings() {

}

bool Settings::checkForTestFile() {
    string file = dataDir + "/" + testFileName;
    ifstream i(file.c_str());

    return (i != 0);
}

bool Settings::checkForConfFile() {
    string file = confDir + confFileName;
    ifstream i(file.c_str());

    return (i != 0);
}

bool Settings::readDataDir() {
    string file = confDir + confFileName;
    ifstream i(file.c_str());

    string line;
    if (i != 0) {
        while (read_line(&i, line)) {
            if (line.size()) {
                boost::cmatch what;
                if (boost::regex_match(line.c_str(), what, datadir_exp)
                        && what[2].matched) {// not all spaces, nor comments
                    dataDir = what[2];
                    return true;
                }
            }
        }
    }

    return false;
}

SettingError Settings::save() {
    // the directory does not exist?
    if (opendir(confDir.c_str()) == 0) {
        // then create it
        if (mkdir(confDir.c_str(), S_IRWXU) != 0) {
            return CANT_CREATE_DIR;
        }
    }

    string file = confDir + confFileName;
    ofstream o;
    o.open(file.c_str());

    if (o) {
        o << "# settings for source-highlight" << endl;
        o << endl;
        o << "datadir = \"" << dataDir << "\"" << endl;
        o.close();
        return NO_SETTING_ERROR;
    }

    return CANT_CREATE_FILE;
}

const string Settings::retrieveDataDir(bool reload) {
    if (globalDataDir != "")
        return globalDataDir;

    static string dataDir;

    if (dataDir != "" && !reload)
        return dataDir; // cached value

    VERBOSELN("retrieving default datadir value...");

    const char *_dataDir = getenv("SOURCE_HIGHLIGHT_DATADIR");
    if (_dataDir) {
        VERBOSELN("using SOURCE_HIGHLIGHT_DATADIR env value " + string(_dataDir));
        dataDir = _dataDir;
        return _dataDir;
    }

    static Settings settings;

    if (!settings.readDataDir()) {
        VERBOSELN("using hardcoded datadir value " ABSOLUTEDATADIR);
        dataDir = ABSOLUTEDATADIR;
        return ABSOLUTEDATADIR;
    }

    dataDir = settings.getDataDir();

    VERBOSELN("using datadir value from conf file " + dataDir);

    return dataDir;
}

const std::string Settings::getDefaultDataDir() {
    return ABSOLUTEDATADIR;
}

void Settings::setGlobalDataDir(const std::string &dataDir) {
    globalDataDir = dataDir;
}

bool Settings::checkSettings() {
    static Settings settings;

    settings.setDataDir(retrieveDataDir());
    return settings.checkForTestFile();
}

}