boost::parse_environment

boost::program_options::parse_environment通过环境变量来设置options

这里需要注意的是需要注意的是:一定要将所需要的环境变量加入到options中。

 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
#include <iostream>
#include <string>
#include <boost/program_options.hpp>

using namespace std;
using namespace boost::program_options;

// compile: g++ boostprogramoption.cpp -o boostprogramoption -lboost_program_options

// envs
const string ENVS[] = {
    "HOME",
    "PKG_CONFIG_PATH",
    "PATH",
    "http_proxy",
};

const int ENVCOUNT = 4;

static std::string name_mapper(const std::string &var_name){
    for (int i = 0; i < ENVCOUNT; ++i) {
        if (var_name == ENVS[i])
            return var_name;
    }

    return "";
}

int main(int argc, char** argv)
{
    // need to pass the buffer size to constructor (e.g. 1024), otherwise:
    // undefined reference to 'boost::program_options::options_description::m_default_line_length'
    options_description desc("Allowed options", 1024);
    desc.add_options()
        ("help,h", "produce help message")
        ("compression,c", value<int>(), "set compression level")

	// need to use the env as option, otherwise exception throwed:
        ("HOME,m", value<string>()->default_value(""), "home dir")
        ("PATH,p", value<string>()->default_value(""), "path")
        ("PKG_CONFIG_PATH,k", value<string>()->default_value(""), "pkg config")
        ("http_proxy", value<string>()->default_value(""), "http proxy");
        ;

    variables_map vm;
    store(parse_command_line(argc, argv, desc), vm);
    store(parse_environment(desc, name_mapper), vm);
    notify(vm);

    if (vm.count("help")) {
        cout << desc << endl;
        return 1;
    }

    if (vm.count("compression")) {
        cout << "Compression level was set to "
             << vm["compression"].as<int>() << endl;
    } else {
        cout << "Compression level was not set." << endl;
    }

    for (int i = 0; i < ENVCOUNT; ++i) {
        if (vm.count(ENVS[i])) {
            cout << ENVS[i] << " is "
                 << vm[ENVS[i]].as<string>() << endl;
        }
    }

    return 0;
}

sudo环境变量

由于sudo运行程序不继承当前用户的环境变量,所以不能使用export var=value; sudo command的方式执行。解决办法:

1
sudo var=value command

设置emacs htmlize.el为等宽字体

我用emacs的htmlize.el elisp来给代码上色,默认情况下并没有设置成为等宽字体,所以显示上不太好看,为htmlize默认添加输出为等宽字体:

1
2
3
4
5
(defun htmlize-font-pre-tag (face-map)
  (let ((fstruct (gethash 'default face-map)))
    (format "<pre style="font-family: 'courier new', courier;color:%s;background-color:%s">"
            (htmlize-fstruct-foreground fstruct)
            (htmlize-fstruct-background fstruct))))

boost::timer

在linux平台下使用boost::timer,得到的并不是所经过的时间,比如以下代码,因为sleep了1s,所以得到的结果应该大于1s,但是得出来的结果确是0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <boost/timer.hpp>
#include <iostream>

using namespace std;
using namespace boost;

int main()
{
    cout << "test boost::timer" << endl;
    timer tmer;
    sleep(1);
    cout << tmer.elapsed() << endl;

    return 0;
}
1
2
3
$ ./boosttimer
test boost::timer
0

究其原因:察看boost::timer的源代码/usr/include/boost/timer.hpp

 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
class timer
{
 public:
         timer() { _start_time = std::clock(); } // postcondition: elapsed()==0
//         timer( const timer& src );      // post: elapsed()==src.elapsed()
//        ~timer(){}
//  timer& operator=( const timer& src );  // post: elapsed()==src.elapsed()
  void   restart() { _start_time = std::clock(); } // post: elapsed()==0
  double elapsed() const                  // return elapsed time in seconds
    { return  double(std::clock() - _start_time) / CLOCKS_PER_SEC; }

  double elapsed_max() const   // return estimated maximum value for elapsed()
  // Portability warning: elapsed_max() may return too high a value on systems
  // where std::clock_t overflows or resets at surprising values.
  {
    return (double((std::numeric_limits<std::clock_t>::max)())
       - double(_start_time)) / double(CLOCKS_PER_SEC);
  }

  double elapsed_min() const            // return minimum value for elapsed()
   { return double(1)/double(CLOCKS_PER_SEC); }

 private:
  std::clock_t _start_time;
}; // timer

可以看到boost::timer的实现在linux下是通过std::clock()实现的.经测试std::clock()每次返回的值都是一样的。原因就是我们在这里使用的是sleep,而man 3 clock得到的是function returns an approximation of processor time used by the program.即得到的是cpu执行的时间,sleep的时候cpu执行时间(cpu  time)为0,所以每次都得到的一样的值,最后结果为0,如果我们将sleep改为一个循环,结果就将是这是的值了!测试的时候不能被sleep所迷惑了,所以我们用来测实际经过的时间的时候不能用boost::timer!可以选择alarm

boost::progress_display

boost::progress_display是个好玩的东东,用来现实一个进度条。测试代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <boost/progress.hpp>

using namespace boost;

int main()
{
    progress_display p(10);     
    for (int i = 0; i < 10; ++i) {
        sleep(1);
        ++p;
    }

    return 0;
}

运行结果:

1
2
3
4
5
$ ./boostprogress

0%   10   20   30   40   50   60   70   80   90   100%
|----|----|----|----|----|----|----|----|----|----|
****************************************