/*
 * Input.cpp
 *
 *  Created on: Sep 6, 2009
 *      Author: marco
 */

#include <new>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <iterator>
#include <fstream>
#include <cassert>

using namespace std;

#define DEFAULT_N_OF_PERIODS 45

class Problem {
public:

	int n_of_events;
	int n_of_rooms;
	int n_of_students;
	int n_of_periods;

		vector<int> *list_events_x_student;
	vector<int> *list_unavailable_x_event;
	vector<int> *list_suitable_x_event;
	/* matrix keeping track of the precedences between events. For A[i][j]:
	 *  0 - no precedence.
	 *  1 - Event i must come before Event j.
	 * -1 - Event i must come after Event j.
	 */
	unsigned char **precedences;

	Problem(ifstream *is) {

		// read in a problem instance from file with extension file format dim
		char *tmp;
		char buf[512]; // line buffer

		if (!is->is_open()) {
			cerr << "Instance file not found\n";
			exit(1);
		}
n_of_periods = DEFAULT_N_OF_PERIODS;
		while (!is->eof()) {
			is->getline(buf, 512); // process each line
			switch (buf[0]) {
			case 'p': {  // p problem dimensions
				char *tmp = strtok(buf, " ");
				tmp = strtok(0, " ");
				n_of_events = atoi(tmp);
				tmp = strtok(0, " ");
				n_of_rooms = atoi(tmp);
				tmp = strtok(0, " ");
				n_of_students = atoi(tmp);
				//tmp = strtok(0, " ");
				tmp = strtok(0, " ");
				if (tmp != NULL && strlen(tmp) != 0 && *tmp != ' ') {
					int n_of_periods_per_day = atoi(tmp);
					tmp = strtok(0, " ");
					int n_of_days = atoi(tmp);
					n_of_periods = n_of_days*n_of_periods_per_day;
				}
				// initializations
				precedences = new unsigned char *[n_of_events];
				for (int v = 0; v < n_of_events; v++) {
					precedences[v] = new unsigned char[n_of_events];
					for (int w = 0; w < n_of_events; w++)
						precedences[v][w] = 0;
				}
				list_events_x_student = new vector<int> [n_of_students];
				list_unavailable_x_event = new vector<int> [n_of_events];
				list_suitable_x_event = new vector<int> [n_of_events];
				break;
			}
			case 'e': {  // e events attended by a student
				char *p;
				p = strtok(buf, " ");
				p = strtok(NULL, " ");
				int student = atoi(p);
				list_events_x_student[student].clear();
				while (p != NULL) {
					p = strtok(NULL, " ");
					if (p != NULL && !(*p == '\t' || strlen(p) == 0 || *p
							== ' '))
						list_events_x_student[student].push_back(atoi(p));
				}
				break;
			}
			case 'u': { //  u event unavailabilities
				char *p;
				p = strtok(buf, " ");
				p = strtok(NULL, " ");
				int event = atoi(p);
				list_unavailable_x_event[event].clear();
				while (p != NULL) {
					p = strtok(NULL, " ");
					if (p != NULL && !(*p == '\t' || strlen(p) == 0 || *p
							== ' '))
						list_unavailable_x_event[event].push_back(atoi(p));
				}
				break;
			}
			case 'r': { //  r rooms suitable for an event
				char *p;
				p = strtok(buf, " ");
				p = strtok(NULL, " ");
				int event = atoi(p);
				list_suitable_x_event[event].clear();
				while (p != NULL) {
					p = strtok(NULL, " ");
					if (p != NULL && !(*p == '\t' || strlen(p) == 0 || *p
							== ' '))
						list_suitable_x_event[event].push_back(atoi(p));
				}
				break;
			}
			case 'a': { //  a precedences
				char *p;
				p = strtok(buf, " ");
				p = strtok(NULL, " ");
				int e1 = atoi(p);
				p = strtok(NULL, " ");
				int e2 = atoi(p);
				precedences[e1][e2] = 1;
				precedences[e2][e1] = -1;
				break;
			}
			default:
				//std::cerr << "Error while reading the input file " << id << std::endl
				//  << "the line " << buf << " was not understandable" << std::endl;
				break;
			}
		}
	}

};

int main(int argc, char** argv) {

	string name_instance = argv[1];
	cout << "read " << basename(name_instance.c_str()) << endl;
	ifstream *ifs;
	ifs = new ifstream(name_instance.c_str());

	Problem p(ifs);

}

