Renamed cscope_db to ctags_db
[vimicxx.git] / src / vimicxx_conf.cc
blob5857c8736894f7bbfaa7a779a8f60ecb4d1cbbf8
1 /*******************************************************************************
2 ********************************************************************************
4 Copyright (c) 2008 Ahmed S. Badran
6 Licensed under the FreeBSD License (see LICENSE)
8 Filename: vimicxx_conf.cc
9 Description: Implementation.
10 Created: 09/07/2008 05:31:00 PM PDT
11 Author: Ahmed S. Badran (Ahmed B.), ahmed.badran@gmail.com
13 ********************************************************************************
14 *******************************************************************************/
15 #include "vimicxx_conf.h"
16 #include "configfile.h"
17 #include <errno.h>
18 #include <sys/stat.h>
19 #include <algorithm>
20 using namespace std;
22 vimicxx_conf::vimicxx_conf(const string& vimicxx_dir):
23 conf(0), vimicxx_home(vimicxx_dir),
24 conf_path(vimicxx_home + "/vimicxx.conf")
26 conf = new configfile(conf_path);
27 struct stat tmp_stat;
28 if (stat(conf_path.c_str(), &tmp_stat) != 0) {
29 string sec_name = "vimicxx.conf";
30 conf->add_section(sec_name);
31 conf->add_key(sec_name, "version", "1.0");
32 conf->add_key(sec_name, "conf_file", conf_path);
36 const std::string
37 vimicxx_conf::err_to_string(int e)
39 switch (e) {
40 case Ok:
41 return "All ok";
42 break;
43 case Err_DuplicatePrjName:
44 return "A project with this name already exists";
45 break;
46 case Err_MissingPrjPath:
47 return "Directory does not exist or is unreadable";
48 break;
52 int
53 vimicxx_conf::add_project(const string& prj_name, const string& prj_path,
54 const vector<string>& ext_list)
56 string new_project = "project." + prj_name;
57 vector<string> projects = conf->get_sections();
58 if (find(projects.begin(), projects.end(), new_project) !=
59 projects.end()) {
60 return Err_DuplicatePrjName;
62 struct stat tmp_stat;
63 if (stat(prj_path.c_str(), &tmp_stat) != 0) {
64 return Err_MissingPrjPath;
68 conf->add_section(new_project);
69 string prj_cache = vimicxx_home + "/" + prj_name;
70 int tmp = stat(prj_cache.c_str(), &tmp_stat);
71 if ((tmp == 0) || (errno != ENOENT)) {
72 return Err_DuplicatePrjCache;
73 } else if (mkdir(prj_cache.c_str(), S_IRUSR | S_IWUSR | S_IXUSR |
74 S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) {
75 perror("mkdir");
76 return Err_Mkdir;
79 conf->add_key(new_project, "path", prj_path);
80 conf->add_key(new_project, "cache_dir", prj_cache);
81 conf->add_key(new_project, "index", prj_cache + "/index");
82 conf->add_key(new_project, "file_list", prj_cache + "/file_list.txt");
83 conf->add_key(new_project, "tags_db", prj_cache + "/tags_db.txt");
84 return Ok;
87 bool
88 vimicxx_conf::get_project_path(const string& prj, string* path) const
90 string secname = "project." + prj;
91 return conf->get_key_value(secname, "path", path);
93 bool
94 vimicxx_conf::get_project_cache_dir(const string& prj, string* cache_dir)
95 const
97 string secname = "project." + prj;
98 return conf->get_key_value(secname, "cache_dir", cache_dir);
101 bool
102 vimicxx_conf::get_project_index(const string& prj, string* index) const
104 string secname = "project." + prj;
105 return conf->get_key_value(secname, "index", index);
108 bool
109 vimicxx_conf::get_project_flist(const string& prj, string* flist) const
111 string secname = "project." + prj;
112 return conf->get_key_value(secname, "file_list", flist);
114 bool
115 vimicxx_conf::get_project_tags_db(const string& prj, string* tags_db)
116 const
118 string secname = "project." + prj;
119 return conf->get_key_value(secname, "tags_db", tags_db);
122 vimicxx_conf::~vimicxx_conf ()
124 conf->save();