Blame view

src/TTOperations/TTOperationsLauncher.cc 4.3 KB
fbe3c2bb   Benjamin Renard   First commit
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
/*
 * OperationsLauncher.cc
 *
 *  Created on: 14 août 2013
 *      Author: CS
 */

#include <iostream>
#include <cstring>
#include "TimeTable.hh"
#include <vector>
#include "TimeTableCatalogFactory.hh"
#include "InternalXMLReader.hh"
#include <boost/algorithm/string.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include "Application.hh"
#include <log4cxx/logger.h>

/**
 * Logger
 */
log4cxx::LoggerPtr _logger(
		log4cxx::Logger::getLogger("AMDA-Kernel.TTOperations"));

/**
 * Shows help on std::cout.
 */
void help() {
	std::cout << "Use : ttOperation OPERATION [OPTIONS] TT...  " << std::endl;
	std::cout << "    with OPERATION in (intersect, union, antiintersect)  "
			<< std::endl;
	std::cout << "         OPTIONS as " << std::endl;
	std::cout << "           --with_msk " << std::endl;
	std::cout << "           --out_dir=<path> default is ." << std::endl;
	std::cout
			<< "           --out_type=<type> with type in (ASCII, VO, Internal) default is Internal"
			<< std::endl;
	std::cout << "         TT a list of time table files  " << std::endl;
}

/**
 * Extracts a value from a pattern key=value.
 * Used for options.
 */
std::string extractValue(std::string& pKeyAndValue) {
	return pKeyAndValue.substr(pKeyAndValue.find("=") + 1);
}

/**
 * Main for ttOperation binary.
 */
int ttOperation(int argc, char *argv[], AMDA::helpers::Properties& /*properties*/) {

	// executable options
	if (argc < 3 || (argc > 1 && strcmp(argv[1], "--help") == 0)) {
		help();
		return -1;
	}

	int curIndex = 2;
	// init options with default values
	std::string outdirectory = ".";
	std::string outformat = TimeTableCatalog::InternalXMLReader::FORMAT;
	TimeTableCatalog::TimeTable::TIME_FORMAT timeformat =
			TimeTableCatalog::TimeTable::TIME_FORMAT::YYYYMMDDThhmmss;

	// search for specified options
	// and fill option values
	while (boost::starts_with(argv[curIndex], "--")) {
		std::string option(argv[curIndex]);
		if (strcmp("--with_msk", argv[curIndex]) == 0) {
			timeformat = TimeTableCatalog::TimeTable::TIME_FORMAT::YYYYMMDDThhmmssmsk;
		} else if (boost::starts_with(argv[curIndex], "--out_dir=")) {
			outdirectory = extractValue(option);
			boost::filesystem::path path(outdirectory);
			if (!boost::filesystem::exists(path)) {
				LOG4CXX_ERROR(_logger, "The path " + outdirectory << " does not exist.");
				return -1;
			}
		} else if (boost::starts_with(argv[curIndex], "--out_type=")) {
			outformat = extractValue(option);
			if (!TimeTableCatalog::TimeTableCatalogFactory::getInstance().canRead(
					outformat)) {
				LOG4CXX_ERROR(_logger, "Unknown type " + outformat << ".");
				return -1;
			}
		} else {
			LOG4CXX_ERROR(_logger, "Unknown option " << argv[curIndex] << ".");
		}
		++curIndex;
	}

	// read tt list
	std::vector<TimeTableCatalog::TimeTable> tts;
	for (int i = curIndex; i < argc; i++) {
		TimeTableCatalog::TimeTable tt;
		std::string path(argv[i]);
		boost::filesystem::path boostpath(path);
		if (!(boost::starts_with(path, "http:")
				|| boost::starts_with(path, "https:"))
				&& !boost::filesystem::exists(boostpath)) {
			LOG4CXX_ERROR(_logger, "The time table " + path << " does not exist.");
			return -1;
		}
		// read tt with auto-detected type
		std::string type = TimeTableCatalog::TimeTableCatalogFactory::getInstance().getReaderType(
								path);
		if(type.empty()){
			LOG4CXX_ERROR(_logger, "Unknown Timetable : " << path);
			return -1;
		}
		tt.read(path,type);
		tts.push_back(tt);
	}

	// do action according to OPERATION
	std::unique_ptr<TimeTableCatalog::TimeTable> tt;
	if (strcmp("intersect", argv[1]) == 0) {
		tt = TimeTableCatalog::TimeTable::intersect(tts);
	} else if (strcmp("union", argv[1]) == 0) {
		tt = TimeTableCatalog::TimeTable::merge(tts);
	} else if (strcmp("antiintersect", argv[1]) == 0) {
		tt = TimeTableCatalog::TimeTable::antiintersect(tts);
	} else {
		LOG4CXX_ERROR(_logger, "Wrong OPERATION...");
		help();
		return -1;
	}

	LOG4CXX_DEBUG(_logger, "Number of resulting Intervals : " << tt->getIntervalNumber());
	// write output with resulting tt
	tt->_timeFormat = timeformat;
	LOG4CXX_INFO(_logger, "Writing output in " << outdirectory << " with format :" << outformat);
	tt->write(outdirectory, outformat);

	return 0;
}

/**
 * Main function
 */
int main(int argc, char *argv[]) {

	AMDA::Common::Application lMain;
ebd575e4   Benjamin Renard   Do not load plugg...
146
	return lMain.main(argc, argv, ttOperation, true);
fbe3c2bb   Benjamin Renard   First commit
147
}