File indexing completed on 2021-12-21 12:50:20

0001 /*
0002  *  ksokoban - a Sokoban game by KDE
0003  *  Copyright (C) 1998  Anders Widell  <awl@hem.passagen.se>
0004  *
0005  *  This program is free software; you can redistribute it and/or modify
0006  *  it under the terms of the GNU General Public License as published by
0007  *  the Free Software Foundation; either version 2 of the License, or
0008  *  (at your option) any later version.
0009  *
0010  *  This program is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *  GNU General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU General Public License
0016  *  along with this program; if not, write to the Free Software
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  */
0019 
0020 #include <stdio.h>
0021 #include <sys/stat.h>
0022 #include <sys/types.h>
0023 #include <fcntl.h>
0024 #include <unistd.h>
0025 #include <assert.h>
0026 
0027 #include <QFile>
0028 #include <QStandardPaths>
0029 
0030 
0031 #include "Bookmark.h"
0032 #include "History.h"
0033 #include "LevelMap.h"
0034 
0035 void
0036 Bookmark::fileName(QString &p) {
0037   p = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
0038 
0039   QString n;
0040   n.setNum(number_);
0041   p += "/bookmark" + n;
0042 }
0043 
0044 Bookmark::Bookmark(int _num) :
0045   number_(_num), collection_(-1), level_(-1), moves_(0), data_(QLatin1String("")) {
0046 
0047   QString p;
0048   fileName(p);
0049 
0050   FILE *file = fopen(p.toLatin1(), "r");
0051   if (file == nullptr) return;
0052 
0053   char buf[4096];
0054   buf[0] = '\0';
0055   fgets (buf, 4096, file);
0056   if (sscanf(buf, "%d %d %d", &collection_, &level_, &moves_) != 3) {
0057     collection_ = level_ = -1;
0058     data_ = QLatin1String("");
0059     fclose(file);
0060     return;
0061   }
0062 
0063   data_ = QLatin1String("");
0064   int len;
0065   while (!feof(file)) {
0066     len = fread(buf, 1, 4095, file);
0067     if (ferror(file)) break;
0068     buf[len] = '\0';
0069     data_ += buf;
0070   }
0071   fclose(file);
0072 
0073   data_ = data_.trimmed();
0074 }
0075 
0076 
0077 
0078 void
0079 Bookmark::set(int _collection, int _level, int _moves, History *_h) {
0080   assert(_collection >= 0);
0081   if (_collection < 0) return;
0082 
0083   collection_ = _collection;
0084   level_ = _level;
0085   moves_ = _moves;
0086 
0087   data_ = QLatin1String("");
0088   _h->save(data_);
0089 
0090   QString p;
0091   fileName(p);
0092   FILE *file = fopen(QFile::encodeName(p), "w");
0093   if (file == nullptr) return;
0094   fprintf(file, "%d %d %d\n", collection_, level_, moves_);
0095   fprintf(file, "%s\n", data_.toLatin1().constData());
0096   fclose(file);
0097 }
0098 
0099 bool
0100 Bookmark::goTo(LevelMap *_map, History *_h) {
0101   return _h->load(_map, data_.toLatin1()) != nullptr;
0102 }
0103 
0104