summaryrefslogtreecommitdiffstats
path: root/fbreader/src/migration/Migration.cpp
blob: 172209df86c2794de45235b3755ca9a915a6b11d (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
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
/*
 * Copyright (C) 2008-2012 Geometer Plus <[email protected]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <cstdlib>

#include <ZLStringUtil.h>

#include "../options/FBCategoryKey.h"

#include "Migration.h"

void Migration::moveOption(
	const ZLCategoryKey &oldCategory, const std::string &oldGroup, const std::string &oldName,
	const ZLCategoryKey &newCategory, const std::string &newGroup, const std::string &newName,
	const std::string &defaultValue
) {
	ZLStringOption newOption(newCategory, newGroup, newName, defaultValue);
	const std::string newValue = newOption.value();
	ZLStringOption oldOption(oldCategory, oldGroup, oldName, newValue);
	const std::string oldValue = oldOption.value();
	if (newValue != oldValue) {
		newOption.setValue(oldValue);
		oldOption.setValue(newValue);
	}
}

bool Migration::isLikeToFileName(const std::string &str) {
	return
		ZLStringUtil::stringStartsWith(str, "/") ||
		ZLStringUtil::stringStartsWith(str, "\\\\") ||
		((str.length() > 2) && (str.substr(1, 2) == ":\\"));
}

Migration::Migration(const std::string &version) : myVersion(version) {
}

Migration::~Migration() {
}

int Migration::extractVersionInformation(const std::string &name) {
	int major = std::atoi(name.c_str());
	int minor = 0;
	int point = 0;
	int index = name.find('.');
	if (index > 0) {
		minor = std::atoi(name.c_str() + index + 1);
		index = name.find('.', index + 1);
		if (index > 0) {
			point = std::atoi(name.c_str() + index + 1);
		}
	}
	return 10000 * major + 100 * minor + point;
}

void Migration::doMigration() {
	ZLStringOption versionOption(FBCategoryKey::SYSTEM, "Version", "FBReaderVersion", "0");
	if (extractVersionInformation(versionOption.value()) <
			extractVersionInformation(myVersion)) {
		doMigrationInternal();
	}
}