summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/service/maintenance/useGlobalProcessor.cpp
blob: f3b3b5a0ab8fcd33a413586b7fd4d05fb3129326 (plain)
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
#include "useGlobalProcessor.h"
#include <modifycommand.h>
#include <tablepatch.h>
#include <fileUtils.h>
#include "utils/dbUtils.h"
#include <glibmm/regex.h>

using namespace AdHoc::FileUtils;

static Glib::RefPtr<Glib::Regex> useDesc = Glib::Regex::create("^([^#\\s][^ ]*)\\s+-\\s+(.*)$", Glib::RegexCompileFlags::REGEX_MULTILINE);

namespace Gentoo {
	namespace Service {
		const int UseGlobalProcessor::FILETYPEID = 5;

		void
		UseGlobalProcessor::created(DB::Connection * dbc, const boost::filesystem::path & fn, const boost::filesystem::path & path)
		{
			modified(dbc, fn, path);
		}

		void
		UseGlobalProcessor::modified(DB::Connection * dbc, const boost::filesystem::path &, const boost::filesystem::path & path)
		{
			DB::TablePatch p;
			p.dest = "gentoobrowse.use_global";
			p.src = Utils::Database::emptyClone(dbc, p.dest);
			p.pk = { "use" };
			p.cols = { "use", "description" };

			AdHoc::FileUtils::MemMap u(path);
			Glib::ustring d(std::string(reinterpret_cast<const char *>(u.data), u.getStat().st_size));
			Glib::MatchInfo matches;
			auto i = Utils::Database::tablePatchInserter(dbc, p);
			for (useDesc->match(d, matches); matches.get_match_count() == 3; matches.next()) {
				i->bindParamS(0, matches.fetch(2));
				i->bindParamS(1, matches.fetch(1));
				i->execute();
			}
			dbc->patchTable(&p);

			Utils::Database::drop(dbc, p.src);
		}

		void
		UseGlobalProcessor::deleted(DB::Connection * dbc, const boost::filesystem::path &)
		{
			dbc->modify("DELETE FROM gentoobrowse.use_global")->execute();
		}
	}
}