MASA-Core
Status.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "Status.hpp"
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 
00027 Status::Status(string path, BestScoreList* bestScoreList) {
00028         status_file = path + "/status";
00029         status_tmp = path + "/status.tmp";
00030         this->bestScoreList = bestScoreList;
00031         this->lastSpecialRow = 0;
00032         this->currentStage = 0;
00033 
00034         load();
00035 }
00036 
00037 Status::~Status() {
00038         save();
00039 }
00040 
00041 int Status::isEmpty() {
00042         return empty;
00043 }
00044 
00045 void Status::load() {
00046     file = fopen(status_file.c_str(), "rt");
00047         if (file == NULL) {
00048             file = fopen(status_tmp.c_str(), "rt");
00049                 if (file == NULL) {
00050                         printf("Empty!!\n");
00051                         empty = true;
00052                         return;
00053                 }
00054                 rename(status_tmp.c_str(), status_file.c_str());
00055         }
00056         empty = false;
00057         int i;
00058         int j;
00059         int score;
00060         fscanf(file, "%d", &currentStage);
00061         fscanf(file, "%d", &lastSpecialRow);
00062         while (fscanf(file, "%d%d%d", &i, &j, &score) == 3) {
00063                 bestScoreList->add(i, j, score);
00064         }
00065 
00066         printf("NOT Empty: %d!!\n", score);
00067         fclose(file);
00068 }
00069 
00070 void Status::save() {
00071     file = fopen(status_tmp.c_str(), "wt");
00072         if (file == NULL) {
00073                 fprintf(stderr, "Error opening status file: %s\n", status_tmp.c_str());
00074                 exit(1);
00075         }
00076         fprintf(file, "%d\n", currentStage);
00077         fprintf(file, "%d\n", lastSpecialRow);
00078         // FIXME This is not thread safe!!!!
00079 //      for (set<score_t>::iterator it=bestScoreList->begin() ; it != bestScoreList->end(); it++ ) {
00080 //              fprintf(file, "%d %d %d\n", it->i, it->j, it->score);
00081 //      }
00082         score_t best = bestScoreList->getBestScore();
00083         fprintf(file, "%d %d %d\n", best.i, best.j, best.score);
00084 
00085         fclose(file);
00086 
00087         remove(status_file.c_str());
00088         rename(status_tmp.c_str(), status_file.c_str());
00089 }
00090 
00091 int Status::getLastSpecialRow() const {
00092         return lastSpecialRow;
00093 }
00094 
00095 void Status::setLastSpecialRow(int lastSpecialRow) {
00096         this->lastSpecialRow = lastSpecialRow;
00097 }
00098 
00099 int Status::getCurrentStage() const {
00100         return currentStage;
00101 }
00102 
00103 void Status::setCurrentStage(int currentStage) {
00104         this->currentStage = currentStage;
00105 }