Fixed showSolution
[vexed] / levelpack.cpp
1 #include "levelpack.h"
2 #include <stdio.h>
3 // TODO: use QSettings?
4
5 LevelPack::LevelPack(const QString &fileName):file(fileName)
6 {
7         file.open(QIODevice::ReadOnly|QIODevice::Text);
8         lookSection("[General]");
9         QFileInfo f(file);
10         props["title"]=f.baseName();
11         while(loadPackProperty()){};
12         while(file.canReadLine()){loadLevel();};
13 }
14 LevelPack::~LevelPack()
15 {
16         while(!levels.isEmpty())
17                 delete levels.takeFirst();
18 }
19 QString LevelPack::getLine()
20 {
21         QString line;
22         bool empty=true;
23         do {
24                 QString lineR(file.readLine());
25                 int i=0;
26                 int s=lineR.size();
27                 empty=true;
28                 while(empty && i<s)
29                         empty &=lineR.at(i++).isSpace();
30                 empty |= lineR.startsWith(';');
31                 if(file.error()!=0)
32                         printf("Error %d\n",file.error());
33                 line=lineR;
34         } while(empty && (file.error()==0) && !file.atEnd());
35         return line;
36 }
37 bool LevelPack::lookSection(const char *section)
38 {
39         QString line(getLine());
40         return line==section;
41 }
42 QPair<QString,QString> *LevelPack::loadProperty()
43 {
44         QString propLine=getLine();
45         if(propLine.startsWith("[") || !propLine.contains('='))
46         {
47                 return 0;
48         }
49         return new QPair<QString,QString>(propLine.section('=',0,0),propLine.section('=',1,1).trimmed());
50 }
51 bool LevelPack::loadPackProperty()
52 {
53         QPair<QString,QString> *pair=loadProperty();
54         if(pair)
55         {
56                 props[pair->first]=pair->second;
57                 delete pair;
58                 return true;
59         }
60         return false;
61 }
62 void LevelPack::loadLevel()
63 {
64         QMap<QString,QString> levelInfo;
65         while(QPair<QString,QString> *prop=loadProperty())
66         {
67                 levelInfo[prop->first]=prop->second;
68                 delete prop;
69         }
70         QString title(levelInfo["title"]);
71         QString board(levelInfo["board"]);
72         QString sol(levelInfo["solution"]);
73         PlayField *level=new PlayField(title,board,sol);
74         levels.append(level);
75 }