TimeTable.hh 4.53 KB
/*
 * TimeTable.h
 *
 *  Created on: 5 août 2013
 *      Author: CS
 */

#ifndef TIMETABLE_H_
#define TIMETABLE_H_

#include <string>
#include <vector>
#include <memory>

#include "TimeInterval.hh"
#include "ParameterDescription.hh"
#include "log4cxx/logger.h"


namespace TimeTableCatalog {

/**
 * TimeTable(TT) definition.
 */
class TimeTable {
public:

	/**
	 * ISO Time format for creation date and interval dates.
	 */
	enum TIME_FORMAT {
		UNKNOWN = 0,
		YYYYMMDDThhmmssmsk  = 1, // YYYY-MM-DDThh:mm:ss.mls
		YYYYMMDDThhmmss     = 2, // YYYY-MM-DDThh:mm:ss
		YYYYMMDDThhmm       = 3, // YYYY-MM-DDThh:mm
		YYYYMMDDThhmmssmsk2 = 4, // YYYY/MM/DD hh:mm:ss.mls
		YYYYMMDDThhmmss2    = 5, // YYYY/MM/DD hh:mm:ss
		YYYYMMDDThhmm2      = 6, // YYYY/MM/DD hh:mm
	};


	TimeTable();
	TimeTable(TIME_FORMAT pFormat);
	~TimeTable();

	/**
	 * Creates a new time table as a union of all given time table.
	 */
	static std::unique_ptr<TimeTable> merge(
			const std::vector<TimeTable>& pTimeTableList);

	/**
	 * Creates a new time table as an intersection of all given time tables.
	 */
	static std::unique_ptr<TimeTable> intersect(
			const std::vector<TimeTable>& pTimeTableList);

	/**
	 * Complementary to intersect operation.
	 */
	static std::unique_ptr<TimeTable> antiintersect(
			const std::vector<TimeTable>& pTimeTableList);

	/**
	 * Sorts time intervals according to their start date.
	 */
	void sort();

	/**
	 * Copies all intervals of a list of time tables into a unique time table.
	 */
	void group(const std::vector<TimeTable>& pTimeTableList);

	/**
	 * Reads a TT.
	 */
	void read(const std::string& pPath, const std::string& ptype);

	/**
	 * Writes a TT.
	 */
	std::string write(const std::string& pPath, const std::string& ptype, const std::string& pName="") const;

	/**
	 * Reads a time table and writes it into another format.
	 */
	void convert(const std::string& pInPath, const std::string& pInType,
			const std::string& pOutPath, const std::string& pName, const std::string& pOutType);

	/**
	 * Unset current TT.
	 */
	void clear();

	/**
	 * Adds an interval to the TT interval list.
	 */
	void addInterval(const TimeInterval& pInterval);

	/**
	 * Gets intervals from TT.
	 */
	const std::vector<TimeInterval>& getIntervals() const {
		return _intervals;
	}

	/**
	 * Gets last intervals from TT.
	 */
	TimeInterval * getLastInterval() {
		if (_intervals.empty()) {
			return NULL;
		}
		return &(_intervals.back());
	}

	/**
	 * Gets number of intervals in TT.
	 */
	int getIntervalNumber() const;

	/**
	 * Downloads distant timetable file in tmp directory.
	 */
	static std::string download(const std::string& pPath);

	/**
	 * Retrieve parameter descriptions list
	 */
	const ParameterDescriptionList& getParameterDescritptions() const {
		return _parameterDescritptions;
	}

	/**
	 * Retrieve last parameter descriptions
	 */
	ParameterDescription * getLastParameterDescritption() {
		return &_parameterDescritptions.back();
	}

	/**
	 * TT metadata name.
	 */
	std::string _name;

	/**
	 * TT metadata creation date.
	 */
	double _creationDate;

	/**
	 * TT metadata description
	 */
	std::vector<std::string> _description;

	/**
	 * TT metadata history
	 */
	std::string _history;

	/**
	 * TT creation date and interval dates ISO format (with or without msk)
	 */
	TIME_FORMAT _timeFormat;

protected:

	/**
	 * Additional parameters descriptions used by Catalog class
	 * these descriptions are not accessible through the Time table class
	 */
	ParameterDescriptionList _parameterDescritptions;

private:

	/**
	 * Logger
	 */
	static log4cxx::LoggerPtr _logger;

	/**
	 * Returns a merged interval list.
	 */
	static std::unique_ptr<std::vector<TimeInterval>> merge(
			const std::vector<TimeInterval>& pTimeIntervalsList);

	/**
	 * Fills an interval list with all non intersections between the two given time tables.
	 */
	static void antiintersect(const TimeTable& ptt1, const TimeTable& ptt2,
			std::vector<TimeInterval>& pIntersetedIntervals);

	/**
	 * Fills an interval list with all intersections between the two given time tables.
	 */
	static void intersect(const TimeTable& ptt1, const TimeTable& ptt2,
			std::vector<TimeInterval>& pIntersetedIntervals);

	/**
	 * Sorts a list of interval according to their start date.
	 */
	static void sort(std::vector<TimeInterval>& pIntervals);

	/**
	 * TT intervals
	 */
	std::vector<TimeInterval> _intervals;

};

std::string join(const std::vector<std::string>& pvalues,
		const std::string& pseparator);

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream);

} /* namespace TimeTableCatalog */
#endif /* TIMETABLE_H_ */