-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestions_CrLf.cpp
More file actions
137 lines (126 loc) · 3.56 KB
/
Questions_CrLf.cpp
File metadata and controls
137 lines (126 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "Questions_CrLf.h"
#include "QuestionsUtils.h"
namespace
{
Answer ParseAnswer(const QStringRef line)
{
Answer answer { };
if (line.size() >= 1)
{
if (line.left(1) == "+" && line.size() > 1)
{
answer.isRight = true;
answer.text = line.right(line.size() - 1).toString();
}
else
{
answer.text = line.toString();
}
}
return answer;
}
Answers ParseAnswers(const QVector<QStringRef>& lines)
{
Answers answers;
if (lines.size() < 2)
{
qDebug() << "Detected question with wrong answers count";
return answers;
}
int rightsCount = 0;
bool notRightWithMinuses = true;
for (auto line : lines)
{
auto answer = ParseAnswer(line);
if (answer.text.isEmpty())
{
return { };
}
if (answer.isRight)
{
++rightsCount;
}
else if (answer.text.size() > 1 && answer.text.left(1) != "-")
{
notRightWithMinuses = false;
}
answers.push_back(answer);
}
if (rightsCount != 1)
{
qDebug() << "Detected " << rightsCount << " right answers";
return { };
}
if (notRightWithMinuses)
{
for (auto& answer : answers)
{
if (!answer.isRight)
{
answer.text = answer.text.right(answer.text.size() - 1);
}
}
}
return answers;
}
Question ParseQuestion(const QVector<QStringRef>& lines)
{
int firstAnswerLine = -1;
int line = 0;
for (; line < lines.size(); ++line)
{
if (lines[line].left(1) == "+" && firstAnswerLine < 0)
{
firstAnswerLine = line;
break;
}
}
Question question;
question.text = ParseQuestionText(lines, firstAnswerLine);
QVector<QStringRef> answersLines;
for (; line < lines.size(); ++line)
{
answersLines.push_back(lines[line]);
}
question.answers = ParseAnswers(answersLines);
if (question.text.isEmpty() || question.answers.size() < 2)
{
return { };
}
return question;
}
}
void ParseQuestions_CrLf(const QString& fileContent, Questions& questions)
{
QVector<QStringRef> lines = fileContent.splitRef("\n");
int prevQuestLine = -1;
QVector<QStringRef> curQuestion;
for (int line = 0; line < lines.size(); ++line)
{
if (lines[line].isEmpty())
{
if (curQuestion.size() >= 3)
{
Question question = ParseQuestion(curQuestion);
if (!question.IsEmpty())
{
questions.push_back(question);
}
else
{
qDebug() << "Can't parse question started at line " << prevQuestLine + 1; // Lines in Notepad++ started from 1
}
}
else if (!AreLinesEmpty(curQuestion))
{
qWarning() << "The question started at line " << prevQuestLine + 1 << " is too small";
}
prevQuestLine = line;
curQuestion.clear();
}
else
{
curQuestion.push_back(lines[line]);
}
}
}