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 #include "SaveAndLoad.h"
18 #include <QTextStream>
19 
20 int Save::indent_ = 0;
21 
22 
Field()23 Field::Field() :
24     string_()
25 {
26 }
27 
Field(const QString & string)28 Field::Field(const QString & string) :
29     string_(string)
30 {
31 }
32 
33 
operator <<(QTextStream & str,const Field & field)34 QTextStream & operator<<(QTextStream & str, const Field & field)
35 {
36     str << (Save::newField(field.string_));
37     return str;
38 }
39 
operator >>(QTextStream & str,Field & field)40 QTextStream & operator>>(QTextStream & str, Field & field)
41 {
42     field = Read::field(str);
43     return str;
44 }
45 
46 
indent(int n)47 QString Save::indent(int n)
48 {
49     QString res;
50     for(int i=0; i<n; i++) res += " ";
51     return res;
52 }
53 
resetIndent()54 void Save::resetIndent()
55 {
56     indent_ = 0;
57 }
58 
incrIndent()59 void Save::incrIndent()
60 {
61     indent_ += 4;
62 }
63 
decrIndent()64 void Save::decrIndent()
65 {
66     indent_ -= 4;
67 }
68 
indent()69 QString Save::indent()
70 {
71     return indent(indent_);
72 }
73 
74 
newField(const QString & fieldName)75 QString Save::newField(const QString & fieldName)
76 {
77     return "\n" + indent() + fieldName + " : ";
78 }
79 
openCurlyBrackets()80 QString Save::openCurlyBrackets()
81 {
82     QString res = "\n" + indent() + "{";
83     incrIndent();
84     return res;
85 }
86 
closeCurlyBrackets()87 QString Save::closeCurlyBrackets()
88 {
89     decrIndent();
90     return "\n" + indent() + "}";
91 }
92 
field(QTextStream & in)93 QString Read::field(QTextStream & in)
94 {
95     QString res, colon;
96     in >> res >> colon;
97     return res;
98 }
string(QTextStream & in)99 QString Read::string(QTextStream & in)
100 {
101     QString res;
102     in >> res;
103     return res;
104 }
105 
106 #include <QtDebug>
107 
skipBracket(QTextStream & in)108 void Read::skipBracket(QTextStream & in)
109 {
110     QString skip;
111     in >> skip;
112     //qDebug() << skip;
113 }
114 
readBracketedBlock(QTextStream & in)115 QString Read::readBracketedBlock(QTextStream & in)
116 {
117     QString res;
118 
119     // Read first opening bracket, ignore everything which is before
120     unsigned int openedBracket = 0;
121     char c;
122     while(!openedBracket)
123     {
124         in >> c;
125         if(c == '[' || c == '(' || c == '{')
126         {
127             openedBracket++;
128             res.append(c);
129         }
130     }
131 
132     // Read until match found
133     while(openedBracket)
134     {
135         in >> c;
136         res.append(c);
137         if(c == '[' || c == '(' || c == '{')
138             openedBracket++;
139         else if(c == ']' || c == ')' || c == '}')
140             openedBracket--;
141     }
142 
143     return res;
144 }
145