1 // Copyright (C) 2012-2019 The VPaint Developers.
2 // See the COPYRIGHT file at the top-level directory of this distribution
3 // and at https://github.com/dalboris/vpaint/blob/master/COPYRIGHT
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef SAVE_AND_LOAD_H
18 #define SAVE_AND_LOAD_H
19 
20 #include <QString>
21 #include <QList>
22 #include <QTextStream>
23 
24 class Field
25 {
26 public:
27     Field();
28     Field(const QString & string);
29 
string()30     QString string() {return string_;}
31 
32     friend QTextStream & operator<<(QTextStream &, const Field & field);
33     friend QTextStream & operator>>(QTextStream &, Field & field);
34 
35 private:
36     QString string_;
37 };
38 
39 class Save
40 {
41 public:
42     static QString indent(int n);
43 
44     static void resetIndent();
45     static void incrIndent();
46     static void decrIndent();
47     static QString indent();
48 
49     static QString newField(const QString & fieldName);
50     static QString openCurlyBrackets();
51     static QString closeCurlyBrackets();
52 
53 private:
54     static int indent_;
55 };
56 
57 class Read
58 {
59 public:
60     static QString string(QTextStream & in);
61     static QString field(QTextStream & in);
62     static void skipBracket(QTextStream & in);
63     static QString readBracketedBlock(QTextStream & in);
64 };
65 
66 
67 
68 
69 template<class T>
70 QTextStream & operator<<(QTextStream & out, const QList<T> & list)
71 {
72     out << "[";
73     for(int i=0; i<list.size(); ++i)
74     {
75         if(i!=0) out << " ,";
76         out << " " << list[i];
77     }
78     out << " ]";
79 
80     return out;
81 }
82 
83 template<class T>
84 QTextStream & operator>>(QTextStream & in, QList<T> & list)
85 {
86     QString listAsString = Read::readBracketedBlock(in);
87 
88     bool isEmpty = false;
89     if(listAsString == "[]" || listAsString == "[ ]")
90         isEmpty = true;
91 
92     // if not empty
93     if(!isEmpty)
94     {
95         QTextStream newIn(&listAsString);
96         QString delimiter;
97         newIn >> delimiter; // read "["
98         delimiter = ",";
99         while(delimiter == ",")
100         {
101             T t;
102             newIn >> t;
103             list << t;
104             newIn >> delimiter;
105         }
106     }
107 
108     return in;
109 }
110 
111 template<class T, class U>
112 QTextStream & operator<<(QTextStream & out, const QPair<T,U> & pair)
113 {
114     out << "( " << pair.first << " , " << pair.second << " )";
115     return out;
116 }
117 
118 template<class T, class U>
119 QTextStream & operator>>(QTextStream & in, QPair<T,U> & pair)
120 {
121     QString s;
122     in >> s >> pair.first >> s >> pair.second >> s;
123     return in;
124 }
125 
126 #endif
127